Skip to content

Commit 07ab0c6

Browse files
committed
test(feature): added mockprovider, function call checks, DAI balance checks, split user/owner
1 parent a1e6839 commit 07ab0c6

File tree

6 files changed

+267
-184
lines changed

6 files changed

+267
-184
lines changed

contracts/SinglePlayerCommit.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import "@openzeppelin/contracts/access/Ownable.sol";
77
import "@openzeppelin/contracts/math/SafeMath.sol";
88
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
99

10-
//TODO Activity deployed in test now only has name/oracle/allowed initialized
11-
//TODO Do we want console.log logging by buidler? Which level?
10+
//TODO Do we want console.log logging, start with Activity constructor?..
1211

1312
contract SinglePlayerCommit is Ownable {
1413
using SafeMath for uint256;
@@ -152,6 +151,7 @@ contract SinglePlayerCommit is Ownable {
152151
}
153152

154153
function deposit(uint256 amount) public returns (bool) {
154+
console.log("Received call for depositing drawing amount %s from sender %s", amount, msg.sender);
155155
// make deposit
156156
require(token.transferFrom(msg.sender, address(this), amount), "SPC::deposit - token transfer failed");
157157

@@ -217,6 +217,7 @@ contract SinglePlayerCommit is Ownable {
217217
}
218218

219219
function withdraw(uint256 amount) public returns (bool) {
220+
console.log("Received call for withdrawing amount %s from sender %s", amount, msg.sender);
220221
uint256 available = balances[msg.sender].sub(commitments[msg.sender].stake);
221222
require(amount >= available, "SPC::withdraw - not enough balance available");
222223

test/SinglePlayerCommit.commitment.ts

Lines changed: 0 additions & 163 deletions
This file was deleted.

test/SinglePlayerCommit.deploy.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { expect } from "chai";
22
import { BytesLike } from "ethers/lib/utils";
33

4-
//TODO Need provider that has a state/memory for check on contract calls (function().to.be.calledOnContract()). Like the one from Waffle?
5-
//Example expect('_deposit').to.be.calledOnContract(this.singlePlayerCommit);
6-
//TODO Now expecting VM Runtime 'invalid opcode' error.
7-
// Should/can this be prevented in contract while using dynamic arrays? Look at revert()
8-
94
export function shouldDeployWithInitialParameters(): void {
105

116
it("has the 'biking' activity and it is allowed", async function () {
@@ -20,7 +15,7 @@ export function shouldDeployWithInitialParameters(): void {
2015
// expect(_activity['ranges']).to.equal([2,1024]);
2116
expect(_activity['oracle']).to.be.properAddress;
2217
expect(_activity['allowed']).to.be.true;
23-
// expect('getActivityName').to.be.calledOnContract(this.singlePlayerCommit);
18+
expect('getActivityName').to.be.calledOnContract(this.singlePlayerCommit);
2419
});
2520

2621
it("has no other activities", async function () {
@@ -30,7 +25,8 @@ export function shouldDeployWithInitialParameters(): void {
3025
} catch(err) {
3126
error = err;
3227
} finally {
33-
expect(error.message).to.equal('Transaction reverted without a reason');
28+
expect('getActivityName').to.be.calledOnContract(this.singlePlayerCommit);
29+
expect(error.results[error.hashes[0]].error).to.equal('invalid opcode');
3430
}
3531
});
3632

@@ -50,7 +46,7 @@ export function shouldDeployWithInitialParameters(): void {
5046
} catch(err) {
5147
error = err;
5248
} finally {
53-
expect(error.message).to.equal('Transaction reverted without a reason');
49+
expect(error.results[error.hashes[0]].error).to.equal('invalid opcode');
5450
}
5551
});
5652

test/SinglePlayerCommit.owner.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect } from "chai";
2+
import { ethers } from "@nomiclabs/buidler";
3+
import { Signer, utils, BigNumber, BytesLike } from "ethers";
4+
import { SinglePlayerCommit } from "../typechain/SinglePlayerCommit";
5+
6+
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+
});
49+
});
50+
}

test/SinglePlayerCommit.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//Setup
22
import chai from "chai";
33
import { ethers } from "@nomiclabs/buidler";
4-
import { BigNumberish, Signer, ContractFactory } from "ethers";
5-
import { deployMockContract, MockContract, solidity } from "ethereum-waffle";
4+
import { BigNumberish, Signer, ContractFactory, Wallet } from "ethers";
5+
import { createFixtureLoader, deployMockContract, loadFixture, MockContract, MockProvider, solidity } from "ethereum-waffle";
66

77
//Artifacts
88
import { SinglePlayerCommit } from "../typechain/SinglePlayerCommit";
@@ -11,29 +11,29 @@ import chainLinkArtifact from "./resources/ChainLink.json";
1111

1212
//Test suites
1313
import { shouldDeployWithInitialParameters } from "./SinglePlayerCommit.deploy";
14-
import { shouldManageCommitments } from "./SinglePlayerCommit.commitment";
14+
import { userCanManageCommitments } from "./SinglePlayerCommit.user";
15+
import { ownerCanManageContract } from "./SinglePlayerCommit.owner";
1516

1617
chai.use(solidity);
1718

1819
setTimeout(async function () {
1920
describe("SinglePlayerCommit contract", async function () {
2021
let accounts: Signer[];
2122
let owner: Signer;
23+
2224
const activity: string = "biking";
2325
const measures: string[] = ["km"];
2426
const ranges: BigNumberish[][] = [[2, 1024]];
2527

2628
before(async function () {
27-
console.log("Getting accounts");
29+
console.log("Setting up environment [provider, signers, mock contracts]")
30+
ethers.provider = new MockProvider();
2831
accounts = await ethers.getSigners();
2932
owner = accounts[0];
30-
console.log("Owner set to ", await owner.getAddress());
31-
32-
console.log("Setting up mock objects for ChainLink and DAI");
3333
this.oracle = await deployMockContract(owner, chainLinkArtifact) as MockContract;
3434
this.token = await deployMockContract(owner, daiArtifact) as MockContract;
3535

36-
console.log("Deploying SPCommit with %s, %s, and %s", activity, measures, ranges[0]);
36+
console.log("Deploying SinglePlayerCommit with %s, %s, and %s", activity, measures, ranges[0]);
3737
const SinglePlayerCommit: ContractFactory = await ethers.getContractFactory("SinglePlayerCommit");
3838

3939
this.singlePlayerCommit = (await SinglePlayerCommit.deploy(
@@ -45,16 +45,17 @@ setTimeout(async function () {
4545
)) as SinglePlayerCommit;
4646
await this.singlePlayerCommit.deployed();
4747

48-
console.log("SinglePlayerCommit contract deployed to ", this.singlePlayerCommit.address);
48+
console.log("SinglePlayerCommit deployed to ", this.singlePlayerCommit.address);
4949

5050
});
5151

5252
describe("Deployment was succesful if contract", function () {
5353
shouldDeployWithInitialParameters();
5454
});
5555

56-
describe("Commitments can be managed", function () {
57-
shouldManageCommitments();
56+
describe("Contract interactions", function () {
57+
ownerCanManageContract();
58+
userCanManageCommitments();
5859
});
5960
});
6061

0 commit comments

Comments
 (0)