Skip to content

Commit

Permalink
update ethers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Oct 12, 2023
1 parent 4a18e72 commit 72c7974
Show file tree
Hide file tree
Showing 50 changed files with 4,514 additions and 39 deletions.
1 change: 1 addition & 0 deletions examples/with-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"start-advanced": "pnpm -w run build-all && tsx src/advanced.ts",
"start-legacy-sepolia": "pnpm -w run build-all && tsx src/legacySepolia.ts",
"clean": "rimraf ./dist ./.cache",
"typechain": "typechain --target ethers-v6 --out-dir ./src/typechain './src/weth-contract-abi.json'",
"typecheck": "tsc --noEmit"
},
"dependencies": {
Expand Down
10 changes: 9 additions & 1 deletion packages/ethers/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("@nomiclabs/hardhat-ethers");
require("@typechain/hardhat");
require("@nomicfoundation/hardhat-ethers");

/** @type import('hardhat/config').HardhatUserConfig */
const config = {
Expand All @@ -8,6 +9,13 @@ const config = {
artifacts: "./src/__tests__/contracts/artifacts",
cache: "./.cache",
},
typechain: {
outDir: 'src/__tests__/typechain',
target: 'ethers-v6',
alwaysGenerateOverloads: false,
externalArtifacts: [],
dontOverrideCompile: false
},
};

module.exports = config;
12 changes: 8 additions & 4 deletions packages/ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,27 @@
"clean": "rimraf ./dist ./.cache",
"compile:contracts": "hardhat compile",
"test": "jest",
"typechain": "typechain --target ethers-v6 --out-dir ./src/__tests__/typechain './src/weth-contract-abi.json'",
"typecheck": "tsc -p tsconfig.typecheck.json"
},
"peerDependencies": {
"ethers": "^5.0.0"
},
"dependencies": {
"@ethersproject/abstract-signer": "^5.7.0",
"@turnkey/http": "workspace:*",
"@turnkey/api-key-stamper": "workspace:*"
"@turnkey/api-key-stamper": "workspace:*",
"@turnkey/http": "workspace:*"
},
"devDependencies": {
"@ethersproject/experimental": "^5.7.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@nomicfoundation/hardhat-ethers": "^3.0.4",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@openzeppelin/contracts": "^4.9.0",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"ethers": "^6.7.1",
"hardhat": "^2.12.7"
"hardhat": "^2.12.7",
"typechain": "^8.3.1"
},
"engines": {
"node": ">=16.0.0"
Expand Down

Large diffs are not rendered by default.

61 changes: 29 additions & 32 deletions packages/ethers/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Eip1193Bridge } from "@ethersproject/experimental";
import { setBalance } from "@nomicfoundation/hardhat-network-helpers";
import { ethers } from "ethers";
import hre from "hardhat";
import { test, expect, beforeEach, describe } from "@jest/globals";
import { TurnkeySigner, TurnkeyActivityError } from "../";
import Test721 from "./contracts/artifacts/src/__tests__/contracts/source/Test721.sol/Test721.json";
import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Test721__factory as Test721Factory } from "./typechain/factories/src/__tests__/contracts/source/Test721__factory";

