Skip to content

return address type for contractoptions #5874

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

Merged
merged 6 commits into from
Jan 3, 2025
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
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
Loading