0

Direct swap using JS template from the starter guide seems to be failing

const sellToken = "DAI";
const buyToken  = "USDC";

/*
    checking allowance
    Ropsten DAI address = 0xad6d458402f60fd3bd25163575031acdce07538d
 */

const erc20_abi     = utils.read_json('./includes/abi_erc20.json');

const erc20 = new web3.eth.Contract(erc20_abi, "0xad6d458402f60fd3bd25163575031acdce07538d");

let allowance = await erc20.methods.allowance(
    this.account.address,
    this.ADDRESS_0x
).call();



/* 
Currently, approval is set to MAX uint256, or
115792089237316195423570985008687907853269984665640564039457584007913129639935

*/



/*
 selllAmount = value of DAI balance of wallet before DAI-USDC swap, which you can see here
 
 https://ropsten.etherscan.io/address/0x4C603B4e1624e78c267AAe53FD729ad8D15984C6
*/


const sellAmount= "1507099614911829913"; 



const swap = {
    "sellToken"         : sellToken, 
    "buyToken"          : buyToken, 
    "sellAmount"        : sellAmount
};

const qs = createQueryString({
    "sellToken" : sellToken,
    "buyToken"  : buyToken,
    "sellAmount": sellAmount,
    "slippagePercentage" : 0.001
});

const API_QUOTE_URL=`https://ropsten.api.0x.org/swap/v1/quote`;
const quoteUrl = `${API_QUOTE_URL}?${qs}`;

const q1 = {
    "json" : await fetch(quoteUrl);
}

const direct_swap = {
    "from"      : account.address,
    "to"        : q1.json.to,
    "data"      : q1.json.data,
    "value"     : q1.json.value,
    "gasPrice"  : q1.json.gasPrice,
    "gas"       : q1.json.gas
};

///////////////////
// this fails
///////////////////


const receipt = await web3.eth.sendTransaction(direct_swap);


What am I doing wrong?

PS: Jacob from this thread mentioned:

Looks like it reverted in Uniswap V3. The contract we're using on Ropsten appears to be old. I'll update this and do some testing.

Wondering if these are related

2 Answers 2

0

It looks like you're not providing the takerAddress parameter with the address of the account performing the swap. Without takerAddress the gas estimation is unfortunately very unreliable so it's possible that the value is too low.

Try providing the takerAddress or if that is not possible, try adding a buffer to the gas value provided in the quote response

3
  • Thank you. Couple of questions. 1) if i specify takerAddress, does the wallet need to have enough balance for the gas estimation to work? I noticed i tried specifying on DAI when balance was 0 ... and i got "gas estimation failed" (http code 111 I think it was) 2) Is it possible to do multi-swap in 1 transaction? ethereum.stackexchange.com/questions/123891/…
    – TaeWoo
    Mar 15, 2022 at 14:53
  • Yes if you supply the takerAddress parameter the taker account needs to have sufficient balances and the appropriate allowances. The gas estimation essentially simulates making the trade, which will fail if any of that is missing
    – kimpers
    Mar 15, 2022 at 15:15
  • I see. Makes sense. ANy thoughts on the multi swap question?
    – TaeWoo
    Mar 15, 2022 at 23:28
0

I had this same issue with my 0x quotes failing each time I sent them to be mined (I'm running on Ethereum mainnet). I could build a transaction and send it, the tx would hit the mempool, but very quickly fail with a Revert Error.

I was not providing a takerAddress and simply using the gas value provided from the quote response. After reading @kimpers answer here, and looking more closely at the 0x docs, I read:

"When provided the gas will be estimated and returned and the entire transaction will be validated for success. If the validation fails a Revert Error will be returned in the response."

I simply changed my gas value to a really high value, e.g. 1000000 and it fixed my Revert Error!

In sum, my experience using the gas estimate from the quote API has been unreliable when I'm also not passing a takerAddress value in my initial request.

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.