From ea875fb4bf79936c7e2f85121534a847bae6105a Mon Sep 17 00:00:00 2001 From: Oren Date: Mon, 16 Oct 2023 15:44:00 +0300 Subject: [PATCH] Add contract call tests --- test/ethers/contractCall.test.ts | 75 ++++++++++++++++++++++++++++++++ test/utils.ts | 2 + test/web3/contractCall.test.ts | 68 +++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 test/ethers/contractCall.test.ts create mode 100644 test/web3/contractCall.test.ts diff --git a/test/ethers/contractCall.test.ts b/test/ethers/contractCall.test.ts new file mode 100644 index 0000000..22edf9c --- /dev/null +++ b/test/ethers/contractCall.test.ts @@ -0,0 +1,75 @@ +import { expect } from "chai" +import * as ethers from "ethers" +import { getEthersFireblocksProviderForTesting } from "../utils" + +const minAmount = ethers.utils.parseEther("0.01") +const provider = getEthersFireblocksProviderForTesting() +const GREETER_ADDRESS = "0x8A470A36a1BDE8B18949599a061892f6B2c4fFAb" +const GREETER_ABI = [ + { + "type": "function", + "name": "greet", + "inputs": [], + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "view", + }, + { + "type": "function", + "name": "setGreeting", + "inputs": [ + { + "internalType": "string", + "name": "_greeting", + "type": "string" + } + ], + "outputs": [], + "stateMutability": "nonpayable", + } +] +const greeting = (new Date()).toISOString() +let greeterContract = new ethers.Contract(GREETER_ADDRESS, GREETER_ABI, provider); + +async function getFirstAddressWithBalance() { + const addresses = await provider.listAccounts() + for (const address of addresses) { + const balance = await provider.getBalance(address) + if (balance.gt(minAmount)) { + return address.toLowerCase() + } + } + + throw new Error(`No vault has balance greater than ${minAmount.toString()}`) +} + +describe("Ethers: Should be able to call a contract method", function () { + this.timeout(600_000) + + it("greet() before", async function () { + const currentGreeting = await greeterContract.greet() + + expect(currentGreeting).to.not.be.equal(greeting) + }) + + it("setGreeting(greeting)", async function () { + const addresses = await provider.listAccounts() + const firstAddressWithBalance = await getFirstAddressWithBalance() + const tx = await greeterContract.connect(provider.getSigner(firstAddressWithBalance)).setGreeting(greeting) + + tx.wait() + + expect(tx.hash).to.be.not.undefined + }) + + it("greet() after", async function () { + const currentGreeting = await greeterContract.greet() + + expect(currentGreeting).to.be.equal(greeting) + }) +}) diff --git a/test/utils.ts b/test/utils.ts index 2e316da..99dff31 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -16,6 +16,8 @@ export function getFireblocksProviderForTesting(extraConfiguration?: any) { apiKey: process.env.FIREBLOCKS_API_KEY, vaultAccountIds: process.env.FIREBLOCKS_VAULT_ACCOUNT_IDS, chainId: ChainId.GOERLI, + rpcUrl: process.env.FIREBLOCKS_RPC_URL, + apiBaseUrl: process.env.FIREBLOCKS_API_BASE_URL, ...extraConfiguration } ) diff --git a/test/web3/contractCall.test.ts b/test/web3/contractCall.test.ts new file mode 100644 index 0000000..cb263d1 --- /dev/null +++ b/test/web3/contractCall.test.ts @@ -0,0 +1,68 @@ +import { expect } from "chai" +import { getWeb3FireblocksProviderForTesting } from "../utils" + +const GREETER_ADDRESS = "0x8A470A36a1BDE8B18949599a061892f6B2c4fFAb" +const GREETER_ABI = [ + { + "type": "function", + "name": "greet", + "inputs": [], + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ] + }, + { + "type": "function", + "name": "setGreeting", + "inputs": [ + { + "internalType": "string", + "name": "_greeting", + "type": "string" + } + ], + "outputs": [] + } +] +const provider = getWeb3FireblocksProviderForTesting() +const greeting = (new Date()).toISOString() +// @ts-ignore +const greeterContract = new provider.eth.Contract(GREETER_ABI, GREETER_ADDRESS) + +async function getFirstAddressWithBalance() { + const addresses = await provider.eth.getAccounts() + for (const address of addresses) { + const balance = await provider.eth.getBalance(address) + if (BigInt(balance) > BigInt(provider.utils.toWei('0.01', 'ether'))) { + return address.toLowerCase() + } + } + + throw new Error(`No vault has balance`) +} + +describe("Web3: Should be able to call a contract method", function () { + this.timeout(60_000) + + it("greet() before", async function () { + const currentGreeting = await greeterContract.methods.greet().call() + + expect(currentGreeting).to.not.be.equal(greeting) + }) + + it("setGreeting(greeting)", async function () { + const receipt = await greeterContract.methods.setGreeting(greeting).send({ from: await getFirstAddressWithBalance() }) + + expect(receipt.transactionHash).to.be.not.undefined + }) + + it("greet() after", async function () { + const currentGreeting = await greeterContract.methods.greet().call() + + expect(currentGreeting).to.be.equal(greeting) + }) +})