update approve logic

This commit is contained in:
owen05
2020-11-16 10:54:05 +08:00
parent c827514ee7
commit bd21a14b53
3 changed files with 19 additions and 8 deletions

View File

@@ -72,12 +72,12 @@ contract SmartSwap is Ownable {
if(curDirection == 0){
address curDodoBase = IDODO(curDodoPair)._BASE_TOKEN_();
uint256 curAmountIn = IERC20(curDodoBase).balanceOf(address(this));
IERC20(curDodoBase).approve(curDodoPair,curAmountIn);
IERC20(curDodoBase).universalApprove(curDodoPair,curAmountIn);
IDODO(curDodoPair).sellBaseToken(curAmountIn, 0, "");
}else {
address curDodoQuote = IDODO(curDodoPair)._QUOTE_TOKEN_();
uint256 curAmountIn = IERC20(curDodoQuote).balanceOf(address(this));
IERC20(curDodoQuote).approve(curDodoPair,curAmountIn);
IERC20(curDodoQuote).universalApprove(curDodoPair,curAmountIn);
uint256 canBuyBaseAmount = dodoSellHelper.querySellQuoteToken(curDodoPair,curAmountIn);
IDODO(curDodoPair).buyBaseToken(canBuyBaseAmount, curAmountIn, "");
}
@@ -112,10 +112,10 @@ contract SmartSwap is Ownable {
if (fromToken != ETH_ADDRESS) {
smartApprove.claimTokens(fromToken, msg.sender, address(this), fromTokenAmount);
fromToken.approve(approveTarget, fromTokenAmount);
fromToken.universalApprove(approveTarget, fromTokenAmount);
}
(bool success, ) = to.call{value: msg.value, gas: gasSwap}(callDataConcat);
(bool success, ) = to.call{value: fromToken == ETH_ADDRESS ? msg.value : 0, gas: gasSwap}(callDataConcat);
require(success, "Contract Swap execution Failed");

View File

@@ -43,7 +43,18 @@ library UniversalERC20 {
function universalApprove(IERC20 token, address to, uint256 amount) internal {
if (token != ZERO_ADDRESS && token != ETH_ADDRESS) {
token.safeApprove(to, amount);
if (amount == 0) {
token.safeApprove(to, 0);
return;
}
uint256 allowance = token.allowance(address(this), to);
if (allowance < amount) {
if (allowance > 0) {
token.safeApprove(to, 0);
}
token.safeApprove(to, amount);
}
}
}

View File

@@ -1,8 +1,8 @@
====================================================
network type: live
Deploy time: 2020/11/15 下午3:25:21
Deploy time: 2020/11/16 上午10:26:34
Deploy type: Smart Route
SmartApprove Address: 0xe380Ad3181A69BF92133D2feb609867c4adC61eA
DODOSellHelper Address: 0x533da777aedce766ceae696bf90f8541a4ba80eb
SmartSwap Address: 0xf40be68442e8bcf900FED714246f99BE556345e7
SmartApprovce setSmartSwap tx: 0xfd22c5e3ede777d21f0a0d2d8a64ccee584cc43fa2b72113c97360408c974beb
SmartSwap Address: 0xcD8392A57ED2fBD6440037E52F176c26dEFc0ba4
SmartApprovce setSmartSwap tx: 0x35eccc657ee0d473b6e58324123d3da568ce12389fedff914a8dada563b35b3a