Skip to content

Commit 2b4a993

Browse files
committed
test(feature): owner specific interactions tested
1 parent 292688a commit 2b4a993

File tree

2 files changed

+119
-43
lines changed

2 files changed

+119
-43
lines changed

test/SinglePlayerCommit.owner.ts

Lines changed: 117 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,124 @@ import { expect } from "chai";
22
import { ethers } from "@nomiclabs/buidler";
33
import { Signer, utils, BigNumber, BytesLike } from "ethers";
44
import { SinglePlayerCommit } from "../typechain/SinglePlayerCommit";
5+
import { Address } from "cluster";
56

67
export function ownerCanManageContract(): void {
7-
context("Owner can", function () {
8-
let owner: Signer;
9-
let contractWithOwner: SinglePlayerCommit;
10-
const _overrides = {
11-
gasLimit: 1000000,
12-
};
13-
14-
before(async function () {
15-
[owner] = await ethers.getSigners();
16-
contractWithOwner = await this.singlePlayerCommit.connect(owner);
17-
});
18-
19-
it("withdraw non-committed balance", async function () {
20-
//Owner balance
21-
const _ownerBalance: BigNumber = await owner.getBalance();
22-
// console.log("Owner balance: ", utils.formatEther(_ownerBalance));
23-
24-
//Committer balance
25-
const _committerBalance: BigNumber = await this.singlePlayerCommit.committerBalance();
26-
// console.log("Committer balance: ", utils.formatEther(_committerBalance));
27-
expect(_committerBalance).to.equal(utils.parseEther("0.0"));
28-
29-
//Contract balance
30-
const margin = utils.parseEther("10");
31-
await this.token.mock.balanceOf.returns(_committerBalance.add(margin));
32-
33-
const _contractBalance: BigNumber = await this.token.balanceOf(contractWithOwner.address);
34-
// console.log("Contract balance: ", utils.formatEther(_contractBalance));
35-
36-
//Transaction
37-
await this.token.mock.transfer.returns(true);
38-
await contractWithOwner.ownerWithdraw(10, _overrides);
39-
40-
//Validate
41-
const _updatedOwnerBalance: BigNumber = await owner.getBalance();
42-
const _updatedCommitterBalance: BigNumber = await this.singlePlayerCommit.committerBalance.call();
43-
44-
expect(1).to.equal(1);
45-
46-
// expect(_updatedOwnerBalance.gt(_ownerBalance)).to.be.true;
47-
// expect(_updatedCommitterBalance.lt(_committerBalance)).to.be.true;
48-
});
8+
context("Owner can", function () {
9+
let owner: Signer;
10+
let contractWithOwner: SinglePlayerCommit;
11+
let contractAddress;
12+
let ownerAddress: string;
13+
const _overrides = {
14+
gasLimit: 1000000,
15+
};
16+
17+
before(async function () {
18+
[owner] = await ethers.getSigners();
19+
contractWithOwner = await this.singlePlayerCommit.connect(owner);
20+
ownerAddress = await owner.getAddress()
21+
});
22+
23+
it("withdraw non-committer balance", async function () {
24+
//Owner balance in wallet [ETH] and contract [DAI]
25+
const _ownerBalance: BigNumber = await owner.getBalance();
26+
expect(_ownerBalance.lt(utils.parseEther("10000000000000000.0"))).to.be.true;
27+
const _ownerDaiBalanceInContract: BigNumber = await this.singlePlayerCommit.balances(ownerAddress);
28+
expect(_ownerDaiBalanceInContract).to.equal(utils.parseEther("0.0"));
29+
30+
//Committer balance on SinglePlayerCommit contract 100 DAI
31+
// Deposit funds in contract
32+
const _amountToDeposit: BigNumber = utils.parseEther("100.0");
33+
await this.token.mock.transferFrom.returns(true);
34+
await expect(contractWithOwner.deposit(_amountToDeposit, _overrides))
35+
.to.emit(this.singlePlayerCommit, "Deposit")
36+
.withArgs(await owner.getAddress(), _amountToDeposit);
37+
expect("transferFrom").to.be.calledOnContract(this.token);
38+
39+
const _committerBalance: BigNumber = await this.singlePlayerCommit.committerBalance();
40+
expect(_committerBalance).to.equal(_amountToDeposit);
41+
42+
//Committer balance on DAI contract 1000 DAI
43+
await this.token.mock.balanceOf.withArgs(contractWithOwner.address).returns(utils.parseEther("1000"));
44+
45+
//Transaction
46+
let _amountToWithdraw: BigNumber = utils.parseEther("800.0");
47+
48+
await this.token.mock.transfer.returns(true);
49+
await contractWithOwner.ownerWithdraw(_amountToWithdraw, _overrides)
50+
51+
expect("balanceOf").to.be.calledOnContract(this.token);
52+
expect("transfer").to.be.calledOnContract(this.token);
53+
54+
//Validate
55+
const _updatedOwnerBalance: BigNumber = await owner.getBalance();
56+
const _updatedOwnerDaiBalanceInContract: BigNumber = await this.singlePlayerCommit.balances(ownerAddress);
57+
const _updatedCommitterBalance: BigNumber = await this.singlePlayerCommit.committerBalance.call();
58+
59+
expect(_updatedOwnerBalance.lt(_ownerBalance)).to.be.true;
60+
expect(_updatedOwnerDaiBalanceInContract.eq(_amountToDeposit)).to.be.true;
61+
expect(_updatedCommitterBalance.eq(_amountToDeposit)).to.be.true;
62+
63+
//Transaction to clean up balance
64+
_amountToWithdraw = utils.parseEther("100.0");
65+
66+
await this.token.mock.transfer.returns(true);
67+
await expect(contractWithOwner.withdraw(_amountToWithdraw, _overrides))
68+
.to.emit(this.singlePlayerCommit, "Withdrawal")
69+
.withArgs(await owner.getAddress(), _amountToWithdraw);
70+
expect("transfer").to.be.calledOnContract(this.token);
71+
});
72+
73+
74+
it("not withdraw balance that belongs to a committer", async function () {
75+
//Owner balance in wallet [ETH] and contract [DAI]
76+
const _ownerBalance: BigNumber = await owner.getBalance();
77+
expect(_ownerBalance.lt(utils.parseEther("10000000000000000.0"))).to.be.true;
78+
const _ownerDaiBalanceInContract: BigNumber = await this.singlePlayerCommit.balances(ownerAddress);
79+
expect(_ownerDaiBalanceInContract).to.equal(utils.parseEther("0.0"));
80+
81+
//Committer balance on SinglePlayerCommit contract 100 DAI
82+
// Deposit funds in contract
83+
const _amountToDeposit: BigNumber = utils.parseEther("100.0");
84+
await this.token.mock.transferFrom.returns(true);
85+
await expect(contractWithOwner.deposit(_amountToDeposit, _overrides))
86+
.to.emit(this.singlePlayerCommit, "Deposit")
87+
.withArgs(await owner.getAddress(), _amountToDeposit);
88+
expect("transferFrom").to.be.calledOnContract(this.token);
89+
90+
const _committerBalance: BigNumber = await this.singlePlayerCommit.committerBalance();
91+
expect(_committerBalance).to.equal(_amountToDeposit);
92+
93+
//Committer balance on DAI contract 1000 DAI
94+
await this.token.mock.balanceOf.withArgs(contractWithOwner.address).returns(utils.parseEther("1000"));
95+
96+
//Transaction
97+
let _amountToWithdraw: BigNumber = utils.parseEther("1200.0");
98+
99+
await this.token.mock.transfer.returns(true);
100+
await expect(
101+
contractWithOwner.ownerWithdraw(_amountToWithdraw, _overrides),
102+
).to.be.revertedWith("SPC::ownerWithdraw - not enough available balance")
103+
104+
expect("balanceOf").to.be.calledOnContract(this.token);
105+
106+
//Validate
107+
const _updatedOwnerBalance: BigNumber = await owner.getBalance();
108+
const _updatedOwnerDaiBalanceInContract: BigNumber = await this.singlePlayerCommit.balances(ownerAddress);
109+
const _updatedCommitterBalance: BigNumber = await this.singlePlayerCommit.committerBalance.call();
110+
111+
expect(_updatedOwnerBalance.lt(_ownerBalance)).to.be.true;
112+
expect(_updatedOwnerDaiBalanceInContract.eq(_amountToDeposit)).to.be.true;
113+
expect(_updatedCommitterBalance.eq(_amountToDeposit)).to.be.true;
114+
115+
//Transaction to clean up balance
116+
_amountToWithdraw = utils.parseEther("100.0");
117+
118+
await this.token.mock.transfer.returns(true);
119+
await expect(contractWithOwner.withdraw(_amountToWithdraw, _overrides))
120+
.to.emit(this.singlePlayerCommit, "Withdrawal")
121+
.withArgs(await owner.getAddress(), _amountToWithdraw);
122+
expect("transfer").to.be.calledOnContract(this.token);
49123
});
124+
});
50125
}

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"resolveJsonModule": true,
1111
"sourceMap": true,
1212
"strict": true,
13-
"target": "es5"
13+
"target": "es5",
14+
"types": ["node"]
1415
},
1516
"exclude": ["node_modules"],
1617
"include": [

0 commit comments

Comments
 (0)