0

I am trying to transfer a token from any address to my contract or another address using solidity but it is not working. the TransferTokenAddress function is the one I'm running.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;


interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);
}



contract CArbi{

    constructor() {
    }
 
    function TransferTokenAddress(address token, address from, address to, uint monto) public {
            IERC20(token).approve(from, monto);
            transferFrom(token, from, to, monto);
        }
}

I tried with the transfer function the error persists

1

1 Answer 1

0

Looks like you are approving as the contract, the from address to move the balance of the contract. Then trying without an allowance to move tokens from the externally owned account.

If you want the contract to be able to move your tokens to itself, then call approve on the token contract from your address. With the contract as spender, and the max amount you want to allow.

It's the same reason you have to approve a router before calling swap, or use a permit.

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.