From 5efdaad499a5280b96374ac36bd580d5d6fd640b Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Fri, 18 Sep 2020 12:05:22 +0800 Subject: [PATCH 1/2] approve upon setup --- packages/loopring_v3/contracts/test/AmmPool.sol | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/loopring_v3/contracts/test/AmmPool.sol b/packages/loopring_v3/contracts/test/AmmPool.sol index 7a3ee1852..6036d6956 100644 --- a/packages/loopring_v3/contracts/test/AmmPool.sol +++ b/packages/loopring_v3/contracts/test/AmmPool.sol @@ -65,9 +65,7 @@ contract AmmPool is IBlockReceiver { uint pos ); - uint public constant MAX_UINT = ~uint(0); - - uint public constant BASE = 10**18; + uint public constant BASE = 10 ** 18; uint public constant INITIAL_SUPPLY = 100 * BASE; uint public constant MAX_AGE_REQUEST_UNTIL_POOL_SHUTDOWN = 7 days; @@ -189,13 +187,19 @@ contract AmmPool is IBlockReceiver { exchange = _exchange; accountID = _accountID; feeBips = _feeBips; + + address depositContract = address(exchange.getDepositContract()); + for (uint i = 0; i < _tokens.length; i++) { - uint16 tokenID = exchange.getTokenID(_tokens[i]); + address token = _tokens[i]; + uint16 tokenID = exchange.getTokenID(token); tokens.push(Token({ - addr: _tokens[i], + addr: token, tokenID: tokenID, weight: _weights[i] })); + + ERC20(token).approve(depositContract, ~uint(0)); } } @@ -707,7 +711,7 @@ contract AmmPool is IBlockReceiver { address depositContract = address(exchange.getDepositContract()); if (ERC20(token.addr).allowance(address(this), depositContract) < _deposit.amount) { // Approve the deposit transfer - ERC20(token.addr).approve(depositContract, MAX_UINT); + ERC20(token.addr).approve(depositContract, ~uint(0)); } } exchange.deposit{value: ethValue}( From d063098362e58197cf9209ff9fc98d29f06f91a5 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Fri, 18 Sep 2020 12:07:08 +0800 Subject: [PATCH 2/2] approve upon setup --- packages/loopring_v3/contracts/test/AmmPool.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/loopring_v3/contracts/test/AmmPool.sol b/packages/loopring_v3/contracts/test/AmmPool.sol index 6036d6956..2cee209e3 100644 --- a/packages/loopring_v3/contracts/test/AmmPool.sol +++ b/packages/loopring_v3/contracts/test/AmmPool.sol @@ -703,13 +703,15 @@ contract AmmPool is IBlockReceiver { require(_deposit.accountID == accountID, "INVALID_TX_DATA"); require(_deposit.tokenID == token.tokenID, "INVALID_TX_DATA"); require(_deposit.amount == amount, "INVALID_TX_DATA"); + // Now do this deposit uint ethValue = 0; if (token.addr == address(0)) { - ethValue = _deposit.amount; + ethValue = amount; } else { address depositContract = address(exchange.getDepositContract()); - if (ERC20(token.addr).allowance(address(this), depositContract) < _deposit.amount) { + uint allowance = ERC20(token.addr).allowance(address(this), depositContract); + if (allowance < amount) { // Approve the deposit transfer ERC20(token.addr).approve(depositContract, ~uint(0)); }