Scroll for EDIT
I'm using Solidity 0.7.5 and I'm trying to swap DAI to ETH from my account, through UniswapRouter02.
Here's my environment
- REMIX IDE
- Metamask with KOVAN testnet
- I have about 2 ETH and 3 DAI on my linked account (msg.sender).
And my code follows
pragma solidity >=0.7.0 <0.8.0;
import "https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";
contract UniswapExample {
address internal constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
IUniswapV2Router02 public uniswapRouter;
address private kovanDAI = 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa;
address private myAccount = msg.sender;
constructor() payable {
uniswapRouter = IUniswapV2Router02(UNISWAP_ROUTER_ADDRESS);
}
function convertDAItoETH(uint daiAmountIn, uint minETHAmountToReceieve) public payable {
IERC20 daiToken = IERC20(kovanDAI);
require(daiToken.transferFrom(myAccount, address(this), daiAmountIn), "transferFrom failed");
require(daiToken.approve(address(uniswapRouter), daiAmountIn), "approve failed");
uint deadline = block.timestamp + 15; // using 'now' for convenience, for mainnet pass deadline from frontend!
uniswapRouter.swapExactTokensForETH(daiAmountIn, minETHAmountToReceieve, getPathForDAItoETH(), myAccount, deadline);
}
function getPathForDAItoETH() private view returns (address[] memory) {
address[] memory path = new address[](2);
path[0] = kovanDAI;
path[1] = uniswapRouter.WETH();
return path;
}
}
I followed the example from various YouTube videos and from Uniswap (https://uniswap.org/docs/v2/smart-contract-integration/trading-from-a-smart-contract/).
I encounter 2 problems.
First, when I click convertDAItoETH with param 280000,0 (random number with 0 ETH lower-limit)
I get this message.
When I ignore it and click Send Transaction, and go through Metamask to confirm the transaction, my transaction is failed soon. And this is my second problem.
When I go to etherscan and see the transaction details, it tells me that it failed
Failed with error 'Dai/insufficient-allowance'
.
What does this even mean? As you can see, I have 3.5 DAI, so swapping 280000 DAI should be Okay.
Please help. Barely started learning this, but I just simply cannot find any good resources on how to debug this type of error.
Thank you!
EDIT
I moved my network to Ropsten.
I've created a function
function setSmartContractAllownace(uint daiAmountIn) public {
daiToken.approve(address(this), daiAmountIn);
}
When I run this it successfully ran through (tx: https://ropsten.etherscan.io/tx/0x1a3b29699f353938f7e8b1825ff69aa44e262d90165e87b107131fc5813d05e3)
And then I go to https://ropsten.etherscan.io/address/0xaD6D458402F60fD3Bd25163575031ACDce07538D#readContract to check the allowance.
_owner (address)
0x548F97658C6acb9a5A0B034116E79bcF98B9a15a -> (my account)
_spender (address)
0x99941f04C4a1cba96c237e02F1B2f2b3C5Bae5ea -> (my contract)
returns 0.
However, if I enter both _owner and _spender as my CONTRACT address, I get what I set to.
I thought approve(spender, amountIn)
approves owner (account)'s token for spender (contract in this case) to spend.
What am I misunderstanding?
0x62dE96237146061E4a71B7c89b1b114185c78695
, contract:0xC27fDB217d640d561539F161516c2E43697Ea4c6
. I get exactly the same result :(