Skip to content

Commit

Permalink
Add contract call tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orenyomtov committed Oct 16, 2023
1 parent 622c31a commit ea875fb
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
75 changes: 75 additions & 0 deletions test/ethers/contractCall.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
2 changes: 2 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
)
Expand Down
68 changes: 68 additions & 0 deletions test/web3/contractCall.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit ea875fb

Please sign in to comment.