Skip to content

Commit d4c423c

Browse files
fix: handle 0n as from/to blocks in getContractEvents (#4919)
Fixes CNCT-1987 <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on improving the validation of `fromBlock` and `toBlock` parameters in the `getContractEvents` function to ensure they are explicitly defined before performing any calculations. ### Detailed summary - Updated condition to check if `fromBlock` and `toBlock` are not `undefined` instead of truthy. - Adjusted logic for setting `restParams.toBlock` and `restParams.fromBlock` based on the new checks. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent b6bf3b9 commit d4c423c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

.changeset/heavy-camels-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix passing 0n as from/to blocks in getContractEvents

packages/thirdweb/src/event/actions/get-events.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,18 @@ export async function getContractEvents<
124124

125125
// Make sure the inputs were properly defined
126126
if (
127-
fromBlock &&
128-
toBlock &&
127+
fromBlock !== undefined &&
128+
toBlock !== undefined &&
129129
BigInt(toBlock) - BigInt(fromBlock) !== BigInt(blockRange)
130130
) {
131131
throw new Error(
132132
"Incompatible blockRange with specified fromBlock and toBlock. Please only define fromBlock or toBlock when specifying blockRange.",
133133
);
134134
}
135135

136-
if (fromBlock) {
136+
if (fromBlock !== undefined) {
137137
restParams.toBlock = BigInt(fromBlock) + BigInt(blockRange) - 1n; // Subtract one because toBlock is inclusive
138-
} else if (toBlock) {
138+
} else if (toBlock !== undefined) {
139139
restParams.fromBlock = BigInt(toBlock) - BigInt(blockRange) + 1n; // Add one because fromBlock is inclusive
140140
} else {
141141
// If no from or to block specified, use the latest block as the to block

0 commit comments

Comments
 (0)