0

I have deployed a smart contract on a local network and I am trying to do swaps from a metamask account. I have allocated a quantity of tokens to the smart contract so it can use it using the approve function.

When calling the function I get no error and it says Transaction succeeded but the balance of USDT tokens in my Metamask doesnt change. I should be swapping USDT for USDC, but nothing seems to happen.

In summary, I want to call this function from the user's wallet, swap his USDT for USDC, and send him back the USDC.

Transaction ID: 0x5ba32142dd9b2f90ce20c992eacb16b6091b7a53833c25ad536466b747789c70

This is what I do to call the swap function in my react app:

await contract.methods.swapMultiHopExactAmountIn('1000', '900').send({ from: account });

and this is the smart contract where I swap the tokens:

contract spendingContract {
address private constant UNISWAP_V2_ROUTER =
    0x4A679253410272dd5232B3Ff7cF5dbB88f295319;

address private constant weth= 0xa85233C63b9Ee964Add6F2cffe00Fd84eb32338f;
address private constant usdt = 0x3Aa5ebB10DC797CAC828524e59A333d0A371443c;
address constant usdc = 0xc6e7DF5E7b4f2A278906862b61205850344D4e7d;

IUniswapV2Router private router = IUniswapV2Router(UNISWAP_V2_ROUTER);
IERC20 private WETH= IERC20(weth);
IERC20 private USDT = IERC20(usdt);
IERC20 private USDC = IERC20(usdc);


// Swap usdt -> weth-> usdc
function swapMultiHopExactAmountIn(
    uint amountIn,
    uint amountOutMin
) external returns (uint amountOut) {
    USDT.transferFrom(msg.sender, address(this), amountIn);
    USDT.approve(address(router), amountIn);

    address[] memory path;
    path = new address[](3);
    path[0] = usdt;
    path[1] = weth;
    path[2] = usdc;

    uint[] memory amounts = router.swapExactTokensForTokens(
        amountIn,
        amountOutMin,
        path,
        msg.sender,
        block.timestamp
    );

    // amounts[0] = usdt amount
    // amounts[1] = weth amount
    // amounts[2] = usdc amount
    
    return amounts[2];
}
receive() external payable { }
}


interface IUniswapV2Router {
function swapExactTokensForTokens(
    uint amountIn,
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external returns (uint[] memory amounts);

function swapTokensForExactTokens(
    uint amountOut,
    uint amountInMax,
    address[] calldata path,
    address to,
    uint deadline
) external returns (uint[] memory amounts);
}

interface IERC20 {
function totalSupply() external view returns (uint);

function balanceOf(address account) external view returns (uint);

function transfer(address recipient, uint amount) external returns (bool);

function allowance(address owner, address spender) external view returns (uint);

function approve(address spender, uint amount) external returns (bool);

function transferFrom(
    address sender,
    address recipient,
    uint amount
) external returns (bool);

event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
1
  • If you are using a local network, there is no help in stating the tx hash, because no one can access your local network. If you need to get a considerable help on this, deploy on a testnet and post the details. Nov 25, 2023 at 18:58

0

Your Answer

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

Browse other questions tagged or ask your own question.