3

I am making a dex using 0x API. I did the swap and allow token spend with web3 But I am facing an issue. Whenever I am swapping tokens I need to allow token spend every time but when you look at PancakeSwap they ask for spending only once, I want to achieve that in my dex.

  const BscABI = await axios.get(`https://api.bscscan.com/api`, {
     params: {
          module: 'contract',
          action: 'getabi',
          address: receipt.sellTokenAddress,
        }
    })
    const TokenAbi = JSON.parse(BscABI.data.result);
    const Tokencontract = new web3.eth.Contract(TokenAbi, receipt.sellTokenAddress);
    const maxApproval = new BigNumber(2).pow(256).minus(1);
    await Tokencontract.methods.approve(receipt.allowanceTarget, maxApproval).send({from: metaMaskAddress, chainId: 56})
    .then((data: any) => {
      alert("Token Spend Allowed");
    })
    .then(async () => {
      const txHash = await web3.eth.sendTransaction({
        from: metaMaskAddress,
        data: receipt.data,
        gas: receipt.gas,
        to: receipt.allowanceTarget,
        chainId: 56
      });
    )
    .catch(() => {
        setMessage("Token Spend Allowance Failed");
        setError(true);
    });
TylerH
  • 20,799
  • 66
  • 75
  • 101
Mohit Chandel
  • 1,838
  • 10
  • 31

0 Answers0