Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: narrow contract instance address #630

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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