Skip to content

Commit

Permalink
return address type for contractoptions (#5874)
Browse files Browse the repository at this point in the history
Signed-off-by: greg <gregfromstl@gmail.com>
Co-authored-by: greg <gregfromstl@gmail.com>
  • Loading branch information
dirtycajunrice and gregfromstl authored Jan 3, 2025
1 parent 0c5b0bb commit 5cb8e3b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
20 changes: 12 additions & 8 deletions packages/thirdweb/src/contract/contract.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import type { Abi } from "abitype";
import type { Chain } from "../chains/types.js";
import type { ThirdwebClient } from "../client/client.js";
import { isAddress } from "../utils/address.js";
import { type Address, isAddress } from "../utils/address.js";

/**
* @contract
*/
export type ContractOptions<abi extends Abi = []> = {
export type ContractOptions<
abi extends Abi = [],
address extends string = string,
> = {
client: ThirdwebClient;
address: string;
address: address;
chain: Chain;
readonly abi?: abi;
};

/**
* @contract
*/
export type ThirdwebContract<abi extends Abi = []> = Readonly<
ContractOptions<abi>
>;
export type ThirdwebContract<
abi extends Abi = [],
address extends string = string,
> = Readonly<ContractOptions<abi, address>>;

/**
* Creates a Thirdweb contract by combining the Thirdweb client and contract options.
Expand All @@ -42,7 +46,7 @@ export type ThirdwebContract<abi extends Abi = []> = Readonly<
*/
export function getContract<const abi extends Abi = []>(
options: ContractOptions<abi>,
): ThirdwebContract<abi> {
): ThirdwebContract<abi, Address> {
if (!options.client) {
throw new Error(
`getContract validation error - invalid client: ${options.client}`,
Expand All @@ -58,5 +62,5 @@ export function getContract<const abi extends Abi = []>(
`getContract validation error - invalid chain: ${options.chain}`,
);
}
return options;
return options as ThirdwebContract<abi, Address>;
}
15 changes: 4 additions & 11 deletions packages/thirdweb/src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { keccak256 } from "./hashing/keccak256.js";
export type AddressInput = string;
export type Address = `0x${string}`;

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

/**
Expand All @@ -26,16 +26,9 @@ export function isAddress(address: string): address is Address {
// biome-ignore lint/style/noNonNullAssertion: the `has` above ensures that this will always be set
return IS_ADDRESS_CACHE.get(address)!;
}
const result = (() => {
if (!ADRESS_REGEX.test(address)) {
return false;
}
if (address.toLowerCase() === address) {
return true;
}

return checksumAddress(address) === address;
})();
const result =
ADDRESS_REGEX.test(address) &&
(address.toLowerCase() === address || checksumAddress(address) === address);
IS_ADDRESS_CACHE.set(address, result);
return result;
}
Expand Down

0 comments on commit 5cb8e3b

Please sign in to comment.