Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 1efed3c

Browse files
committed
dry up code
1 parent 0cc7cc8 commit 1efed3c

File tree

3 files changed

+69
-87
lines changed

3 files changed

+69
-87
lines changed

test/setToken-base.spec.ts

Lines changed: 44 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ BigNumberSetup.configure();
2222
ChaiSetup.configure();
2323
const { expect, assert } = chai;
2424

25+
import {
26+
assertTokenBalance,
27+
expectInvalidOpcodeError,
28+
expectRevertError,
29+
} from "./utils/tokenAssertions";
2530
import { INVALID_OPCODE, REVERT_ERROR } from "./constants/txn_error";
2631

2732
contract("{Set}", (accounts) => {
2833
let componentA: any;
2934
const unitsA: BigNumber = gWei(1);
3035
let componentB: any;
3136
const unitsB: BigNumber = gWei(2);
37+
let componentC: any;
38+
const unitsC: BigNumber = gWei(2);
3239

3340
const [testAccount] = accounts;
3441
let setToken: any;
@@ -80,37 +87,25 @@ contract("{Set}", (accounts) => {
8087
});
8188

8289
it("should not allow creation of a {Set} with mismatched quantity of units and tokens", async () => {
83-
await expect(
84-
SetToken.new(
85-
[componentA.address, componentB.address],
86-
[unitsA],
87-
TX_DEFAULTS,
88-
),
89-
).to.eventually.be.rejectedWith(REVERT_ERROR);
90+
expectRevertError(SetToken.new([componentA.address, componentB.address], [unitsA], TX_DEFAULTS));
9091
});
9192

9293
it("should not allow creation of a {Set} with no inputs", async () => {
93-
await expect(
94-
SetToken.new([], [], TX_DEFAULTS),
95-
).to.eventually.be.rejectedWith(REVERT_ERROR);
94+
expectRevertError(SetToken.new([], [], TX_DEFAULTS));
9695
});
9796

9897
it("should not allow creation of a {Set} with units of 0 value", async () => {
9998
const badUnit = 0;
10099

101-
await expect(
102-
SetToken.new(
103-
[componentA.address, componentB.address],
104-
[unitsA, badUnit],
105-
TX_DEFAULTS,
106-
),
107-
).to.eventually.be.rejectedWith(REVERT_ERROR);
100+
expectRevertError(SetToken.new(
101+
[componentA.address, componentB.address],
102+
[unitsA, badUnit],
103+
TX_DEFAULTS,
104+
));
108105
});
109106

110107
it("should not allow creation of a {Set} with address of 0", async () => {
111-
await expect(
112-
SetToken.new([componentA.address, null], [unitsA, unitsB], TX_DEFAULTS),
113-
).to.eventually.be.rejectedWith(REVERT_ERROR);
108+
expectRevertError(SetToken.new([componentA.address, null], [unitsA, unitsB], TX_DEFAULTS));
114109
});
115110
});
116111

@@ -150,26 +145,9 @@ contract("{Set}", (accounts) => {
150145
// The logs should have the right quantity
151146
expect(issuanceLog._quantity).to.be.bignumber.equal(quantity);
152147

153-
// User should have less A token
154-
const postIssueBalanceAofOwner = await componentA.balanceOf(testAccount);
155-
expect(postIssueBalanceAofOwner).to.be.bignumber.equal(
156-
initialTokens.sub(quantityA),
157-
"Component A Balance",
158-
);
159-
160-
// User should have less B token
161-
const postIssueBalanceBofOwner = await componentB.balanceOf(testAccount);
162-
expect(postIssueBalanceBofOwner).to.be.bignumber.equal(
163-
initialTokens.sub(quantityB),
164-
"Component B Balance",
165-
);
166-
167-
// User should have an/multiple Set tokens
168-
const postIssueBalanceIndexofOwner = await setToken.balanceOf(testAccount);
169-
expect(postIssueBalanceIndexofOwner).to.be.bignumber.equal(
170-
quantity,
171-
"Set Component Balance",
172-
);
148+
assertTokenBalance(componentA, initialTokens.sub(quantityA), testAccount);
149+
assertTokenBalance(componentB, initialTokens.sub(quantityB), testAccount);
150+
assertTokenBalance(setToken, quantity, testAccount);
173151
});
174152

