Skip to content

Commit

Permalink
feat: narrow contract instance address (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Jun 1, 2023
1 parent c47bf19 commit a446a50
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-suns-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Narrowed contract instance address.
4 changes: 4 additions & 0 deletions src/actions/getContract.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ test('empty abi', () => {
walletClient,
})
expectTypeOf<keyof typeof contract>().toEqualTypeOf<'address' | 'abi'>()
expectTypeOf(contract.abi).toEqualTypeOf<[]>()
expectTypeOf(
contract.address,
).toEqualTypeOf<'0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2'>()
})

test('argument permutations', async () => {
Expand Down
23 changes: 14 additions & 9 deletions src/actions/getContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ export type GetContractParameters<
TWalletClient extends
| WalletClient<TTransport, TChain, TAccount>
| unknown = unknown,
TAddress extends Address = Address,
> = {
/** Contract ABI */
abi: Narrow<TAbi>
/** Contract address */
address: Address
address: TAddress
/**
* Public client
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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,
Expand All @@ -401,21 +404,22 @@ export function getContract<
TAccount,
TAbi,
TPublicClient,
TWalletClient
>): GetContractReturnType<TAbi, TPublicClient, TWalletClient> {
TWalletClient,
TAddress
>): GetContractReturnType<TAbi, TPublicClient, TWalletClient, TAddress> {
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
Expand Down Expand Up @@ -544,7 +548,7 @@ export function getContract<
eventName,
args,
...options,
} as WatchContractEventParameters)
} as unknown as WatchContractEventParameters)
}
},
},
Expand Down Expand Up @@ -623,7 +627,8 @@ export function getContract<
return contract as unknown as GetContractReturnType<
TAbi,
TPublicClient,
TWalletClient
TWalletClient,
TAddress
>
}

Expand Down

1 comment on commit a446a50

@vercel
Copy link

@vercel vercel bot commented on a446a50 Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.