-
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.
Take into account bank events when converting time parameters to blocks.
- Loading branch information
Showing
3 changed files
with
73 additions
and
47 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,63 @@ | ||
import { Op } from 'sequelize' | ||
|
||
import { BankStateEvent, WasmStateEvent } from '@/db' | ||
|
||
import { Block } from '../types' | ||
|
||
export const getBlockForTime = async ( | ||
blockTimeUnixMs: bigint | ||
): Promise<Block | undefined> => { | ||
const [wasmEvent, bankEvent] = await Promise.all([ | ||
await WasmStateEvent.findOne({ | ||
where: { | ||
blockTimeUnixMs: { | ||
[Op.gt]: 0, | ||
[Op.lte]: blockTimeUnixMs, | ||
}, | ||
}, | ||
order: [['blockTimeUnixMs', 'DESC']], | ||
}), | ||
await BankStateEvent.findOne({ | ||
where: { | ||
blockTimeUnixMs: { | ||
[Op.gt]: 0, | ||
[Op.lte]: blockTimeUnixMs, | ||
}, | ||
}, | ||
order: [['blockTimeUnixMs', 'DESC']], | ||
}), | ||
]) | ||
|
||
// Choose latest block. | ||
return [ | ||
...(wasmEvent ? [wasmEvent.block] : []), | ||
...(bankEvent ? [bankEvent.block] : []), | ||
].sort((a, b) => Number(b.height - a.height))[0] | ||
} | ||
|
||
export const getFirstBlock = async (): Promise<Block | undefined> => { | ||
const [wasmEvent, bankEvent] = await Promise.all([ | ||
await WasmStateEvent.findOne({ | ||
where: { | ||
blockTimeUnixMs: { | ||
[Op.gt]: 0, | ||
}, | ||
}, | ||
order: [['blockTimeUnixMs', 'ASC']], | ||
}), | ||
await BankStateEvent.findOne({ | ||
where: { | ||
blockTimeUnixMs: { | ||
[Op.gt]: 0, | ||
}, | ||
}, | ||
order: [['blockTimeUnixMs', 'ASC']], | ||
}), | ||
]) | ||
|
||
// Choose latest block. | ||
return [ | ||
...(wasmEvent ? [wasmEvent.block] : []), | ||
...(bankEvent ? [bankEvent.block] : []), | ||
].sort((a, b) => Number(b.height - a.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