From d4d8c5cec389c3b7a3d274ecb8880600a55d05d6 Mon Sep 17 00:00:00 2001 From: Brecht Date: Mon, 17 Dec 2018 22:37:21 +0100 Subject: [PATCH] Better testing with ERC20 decimals --- package.json | 2 +- src/protocol_validator.ts | 27 +++++++++++++++------------ src/ring.ts | 4 ++-- src/types.ts | 1 + 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 7033a87..346e814 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/protocol_validator.ts b/src/protocol_validator.ts index e209aaa..65c7367 100644 --- a/src/protocol_validator.ts +++ b/src/protocol_validator.ts @@ -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)) { @@ -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 @@ -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 @@ -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 @@ -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, @@ -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 @@ -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); } } diff --git a/src/ring.ts b/src/ring.ts index 96454d9..bcd1bbc 100644 --- a/src/ring.ts +++ b/src/ring.ts @@ -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; } diff --git a/src/types.ts b/src/types.ts index 027f696..ce48a1c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -126,6 +126,7 @@ export interface TransactionExpectation { revert?: boolean; revertMessage?: string; rings?: RingExpectation[]; + decimalsPrecision?: number; } export interface RingsInfo {