Skip to content

Commit 16b8e1c

Browse files
committed
test(bdd): split features. Improved deployment, working on commitment management
1 parent e3a7dce commit 16b8e1c

File tree

6 files changed

+103
-64
lines changed

6 files changed

+103
-64
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,4 @@ This repository is a fork from [Paul Berg's Solidity Template](https://github.co
5454

5555
5656

57-
Subscribe to our [Substack](https://commit.substack.com/)
58-
59-
60-
57+
Subscribe to our [Substack](https://commit.substack.com/)

contracts/SinglePlayerCommit.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ contract SinglePlayerCommit is Ownable {
2424
bool allowed;
2525
}
2626

27+
//TODO Activity deployed in test now only has name/oracle/allowed initialized
2728
struct Activity {
2829
string name; // e.g. "cycling"
2930
bytes32[] measures; // keys from allowedMeasures

test/SinglePlayerCommit.behavior.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { expect } from "chai";
2+
import { Wallet, utils } from "ethers";
3+
import { BytesLike } from "ethers/lib/utils";
4+
import { SinglePlayerCommit } from "../typechain/SinglePlayerCommit";
5+
6+
export function shouldManageCommitments(wallet: Wallet, walletTo: Wallet): void {
7+
8+
it("allows the user to deposit for staking", async function () {
9+
// const _activity = "biking";
10+
// const _measureIndex = await this.singlePlayerCommit.measureList(0);
11+
console.log(await walletTo.getBalance());
12+
const _amount = utils.parseEther("1.0");
13+
let contractWithSigner = await this.singlePlayerCommit.connect(walletTo);
14+
console.log("Connected to contract")
15+
let output = await contractWithSigner.deposit(_amount);
16+
console.log(output)
17+
18+
expect(1).to.equal(1);
19+
});
20+
21+
it.skip("allows users with sufficient staked funds to stake on a commitment of biking 50 kms", async function () {
22+
// const _activity = "biking";
23+
// const _measureIndex = await this.singlePlayerCommit.measureList(0);
24+
console.log(await walletTo.getBalance());
25+
const _activity = await this.singlePlayerCommit.activityList(0);
26+
const _measureIndex = 0
27+
const _goal = 50;
28+
const _startTime = Date.now();
29+
const _stake = 10;
30+
let contractWithSigner = await this.singlePlayerCommit.connect(walletTo);
31+
let output = await contractWithSigner.makeCommitment(_activity, _measureIndex, _goal, _startTime, _stake);
32+
console.log(output)
33+
34+
expect(1).to.equal(1);
35+
});
36+
37+
}

test/SinglePlayerCommit.deploy.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { expect } from "chai";
2+
import { BytesLike } from "ethers/lib/utils";
3+
import { SinglePlayerCommit } from "../typechain/SinglePlayerCommit";
4+
5+
export function shouldDeployWithInitialParameters(): void {
6+
7+
it("has the 'biking' activity and it is allowed", async function () {
8+
const activityKey: BytesLike = await this.singlePlayerCommit.activityList(0);
9+
const _activityName: string = await this.singlePlayerCommit.getActivityName(activityKey);
10+
11+
const _activity = await this.singlePlayerCommit.allowedActivities(activityKey);
12+
13+
expect(_activityName).to.equal('biking');
14+
expect(_activity['name']).to.equal(_activityName);
15+
// expect(_activity['measures'][0]).to.equal('km');
16+
// expect(_activity['ranges']).to.equal([2,1024]);
17+
expect(_activity['oracle']).to.be.properAddress;
18+
expect(_activity['allowed']).to.be.true;
19+
expect('getActivityName').to.be.calledOnContract(this.singlePlayerCommit);
20+
});
21+
22+
//TODO Now expecting VM Runtime 'invalid opcode' error.
23+
// Should/can this be prevented in contract while using dynamic arrays?
24+
it("has no other activities", async function () {
25+
let error;
26+
try{
27+
await this.singlePlayerCommit.activityList(1)
28+
} catch(err) {
29+
error = err;
30+
} finally {
31+
expect('activityList').to.be.calledOnContract(this.singlePlayerCommit);
32+
expect(error.results[error.hashes[0]]['error']).to.equal('invalid opcode');
33+
}
34+
});
35+
36+
it("has the 'km' measure and it is allowed", async function () {
37+
const measureKey: BytesLike = await this.singlePlayerCommit.measureList(0);
38+
const activityMeasure: string[] = await this.singlePlayerCommit.allowedMeasures(measureKey);
39+
40+
expect(activityMeasure[0]).to.equal('km');
41+
expect(activityMeasure[1]).to.be.true;
42+
43+
});
44+
45+
it("has no other measures", async function () {
46+
let error;
47+
try{
48+
await this.singlePlayerCommit.measureList(1)
49+
} catch(err) {
50+
error = err;
51+
} finally {
52+
expect('measureList').to.be.calledOnContract(this.singlePlayerCommit);
53+
expect(error.results[error.hashes[0]]['error']).to.equal('invalid opcode');
54+
}
55+
});
56+
57+
}

test/SinglePlayerCommit.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import chainLinkArtifact from "./resources/ChainLink.json";
77

88
import { SinglePlayerCommit } from "../typechain/SinglePlayerCommit";
99
import { BigNumberish } from "ethers";
10-
import { shouldDeployWithInitialParameters, shouldAllowUserToMakeCommitment, shouldSettleCompletedCommitment } from "./SinglePlayerCommit.behavior";
10+
import { shouldDeployWithInitialParameters } from "./SinglePlayerCommit.deploy";
11+
import { shouldManageCommitments } from './SinglePlayerCommit.commitment';
1112

1213
chai.use(solidity);
1314

1415
setTimeout(async function () {
15-
const provider = new MockProvider();
16-
const [wallet] = provider.getWallets();
16+
const [wallet, walletTo] = new MockProvider().getWallets();
1717

1818
describe("SinglePlayerCommit contract", function () {
19+
1920
const activity: string = "biking";
2021
const measures: string[] = ["km"];
2122
const ranges: BigNumberish[][] = [[2, 1024]];
@@ -36,17 +37,11 @@ setTimeout(async function () {
3637
});
3738

3839
describe("Deployment was succesful if contract", function () {
39-
shouldDeployWithInitialParameters(activity, measures);
40-
});
41-
42-
//TODO User can manage commitments
43-
describe("User can", function() {
44-
shouldAllowUserToMakeCommitment(activity, measures);
40+
shouldDeployWithInitialParameters();
4541
});
4642

47-
//TODO Contract pays out stake upon completion
48-
describe("Contract can", function() {
49-
shouldSettleCompletedCommitment(activity, measures);
43+
describe("Commitments can be managed", function() {
44+
shouldManageCommitments(wallet, walletTo);
5045
});
5146
});
5247

0 commit comments

Comments
 (0)