1

I am trying sell ETH and buy DAI. Trying to test it on Ropsten but the transaction fails saying - invalid transaction key - price.

Here is the my code

let wallet = new ethers.Wallet(wallet_privateKey);
let walletSigner = wallet.connect(provTest);

// Fetch quote from 0x API
const response = await fetch(
      'https://ropsten.api.0x.org/swap/v1/quote?buyToken=DAI&sellToken=ETH&buyAmount=10000000000000000000&takerAddress=0xbd2e8c4026a3309AEb5978b51326278ffA01cb7a',
    );

const quote = await response.json();

// sending the transaction
await walletSigner.sendTransaction(await quote).then(transaction => {
      console.log(transaction);
      alert('Swap finished!');
    });

Here is the transaction fail error screenshot enter image description here

1
  • not the answer but you dont use await and `.then(...`` in the same place.
    – Majd TL
    Commented May 4, 2022 at 16:29

2 Answers 2

1

Going off of 0x_jess's answer as it led me to my version of a solution.

So the issue definitely is that web3.js is more permissive with extraneous fields where as ethers is not. In fact if you have fields it doesn't expect it hard errors like your original error.

The solution is to explicitly parse out only the fields that relate to sending the transaction from the 0x quote response.

Here's an example of one such function

const parse0xQuoteData = (quote) => {
 return {
   from: quote.from,
   to: quote.to,
   data: quote.data,
   value: quote.value,
   gasPrice: quote.gasPrice,
 };
};

This is also in the docs as a bit too small of a footnote https://docs.0x.org/0x-api-swap/guides/swap-tokens-with-0x-api#ethers.js

0

Edit: Took another pass at this question. I think you're getting the error because ethers.js is more explicit than web3 and requires you to pull out the required parameters rather than just directly plugging in the entire response.

  • This example shows directly plugging the entire json response into web3.eth.sendTransaction() like what you are doing.
  • I don't have a ethers.js example off-hand, but this this web3.js example shows pulling out the required params and only submitting those.

It looks like there's an issue with an unhandled promise reject rather than what's returned from the API (The 0x API request is working properly - https://ropsten.api.0x.org/swap/v1/quote?buyToken=DAI&sellToken=ETH&buyAmount=10000000000000000000&takerAddress=0xbd2e8c4026a3309AEb5978b51326278ffA01cb7a).

Can you check what's happening in the sending the transaction section?

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.