Skip to content
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

feat(ethers6 adapter): allow optional account to be passed to contract.toEthers #3063

Merged
merged 2 commits into from
May 17, 2024
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
5 changes: 5 additions & 0 deletions .changeset/good-cobras-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

ethers6 adapter: allow optional account to be passed to `contract.toEthers` that will automatically hook up the contract with a signer instead of provider
28 changes: 20 additions & 8 deletions packages/thirdweb/src/adapters/ethers6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* @example
* ```ts
* import { ethers6Adapter } from "thirdweb/adapters/ethers6";
* const provider = ethers6Adapter.provider.toEthers({ client, chainId });
* const provider = ethers6Adapter.provider.toEthers({ client, chain });
* ```
*/
toEthers: (options: { client: ThirdwebClient; chain: Chain }) => {
Expand All @@ -79,9 +79,16 @@
* const ethersContract = await ethers6Adapter.contract.toEthers({ thirdwebContract });
* ```
*/
toEthers: (options: { thirdwebContract: ThirdwebContract }) => {
toEthers: (options: {
thirdwebContract: ThirdwebContract;
account?: Account;
}) => {

Check warning on line 85 in packages/thirdweb/src/adapters/ethers6.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/adapters/ethers6.ts#L83-L85

Added lines #L83 - L85 were not covered by tests
assertEthers6(ethers);
return toEthersContract(ethers, options.thirdwebContract);
return toEthersContract(
ethers,
options.thirdwebContract,
options.account,
);

Check warning on line 91 in packages/thirdweb/src/adapters/ethers6.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/adapters/ethers6.ts#L87-L91

Added lines #L87 - L91 were not covered by tests
},
/**
* Creates a ThirdwebContract instance from an ethers.js contract.
Expand Down Expand Up @@ -186,12 +193,15 @@
async function toEthersContract<abi extends Abi = []>(
ethers: Ethers6,
twContract: ThirdwebContract<abi>,
account?: Account,

Check warning on line 196 in packages/thirdweb/src/adapters/ethers6.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/adapters/ethers6.ts#L196

Added line #L196 was not covered by tests
): Promise<ethers6.Contract> {
if (twContract.abi) {
return new ethers.Contract(
twContract.address,
JSON.stringify(twContract.abi),
toEthersProvider(ethers, twContract.client, twContract.chain),
account
? toEthersSigner(ethers, twContract.client, account, twContract.chain)
: toEthersProvider(ethers, twContract.client, twContract.chain),

Check warning on line 204 in packages/thirdweb/src/adapters/ethers6.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/adapters/ethers6.ts#L202-L204

Added lines #L202 - L204 were not covered by tests
);
}

Expand All @@ -204,7 +214,9 @@
return new ethers.Contract(
twContract.address,
JSON.stringify(abi),
toEthersProvider(ethers, twContract.client, twContract.chain),
account
? toEthersSigner(ethers, twContract.client, account, twContract.chain)
: toEthersProvider(ethers, twContract.client, twContract.chain),

Check warning on line 219 in packages/thirdweb/src/adapters/ethers6.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/adapters/ethers6.ts#L217-L219

Added lines #L217 - L219 were not covered by tests
);
}

Expand Down Expand Up @@ -278,15 +290,15 @@
* @param client - The Thirdweb client.
* @param chain - The blockchain chain.
* @param account - The Thirdweb account.
* @returns A promise that resolves to an ethers.js signer.
* @returns An ethers.js signer.
* @internal
*/
export async function toEthersSigner(
export function toEthersSigner(
ethers: Ethers6,
client: ThirdwebClient,
account: Account,
chain: Chain,
): Promise<ethers6.Signer> {
): ethers6.Signer {
class ThirdwebAdapterSigner extends ethers.AbstractSigner<ethers6.JsonRpcProvider> {
private address: string;
override provider: ethers6.ethers.JsonRpcProvider;
Expand Down