1

I'm trying to make a simple swap from 10 USDC to 10 USDT using 0x API quote.

I'm doing this:

import type { UnsignedTransaction } from '@ethersproject/transactions'
import { Web3Provider } from '@ethersproject/providers'

const provider = new Web3Provider(window.ethereum)

const [address] = await window.ethereum.request({ method: 'eth_accounts' })

const res = await fetch('https://api.0x.org/swap/v1/quote?buyAmount=1000000&buyToken=USDT&sellToken=USDC')

const quote = await res.json()

// not sure what to do next?

What should be the next step to perform a swap? Using a Web3Provider.

Reference: https://0x.org/docs/guides/swap-tokens-with-0x-api#selling-erc20-tokens

3 Answers 3

0

0x API returns a JSON object with standard ethereum transaction fields already populated. You should follow your web3 library's guide for sending transaction objects. Depending on whether you pass in takerAddress as part of your request, you may also have to populate the gas fied on your own.

Don't forget to set appropriate token allowances in advance.

2
  • 1
    Could you please show a code example with ethers.js code? I'm not getting what I should put in types and domain for 0x Protocol.
    – wutt
    Sep 24, 2021 at 8:14
  • Not sure where you're seeing that. You shouldn't need to worry about those concepts if you're just trying to fill a swap quote. Sep 27, 2021 at 17:41
0

Since the response of /quote endpoint is already a transaction, I think the line you have to add is :

const mintTx = await provider.sendTransaction(quote);
0

just seeing this, this setup worked for me, I pulled in the signer with the wagmi lib (useSigner hook):

await signer?.sendTransaction({
                        from: yourConnectedAddress,
                        to: quote.to,
                        data: quote.data,
                        value: quote.value,
                        gasLimit: quote.estimatedGas,
                    });

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.