0

I am using 0x for swapping my Binance token to other respected tokens. I got the required data from the 0X API but every time it's showing that the transaction reverted without reason error by the EVM.

swap error because of :  Error: Transaction was not mined within 750 seconds, please make sure your transaction was properly se
nt. Be aware that it might still be mined!
    at Object.TransactionError (node_modules\web3-eth\node_modules\
web3-core-helpers\lib\errors.js:87:21)
    at node_modules\web3-eth\node_modules\web3-core-method\lib\inde
x.js:418:49
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  receipt: undefined
}

This is our token -

https://bscscan.com/address/0x67b198a69f8cdab39524a00554c3dd50b6ba7fec

JS -

 await web3.eth.accounts.wallet.add('pvt_key');

const response = await fetch(
  `https://bsc.api.0x.org/swap/v1/quote?buyToken=wbnb&sellToken=0x67B198A69F8cdAb39524a00554c3DD50B6bA7feC&sellAmount=1000000000000000`
);

const quote = await response.json();

const contractApprove = await contract.methods
  .approve(quote.allowanceTarget, quote.sellAmount)
  .send({
    from: '0x69DcDd813a4F437983c8bB5c9a1370c3211B91A4',
    gas: '500000',
  });

console.log('contractApprove', contractApprove);
const nonce = await web3.eth.getTransactionCount(
    '0x69DcDd813a4F437983c8bB5c9a1370c3211B91A4'
);

  const struct = {
  from: '0x69dcdd813a4f437983c8bb5c9a1370c3211b91a4',
  to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
  value: quote.value.toString(),
  gas: '500000',
  gasPrice: '500000',
  data: quote.data,
  nonce,
  chainId: '0x38',
};

const swap = await web3.eth.sendTransaction(struct);
console.log('swap', JSON.stringify(swap));
await successHandler(res, swap, 'Swap is successful.');

It's not working yet. My latest two transaction URL is this -

My wallet data - https://bscscan.com/address/0x69dcdd813a4f437983c8bb5c9a1370c3211b91a4

Any Help will be appreciated.

1 Answer 1

2

Based on the transaction tracing, it reverts because you did not approve an allowance to the 0x contract.

Simply call the approve function on the token with 0xDef1C0ded9bec7F1a1670819833240f027b25EfF as sender and with an amount greater than or equal the amount you want to swap. Your swap should work after that.

EDIT: With the last tx you linked to (after your edit), you did not provide enough gas. Increase the gas limit from 111000 to something like 500000.

EDIT 2:

Change:

  gas: '500000',
  gasPrice: '500000',

To:

  gas: '500000',
  gasPrice: '5000000000',
10
  • I have updated the question. Nov 16, 2021 at 8:23
  • With the last tx you linked to, you did not provide enough gas. Increase the gas limit from 111000 to something like 500000.
    – Undead8
    Nov 16, 2021 at 15:59
  • Ok did it now it's showing this error. swap error because of : Error: Returned error: nonce too low Nov 18, 2021 at 6:54
  • It is because you use the same nonce for the approve transaction and the swap transaction. Either increment the nonce manually, or wait for the approve transaction to be validated before creating the swap transaction.
    – Undead8
    Nov 18, 2021 at 16:03
  • Yes, I have modified the code as per the guide and now I am getting this error and updated the question with the latest info. Nov 19, 2021 at 12:22

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.