From 7e66fb59a33daaa7db889ca2845aec86e832e7d1 Mon Sep 17 00:00:00 2001 From: Tom Meagher Date: Thu, 1 Jun 2023 08:30:51 -0400 Subject: [PATCH] feat: narrow contract instance address --- .changeset/weak-suns-fail.md | 5 +++++ src/actions/getContract.test-d.ts | 4 ++++ src/actions/getContract.ts | 23 ++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 .changeset/weak-suns-fail.md diff --git a/.changeset/weak-suns-fail.md b/.changeset/weak-suns-fail.md new file mode 100644 index 0000000000..e454ef1299 --- /dev/null +++ b/.changeset/weak-suns-fail.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Narrowed contract instance address. diff --git a/src/actions/getContract.test-d.ts b/src/actions/getContract.test-d.ts index a507ed33bb..b3cc2d36d3 100644 --- a/src/actions/getContract.test-d.ts +++ b/src/actions/getContract.test-d.ts @@ -591,6 +591,10 @@ test('empty abi', () => { walletClient, }) expectTypeOf().toEqualTypeOf<'address' | 'abi'>() + expectTypeOf(contract.abi).toEqualTypeOf<[]>() + expectTypeOf( + contract.address, + ).toEqualTypeOf<'0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'>() }) test('argument permutations', async () => { diff --git a/src/actions/getContract.ts b/src/actions/getContract.ts index 1fcc3dfa45..72793d8992 100644 --- a/src/actions/getContract.ts +++ b/src/actions/getContract.ts @@ -68,11 +68,12 @@ export type GetContractParameters< TWalletClient extends | WalletClient | unknown = unknown, + TAddress extends Address = Address, > = { /** Contract ABI */ abi: Narrow /** Contract address */ - address: Address + address: TAddress /** * Public client * @@ -100,6 +101,7 @@ export type GetContractReturnType< TAbi extends Abi | readonly unknown[] = Abi, TPublicClient extends PublicClient | unknown = unknown, TWalletClient extends WalletClient | unknown = unknown, + TAddress extends Address = Address, _EventNames extends string = TAbi extends Abi ? Abi extends TAbi ? string @@ -352,7 +354,7 @@ export type GetContractReturnType< } } : unknown) -> & { address: Address; abi: TAbi } +> & { address: TAddress; abi: TAbi } /** * Gets type-safe interface for performing contract-related actions with a specific `abi` and `address`. @@ -381,6 +383,7 @@ export type GetContractReturnType< */ export function getContract< TTransport extends Transport, + TAddress extends Address, TAbi extends Abi | readonly unknown[], TChain extends Chain | undefined = Chain | undefined, TAccount extends Account | undefined = Account | undefined, @@ -401,21 +404,22 @@ export function getContract< TAccount, TAbi, TPublicClient, - TWalletClient ->): GetContractReturnType { + TWalletClient, + TAddress +>): GetContractReturnType { const hasPublicClient = publicClient !== undefined && publicClient !== null const hasWalletClient = walletClient !== undefined && walletClient !== null const contract: { [_ in + | 'abi' + | 'address' | 'createEventFilter' | 'estimateGas' | 'read' | 'simulate' | 'watchEvent' - | 'write' - | 'address' - | 'abi']?: unknown + | 'write']?: unknown } = {} let hasReadFunction = false @@ -544,7 +548,7 @@ export function getContract< eventName, args, ...options, - } as WatchContractEventParameters) + } as unknown as WatchContractEventParameters) } }, }, @@ -623,7 +627,8 @@ export function getContract< return contract as unknown as GetContractReturnType< TAbi, TPublicClient, - TWalletClient + TWalletClient, + TAddress > }