-
-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: narrow return types for actions that include
blockTag
or `inc…
…ludeTransactions` (#847) * feat: narrow return types for actions that use or * tests: fix * refactor: default cases * refactor: include transactions on block type * feat: support pending on formatted blocks/txns * feat: add missing decorator generics * refactor * refactor * chore: changesets * Update brown-sheep-visit.md * Update wicked-bananas-cover.md
- Loading branch information
Showing
36 changed files
with
1,119 additions
and
196 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,41 @@ | ||
--- | ||
"viem": minor | ||
--- | ||
|
||
Narrowed `getBlock`, `watchBlocks`, `getFilterChanges`, `getFilterLogs` & `getLogs` return types for when `blockTag` or `includeTransactions` is provided. | ||
|
||
- When `blockTag !== 'pending'`, the return type will now include some non-nullish properties if it were dependent on pending blocks. Example: For `getBlock`, the `block.number` type is now non-nullish since `blockTag !== 'pending'`. | ||
- On the other hand, when `blockTag: 'pending'`, some properties will be nullish. Example: For `getBlock`, the `block.number` type is now `null` since `blockTag === 'pending'`. | ||
- When `includeTransactions` is provided, the return type of will narrow the `transactions` property type. Example: `block.transactions` will be `Transaction[]` when `includeTransactions: true` instead of `Hash[] | Transaction[]`. | ||
|
||
TLDR; | ||
|
||
```ts | ||
// Before | ||
const block = publicClient.getBlock({ includeTransactions: true }) | ||
block.transactions | ||
// ^? Hash[] | Transaction[] | ||
block.transactions[0].blockNumber | ||
// ^? bigint | null | ||
|
||
// After | ||
const block = publicClient.getBlock({ includeTransactions: true }) | ||
block.transactions | ||
// ^? Transaction[] | ||
block.transactions[0].blockNumber | ||
// ^? bigint | ||
|
||
// Before | ||
const block = publicClient.getBlock({ blockTag: 'pending', includeTransactions: true }) | ||
block.number | ||
// ^? number | null | ||
block.transactions[0].blockNumber | ||
// ^? bigint | null | ||
|
||
// After | ||
const block = publicClient.getBlock({ blockTag: 'pending', includeTransactions: true }) | ||
block.number | ||
// ^? null | ||
block.transactions[0].blockNumber | ||
// ^? null | ||
``` |
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,19 @@ | ||
--- | ||
"viem": minor | ||
--- | ||
|
||
**Type Change**: `TPending` has been added to slot 2 of the `Log` generics. | ||
|
||
```diff | ||
type Log< | ||
TQuantity = bigint, | ||
TIndex = number, | ||
+ TPending extends boolean = boolean, | ||
TAbiEvent extends AbiEvent | undefined = undefined, | ||
TStrict extends boolean | undefined = undefined, | ||
TAbi extends Abi | readonly unknown[] = [TAbiEvent], | ||
TEventName extends string | undefined = TAbiEvent extends AbiEvent | ||
? TAbiEvent['name'] | ||
: undefined, | ||
> | ||
``` |
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,18 @@ | ||
--- | ||
"viem": minor | ||
--- | ||
|
||
**Type Change**: `TIncludeTransactions` & `TBlockTag` has been added to slot 1 & 2 of the `Block` generics. | ||
|
||
```diff | ||
type Block< | ||
TQuantity = bigint, | ||
+ TIncludeTransactions extends boolean = boolean, | ||
+ TBlockTag extends BlockTag = BlockTag, | ||
TTransaction = Transaction< | ||
bigint, | ||
number, | ||
TBlockTag extends 'pending' ? true : false | ||
>, | ||
> | ||
``` |
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,26 @@ | ||
import { usdcContractConfig } from '../../_test/abis.js' | ||
import { publicClient } from '../../_test/utils.js' | ||
import { createContractEventFilter } from './createContractEventFilter.js' | ||
import { expectTypeOf, test } from 'vitest' | ||
|
||
test('fromBlock/toBlock', async () => { | ||
const filter = await createContractEventFilter(publicClient, { | ||
abi: usdcContractConfig.abi, | ||
}) | ||
expectTypeOf(filter.fromBlock).toBeUndefined() | ||
expectTypeOf(filter.toBlock).toBeUndefined() | ||
|
||
const filter_fromBlock = await createContractEventFilter(publicClient, { | ||
abi: usdcContractConfig.abi, | ||
fromBlock: 69n, | ||
}) | ||
expectTypeOf(filter_fromBlock.fromBlock).toMatchTypeOf<69n | undefined>() | ||
expectTypeOf(filter_fromBlock.toBlock).toBeUndefined() | ||
|
||
const filter_toBlock = await createContractEventFilter(publicClient, { | ||
abi: usdcContractConfig.abi, | ||
toBlock: 69n, | ||
}) | ||
expectTypeOf(filter_toBlock.toBlock).toMatchTypeOf<69n | undefined>() | ||
expectTypeOf(filter_toBlock.fromBlock).toBeUndefined() | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { publicClient } from '../../_test/utils.js' | ||
import { createEventFilter } from './createEventFilter.js' | ||
import { expectTypeOf, test } from 'vitest' | ||
|
||
test('fromBlock/toBlock', async () => { | ||
const filter = await createEventFilter(publicClient) | ||
expectTypeOf(filter.fromBlock).toBeUndefined() | ||
expectTypeOf(filter.toBlock).toBeUndefined() | ||
|
||
const filter_fromBlock = await createEventFilter(publicClient, { | ||
fromBlock: 69n, | ||
}) | ||
expectTypeOf(filter_fromBlock.fromBlock).toMatchTypeOf<69n | undefined>() | ||
expectTypeOf(filter_fromBlock.toBlock).toBeUndefined() | ||
|
||
const filter_toBlock = await createEventFilter(publicClient, { | ||
toBlock: 69n, | ||
}) | ||
expectTypeOf(filter_toBlock.toBlock).toMatchTypeOf<69n | undefined>() | ||
expectTypeOf(filter_toBlock.fromBlock).toBeUndefined() | ||
}) |
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
Oops, something went wrong.
1e5d454
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viem – ./
viem.vercel.app
viem-git-main-wagmi-dev.vercel.app
viem-wagmi-dev.vercel.app
viem-site.vercel.app
www.viem.sh
viem.sh