I am trying to call a simple contract, which I created, from ether.js to swap DAI to USDT on KOVAN. Essentially, the sol function takes the passed array of address[] and is doing a transfer from sender (i.e. me) to the contract address.
Solidity :
function swapTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
uint256 deadline
)
external
payable
// returns (uint[] memory amounts)
{
// Transfer tokens from senders address to our smart contract
// with the amountIn argument passed
IERC20(path[0]).transferFrom(msg.sender, address(this), amountIn);
I am calling this via Javascript :
const signer = new ethers.Wallet(PRIVATE_KEY)
const account = signer.connect(provider)
let MY_CONT_ADDRESS_KOVAN = '0xF54f5E75B3cACe5706e27F5d44B2C06fcF28a8d6'
const my_specific_cont = new ethers.Contract(
MY_CONT_ADDRESS_KOVAN,
['function swapTokensForTokens(uint256 amountIn, uint256 amountOutMin, address[] calldata path, uint256 deadline ) external payable'],
account
)
const KOV_DAI_CONT_ADD = '0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa'
const KOV_DAI_CONT_ABI = '… removed for brevity'
const KOV_USDT_CONT_ADD = '0x07de306ff27a2b630b1141956844eb1552b956b5'
const KOV_USDT_CONT_ABI = '… removed for brevity'
const receiverWallet = new ethers.Wallet(PRIVATE_KEY, provider)
const deadline = Math.floor(Date.now() / 1000) + 60 * 5
const tx = await my_specific_cont.swapTokensForTokens(
1,
1,
[
KOV_DAI_CONT_ADD, //dai
KOV_USDT_CONT_ADD //USDT
],
deadline,
{ value: 1 }
)
log('tx', tx)
Output Error :
(node:24236) UnhandledPromiseRejectionWarning: Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"reason":"cannot estimate gas; transaction may fail or may require manual gas
limit","code":"UNPREDICTABLE_GAS_LIMIT","method":"estimateGas","transaction":{"from":"0x2aeEcb55A573a6b4dea9C010020EaD9536E2606D","gasPrice":{"type":"BigNumber","hex":"0x04a817c800"},"to":"0xF54f5E75B3cACe5706e27F5d44B2C06fcF28a8d6","value":{"type":"BigNumber","hex":"0x64"},"data":"0xcf89fc450……………….removed for brevity"
When I comment the following line and all other lines in my sol function, transaction succeeds. So there is something wrong what I am doing here:
IERC20(path[0]).transferFrom(msg.sender, address(this), amountIn);
I have spent many hours but can't seem to figure out what am I doing wrong. Any help will be really appreciated.