-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
130 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { ModelStatic, Op } from 'sequelize' | ||
|
||
import { Block, DependableEventModel } from '@/types' | ||
|
||
import { getDependableEventModels } from './dependable' | ||
|
||
/** | ||
* Get the latest block before or equal to the requested block height. | ||
*/ | ||
export const getBlockForHeight = async ( | ||
blockHeight: bigint, | ||
// Optionally set a minimum. | ||
after = 0n | ||
): Promise<Block | undefined> => { | ||
const events = await Promise.all( | ||
getDependableEventModels().map((DependableEventModel) => | ||
( | ||
DependableEventModel as unknown as ModelStatic<DependableEventModel> | ||
).findOne({ | ||
where: { | ||
[DependableEventModel.blockHeightKey]: { | ||
[Op.gt]: after, | ||
[Op.lte]: blockHeight, | ||
}, | ||
}, | ||
order: [[DependableEventModel.blockHeightKey, 'DESC']], | ||
}) | ||
) | ||
) | ||
|
||
// Choose latest block. | ||
return events | ||
.flatMap((event) => event?.block || []) | ||
.sort((a, b) => Number(b.height - a.height))[0] | ||
} | ||
|
||
/** | ||
* Get the latest block before or equal to the requested block time. | ||
*/ | ||
export const getBlockForTime = async ( | ||
blockTimeUnixMs: bigint, | ||
// Optionally set a minimum. | ||
after = 0n | ||
): Promise<Block | undefined> => { | ||
const events = await Promise.all( | ||
getDependableEventModels().map((DependableEventModel) => | ||
( | ||
DependableEventModel as unknown as ModelStatic<DependableEventModel> | ||
).findOne({ | ||
where: { | ||
[DependableEventModel.blockTimeUnixMsKey]: { | ||
[Op.gt]: after, | ||
[Op.lte]: blockTimeUnixMs, | ||
}, | ||
}, | ||
order: [[DependableEventModel.blockTimeUnixMsKey, 'DESC']], | ||
}) | ||
) | ||
) | ||
|
||
// Choose latest block. | ||
return events | ||
.flatMap((event) => event?.block || []) | ||
.sort((a, b) => Number(b.height - a.height))[0] | ||
} | ||
|
||
/** | ||
* Get the first block. | ||
*/ | ||
export const getFirstBlock = async (): Promise<Block | undefined> => { | ||
const events = await Promise.all( | ||
getDependableEventModels().map((DependableEventModel) => | ||
( | ||
DependableEventModel as unknown as ModelStatic<DependableEventModel> | ||
).findOne({ | ||
where: { | ||
[DependableEventModel.blockHeightKey]: { | ||
[Op.gt]: 0, | ||
}, | ||
}, | ||
order: [[DependableEventModel.blockHeightKey, 'ASC']], | ||
}) | ||
) | ||
) | ||
|
||
// Choose first block. | ||
return events | ||
.flatMap((event) => event?.block || []) | ||
.sort((a, b) => Number(a.height - b.height))[0] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,10 @@ | ||
export const getTypedFormula = jest.fn() | ||
import * as utils from '@/formulas/utils' | ||
|
||
const mocks = { | ||
'@/formulas': { | ||
getTypedFormula, | ||
}, | ||
} | ||
export const getTypedFormula = jest.spyOn(utils, 'getTypedFormula') | ||
|
||
// Creates mocks with default implementations. | ||
export const restoreOriginalMocks = () => { | ||
Object.entries(mocks).forEach(([module, mocks]) => { | ||
jest.mock(module, () => mocks) | ||
|
||
// Mock with default implementation unless overridden. | ||
Object.entries(mocks).forEach(([name, mock]) => { | ||
mock.mockImplementation(jest.requireActual(module)[name]) | ||
}) | ||
}) | ||
getTypedFormula.mockReset() | ||
} | ||
|
||
restoreOriginalMocks() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters