From b1f38022dc01be7071a7742b7e3eb56771edb365 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Thu, 13 Dec 2018 21:25:57 +0530 Subject: [PATCH 1/5] Fixed rounding off errors --- contracts/modules/STO/USDTieredSTO.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/modules/STO/USDTieredSTO.sol b/contracts/modules/STO/USDTieredSTO.sol index 825df2122..36dd43ca3 100644 --- a/contracts/modules/STO/USDTieredSTO.sol +++ b/contracts/modules/STO/USDTieredSTO.sol @@ -455,7 +455,7 @@ contract USDTieredSTO is USDTieredSTOStorage, ISTO, ReentrancyGuard { fundsRaisedUSD = fundsRaisedUSD.add(spentUSD); } - spentValue = DecimalMath.mul(DecimalMath.div(spentUSD, originalUSD), _investmentValue); + spentValue = DecimalMath.div(DecimalMath.mul(spentUSD, _investmentValue), originalUSD); } /** @@ -494,7 +494,7 @@ contract USDTieredSTO is USDTieredSTOStorage, ISTO, ReentrancyGuard { } } - spentValue = DecimalMath.mul(DecimalMath.div(spentUSD, originalUSD), _investmentValue); + spentValue = DecimalMath.div(DecimalMath.mul(spentUSD, _investmentValue), originalUSD); } function _buyTokensChecks( From a72bcb220f83b439d7ee3eb5f76fe7c13fbdd842 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Thu, 13 Dec 2018 22:30:33 +0530 Subject: [PATCH 2/5] Increased tolerance --- contracts/libraries/DecimalMath.sol | 4 ++-- test/q_usd_tiered_sto_sim.js | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/contracts/libraries/DecimalMath.sol b/contracts/libraries/DecimalMath.sol index 242daffab..a4cf0d81b 100644 --- a/contracts/libraries/DecimalMath.sol +++ b/contracts/libraries/DecimalMath.sol @@ -11,7 +11,7 @@ library DecimalMath { * @return uint256 Result of multiplication represented as (decimal * 10**DECIMALS) */ function mul(uint256 x, uint256 y) internal pure returns (uint256 z) { - z = SafeMath.add(SafeMath.mul(x, y), (10 ** 18) / 2) / (10 ** 18); + z = SafeMath.mul(x, y) / (10 ** 18); } /** @@ -19,7 +19,7 @@ library DecimalMath { * @return uint256 Result of division represented as (decimal * 10**DECIMALS) */ function div(uint256 x, uint256 y) internal pure returns (uint256 z) { - z = SafeMath.add(SafeMath.mul(x, (10 ** 18)), y / 2) / y; + z = SafeMath.mul(x, (10 ** 18)) / y; } } \ No newline at end of file diff --git a/test/q_usd_tiered_sto_sim.js b/test/q_usd_tiered_sto_sim.js index e8b87b3df..16b2210f4 100644 --- a/test/q_usd_tiered_sto_sim.js +++ b/test/q_usd_tiered_sto_sim.js @@ -17,7 +17,7 @@ const Web3 = require("web3"); const BigNumber = require("bignumber.js"); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // Hardcoded development port -const TOLERANCE = 2; // Allow balances to be off by 2 WEI for rounding purposes +const TOLERANCE = 10; // Allow balances to be off by 10 WEI for rounding purposes contract("USDTieredSTO Sim", accounts => { // Accounts Variable declaration @@ -589,13 +589,13 @@ contract("USDTieredSTO Sim", accounts => { let investment_DAI = new BigNumber(10 * 10 ** 18); // 10 USD = DAI DAI if (isPoly) { - await I_PolyToken.getTokens(investment_POLY, _investor); + await I_PolyToken.getTokens(investment_POLY.add(TOLERANCE), _investor); await I_PolyToken.approve(I_USDTieredSTO_Array[stoId].address, investment_POLY, { from: _investor }); await catchRevert( I_USDTieredSTO_Array[stoId].buyWithPOLY(_investor, investment_POLY, { from: _investor, gasPrice: GAS_PRICE }) ); } else if (isDAI) { - await I_DaiToken.getTokens(investment_DAI, _investor); + await I_DaiToken.getTokens(investment_DAI.add(TOLERANCE), _investor); await I_DaiToken.approve(I_USDTieredSTO_Array[stoId].address, investment_DAI, { from: _investor }); await catchRevert( I_USDTieredSTO_Array[stoId].buyWithUSD(_investor, investment_DAI, { from: _investor, gasPrice: GAS_PRICE }) @@ -641,10 +641,10 @@ contract("USDTieredSTO Sim", accounts => { `); if (isPoly) { - await I_PolyToken.getTokens(investment_POLY, _investor); + await I_PolyToken.getTokens(investment_POLY.add(TOLERANCE), _investor); await I_PolyToken.approve(I_USDTieredSTO_Array[stoId].address, investment_POLY, { from: _investor }); } else if (isDai) { - await I_DaiToken.getTokens(investment_DAI, _investor); + await I_DaiToken.getTokens(investment_DAI.add(TOLERANCE), _investor); await I_DaiToken.approve(I_USDTieredSTO_Array[stoId].address, investment_DAI, { from: _investor }); } @@ -740,6 +740,7 @@ contract("USDTieredSTO Sim", accounts => { TOLERANCE, "Investor ETH Balance not changed as expected" ); + console.log(final_InvestorPOLYBal.toNumber(), init_InvestorPOLYBal.toNumber()); assert.closeTo( final_InvestorPOLYBal.toNumber(), init_InvestorPOLYBal.sub(investment_POLY).toNumber(), @@ -808,6 +809,7 @@ contract("USDTieredSTO Sim", accounts => { TOLERANCE, "Investor ETH Balance not changed as expected" ); + console.log(final_InvestorDAIBal.toNumber(), init_InvestorDAIBal.toNumber()); assert.closeTo( final_InvestorDAIBal.toNumber(), init_InvestorDAIBal.sub(investment_DAI).toNumber(), From 7f3ad511df42048b9dbd38b1c72251ba6f53e090 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Fri, 14 Dec 2018 09:52:00 +0530 Subject: [PATCH 3/5] Reduced tolerance to zero --- contracts/libraries/DecimalMath.sol | 4 ++-- contracts/modules/STO/USDTieredSTO.sol | 4 ++-- test/q_usd_tiered_sto_sim.js | 10 +++++----- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/libraries/DecimalMath.sol b/contracts/libraries/DecimalMath.sol index a4cf0d81b..242daffab 100644 --- a/contracts/libraries/DecimalMath.sol +++ b/contracts/libraries/DecimalMath.sol @@ -11,7 +11,7 @@ library DecimalMath { * @return uint256 Result of multiplication represented as (decimal * 10**DECIMALS) */ function mul(uint256 x, uint256 y) internal pure returns (uint256 z) { - z = SafeMath.mul(x, y) / (10 ** 18); + z = SafeMath.add(SafeMath.mul(x, y), (10 ** 18) / 2) / (10 ** 18); } /** @@ -19,7 +19,7 @@ library DecimalMath { * @return uint256 Result of division represented as (decimal * 10**DECIMALS) */ function div(uint256 x, uint256 y) internal pure returns (uint256 z) { - z = SafeMath.mul(x, (10 ** 18)) / y; + z = SafeMath.add(SafeMath.mul(x, (10 ** 18)), y / 2) / y; } } \ No newline at end of file diff --git a/contracts/modules/STO/USDTieredSTO.sol b/contracts/modules/STO/USDTieredSTO.sol index 36dd43ca3..1d7278796 100644 --- a/contracts/modules/STO/USDTieredSTO.sol +++ b/contracts/modules/STO/USDTieredSTO.sol @@ -455,7 +455,7 @@ contract USDTieredSTO is USDTieredSTOStorage, ISTO, ReentrancyGuard { fundsRaisedUSD = fundsRaisedUSD.add(spentUSD); } - spentValue = DecimalMath.div(DecimalMath.mul(spentUSD, _investmentValue), originalUSD); + spentValue = DecimalMath.div(spentUSD, _rate); } /** @@ -494,7 +494,7 @@ contract USDTieredSTO is USDTieredSTOStorage, ISTO, ReentrancyGuard { } } - spentValue = DecimalMath.div(DecimalMath.mul(spentUSD, _investmentValue), originalUSD); + spentValue = DecimalMath.div(spentUSD, rate); } function _buyTokensChecks( diff --git a/test/q_usd_tiered_sto_sim.js b/test/q_usd_tiered_sto_sim.js index 16b2210f4..1365a284d 100644 --- a/test/q_usd_tiered_sto_sim.js +++ b/test/q_usd_tiered_sto_sim.js @@ -17,7 +17,7 @@ const Web3 = require("web3"); const BigNumber = require("bignumber.js"); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // Hardcoded development port -const TOLERANCE = 10; // Allow balances to be off by 10 WEI for rounding purposes +const TOLERANCE = 0; // Allow balances to be off by 10 WEI for rounding purposes contract("USDTieredSTO Sim", accounts => { // Accounts Variable declaration @@ -589,13 +589,13 @@ contract("USDTieredSTO Sim", accounts => { let investment_DAI = new BigNumber(10 * 10 ** 18); // 10 USD = DAI DAI if (isPoly) { - await I_PolyToken.getTokens(investment_POLY.add(TOLERANCE), _investor); + await I_PolyToken.getTokens(investment_POLY, _investor); await I_PolyToken.approve(I_USDTieredSTO_Array[stoId].address, investment_POLY, { from: _investor }); await catchRevert( I_USDTieredSTO_Array[stoId].buyWithPOLY(_investor, investment_POLY, { from: _investor, gasPrice: GAS_PRICE }) ); } else if (isDAI) { - await I_DaiToken.getTokens(investment_DAI.add(TOLERANCE), _investor); + await I_DaiToken.getTokens(investment_DAI, _investor); await I_DaiToken.approve(I_USDTieredSTO_Array[stoId].address, investment_DAI, { from: _investor }); await catchRevert( I_USDTieredSTO_Array[stoId].buyWithUSD(_investor, investment_DAI, { from: _investor, gasPrice: GAS_PRICE }) @@ -641,10 +641,10 @@ contract("USDTieredSTO Sim", accounts => { `); if (isPoly) { - await I_PolyToken.getTokens(investment_POLY.add(TOLERANCE), _investor); + await I_PolyToken.getTokens(investment_POLY, _investor); await I_PolyToken.approve(I_USDTieredSTO_Array[stoId].address, investment_POLY, { from: _investor }); } else if (isDai) { - await I_DaiToken.getTokens(investment_DAI.add(TOLERANCE), _investor); + await I_DaiToken.getTokens(investment_DAI, _investor); await I_DaiToken.approve(I_USDTieredSTO_Array[stoId].address, investment_DAI, { from: _investor }); } From 95346ebefb0f41871d706e557a16dba9ceac0bf6 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Fri, 14 Dec 2018 10:12:36 +0530 Subject: [PATCH 4/5] Comment updated --- test/q_usd_tiered_sto_sim.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/q_usd_tiered_sto_sim.js b/test/q_usd_tiered_sto_sim.js index 1365a284d..ce36ea582 100644 --- a/test/q_usd_tiered_sto_sim.js +++ b/test/q_usd_tiered_sto_sim.js @@ -17,7 +17,7 @@ const Web3 = require("web3"); const BigNumber = require("bignumber.js"); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // Hardcoded development port -const TOLERANCE = 0; // Allow balances to be off by 10 WEI for rounding purposes +const TOLERANCE = 0; // Allow balances to be off by X WEI for rounding purposes. Currently set to 0 as js rounding and solidity rounding are in sync. contract("USDTieredSTO Sim", accounts => { // Accounts Variable declaration @@ -740,7 +740,6 @@ contract("USDTieredSTO Sim", accounts => { TOLERANCE, "Investor ETH Balance not changed as expected" ); - console.log(final_InvestorPOLYBal.toNumber(), init_InvestorPOLYBal.toNumber()); assert.closeTo( final_InvestorPOLYBal.toNumber(), init_InvestorPOLYBal.sub(investment_POLY).toNumber(), @@ -809,7 +808,6 @@ contract("USDTieredSTO Sim", accounts => { TOLERANCE, "Investor ETH Balance not changed as expected" ); - console.log(final_InvestorDAIBal.toNumber(), init_InvestorDAIBal.toNumber()); assert.closeTo( final_InvestorDAIBal.toNumber(), init_InvestorDAIBal.sub(investment_DAI).toNumber(), From e91eddb5705dd1041f10989f83e75ddee8bcf7c7 Mon Sep 17 00:00:00 2001 From: Mudit Gupta Date: Fri, 14 Dec 2018 10:19:45 +0530 Subject: [PATCH 5/5] Tolerance set to 2 --- test/q_usd_tiered_sto_sim.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/q_usd_tiered_sto_sim.js b/test/q_usd_tiered_sto_sim.js index ce36ea582..e8b87b3df 100644 --- a/test/q_usd_tiered_sto_sim.js +++ b/test/q_usd_tiered_sto_sim.js @@ -17,7 +17,7 @@ const Web3 = require("web3"); const BigNumber = require("bignumber.js"); const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); // Hardcoded development port -const TOLERANCE = 0; // Allow balances to be off by X WEI for rounding purposes. Currently set to 0 as js rounding and solidity rounding are in sync. +const TOLERANCE = 2; // Allow balances to be off by 2 WEI for rounding purposes contract("USDTieredSTO Sim", accounts => { // Accounts Variable declaration