Skip to main content
Edited based on comment info
Source Link
Undead8
  • 3.6k
  • 10
  • 28

You did not approve USDT to the router address before attempting the swap.

The USDT contract uses assert instead of revert or throw to cancel the transaction because of the lack of approval. Using assert will consume all the gas left. It's bad practice by Tether to use assert, but it's probably because their contract is really old.

EDIT: Also, the USDT contract requires you to set the allowance to 0 before making another approve call. Here is the relevant code of the USDT contract:

function approve(address _spender, uint _value) public onlyPayloadSize(2 * 32) {

    // To change the approve amount you first have to reduce the addresses`
    //  allowance to zero by calling `approve(_spender, 0)` if it is not
    //  already 0 to mitigate the race condition described here:
    //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
    require(!((_value != 0) && (allowed[msg.sender][_spender] != 0)));

    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
}

You did not approve USDT to the router address before attempting the swap.

The USDT contract uses assert instead of revert or throw to cancel the transaction because of the lack of approval. Using assert will consume all the gas left. It's bad practice by Tether to use assert, but it's probably because their contract is really old.

You did not approve USDT to the router address before attempting the swap.

The USDT contract uses assert instead of revert or throw to cancel the transaction because of the lack of approval. Using assert will consume all the gas left. It's bad practice by Tether to use assert, but it's probably because their contract is really old.

EDIT: Also, the USDT contract requires you to set the allowance to 0 before making another approve call. Here is the relevant code of the USDT contract:

function approve(address _spender, uint _value) public onlyPayloadSize(2 * 32) {

    // To change the approve amount you first have to reduce the addresses`
    //  allowance to zero by calling `approve(_spender, 0)` if it is not
    //  already 0 to mitigate the race condition described here:
    //  https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
    require(!((_value != 0) && (allowed[msg.sender][_spender] != 0)));

    allowed[msg.sender][_spender] = _value;
    Approval(msg.sender, _spender, _value);
}
Source Link
Undead8
  • 3.6k
  • 10
  • 28

You did not approve USDT to the router address before attempting the swap.

The USDT contract uses assert instead of revert or throw to cancel the transaction because of the lack of approval. Using assert will consume all the gas left. It's bad practice by Tether to use assert, but it's probably because their contract is really old.