175153
it(`should allow a user to redeem ${quantity} token from the index fund`, async () => {
@@ -187,17 +165,9 @@ contract("{Set}", (accounts) => {
187165
// The logs should have the right quantity
188166
expect(redeemLog._quantity).to.be.bignumber.equal(quantity);
189167

190-
// User should have more A token
191-
const postRedeemBalanceAofOwner = await componentA.balanceOf(testAccount);
192-
expect(postRedeemBalanceAofOwner).to.be.bignumber.equal(initialTokens);
193-
194-
// User should have more B token
195-
const postRedeemBalanceBofOwner = await componentB.balanceOf(testAccount);
196-
expect(postRedeemBalanceBofOwner).to.be.bignumber.equal(initialTokens);
197-
198-
// User should have 0 Set token
199-
const postRedeemBalanceIndexofOwner = await setToken.balanceOf(testAccount);
200-
expect(postRedeemBalanceIndexofOwner).to.be.bignumber.equal(0);
168+
assertTokenBalance(componentA, initialTokens, testAccount);
169+
assertTokenBalance(componentB, initialTokens, testAccount);
170+
assertTokenBalance(setToken, new BigNumber(0), testAccount);
201171
});
202172
}
203173
});
@@ -222,23 +192,13 @@ contract("{Set}", (accounts) => {
222192

223193
await setToken.issue(quantityInWei, TX_DEFAULTS);
224194

225-
// User should have less A token
226-
const postIssueBalanceAofOwner = await componentA.balanceOf(testAccount);
227-
expect(postIssueBalanceAofOwner).to.be.bignumber.equal(initialTokens.sub(quantityA));
228-
229-
// User should have an/multiple Set tokens
230-
const postIssueBalanceIndexofOwner = await setToken.balanceOf(testAccount);
231-
expect(postIssueBalanceIndexofOwner).to.be.bignumber.equal(quantityInWei);
195+
assertTokenBalance(componentA, initialTokens.sub(quantityA), testAccount);
196+
assertTokenBalance(setToken, quantityInWei, testAccount);
232197

233198
await setToken.redeem(quantityInWei, TX_DEFAULTS);
234199

235-
// User should have more A token
236-
const postRedeemBalanceAofOwner = await componentA.balanceOf(testAccount);
237-
expect(postRedeemBalanceAofOwner).to.be.bignumber.equal(initialTokens);
238-
239-
// User should have 0 Set token
240-
const postRedeemBalanceIndexofOwner = await setToken.balanceOf(testAccount);
241-
expect(postRedeemBalanceIndexofOwner).to.be.bignumber.equal(0);
200+
assertTokenBalance(componentA, initialTokens, testAccount);
201+
assertTokenBalance(setToken, new BigNumber(0), testAccount);
242202
});
243203

244204
it("should disallow issuing a Set when the amount is too low", async () => {
@@ -258,9 +218,7 @@ contract("{Set}", (accounts) => {
258218

259219
await componentA.approve(setToken.address, quantityA, TX_DEFAULTS);
260220

261-
await expect(setToken.issue(quantityInWei, TX_DEFAULTS)).to.eventually.be.rejectedWith(
262-
INVALID_OPCODE,
263-
);
221+
expectInvalidOpcodeError(setToken.issue(quantityInWei, TX_DEFAULTS));
264222
});
265223
});
266224

