-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
PaymentSplitter.test.js
217 lines (177 loc) · 8.46 KB
/
PaymentSplitter.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const { balance, constants, ether, expectEvent, send, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;
const { expect } = require('chai');
const PaymentSplitter = artifacts.require('PaymentSplitter');
const Token = artifacts.require('ERC20Mock');
contract('PaymentSplitter', function (accounts) {
const [ owner, payee1, payee2, payee3, nonpayee1, payer1 ] = accounts;
const amount = ether('1');
it('rejects an empty set of payees', async function () {
await expectRevert(PaymentSplitter.new([], []), 'PaymentSplitter: no payees');
});
it('rejects more payees than shares', async function () {
await expectRevert(PaymentSplitter.new([payee1, payee2, payee3], [20, 30]),
'PaymentSplitter: payees and shares length mismatch',
);
});
it('rejects more shares than payees', async function () {
await expectRevert(PaymentSplitter.new([payee1, payee2], [20, 30, 40]),
'PaymentSplitter: payees and shares length mismatch',
);
});
it('rejects null payees', async function () {
await expectRevert(PaymentSplitter.new([payee1, ZERO_ADDRESS], [20, 30]),
'PaymentSplitter: account is the zero address',
);
});
it('rejects zero-valued shares', async function () {
await expectRevert(PaymentSplitter.new([payee1, payee2], [20, 0]),
'PaymentSplitter: shares are 0',
);
});
it('rejects repeated payees', async function () {
await expectRevert(PaymentSplitter.new([payee1, payee1], [20, 30]),
'PaymentSplitter: account already has shares',
);
});
context('once deployed', function () {
beforeEach(async function () {
this.payees = [payee1, payee2, payee3];
this.shares = [20, 10, 70];
this.contract = await PaymentSplitter.new(this.payees, this.shares);
this.token = await Token.new('MyToken', 'MT', owner, ether('1000'));
});
it('has total shares', async function () {
expect(await this.contract.totalShares()).to.be.bignumber.equal('100');
});
it('has payees', async function () {
await Promise.all(this.payees.map(async (payee, index) => {
expect(await this.contract.payee(index)).to.equal(payee);
expect(await this.contract.released(payee)).to.be.bignumber.equal('0');
expect(await this.contract.releasable(payee)).to.be.bignumber.equal('0');
}));
});
describe('accepts payments', function () {
it('Ether', async function () {
await send.ether(owner, this.contract.address, amount);
expect(await balance.current(this.contract.address)).to.be.bignumber.equal(amount);
});
it('Token', async function () {
await this.token.transfer(this.contract.address, amount, { from: owner });
expect(await this.token.balanceOf(this.contract.address)).to.be.bignumber.equal(amount);
});
});
describe('shares', function () {
it('stores shares if address is payee', async function () {
expect(await this.contract.shares(payee1)).to.be.bignumber.not.equal('0');
});
it('does not store shares if address is not payee', async function () {
expect(await this.contract.shares(nonpayee1)).to.be.bignumber.equal('0');
});
});
describe('release', function () {
describe('Ether', function () {
it('reverts if no funds to claim', async function () {
await expectRevert(this.contract.release(payee1),
'PaymentSplitter: account is not due payment',
);
});
it('reverts if non-payee want to claim', async function () {
await send.ether(payer1, this.contract.address, amount);
await expectRevert(this.contract.release(nonpayee1),
'PaymentSplitter: account has no shares',
);
});
});
describe('Token', function () {
it('reverts if no funds to claim', async function () {
await expectRevert(this.contract.release(this.token.address, payee1),
'PaymentSplitter: account is not due payment',
);
});
it('reverts if non-payee want to claim', async function () {
await this.token.transfer(this.contract.address, amount, { from: owner });
await expectRevert(this.contract.release(this.token.address, nonpayee1),
'PaymentSplitter: account has no shares',
);
});
});
});
describe('tracks releasable and released', function () {
it('Ether', async function () {
await send.ether(payer1, this.contract.address, amount);
const payment = amount.divn(10);
expect(await this.contract.releasable(payee2)).to.be.bignumber.equal(payment);
await this.contract.release(payee2);
expect(await this.contract.releasable(payee2)).to.be.bignumber.equal('0');
expect(await this.contract.released(payee2)).to.be.bignumber.equal(payment);
});
it('Token', async function () {
await this.token.transfer(this.contract.address, amount, { from: owner });
const payment = amount.divn(10);
expect(await this.contract.releasable(this.token.address, payee2, {})).to.be.bignumber.equal(payment);
await this.contract.release(this.token.address, payee2);
expect(await this.contract.releasable(this.token.address, payee2, {})).to.be.bignumber.equal('0');
expect(await this.contract.released(this.token.address, payee2)).to.be.bignumber.equal(payment);
});
});
describe('distributes funds to payees', function () {
it('Ether', async function () {
await send.ether(payer1, this.contract.address, amount);
// receive funds
const initBalance = await balance.current(this.contract.address);
expect(initBalance).to.be.bignumber.equal(amount);
// distribute to payees
const tracker1 = await balance.tracker(payee1);
const receipt1 = await this.contract.release(payee1);
const profit1 = await tracker1.delta();
expect(profit1).to.be.bignumber.equal(ether('0.20'));
expectEvent(receipt1, 'PaymentReleased', { to: payee1, amount: profit1 });
const tracker2 = await balance.tracker(payee2);
const receipt2 = await this.contract.release(payee2);
const profit2 = await tracker2.delta();
expect(profit2).to.be.bignumber.equal(ether('0.10'));
expectEvent(receipt2, 'PaymentReleased', { to: payee2, amount: profit2 });
const tracker3 = await balance.tracker(payee3);
const receipt3 = await this.contract.release(payee3);
const profit3 = await tracker3.delta();
expect(profit3).to.be.bignumber.equal(ether('0.70'));
expectEvent(receipt3, 'PaymentReleased', { to: payee3, amount: profit3 });
// end balance should be zero
expect(await balance.current(this.contract.address)).to.be.bignumber.equal('0');
// check correct funds released accounting
expect(await this.contract.totalReleased()).to.be.bignumber.equal(initBalance);
});
it('Token', async function () {
expect(await this.token.balanceOf(payee1)).to.be.bignumber.equal('0');
expect(await this.token.balanceOf(payee2)).to.be.bignumber.equal('0');
expect(await this.token.balanceOf(payee3)).to.be.bignumber.equal('0');
await this.token.transfer(this.contract.address, amount, { from: owner });
expectEvent(
await this.contract.release(this.token.address, payee1),
'ERC20PaymentReleased',
{ token: this.token.address, to: payee1, amount: ether('0.20') },
);
await this.token.transfer(this.contract.address, amount, { from: owner });
expectEvent(
await this.contract.release(this.token.address, payee1),
'ERC20PaymentReleased',
{ token: this.token.address, to: payee1, amount: ether('0.20') },
);
expectEvent(
await this.contract.release(this.token.address, payee2),
'ERC20PaymentReleased',
{ token: this.token.address, to: payee2, amount: ether('0.20') },
);
expectEvent(
await this.contract.release(this.token.address, payee3),
'ERC20PaymentReleased',
{ token: this.token.address, to: payee3, amount: ether('1.40') },
);
expect(await this.token.balanceOf(payee1)).to.be.bignumber.equal(ether('0.40'));
expect(await this.token.balanceOf(payee2)).to.be.bignumber.equal(ether('0.20'));
expect(await this.token.balanceOf(payee3)).to.be.bignumber.equal(ether('1.40'));
});
});
});
});