Skip to content

Commit feeb1c9

Browse files
JGcarvfrangio
authored andcommitted
reapply "Proposal for balance redesign (#13)"
* intial implementation * small refactoring * changed readme * implemente pr comments * fixed linter errors * addressed remaning comments * fixed integration test * removed array usage * using balanceCurrent for fetching tracker balance * fixed small issue in test
1 parent bd09743 commit feeb1c9

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

README.md

+27-7
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,36 @@ All returned numbers are of type [BN](https://github.com/indutny/bn.js).
4949
---
5050

5151
### balance
52-
#### async balance.current (account)
53-
Returns the current Ether balance of an account.
52+
Helper to keep track of ether balances of a specific account
5453

55-
#### async balance.difference (account, promiseFunc)
56-
Returns the change in the Ether balance of an account caused by executing `promiseFunc` (which will be awaited on).
54+
#### balance current
55+
##### async balance.current(account)
56+
Returns the current balance of an account
57+
```javascript
58+
const balance = await balance.current(account)
59+
```
5760

61+
#### balance tracker
62+
##### async balance.get
63+
Returns the current Ether balance of an account.
64+
```javascript
65+
const balanceTracker = await balance.tracker(account) //instantiation
66+
const accounBalance = await balanceTracker.get() //returns the current balance of account
67+
```
68+
##### async balance.delta
69+
Returns the change in the Ether since the last check(either `get()` or `delta()`)
70+
71+
```javascript
72+
const balanceTracker = await balance.tracker(receiver)
73+
send.ether(sender, receiver, ether('10'))
74+
(await balanceTracker.delta()).should.be.bignumber.equal('10');
75+
(await balanceTracker.delta()).should.be.bignumber.equal('0');
76+
```
77+
Or using `get()`:
5878
```javascript
59-
(await balance.difference(receiver, () =>
60-
send.ether(sender, receiver, ether('1')))
61-
).should.be.bignumber.equal(ether('1'));
79+
const balanceTracker = await balance.tracker(account) //instantiation
80+
const accounBalance = await balanceTracker.get() //returns the current balance of account
81+
(await balanceTracker.delta()).should.be.bignumber.equal('0');
6282
```
6383

6484
---

src/balance.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
const { BN } = require('./setup');
22

3-
async function balanceCurrent (account) {
4-
return new BN(await web3.eth.getBalance(account));
3+
class Tracker {
4+
constructor (acc) {
5+
this.account = acc;
6+
}
7+
async delta () {
8+
const current = await balanceCurrent(this.account);
9+
const delta = current.sub(this.prev);
10+
this.prev = current;
11+
return delta;
12+
}
13+
async get () {
14+
this.prev = await balanceCurrent(this.account);
15+
return this.prev;
16+
}
17+
}
18+
19+
async function balanceTracker (owner) {
20+
const tracker = new Tracker(owner);
21+
await tracker.get();
22+
return tracker;
523
}
624

7-
async function balanceDifference (account, promiseFunc) {
8-
const balanceBefore = new BN(await web3.eth.getBalance(account));
9-
await promiseFunc();
10-
const balanceAfter = new BN(await web3.eth.getBalance(account));
11-
return balanceAfter.sub(balanceBefore);
25+
async function balanceCurrent (account) {
26+
return new BN(await web3.eth.getBalance(account));
1227
}
1328

1429
module.exports = {
1530
current: balanceCurrent,
16-
difference: balanceDifference,
31+
tracker: balanceTracker,
1732
};

test-integration/simple-project-truffle-5.x/test/accounts.test.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ const { balance, BN, ether, send } = require('openzeppelin-test-helpers');
33
contract('accounts', function (accounts) {
44
it('sends ether and tracks balances', async function () {
55
const value = ether('42', 'ether');
6-
7-
(await balance.difference(accounts[0], () =>
8-
send.ether(accounts[0], accounts[1], value)
9-
)).should.be.bignumber.equals(value.neg());
6+
const tracker = await balance.tracker(accounts[0]);
7+
await send.ether(accounts[0], accounts[1], value);
8+
(await tracker.delta()).should.be.bignumber.equals(value.neg());
109
});
1110
});

test/src/balance.test.js

+34-7
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,44 @@ contract('balance', function ([sender, receiver]) {
1111
});
1212
});
1313

14-
describe('difference', function () {
14+
describe('balance tracker', function () {
15+
it('returns current balance ', async function () {
16+
const tracker = await balance.tracker(receiver);
17+
(await tracker.get()).should.be.bignumber.equal(await web3.eth.getBalance(receiver));
18+
});
19+
20+
it('get() adds a new checkpoint ', async function () {
21+
const tracker = await balance.tracker(sender);
22+
await send.ether(sender, receiver, ether('1'));
23+
await tracker.get();
24+
(await tracker.delta()).should.be.bignumber.equal('0');
25+
});
26+
27+
it('returns correct deltas after get() checkpoint', async function () {
28+
const tracker = await balance.tracker(receiver);
29+
await send.ether(sender, receiver, ether('1'));
30+
await tracker.get();
31+
await send.ether(sender, receiver, ether('1'));
32+
(await tracker.delta()).should.be.bignumber.equal(ether('1'));
33+
});
34+
1535
it('returns balance increments', async function () {
16-
(await balance.difference(receiver, () =>
17-
send.ether(sender, receiver, ether('1')))
18-
).should.be.bignumber.equal(ether('1'));
36+
const tracker = await balance.tracker(receiver);
37+
await send.ether(sender, receiver, ether('1'));
38+
(await tracker.delta()).should.be.bignumber.equal(ether('1'));
1939
});
2040

2141
it('returns balance decrements', async function () {
22-
(await balance.difference(sender, () =>
23-
send.ether(sender, receiver, ether('1')))
24-
).should.be.bignumber.equal(ether('-1'));
42+
const tracker = await balance.tracker(sender);
43+
await send.ether(sender, receiver, ether('1'));
44+
(await tracker.delta()).should.be.bignumber.equal(ether('-1'));
45+
});
46+
47+
it('returns consecutive deltas', async function () {
48+
const tracker = await balance.tracker(sender);
49+
await send.ether(sender, receiver, ether('1'));
50+
await tracker.delta();
51+
(await tracker.delta()).should.be.bignumber.equal('0');
2552
});
2653
});
2754
});

0 commit comments

Comments
 (0)