@@ -287,9 +245,7 @@ contract("{Set}", (accounts) => {
287245
);
288246
const quantityOverflow = overflow.plus(quantity);
289247

290-
await expect(setToken.issue(quantityOverflow, TX_DEFAULTS)).to.eventually.be.rejectedWith(
291-
INVALID_OPCODE,
292-
);
248+
expectInvalidOpcodeError(setToken.issue(quantityOverflow, TX_DEFAULTS));
293249
});
294250
});
295251
});
@@ -298,25 +254,29 @@ contract("{Set}", (accounts) => {
298254
const quantityIssued = ether(10);
299255
let quantityA: BigNumber;
300256
let quantityB: BigNumber;
257+
let quantityC: BigNumber;
301258

302259
// Create a Set two components with set tokens issued
303260
beforeEach(async () => {
304261
componentA = await StandardTokenMock.new(testAccount, initialTokens, "Component A", "A");
305262
componentB = await StandardTokenMock.new(testAccount, initialTokens, "Component B", "B");
263+
componentC = await StandardTokenMock.new(testAccount, initialTokens, "Component C", "C");
306264

307265
setToken = await SetToken.new(
308-
[componentA.address, componentB.address],
309-
[unitsA, unitsB],
266+
[componentA.address, componentB.address, componentC.address],
267+
[unitsA, unitsB, unitsC],
310268
TX_DEFAULTS,
311269
);
312270

313271
// Expected Quantities of tokens moved are divided by a gWei
314272
// to reflect the new units in set instantiation
315273
quantityA = unitsA.mul(quantityIssued).div(gWei(1));
316274
quantityB = unitsB.mul(quantityIssued).div(gWei(1));
275+
quantityC = unitsC.mul(quantityIssued).div(gWei(1));
317276

318277
await componentA.approve(setToken.address, quantityA, TX_DEFAULTS);
319278
await componentB.approve(setToken.address, quantityB, TX_DEFAULTS);
279+
await componentC.approve(setToken.address, quantityC, TX_DEFAULTS);
320280

321281
await setToken.issue(quantityIssued, TX_DEFAULTS);
322282
});
@@ -328,19 +288,22 @@ contract("{Set}", (accounts) => {
328288
const postRedeemBalanceIndexofOwner = await setToken.balanceOf(testAccount);
329289
expect(postRedeemBalanceIndexofOwner).to.be.bignumber.equal(0, "Post Balance Set");
330290

331-
// The user should have 0 of TokenA
332-
const postRedeemBalanceAofOwner = await componentA.balanceOf(testAccount);
333-
expect(postRedeemBalanceAofOwner).to.be.bignumber.equal(
334-
initialTokens.sub(quantityA));
291+
assertTokenBalance(componentA, initialTokens.sub(quantityA), testAccount);
335292

336293
// The user should have balance of Token A in excluded Tokens
337294
const [excludedBalanceAofOwner] = await setToken.unredeemedComponents(componentA.address, testAccount);
338295
expect(excludedBalanceAofOwner).to.be.bignumber.equal(
339296
quantityA);
340297

341-
// The user should have 10 token B
342-
const postRedeemBalanceBofOwner = await componentB.balanceOf(testAccount);
343-
expect(postRedeemBalanceBofOwner).to.be.bignumber.equal(initialTokens, "Post Balance B");
298+
assertTokenBalance(componentB, initialTokens, testAccount);
299+
});
300+
301+
it("should fail partial redeem with duplicate entries", async () => {
302+
expectInvalidOpcodeError(setToken.partialRedeem(
303+
quantityIssued,
304+
[componentA.address, componentA.address],
305+
TX_DEFAULTS,
306+
));
344307
});
345308
});
346309

@@ -377,9 +340,7 @@ contract("{Set}", (accounts) => {
377340
it("should successfully redeem excluded a standard Set", async () => {
378341
await setToken.redeemExcluded(quantityA, componentA.address, TX_DEFAULTS);
379342

380-
// The user should have a balance of TokenA
381-
const postRedeemBalanceAofOwner = await componentA.balanceOf(testAccount);
382-
expect(postRedeemBalanceAofOwner).to.be.bignumber.equal(initialTokens);
343+
assertTokenBalance(componentA, initialTokens, testAccount);
383344

384345
// The user should have no balance of Token A in excluded Tokens
385346
const [excludedBalanceAofOwner] = await setToken.unredeemedComponents(componentA.address, testAccount);

test/utils/tokenAssertions.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as chai from "chai";
2+
import { BigNumber } from "bignumber.js";
3+
4+
import ChaiSetup from "../config/chai_setup";
5+
ChaiSetup.configure();
6+
const { expect, assert } = chai;
7+
8+
import { INVALID_OPCODE, REVERT_ERROR } from "../constants/txn_error";
9+
10+
export async function assertTokenBalance(token: any, amount: BigNumber, testAccount: string) {
11+
const tokenBalance = await token.balanceOf(testAccount);
12+
expect(tokenBalance).to.be.bignumber.equal(amount);
13+
}
14+
15+
export async function expectRevertError(asyncTxn: any) {
16+
await expect(asyncTxn).to.eventually.be.rejectedWith(REVERT_ERROR);
17+
}
18+
19+
export async function expectInvalidOpcodeError(asyncTxn: any) {
20+
await expect(asyncTxn).to.eventually.be.rejectedWith(INVALID_OPCODE);
21+
}

test/utils/units.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import * as Web3 from "web3";
33
const web3 = new Web3();
44

55
export function ether(amount: number): BigNumber {
6-
const weiString = web3.toWei(amount, "ether");
7-
return new BigNumber(weiString);
6+
const weiString = web3.toWei(amount, "ether");
7+
return new BigNumber(weiString);
88
}
99

1010
export function gWei(amount: number): BigNumber {
11-
const weiString = web3.toWei(amount, "gwei");
12-
return new BigNumber(weiString);
11+
const weiString = web3.toWei(amount, "gwei");
12+
return new BigNumber(weiString);
1313
}

0 commit comments

Comments
 (0)