Skip to content

Amm approve erc20 on setup #1762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/loopring_v3/contracts/test/AmmPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ contract AmmPool is IBlockReceiver {
uint pos
);

uint public constant MAX_UINT = ~uint(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a constant?


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;
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -699,15 +703,17 @@ 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, MAX_UINT);
ERC20(token.addr).approve(depositContract, ~uint(0));
}
}
exchange.deposit{value: ethValue}(
Expand Down