Skip to content

Commit 5cb8e3b

Browse files
return address type for contractoptions (#5874)
Signed-off-by: greg <gregfromstl@gmail.com> Co-authored-by: greg <gregfromstl@gmail.com>
1 parent 0c5b0bb commit 5cb8e3b

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

packages/thirdweb/src/contract/contract.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
import type { Abi } from "abitype";
22
import type { Chain } from "../chains/types.js";
33
import type { ThirdwebClient } from "../client/client.js";
4-
import { isAddress } from "../utils/address.js";
4+
import { type Address, isAddress } from "../utils/address.js";
55

66
/**
77
* @contract
88
*/
9-
export type ContractOptions<abi extends Abi = []> = {
9+
export type ContractOptions<
10+
abi extends Abi = [],
11+
address extends string = string,
12+
> = {
1013
client: ThirdwebClient;
11-
address: string;
14+
address: address;
1215
chain: Chain;
1316
readonly abi?: abi;
1417
};
1518

1619
/**
1720
* @contract
1821
*/
19-
export type ThirdwebContract<abi extends Abi = []> = Readonly<
20-
ContractOptions<abi>
21-
>;
22+
export type ThirdwebContract<
23+
abi extends Abi = [],
24+
address extends string = string,
25+
> = Readonly<ContractOptions<abi, address>>;
2226

2327
/**
2428
* Creates a Thirdweb contract by combining the Thirdweb client and contract options.
@@ -42,7 +46,7 @@ export type ThirdwebContract<abi extends Abi = []> = Readonly<
4246
*/
4347
export function getContract<const abi extends Abi = []>(
4448
options: ContractOptions<abi>,
45-
): ThirdwebContract<abi> {
49+
): ThirdwebContract<abi, Address> {
4650
if (!options.client) {
4751
throw new Error(
4852
`getContract validation error - invalid client: ${options.client}`,
@@ -58,5 +62,5 @@ export function getContract<const abi extends Abi = []>(
5862
`getContract validation error - invalid chain: ${options.chain}`,
5963
);
6064
}
61-
return options;
65+
return options as ThirdwebContract<abi, Address>;
6266
}

packages/thirdweb/src/utils/address.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { keccak256 } from "./hashing/keccak256.js";
55
export type AddressInput = string;
66
export type Address = `0x${string}`;
77

8-
const ADRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
8+
const ADDRESS_REGEX = /^0x[a-fA-F0-9]{40}$/;
99
const IS_ADDRESS_CACHE = new LruMap<boolean>(4096);
1010

1111
/**
@@ -26,16 +26,9 @@ export function isAddress(address: string): address is Address {
2626
// biome-ignore lint/style/noNonNullAssertion: the `has` above ensures that this will always be set
2727
return IS_ADDRESS_CACHE.get(address)!;
2828
}
29-
const result = (() => {
30-
if (!ADRESS_REGEX.test(address)) {
31-
return false;
32-
}
33-
if (address.toLowerCase() === address) {
34-
return true;
35-
}
36-
37-
return checksumAddress(address) === address;
38-
})();
29+
const result =
30+
ADDRESS_REGEX.test(address) &&
31+
(address.toLowerCase() === address || checksumAddress(address) === address);
3932
IS_ADDRESS_CACHE.set(address, result);
4033
return result;
4134
}

0 commit comments

Comments
 (0)