Skip to content

Commit

Permalink
refactor: Simplify ethers5 and ethers6 adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls committed Jan 20, 2024
1 parent b95b804 commit 7f90db7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
30 changes: 17 additions & 13 deletions packages/thirdweb/src/adapters/ethers5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -49,9 +49,9 @@ function toProvider(
});
}

function toContract<abi extends ethers5.ContractInterface>(
function contract<abi extends ethers5.ContractInterface>(
ethers: Ethers5,
contract: ThirdwebContract,
twContract: ThirdwebContract,
abi?: abi,
): abi extends ethers5.ContractInterface
? ethers5.Contract
Expand All @@ -62,14 +62,18 @@ function toContract<abi extends ethers5.ContractInterface>(
// @ts-expect-error - typescript can't understand this
return import("../abi/resolveContractAbi.js")
.then((m) => {
return m.resolveAbi(contract) as Promise<ethers5.ContractInterface>;
return m.resolveAbi(twContract) as Promise<ethers5.ContractInterface>;
})
.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),
);
}
30 changes: 17 additions & 13 deletions packages/thirdweb/src/adapters/ethers6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -46,9 +46,9 @@ function toProvider(ethers: Ethers6, client: RawClient, chainId: number) {
});
}

function toContract<abi extends ethers6.InterfaceAbi>(
function contract<abi extends ethers6.InterfaceAbi>(
ethers: Ethers6,
contract: ThirdwebContract,
twContract: ThirdwebContract,
abi?: abi,
): abi extends ethers6.InterfaceAbi
? ethers6.Contract
Expand All @@ -59,14 +59,18 @@ function toContract<abi extends ethers6.InterfaceAbi>(
// @ts-expect-error - typescript can't understand this
return import("../abi/resolveContractAbi.js")
.then((m) => {
return m.resolveAbi(contract) as Promise<ethers6.InterfaceAbi>;
return m.resolveAbi(twContract) as Promise<ethers6.InterfaceAbi>;
})
.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),
);
}

0 comments on commit 7f90db7

Please sign in to comment.