From 7f90db78b30684299f337f6d5a7cdb432844e98f Mon Sep 17 00:00:00 2001 From: Jonas Daniels Date: Sat, 20 Jan 2024 14:28:08 +1300 Subject: [PATCH] refactor: Simplify ethers5 and ethers6 adapters --- packages/thirdweb/src/adapters/ethers5.ts | 30 +++++++++++++---------- packages/thirdweb/src/adapters/ethers6.ts | 30 +++++++++++++---------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/packages/thirdweb/src/adapters/ethers5.ts b/packages/thirdweb/src/adapters/ethers5.ts index 0d612ffeec5..d7a59d40858 100644 --- a/packages/thirdweb/src/adapters/ethers5.ts +++ b/packages/thirdweb/src/adapters/ethers5.ts @@ -22,18 +22,18 @@ function assertEthers5( } } -export function ethers5Adapter() { +export const ethers5Adapter = /* @__PURE__ */ (() => { const ethers = universalethers; assertEthers5(ethers); return { - toProvider: (client: RawClient, chainId: number) => - toProvider(ethers, client, chainId), - toContract: (contract: ThirdwebContract, abi?: ethers5.ContractInterface) => - toContract(ethers, contract, abi), + provider: (client: RawClient, chainId: number) => + provider(ethers, client, chainId), + contract: (twContract: ThirdwebContract, abi?: ethers5.ContractInterface) => + contract(ethers, twContract, abi), }; -} +})(); -function toProvider( +function provider( ethers: Ethers5, client: RawClient, chainId: number, @@ -49,9 +49,9 @@ function toProvider( }); } -function toContract( +function contract( ethers: Ethers5, - contract: ThirdwebContract, + twContract: ThirdwebContract, abi?: abi, ): abi extends ethers5.ContractInterface ? ethers5.Contract @@ -62,14 +62,18 @@ function toContract( // @ts-expect-error - typescript can't understand this return import("../abi/resolveContractAbi.js") .then((m) => { - return m.resolveAbi(contract) as Promise; + return m.resolveAbi(twContract) as Promise; }) .then((abi_) => { // call self again this time with the resolved abi - return toContract(ethers, contract, abi_); + return contract(ethers, twContract, abi_); }); } - const provider = toProvider(ethers, contract, contract.chainId); + // @ts-expect-error - typescript can't understand this - return new ethers.Contract(contract.address, abi, provider); + return new ethers.Contract( + twContract.address, + abi, + provider(ethers, twContract, twContract.chainId), + ); } diff --git a/packages/thirdweb/src/adapters/ethers6.ts b/packages/thirdweb/src/adapters/ethers6.ts index 6ab8c791a50..44b0318f61b 100644 --- a/packages/thirdweb/src/adapters/ethers6.ts +++ b/packages/thirdweb/src/adapters/ethers6.ts @@ -22,18 +22,18 @@ function assertEthers6( } } -export function ethers6Adapter() { +export const ethers6Adapter = /* @__PURE__ */ (() => { const ethers = universalethers; assertEthers6(ethers); return { - toProvider: (client: RawClient, chainId: number) => - toProvider(ethers, client, chainId), - toContract: (contract: ThirdwebContract, abi?: ethers6.InterfaceAbi) => - toContract(ethers, contract, abi), + provider: (client: RawClient, chainId: number) => + provider(ethers, client, chainId), + contract: (twContract: ThirdwebContract, abi?: ethers6.InterfaceAbi) => + contract(ethers, twContract, abi), }; -} +})(); -function toProvider(ethers: Ethers6, client: RawClient, chainId: number) { +function provider(ethers: Ethers6, client: RawClient, chainId: number) { const url = `https://${chainId}.rpc.thirdweb.com/${client.clientId}`; const fetchRequest = new ethers.FetchRequest(url); @@ -46,9 +46,9 @@ function toProvider(ethers: Ethers6, client: RawClient, chainId: number) { }); } -function toContract( +function contract( ethers: Ethers6, - contract: ThirdwebContract, + twContract: ThirdwebContract, abi?: abi, ): abi extends ethers6.InterfaceAbi ? ethers6.Contract @@ -59,14 +59,18 @@ function toContract( // @ts-expect-error - typescript can't understand this return import("../abi/resolveContractAbi.js") .then((m) => { - return m.resolveAbi(contract) as Promise; + return m.resolveAbi(twContract) as Promise; }) .then((abi_) => { // call self again this time with the resolved abi - return toContract(ethers, contract, abi_); + return contract(ethers, twContract, abi_); }); } - const provider = toProvider(ethers, contract, contract.chainId); + // @ts-expect-error - typescript can't understand this - return new ethers.Contract(contract.address, abi, provider); + return new ethers.Contract( + twContract.address, + abi, + provider(ethers, twContract, twContract.chainId), + ); }