Skip to content

Commit

Permalink
Better testing with ERC20 decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
Brechtpd committed Dec 17, 2018
1 parent 6140d41 commit d4d8c5c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protocol2-js",
"version": "0.2.24",
"version": "0.2.25",
"description": "loopring protocol simulator core lib",
"main": "index.ts",
"repository": {
Expand Down
27 changes: 15 additions & 12 deletions src/protocol_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class ProtocolValidator {
return;
}

const decimalsPrecision = (ringsInfo.expected.decimalsPrecision !== undefined)
? ringsInfo.expected.decimalsPrecision : 18;

// Copy balances before
const expectedBalances: { [id: string]: any; } = {};
for (const token of Object.keys(report.balancesBefore)) {
Expand Down Expand Up @@ -125,7 +128,7 @@ export class ProtocolValidator {
if (orderExpectation.margin !== undefined) {
// Check if the margin is as expected
this.assertAlmostEqual(orderSettlement.splitS.toNumber(), orderExpectation.margin,
"Margin does not match the expected value");
"Margin does not match the expected value", decimalsPrecision);
}

// RingMined fill for this order
Expand Down Expand Up @@ -192,7 +195,7 @@ export class ProtocolValidator {
// expectedBalances[token][owner].toNumber() / 1e18 + " " + tokenSymbol);
this.assertAlmostEqual(report.balancesAfter[token][owner].toNumber(),
expectedBalances[token][owner].toNumber(),
"Balance different than expected");
"Balance different than expected", decimalsPrecision);
}
}
// Check fee balances
Expand All @@ -209,7 +212,7 @@ export class ProtocolValidator {
// console.log("[Exp]" + ownerName + ": " + expectedFeeBalances[token][owner] / 1e18 + " " + tokenSymbol);
this.assertAlmostEqual(report.feeBalancesAfter[token][owner].toNumber(),
expectedFeeBalances[token][owner].toNumber(),
"Fee balance different than expected");
"Fee balance different than expected", decimalsPrecision);
}
}
// Check filled
Expand All @@ -218,7 +221,7 @@ export class ProtocolValidator {
this.assertAlmostEqual(report.filledAmountsAfter[orderHash].toNumber(),
expectedfilledAmounts[orderHash].toNumber(),
"Filled amount different than expected",
6);
decimalsPrecision, 6);
}
// Check RingMined events
assert.equal(report.ringMinedEvents.length, ringMinedEvents.length,
Expand All @@ -237,15 +240,15 @@ export class ProtocolValidator {
assert.equal(expectedFill.owner, simulatorFill.owner, "owner does not match");
assert.equal(expectedFill.tokenS, simulatorFill.tokenS, "tokenS does not match");
this.assertAlmostEqual(expectedFill.amountS.toNumber(), simulatorFill.amountS.toNumber(),
"amountS does not match");
"amountS does not match", decimalsPrecision);
this.assertAlmostEqual(expectedFill.split.toNumber(), simulatorFill.split.toNumber(),
"split does not match");
"split does not match", decimalsPrecision);
this.assertAlmostEqual(expectedFill.feeAmount.toNumber(), simulatorFill.feeAmount.toNumber(),
"feeAmount does not match");
"feeAmount does not match", decimalsPrecision);
this.assertAlmostEqual(expectedFill.feeAmountS.toNumber(), simulatorFill.feeAmountS.toNumber(),
"feeAmountS does not match");
"feeAmountS does not match", decimalsPrecision);
this.assertAlmostEqual(expectedFill.feeAmountB.toNumber(), simulatorFill.feeAmountB.toNumber(),
"feeAmountB does not match");
"feeAmountB does not match", decimalsPrecision);
}
}
// Check InvalidRing events
Expand Down Expand Up @@ -470,9 +473,9 @@ export class ProtocolValidator {
}
}

private assertAlmostEqual(n1: number, n2: number, description: string, precision: number = 8) {
const numStr1 = (n1 / 1e18).toFixed(precision);
const numStr2 = (n2 / 1e18).toFixed(precision);
private assertAlmostEqual(n1: number, n2: number, description: string, decimals: number, precision: number = 8) {
const numStr1 = (n1 / (10 ** decimals)).toFixed(precision);
const numStr2 = (n2 / (10 ** decimals)).toFixed(precision);
return assert.equal(Number(numStr1), Number(numStr2), description);
}
}
4 changes: 2 additions & 2 deletions src/ring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ export class Ring {
"rounding error larger than 1% when calculating fillAmountB");

// We at least need to buy and sell something
valid = valid && p.fillAmountS.gt(0);
valid = valid && p.fillAmountS.gt(0);
valid = valid && ensure(p.fillAmountS.gt(0), "fillAmountS needs te be > 0");
valid = valid && ensure(p.fillAmountB.gt(0), "fillAmountB needs to be > 0");

return valid;
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface TransactionExpectation {
revert?: boolean;
revertMessage?: string;
rings?: RingExpectation[];
decimalsPrecision?: number;
}

export interface RingsInfo {
Expand Down

0 comments on commit d4d8c5c

Please sign in to comment.