0
pragma solidity 0.7.5;

import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Swap {

    address private constant QUICKSWAP_ROUTER_ADDRESS = 0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff;
    address private constant USDC_MUM_POLY_ADDRESS = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;

    IUniswapV2Router02 private quickSwapRouter = IUniswapV2Router02(QUICKSWAP_ROUTER_ADDRESS);

    function convertEthToUsdc() external payable {
        require(msg.value > 0, "Cannot handle 0 ETH");


        uint256 amountOutMin = 0;
        uint256 deadline = block.timestamp + 15;

        address[] memory path = new address[](2);
        path[0] = quickSwapRouter.WETH();
        path[1] = USDC_MUM_POLY_ADDRESS;


        quickSwapRouter.swapExactETHForTokens{value: msg.value}(
            amountOutMin,
            path,
            msg.sender,
            deadline
        );
    }

    function swapExactTokensForTokensProxy() external payable {
        require(msg.value > 0, "Cannot handle 0 ETH");

        ERC20(quickSwapRouter.WETH()).increaseAllowance(QUICKSWAP_ROUTER_ADDRESS, msg.value);

        address[] memory path = new address[](2);
        path[0] = quickSwapRouter.WETH();
        path[1] = USDC_MUM_POLY_ADDRESS;

        uint256 amountOutMin = 0;
        uint256 deadline = block.timestamp + 15;
        uint256 amountIn = msg.value;
        address to = msg.sender;

        swapExact(amountIn, amountOutMin, path, to, deadline);
    }

    function swapExact(uint256 amountIn, uint256 amountOutMin, 
        address[] memory path, address to, uint256 deadline) internal {
        quickSwapRouter.swapExactTokensForTokens(amountIn, amountOutMin, path, to, deadline);
    }
}

Above the solidity code, I'm using to convert the funds sent by the user to USDC. I'm using hardhat. Below is the deploy script. But the deployment always fails.

Txn :: https://polygonscan.com/tx/0x1b5fb063da9ef32aa32981bbf4e28924a53019e6b851bc1d254154c2eb18064e

async function main() {
   // Grab the contract factory
   const Swap = await ethers.getContractFactory("Swap");

   // Start deployment
   const swap = await Swap.deploy(); // Instance of the contract
   console.log("Contract deployed to address:", swap.address);

   const accounts = await hre.ethers.getSigners();
   const signer = accounts[0];

   //console.log("accounts", accounts);
   const swapContract = new ethers.Contract(swap.address, Swap.interface, signer);

   let transactionResponse = await swapContract.swapExactTokensForTokensProxy({value: 1000000000000000, from: accounts[0].address});
}

main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

Can someone please help to debug the issue? Thanks.

1 Answer 1

0

From the transaction trace the contract makes calls to two contracts.

The first is to the WETH() function at 0xa5e0829caced8ffdd4de3c43696c57f7d7a678ff. It is successful and returns WMATIC contract.

The second one is to the increaseAllowance(..) function at 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270. It doesn't revert but the output value is empty 0x, it should have returned a bool (32 bytes after ABI encoding).

Solidity now check the returned data size matches the declaration, since it doesn't match 0 versus the 32 expected it reverts.

Looking at WMATIC verified contract it doesn't implement increaseAllowance(..). To make the contract work you have to use approve(..) instead.

2
  • how to understand the txn trace. Could you please suggest some sources which can explain the same. Thanks. Commented Mar 15, 2022 at 19:08
  • @PalakondaShivaPrasad Sorry, but I don't have any documentation. I've learned how to use it by answering questions here. If you know how solidity works at low level you'll be able to understand the trace.
    – Ismael
    Commented Mar 15, 2022 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.