What are the correct units and parameters for generating swap calldata using the 1inch API?

29 Views Asked by At

I am new Web3 and am using the 1inch API to build a dex and am struggling to execute a swap correctly. I am little confused as to when to use WEI and when to use the actual number of tokens I am desiring to swap. Currently, I am trying to swap ARB for ETH using the 1inch API. I make a call to a local backend API which holds my 1inch API key. I am passing the parameters directly from the swap transaction to the 1inch API that generates the swap call data (https://portal.1inch.dev/documentation/swap/swagger?method=get&path=%2Fv5.2%2F1%2Fswap)) on the back-end and then signing and submitting the transaction on the front-end with Metamask.

I am successfully receiving the call data from my local back-end and submitting the transaction with Metamask but the amounts are wrong. I am trying to swap 1 ARB for ETH on the Arbitrum network but after the transaction succeeds my ARB balance has only decreased by 0.00060121284.

I have experimented with using WEI and the actual amount of tokens I would like to swap as the parameters for the approval and swap amounts but it is not clear to me from the docs when I should use WEI and the actual amount of tokens, or what units the allowance returned by the allowance API are in (for example, the swap call data docs say to use amount in WEI but the approve calldata docs say to use the number of tokens).

If I want to swap 1 ARB for ETH using the 1inch router, what are the units I should be using for the approval, allowance check, and swap call data generation?

Here is my front-end code calling the back-end that passes the parameters to the 1inch API:

async function fetchBackendSwap(){
  const tokenOneWEI = ethers.utils.parseUnits(`${tokenOneAmount * tokenOneETH}`, 'ether').toNumber();
  const allowance = await axios.get(`http://localhost:3001/allowance?tokenAddress=${tokenOne.address}&walletAddress=${address}`)
  const tokenOnePadded = tokenOneAmount.padEnd(tokenOne.decimals+tokenOneAmount.length, '0')
  if (parseInt(allowance.data.allowance) < tokenOneAmount ) {
    const approve = await axios.get(`http://localhost:3001/approvetx?tokenAddress=${tokenOne.address}&walletAddress=${address}&amount=${tokenOnePadded}`)
    setTxDetails(approve.data);
    return
  }

  const tx = await axios.get(
    `http://localhost:3001/swaptx?fromTokenAddress=${tokenOne.address}&toTokenAddress=${tokenTwo.address}&amount=${tokenOneWEI}&walletAddress=${address}&slippage=${slippage}`
  )

  setTxDetails(tx.data.tx);
}

I expected to see 1 ARB less in my Metamask wallet and an increase of an equivalent amount in my ETH balance but the amounts are far less than expected as mentioned above.

0

There are 0 best solutions below