Skip to content

Commit ed32f24

Browse files
authoredJun 25, 2020
update token.approve to max allowance in kyberNetwork contract. from '2**255' to '2*256-1'
* update token.approve to max allowance. from 2**255 to 2*256-1 When trying to list COMP token (compound) we found we can't use approve(2**255) can only accept [max int] or [amount <= 2**96]
1 parent 4305bba commit ed32f24

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed
 

‎contracts/sol6/KyberNetwork.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ contract KyberNetwork is WithdrawableNoModifiers, Utils5, IKyberNetwork, Reentra
225225
require(msg.sender == address(kyberStorage), "only kyberStorage");
226226

227227
if (add) {
228-
token.safeApprove(reserve, 2**255);
228+
token.safeApprove(reserve, MAX_ALLOWANCE);
229229
setDecimals(token);
230230
} else {
231231
token.safeApprove(reserve, 0);
@@ -264,7 +264,7 @@ contract KyberNetwork is WithdrawableNoModifiers, Utils5, IKyberNetwork, Reentra
264264

265265
for(uint i = 0; i < reserves.length; i++) {
266266
if (add) {
267-
token.safeApprove(reserves[i], 2**255);
267+
token.safeApprove(reserves[i], MAX_ALLOWANCE);
268268
setDecimals(token);
269269
} else {
270270
token.safeApprove(reserves[i], 0);

‎contracts/sol6/utils/Utils5.sol

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ contract Utils5 {
1919
uint256 internal constant MAX_DECIMALS = 18;
2020
uint256 internal constant ETH_DECIMALS = 18;
2121
uint256 constant BPS = 10000; // Basic Price Steps. 1 step = 0.01%
22+
uint256 internal constant MAX_ALLOWANCE = uint256(-1); // token.approve inifinite
2223

2324
mapping(IERC20 => uint256) internal decimals;
2425

‎test/helper.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const emptyHint = '0x';
1515
const zeroBN = new BN(0);
1616
const MAX_QTY = new BN(10).pow(new BN(28));
1717
const MAX_RATE = precisionUnits.mul(new BN(10).pow(new BN(7)));
18-
module.exports = {BPS, precisionUnits, ethDecimals, ethAddress, zeroAddress, emptyHint, zeroBN, MAX_QTY, MAX_RATE};
18+
const MAX_ALLOWANCE = ((new BN(2)).pow(new BN(256))).sub(new BN(1));
19+
module.exports = {BPS, precisionUnits, ethDecimals, ethAddress, zeroAddress,
20+
emptyHint, zeroBN, MAX_QTY, MAX_RATE, MAX_ALLOWANCE};
1921

2022
module.exports.isRevertErrorMessageContains = function(error, msg) {
2123
return (error.message.search(msg) >= 0);

‎test/sol6/kyberNetwork.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ const nwHelper = require("./networkHelper.js");
2525
const BN = web3.utils.BN;
2626
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
2727

28-
const {BPS, precisionUnits, ethDecimals, ethAddress, zeroAddress, emptyHint, zeroBN, MAX_QTY, MAX_RATE} = require("../helper.js");
28+
const {BPS, precisionUnits, ethDecimals, ethAddress, zeroAddress, emptyHint,
29+
zeroBN, MAX_QTY, MAX_RATE, MAX_ALLOWANCE} = require("../helper.js");
2930
const {APR_ID, BRIDGE_ID, MOCK_ID, FPR_ID, type_apr, type_fpr, type_MOCK,
3031
MASK_IN_HINTTYPE, MASK_OUT_HINTTYPE, SPLIT_HINTTYPE, BEST_OF_ALL_HINTTYPE, ReserveType} = require('./networkHelper.js');
3132

@@ -551,7 +552,7 @@ contract('KyberNetwork', function(accounts) {
551552
{from: tempStorage}
552553
);
553554
Helper.assertEqual(
554-
new BN(2).pow(new BN(255)),
555+
MAX_ALLOWANCE,
555556
await token.allowance(tempNetwork.address, mockReserve.address)
556557
);
557558
});
@@ -662,7 +663,7 @@ contract('KyberNetwork', function(accounts) {
662663
Helper.assertEqual(eventLogs.args.reserves.length, 1);
663664
Helper.assertEqual(reserveAddresses[0], eventLogs.args.reserves[0]);
664665
Helper.assertEqual(
665-
new BN(2).pow(new BN(255)),
666+
MAX_ALLOWANCE,
666667
await token.allowance(tempNetwork.address, reserveAddresses[0])
667668
);
668669
// unlist reserves
@@ -711,7 +712,7 @@ contract('KyberNetwork', function(accounts) {
711712
for(let i = 0; i < 1; i++) {
712713
Helper.assertEqual(reserveAddresses[i], eventLogs.args.reserves[i]);
713714
Helper.assertEqual(
714-
new BN(2).pow(new BN(255)),
715+
MAX_ALLOWANCE,
715716
await token.allowance(tempNetwork.address, reserveAddresses[i])
716717
);
717718
}

0 commit comments

Comments
 (0)
Please sign in to comment.