From abe6bc5f18382096c44624e5885cea388ba1151b Mon Sep 17 00:00:00 2001 From: arthurgousset <46296830+arthurgousset@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:28:19 +0100 Subject: [PATCH] test(transfer): moves tests and renames file - moves tests from `deploy.test.ts` to `transfer.test.ts` - renames file from `transfer.test.ts` to `transactions.test.ts` I moved the tests into a single file, because running them concurrently made the tests flakey. Specifically either the contract deployment or the first transfer test would fail because of a nonce issue. I expect it's because both tests query `eth_getTransactionCount` and get the same result milliseconds apart. So they submit transactions with the same nonce. --- tests/deploy.test.ts | 46 ---------- ...transfers.test.ts => transactions.test.ts} | 84 ++++++++++++++----- 2 files changed, 64 insertions(+), 66 deletions(-) delete mode 100644 tests/deploy.test.ts rename tests/{transfers.test.ts => transactions.test.ts} (69%) diff --git a/tests/deploy.test.ts b/tests/deploy.test.ts deleted file mode 100644 index 82fb986..0000000 --- a/tests/deploy.test.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { test, expect } from "@jest/globals"; -import { getSigner } from "./common"; -import { Contract, ContractFactory } from "ethers"; -import HelloWorldContract from "./HelloWorld"; -import { BLOCK_TIME } from "./consts"; - -let contract: ReturnType; -async function deployContract() { - if (contract) return contract; - - contract = new Promise(async (resolve) => { - const signer = getSigner(); - const factory = new ContractFactory( - HelloWorldContract.abi, - HelloWorldContract.bytecode, - signer - ); - resolve(await factory.deploy()); - }); - - return contract; -} - -test( - "can deploy a contract", - async () => { - const contract = await deployContract(); - const receipt = await contract.deploymentTransaction()?.wait(); - expect(receipt?.contractAddress).toMatch(/0x.{40}/); - }, - BLOCK_TIME * 3 -); - -test( - "can call a function on a newly deployedContract", - async () => { - const contract = await deployContract(); - await contract.deploymentTransaction()?.wait(); - - const txResponse = await (contract as Contract).setName("myName"); - const txReceipt = await txResponse.wait(); - - expect(txReceipt?.hash).toMatch(/0x.{40}/); - }, - BLOCK_TIME * 3 -); diff --git a/tests/transfers.test.ts b/tests/transactions.test.ts similarity index 69% rename from tests/transfers.test.ts rename to tests/transactions.test.ts index ec22032..9bf4d74 100644 --- a/tests/transfers.test.ts +++ b/tests/transactions.test.ts @@ -1,8 +1,9 @@ import { test, expect, describe } from "@jest/globals"; import { getSigner, getTransactionByHash, MINIMAL_USDC_ABI } from "./common"; import { BLOCK_TIME, USDC_ADAPTER_ALFAJORES_ADDRESS, USDC_ALFAJORES_ADDRESS } from "./consts"; -import { Contract } from "ethers"; +import { Contract, ContractFactory } from "ethers"; import { TxTypeToPrefix } from "../src/lib/transactions"; +import HelloWorldContract from "./HelloWorld"; const signer = getSigner(); const usdc = new Contract(USDC_ALFAJORES_ADDRESS, MINIMAL_USDC_ABI, signer); @@ -33,30 +34,30 @@ describe("[setup] supplied wallet has sufficient tokens to run tests", () => { describe("[ethereum-compatibility] when sending a transaction with gas in CELO, then the transaction is always Ethereum-compatible", () => { test( - "can transfer CELO with CELO as gas", - async () => { - const txResponse = await signer.sendTransaction({ - to: signer.address, - value: BigInt(1), - maxFeePerGas: BigInt(10e9) * BigInt(2) + BigInt(10e9), // ( base fee * 2 ) + tip - maxPriorityFeePerGas: BigInt(10e9), - }); - const txReceipt = await txResponse.wait(); - expect(txReceipt).not.toBeNull(); - const jsonRpcResponse = await getTransactionByHash(txReceipt!.hash); - - expect(jsonRpcResponse?.result.ethCompatible).toBe(true); // transaction is ethereum-compatible - expect(txReceipt?.type).toEqual(TxTypeToPrefix.eip1559); // transaction is EIP1559 - expect(txReceipt?.hash).toMatch(/0x.{40}/); // transaction is successful - }, - BLOCK_TIME * 3 - ); + "can transfer CELO with CELO as gas", + async () => { + const txResponse = await signer.sendTransaction({ + to: signer.address, + value: BigInt(1), + maxFeePerGas: BigInt(5e9) * BigInt(2) + BigInt(100e9), // ( base fee * 2 ) + tip + maxPriorityFeePerGas: BigInt(100e9), + }); + const txReceipt = await txResponse.wait(); + expect(txReceipt).not.toBeNull(); + const jsonRpcResponse = await getTransactionByHash(txReceipt!.hash); + + expect(jsonRpcResponse?.result.ethCompatible).toBe(true); // transaction is ethereum-compatible + expect(txReceipt?.type).toEqual(TxTypeToPrefix.eip1559); // transaction is EIP1559 + expect(txReceipt?.hash).toMatch(/0x.{40}/); // transaction is successful + }, + BLOCK_TIME * 3 + ); test( "can transfer CELO with CELO as gas, and estimate gas parameters", async () => { const txResponse = await signer.sendTransaction({ to: signer.address, - value: 1n, + value: BigInt(1), }); const txReceipt = await txResponse.wait(); expect(txReceipt).not.toBeNull(); @@ -143,3 +144,46 @@ describe("[fee currency support] when sending transactions with gas in fee curre BLOCK_TIME * 3 ); }); + +describe("Contract deployment", () => { + let contract: ReturnType; + async function deployContract() { + if (contract) return contract; + + contract = new Promise(async (resolve) => { + const signer = getSigner(); + const factory = new ContractFactory( + HelloWorldContract.abi, + HelloWorldContract.bytecode, + signer + ); + resolve(await factory.deploy()); + }); + + return contract; + } + + test( + "can deploy a contract", + async () => { + const contract = await deployContract(); + const receipt = await contract.deploymentTransaction()?.wait(); + expect(receipt?.contractAddress).toMatch(/0x.{40}/); + }, + BLOCK_TIME * 3 + ); + + test( + "can call a function on a newly deployedContract", + async () => { + const contract = await deployContract(); + await contract.deploymentTransaction()?.wait(); + + const txResponse = await (contract as Contract).setName("myName"); + const txReceipt = await txResponse.wait(); + + expect(txReceipt?.hash).toMatch(/0x.{40}/); + }, + BLOCK_TIME * 3 + ); +});