Skip to content

Commit

Permalink
[SDK] Add tests for ethers5 Adapter (#4339)
Browse files Browse the repository at this point in the history
## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to export functions `toEthersProvider`, `toEthersContract`, and `fromEthersContract` in `ethers5.ts`, and update test cases in `ethers5.test.ts` for these functions.

### Detailed summary
- Exported `toEthersProvider`, `toEthersContract`, and `fromEthersContract` functions in `ethers5.ts`
- Updated test cases in `ethers5.test.ts` for `toEthersContract` and `fromEthersContract`
- Added imports and test cases for new functions and modules

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
kien-ngo committed Aug 31, 2024
1 parent 29374a7 commit ff3abfa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
45 changes: 42 additions & 3 deletions packages/thirdweb/src/adapters/ethers5.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import * as ethers5 from "ethers5";
import { describe, expect, test } from "vitest";
import { ANVIL_CHAIN } from "../../test/src/chains.js";
import { describe, expect, it, test } from "vitest";
import { USDT_CONTRACT } from "~test/test-contracts.js";
import { ANVIL_CHAIN, FORKED_ETHEREUM_CHAIN } from "../../test/src/chains.js";
import { TEST_CLIENT } from "../../test/src/test-clients.js";
import { ANVIL_PKEY_A, TEST_ACCOUNT_B } from "../../test/src/test-wallets.js";
import { resolveContractAbi } from "../contract/actions/resolve-abi.js";
import { decimals } from "../extensions/erc20/__generated__/IERC20/read/decimals.js";
import { randomBytesBuffer } from "../utils/random.js";
import { privateKeyToAccount } from "../wallets/private-key.js";
import { toEthersSigner } from "./ethers5.js";
import {
fromEthersContract,
toEthersContract,
toEthersProvider,
toEthersSigner,
} from "./ethers5.js";

const account = privateKeyToAccount({
privateKey: ANVIL_PKEY_A,
Expand Down Expand Up @@ -110,4 +118,35 @@ describe("ethers5 adapter", () => {
});
expect(txResponse.hash.length).toBe(66);
});

it("toEthersContract should work", async () => {
const ethersContract = await toEthersContract(ethers5, USDT_CONTRACT);
expect(ethersContract.address.toLowerCase()).toBe(
USDT_CONTRACT.address.toLowerCase(),
);
const decimals = await ethersContract.decimals();
expect(decimals.toString()).toBe("6");
});

it("fromEthersContract should work", async () => {
const provider = toEthersProvider(
ethers5,
TEST_CLIENT,
FORKED_ETHEREUM_CHAIN,
);
const ethersContract = new ethers5.Contract(
USDT_CONTRACT.address,
await resolveContractAbi(USDT_CONTRACT),
provider,
);

const thirdwebContract = await fromEthersContract({
client: TEST_CLIENT,
ethersContract,
chain: FORKED_ETHEREUM_CHAIN,
});

const _decimals = await decimals({ contract: thirdwebContract });
expect(_decimals).toBe(6);
});
});
6 changes: 3 additions & 3 deletions packages/thirdweb/src/adapters/ethers5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export const ethers5Adapter = /* @__PURE__ */ (() => {
* @returns The ethers.js provider.
* @internal
*/
function toEthersProvider(
export function toEthersProvider(
ethers: Ethers5,
client: ThirdwebClient,
chain: Chain,
Expand All @@ -299,7 +299,7 @@ function toEthersProvider(
* @returns A Promise that resolves to an ethers.js Contract.
* @internal
*/
async function toEthersContract<abi extends Abi = []>(
export async function toEthersContract<abi extends Abi = []>(
ethers: Ethers5,
twContract: ThirdwebContract<abi>,
): Promise<ethers5.Contract> {
Expand Down Expand Up @@ -336,7 +336,7 @@ type FromEthersContractOptions = {
* @returns A promise that resolves to a ThirdwebContract instance.
* @internal
*/
async function fromEthersContract<abi extends Abi>(
export async function fromEthersContract<abi extends Abi>(
options: FromEthersContractOptions,
): Promise<ThirdwebContract<abi>> {
return getContract({
Expand Down

0 comments on commit ff3abfa

Please sign in to comment.