// @ts-expect-error
const testCase: typeof test = (...argList) => {
Expand All @@ -21,10 +20,10 @@ const testCase: typeof test = (...argList) => {
describe("TurnkeySigner", () => {
let connectedSigner: TurnkeySigner;
let signerWithProvider: TurnkeySigner;
let chainId: number;
let chainId: bigint;
let expectedEthAddress: string;
let bannedToAddress: string;
let eip1193: Eip1193Bridge;
let eip1193Provider: ethers.Provider;

beforeEach(async () => {
if (!process.env.BANNED_TO_ADDRESS) {
Expand Down Expand Up @@ -94,25 +93,26 @@ describe("TurnkeySigner", () => {

chainId = (await connectedSigner.provider!.getNetwork()).chainId;

eip1193 = new Eip1193Bridge(connectedSigner, provider);
// need a node-compatible (read: non-`window.ethereum`) Eip1193Provider
eip1193Provider = new ethers.BrowserProvider(provider);

setBalance(expectedEthAddress, ethers.utils.parseEther("999999"));
setBalance(expectedEthAddress, ethers.parseEther("999999"));
});

testCase("basics for connected signer", async () => {
expect(ethers.Signer.isSigner(connectedSigner)).toBe(true);
expect(connectedSigner.signMessage).toBeTruthy();
expect(await connectedSigner.getAddress()).toBe(expectedEthAddress);
});

testCase("basics for connected signer via constructor", async () => {
expect(ethers.Signer.isSigner(signerWithProvider)).toBe(true);
expect(signerWithProvider.signMessage).toBeTruthy();
expect(await signerWithProvider.getAddress()).toBe(expectedEthAddress);
});

testCase("it signs transactions", async () => {
const tx = await connectedSigner.signTransaction({
to: "0x2Ad9eA1E677949a536A270CEC812D6e868C88108",
value: ethers.utils.parseEther("1.0"),
value: ethers.parseEther("1.0"),
chainId,
nonce: 0,
gasLimit: 21000,
Expand All @@ -128,7 +128,7 @@ describe("TurnkeySigner", () => {
const goodTx = await connectedSigner.signTransaction({
from: expectedEthAddress,
to: "0x2Ad9eA1E677949a536A270CEC812D6e868C88108",
value: ethers.utils.parseEther("1.0"),
value: ethers.parseEther("1.0"),
chainId,
nonce: 0,
gasLimit: 21000,
Expand All @@ -145,7 +145,7 @@ describe("TurnkeySigner", () => {
await connectedSigner.signTransaction({
from: badFromAddress,
to: "0x2Ad9eA1E677949a536A270CEC812D6e868C88108",
value: ethers.utils.parseEther("1.0"),
value: ethers.parseEther("1.0"),
chainId,
nonce: 0,
gasLimit: 21000,
Expand All @@ -165,15 +165,15 @@ describe("TurnkeySigner", () => {
testCase("it sends transactions", async () => {
const tx = await connectedSigner.sendTransaction({
to: "0x2Ad9eA1E677949a536A270CEC812D6e868C88108",
value: ethers.utils.parseEther("2"),
value: ethers.parseEther("2"),
type: 2,
});
const receipt = await tx.wait();

expect(receipt.status).toBe(1);
expect(receipt.type).toBe(2);
expect(receipt.from).toBe(expectedEthAddress);
expect(receipt.transactionHash).toMatch(/^0x/);
expect(receipt!.status).toBe(1);
expect(receipt!.type).toBe(2);
expect(receipt!.from).toBe(expectedEthAddress);
expect(receipt!.hash).toMatch(/^0x/);
});

testCase(
Expand All @@ -182,7 +182,7 @@ describe("TurnkeySigner", () => {
try {
await connectedSigner.signTransaction({
to: bannedToAddress,
value: ethers.utils.parseEther("1.0"),
value: ethers.parseEther("1.0"),
chainId,
nonce: 0,
gasLimit: 21000,
Expand Down Expand Up @@ -222,7 +222,7 @@ describe("TurnkeySigner", () => {
const signMessageSignature = await connectedSigner.signMessage(message);

expect(signMessageSignature).toMatch(/^0x/);
expect(ethers.utils.verifyMessage(message, signMessageSignature)).toEqual(
expect(ethers.verifyMessage(message, signMessageSignature)).toEqual(
expectedEthAddress
);
});
Expand Down Expand Up @@ -256,7 +256,7 @@ describe("TurnkeySigner", () => {

expect(signTypedDataSignature).toMatch(/^0x/);
expect(
ethers.utils.verifyTypedData(
ethers.verifyTypedData(
typedData.domain,
typedData.types,
typedData.message,
Expand Down Expand Up @@ -290,32 +290,29 @@ describe("TurnkeySigner", () => {
// In a real-world scenario you should also verify that `from` matches the wallet's address
delete payload.params[0].from;

const tx = await eip1193.request(payload);
const tx = await eip1193Provider.request(payload);
expect(tx).toMatch(/^0x/);
});
});

// Use `pnpm run compile:contracts` to update the ABI if needed
testCase("ERC-721", async () => {
const { abi, bytecode } = Test721;
const factory = new ethers.ContractFactory(abi, bytecode).connect(
connectedSigner
);
// Deploy contract
const contract = await new Test721Factory(connectedSigner).deploy();

// Deploy
const contract = await factory.deploy();
await contract.deployed();
const deploymentAddress = await contract.getAddress();
const deploymentTransaction = await contract.deploymentTransaction();

expect(contract.address).toMatch(/^0x/);
expect(contract.deployTransaction.from).toEqual(expectedEthAddress);
expect(deploymentAddress).toMatch(/^0x/);
expect(deploymentTransaction!.from).toEqual(expectedEthAddress);

// Mint
const mintTx = await contract.safeMint(expectedEthAddress);
await mintTx.wait();

expect(mintTx.hash).toMatch(/^0x/);
expect(mintTx.from).toEqual(expectedEthAddress);
expect(mintTx.to).toEqual(contract.address);
expect(mintTx.to).toEqual(deploymentAddress);

// Approve
const approveTx = await contract.approve(
Expand All @@ -326,7 +323,7 @@ describe("TurnkeySigner", () => {

expect(approveTx.hash).toMatch(/^0x/);
expect(approveTx.from).toEqual(expectedEthAddress);
expect(approveTx.to).toEqual(contract.address);
expect(approveTx.to).toEqual(deploymentAddress);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type {
BaseContract,
BytesLike,
FunctionFragment,
Result,
Interface,
EventFragment,
AddressLike,
ContractRunner,
ContractMethod,
Listener,
} from "ethers";
import type {
TypedContractEvent,
TypedDeferredTopicFilter,
TypedEventLog,
TypedLogDescription,
TypedListener,
TypedContractMethod,
} from "../../../common";

export interface OwnableInterface extends Interface {
getFunction(
nameOrSignature: "owner" | "renounceOwnership" | "transferOwnership"
): FunctionFragment;

getEvent(nameOrSignatureOrTopic: "OwnershipTransferred"): EventFragment;

encodeFunctionData(functionFragment: "owner", values?: undefined): string;
encodeFunctionData(
functionFragment: "renounceOwnership",
values?: undefined
): string;
encodeFunctionData(
functionFragment: "transferOwnership",
values: [AddressLike]
): string;

decodeFunctionResult(functionFragment: "owner", data: BytesLike): Result;
decodeFunctionResult(
functionFragment: "renounceOwnership",
data: BytesLike
): Result;
decodeFunctionResult(
functionFragment: "transferOwnership",
data: BytesLike
): Result;
}

export namespace OwnershipTransferredEvent {
export type InputTuple = [previousOwner: AddressLike, newOwner: AddressLike];
export type OutputTuple = [previousOwner: string, newOwner: string];
export interface OutputObject {
previousOwner: string;
newOwner: string;
}
export type Event = TypedContractEvent<InputTuple, OutputTuple, OutputObject>;
export type Filter = TypedDeferredTopicFilter<Event>;
export type Log = TypedEventLog<Event>;
export type LogDescription = TypedLogDescription<Event>;
}

export interface Ownable extends BaseContract {
connect(runner?: ContractRunner | null): Ownable;
waitForDeployment(): Promise<this>;

interface: OwnableInterface;

queryFilter<TCEvent extends TypedContractEvent>(
event: TCEvent,
fromBlockOrBlockhash?: string | number | undefined,
toBlock?: string | number | undefined
): Promise<Array<TypedEventLog<TCEvent>>>;
queryFilter<TCEvent extends TypedContractEvent>(
filter: TypedDeferredTopicFilter<TCEvent>,
fromBlockOrBlockhash?: string | number | undefined,
toBlock?: string | number | undefined
): Promise<Array<TypedEventLog<TCEvent>>>;

on<TCEvent extends TypedContractEvent>(
event: TCEvent,
listener: TypedListener<TCEvent>
): Promise<this>;
on<TCEvent extends TypedContractEvent>(
filter: TypedDeferredTopicFilter<TCEvent>,
listener: TypedListener<TCEvent>
): Promise<this>;

once<TCEvent extends TypedContractEvent>(
event: TCEvent,
listener: TypedListener<TCEvent>
): Promise<this>;
once<TCEvent extends TypedContractEvent>(
filter: TypedDeferredTopicFilter<TCEvent>,
listener: TypedListener<TCEvent>
): Promise<this>;

listeners<TCEvent extends TypedContractEvent>(
event: TCEvent
): Promise<Array<TypedListener<TCEvent>>>;
listeners(eventName?: string): Promise<Array<Listener>>;
removeAllListeners<TCEvent extends TypedContractEvent>(
event?: TCEvent
): Promise<this>;

owner: TypedContractMethod<[], [string], "view">;

renounceOwnership: TypedContractMethod<[], [void], "nonpayable">;

transferOwnership: TypedContractMethod<
[newOwner: AddressLike],
[void],
"nonpayable"
>;

getFunction<T extends ContractMethod = ContractMethod>(
key: string | FunctionFragment
): T;

getFunction(
nameOrSignature: "owner"
): TypedContractMethod<[], [string], "view">;
getFunction(
nameOrSignature: "renounceOwnership"
): TypedContractMethod<[], [void], "nonpayable">;
getFunction(
nameOrSignature: "transferOwnership"
): TypedContractMethod<[newOwner: AddressLike], [void], "nonpayable">;

getEvent(
key: "OwnershipTransferred"
): TypedContractEvent<
OwnershipTransferredEvent.InputTuple,
OwnershipTransferredEvent.OutputTuple,
OwnershipTransferredEvent.OutputObject
>;

filters: {
"OwnershipTransferred(address,address)": TypedContractEvent<
OwnershipTransferredEvent.InputTuple,
OwnershipTransferredEvent.OutputTuple,
OwnershipTransferredEvent.OutputObject
>;
OwnershipTransferred: TypedContractEvent<
OwnershipTransferredEvent.InputTuple,
OwnershipTransferredEvent.OutputTuple,
OwnershipTransferredEvent.OutputObject
>;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
export type { Ownable } from "./Ownable";
Loading

0 comments on commit 72c7974

Please sign in to comment.