in

ERC20 Error approving an amount from solidity

Hi guys I’m trying to study solidity by doing a flash loan that buys USDT from Uniswap and sells them for WETH on Sushiswap (On Kovan Testnet).

Everything works until I have to sell them on Sushiswap, in particular, the error raise while approving an amount (pathB.route[0] is USDT address):

IERC20(pathB.route[0]).approve(address(sushiswapRouter), amountReceived),

What could be the problem? I’m sure that the smart contract has that amount of USDT at that point.

That looks strange stange for me because it’s the same code used to swap on uniswap…

Tx: [https://kovan.etherscan.io/tx/0x23b0deed9ea022cd19cb8ac31c452ed332523b9a34b34ba7b356217280210d0b](https://kovan.etherscan.io/tx/0x23b0deed9ea022cd19cb8ac31c452ed332523b9a34b34ba7b356217280210d0b)

contract FlashloanV2 is FlashLoanReceiverBaseV2, Withdrawable {
IUniswapV2Router02 immutable uniswapV2Router;
IUniswapV2Router02 immutable sushiswapRouter;

struct Path {
address[] route;
}

constructor(address _addressProvider) FlashLoanReceiverBaseV2(_addressProvider) public {
uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
sushiswapRouter = IUniswapV2Router02(0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506);
}

function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
)
external
override
returns (bool)
{
(Path memory pathA, Path memory pathB) = abi.decode(params, (Path, Path));
uint256 deadline = getDeadline();
for (uint i = 0; i < assets.length; i++) {
require(
IERC20(pathA.route[0]).approve(address(uniswapV2Router), amounts[i]),
“Could not approve token0A sell”
);
uint256 amountReceived = uniswapV2Router.swapExactTokensForTokens(amounts[i], 0, pathA.route, address(this), deadline)[1];

//Everything above is ok, here start the problems

require(
IERC20(pathB.route[0]).approve(address(sushiswapRouter), amountReceived),
“Could not approve token0B sell”
);
sushiswapRouter.swapExactTokensForTokens(amountReceived, 0, pathB.route, address(this), deadline);

uint amountOwing = amounts[i].add(premiums[i]);
IERC20(assets[i]).approve(address(LENDING_POOL), amountOwing);
}
return true;
}

function _flashloan(address[] memory assets, uint256[] memory amounts, bytes memory params) internal {
address receiverAddress = address(this);
address onBehalfOf = address(this);
uint16 referralCode = 0;
uint256[] memory modes = new uint256[](assets.length);
for (uint256 i = 0; i < assets.length; i++) {
modes[i] = 0;
}
LENDING_POOL.flashLoan(
receiverAddress,
assets,
amounts,
modes,
onBehalfOf,
params,
referralCode
);
}
}

What do you think?

10 Points
Upvote Downvote

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

WSB DApp (by Apes 🐵) Decentralized application that brings ETFs to DeFi! 📊 Everything great about DeFi united in one single project! 🤩

Poshmark BULL DD: Its not Roger Federer, but who says you can’t make some money on Serena Williams