Skip to content

Commit e20f9a7

Browse files
committed
test: getBalance of contract
1 parent 1e2dbd5 commit e20f9a7

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.19;
4+
5+
contract Receive {
6+
function give() external payable{}
7+
}

client/src/tests/eth/getBalance.ts

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
import { blockchain, wallet } from "../../tests";
1+
import * as tests from "../../tests";
2+
import Receive from "./getBalance.sol";
23
import assert from "assert";
4+
import { ethers } from "ethers";
5+
6+
const blockchain = tests.blockchain;
7+
const wallet = tests.wallet;
8+
9+
if (!blockchain || !wallet) {
10+
throw "not ready";
11+
}
312

413
describe("getBalance", () => {
5-
it("sending ether to address increases balance", async () => {
6-
if (!blockchain || !wallet) {
7-
throw "not ready";
8-
}
14+
let contract: ethers.Contract;
15+
before(async () => {
16+
const deployer = (await blockchain.listAccounts())[0];
17+
const factory = ethers.ContractFactory.fromSolidity(
18+
Receive.Receive,
19+
deployer
20+
);
921

22+
contract = await factory.deploy();
23+
await blockchain.send("evm_mine", [{ blocks: 1 }]);
24+
await contract.deploymentTransaction()?.wait(1);
25+
});
26+
27+
it("sending ether to address increases balance", async () => {
1028
const src = (await blockchain.listAccounts())[0];
1129
const dest = (await wallet.listAccounts())[0];
1230

@@ -49,4 +67,44 @@ describe("getBalance", () => {
4967

5068
assert.equal(walletFinalBalance, expected);
5169
});
70+
71+
it("sending ether to contract increases balance", async () => {
72+
const src = (await blockchain.listAccounts())[0];
73+
74+
const balance = "0x100000000000000000000";
75+
await blockchain.send("evm_setAccountBalance", [src.address, balance]);
76+
77+
const walletInitalBalance = await wallet.getBalance(contract);
78+
const ganacheInitalBalance = await blockchain.getBalance(contract);
79+
80+
assert.equal(
81+
walletInitalBalance.toString(),
82+
ganacheInitalBalance.toString(),
83+
"initalBalance"
84+
);
85+
86+
const value = 1n;
87+
const response = await contract.give({ value });
88+
89+
await blockchain.send("evm_mine", [{ blocks: 5000 }]);
90+
91+
const transaction = await wallet.getTransaction(response.hash);
92+
if (!transaction) {
93+
throw "no transaction";
94+
}
95+
await transaction.wait(10);
96+
97+
const walletFinalBalance = await wallet.getBalance(contract);
98+
const ganacheFinalBalance = await blockchain.getBalance(contract);
99+
100+
assert.equal(
101+
walletFinalBalance.toString(),
102+
ganacheFinalBalance.toString(),
103+
"finalBalance"
104+
);
105+
106+
const expected = value + walletInitalBalance;
107+
108+
assert.equal(walletFinalBalance, expected);
109+
});
52110
});

0 commit comments

Comments
 (0)