-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
622c31a
commit ea875fb
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |