getBuyWithFiatQuote

Get a quote of type BuyWithFiatQuote to buy given token with fiat currency. This quote contains the information about the swap such as token amounts, processing fees, estimated time etc.

Rendering the On-Ramp provider UI

Once you have the quote , you can open the quote.onRampLink in a new tab - This will prompt the user to buy the token with fiat currency

Determining the steps required

If quote.onRampToken.token is same as quote.toToken ( same chain + same token address ) - This means that the token can be directly bought from the on-ramp provider. But if they are different, On-ramp provider will send the quote.onRampToken to the user's wallet address and a swap is required to swap it to the desired token onchain.

You can use the isSwapRequiredPostOnramp utility function to check if a swap is required after the on-ramp is done.

Polling for the status

Once you open the quote.onRampLink in a new tab, you can start polling for the status using getBuyWithFiatStatus to get the status of the transaction.

getBuyWithFiatStatus returns a status object of type BuyWithFiatStatus .

  • If no swap is required - the status will become "ON_RAMP_TRANSFER_COMPLETED" once the on-ramp provider has sent the desired token to the user's wallet address. Once you receive this status, the process is complete.

  • If a swap is required - the status will become "CRYPTO_SWAP_REQUIRED" once the on-ramp provider has sent the tokens to the user's wallet address. Once you receive this status, you need to start the swap process.

Swap Process

On receiving the "CRYPTO_SWAP_REQUIRED" status, you can use the getPostOnRampQuote function to get the quote for the swap of type BuyWithCryptoQuote .

Once you have this quote - You can follow the same steps as mentioned in the getBuyWithCryptoQuote documentation to perform the swap.

Example

Get a quote for buying 10 USDC on polygon chain (chainId: 137) with USD fiat currency:

import { getBuyWithFiatQuote } from "thirdweb/pay";
const quote = await getBuyWithFiatQuote({
client: client, // thirdweb client
fromCurrencySymbol: "USD", // fiat currency symbol
toChainId: 137, // polygon chain id
toAmount: "10", // amount of USDC to buy
toTokenAddress: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" // USDC token address in polygon chain
toAddress: "0x...", // user's wallet address
isTestMode: false, // whether to use onramp in test mode for testing purpose (defaults to false)
});
window.open(quote.onRampLink, "_blank");

Parameters

Returns

Object of type BuyWithFiatQuote which contains the information about the quote such as processing fees, estimated time, converted token amounts, etc.