Truffle Test - Uniswap Router - Swap ETH for Token

0

I am trying to add an test for swapping ETH to Token. However, this works only if I am specifying as Receiver the Owner of the contract. As soon as I am changing the “to” parameter to another address than the contract owner I am receiving an “revert UniswapV2: TRANSFER_FAILED”.

Working code:


await uniswapRouter.swapExactETHForTokensSupportingFeeOnTransferTokens
            .sendTransaction(new BN("1000000000000000000"), 
                            [WETH_ADDRESS, contract.address],
                            expectedOwner, 
                            uniswapBuyExpires, 
                            { value: web3.utils.toWei(new BN(1), 'ether'), from: expectedOwner});

Not working (throwing an TRANSFER_FAILED):

await uniswapRouter.swapExactETHForTokensSupportingFeeOnTransferTokens
            .sendTransaction(new BN("1000000000000000000"), 
                            [WETH_ADDRESS, contract.address],
                            accounts[5], 
                            uniswapBuyExpires, 
                            { value: web3.utils.toWei(new BN(1), 'ether'), from: expectedOwner});

as well as (changing “from”)

await uniswapRouter.swapExactETHForTokensSupportingFeeOnTransferTokens
            .sendTransaction(new BN("1000000000000000000"), 
                            [WETH_ADDRESS, contract.address],
                            accounts[5], 
                            uniswapBuyExpires, 
                            { value: web3.utils.toWei(new BN(1), 'ether'), from: accounts[5]});

Even increasing the allowance of the accounts[5] address won’t solve the error message. I would appreciate any ideas!

Best regards,

Hello! Is expectedOwner a contract or an EOA? Any other details you can share about those variables or contracts?

1 Like

also what token is this? do you control expectedOwner?

1 Like

Hey! I’ve actually found the solution - the problem was the amount I’ve provided. I called the “getAmountsIn()” function of IUniswapRouter to get an estimated number of tokens for a given amount of ether.

Thanks anyway!

Best regards,