Skip to content

Commit 58e355e

Browse files
committed
fix(eth): fix corner case of locating blocks (#1216)
1 parent 077cd5c commit 58e355e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

packages/sdk/src/utils/block.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ describe('block estimate', () => {
1313
const testIf = haveProviders ? test : test.skip
1414

1515
testIf('get block number at mainnet', async () => {
16-
const targetDate = new Date('2023-05-02T00:00:00Z')
17-
const estimatedBlockNumber = await estimateBlockNumberAtDate(getProvider(), targetDate, 0)
16+
let targetDate = new Date('2023-05-02T00:00:00Z')
17+
let estimatedBlockNumber = await estimateBlockNumberAtDate(getProvider(), targetDate, 0)
1818
expect(estimatedBlockNumber).to.equal(17169395)
19+
20+
targetDate = new Date('1990-05-02T00:00:00Z')
21+
estimatedBlockNumber = await estimateBlockNumberAtDate(getProvider(), targetDate, 0)
22+
expect(estimatedBlockNumber).to.equal(0)
1923
})
2024
})

packages/sdk/src/utils/block.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ export async function estimateBlockNumberAtDate(
2828
result = await provider.send('sentio_estimateBlockNumberAtDate', [timestampHex, rangeStart, rangeEnd, strategy])
2929
} catch (e) {
3030
const serverError = e as EthersError
31-
if (serverError.code === 'SERVER_ERROR') {
31+
if (
32+
serverError.code === 'SERVER_ERROR' ||
33+
serverError.code === 'UNKNOWN_ERROR' ||
34+
serverError.code === 'UNSUPPORTED_OPERATION'
35+
) {
3236
return await estimateBlockNumberAtDateSlow(provider, targetDate, startBlock)
3337
}
3438
throw e
3539
}
3640

3741
if (result === null) {
38-
throw Error("Block can't be located.")
42+
return 0
3943
}
4044

4145
return parseInt(result, 16)
@@ -71,7 +75,9 @@ export async function estimateBlockNumberAtDateSlow(
7175
}
7276
}
7377

74-
// If exact timestamp is not found, return the closest block number
78+
// If exact timestamp is not found, return 0
79+
if (high === -1) return 0
80+
7581
const closestBlock = await getBlockSafely(provider, high)
7682
return closestBlock.number
7783
}

0 commit comments

Comments
 (0)