Transferring the remainder to a user

After buying an NFT or paying a set amount, there may be tokens left over, since the amountOut from the swap can't be determined in advance.

It's possible to transfer the remainder of the tokens after the swap and the purchase to the user's address.

You'll need to use a different call type based on the type of token you are transferring

const SquidCallType = {
  "DEFAULT": 0,
  "FULL_TOKEN_BALANCE": 1,
  "FULL_NATIVE_BALANCE": 2,
  "COLLECT_TOKEN_BALANCE": 3
}

Transferring remainder if the token is an ERC20:

const erc20ContractInterface = new ethers.utils.Interface(erc20Abi);
const transferRemainingEncodeData = erc20ContractInterface.encodeFunctionData(
    "transfer",
    [signer.address, "0"] // amount is set to zero, but full balance is used, see comment below
);

{
        callType: SquidCallType.FULL_TOKEN_BALANCE,
        target: erc20TokenAddress,
        value: "0",
        callData: transferRemainingEncodeData,
        payload: {
          tokenAddress: erc20TokenAddress,
          inputPos: 1, // argument position of amount
        },
        estimatedGas: "50000",
},    

Transferring remainder if the token is native:

If you want to transfer the remaining native balance to a user, you should use FULL_NATIVE_BALANCE

{
    callType: SquidCallType.FULL_NATIVE_BALANCE,
    target: recipientAddress,
    value: "0",
    callData: "0000", // unused for full native balance
    payload: {
      tokenAddress: "0x", // unused for full native balance
      inputPos: 0, // unused for full native balance
    },
    estimatedGas: "50000",
}

Last updated