Get Started
Learn how to get a quote and send crypto purchase transactions.
Using the Connect UI
Buy With Crypto comes by default with our ConnectButton React component.
You can simply add the ConnectButton
component to your React app. Connect handles everything including connecting the user to various wallets, sending & receiving tokens, and buying with crypto.
Using the SDK
There are 3 main steps to perform a Buy With Crypto transaction:
- Get a price quote for the transaction
- Execute the transaction with users connected wallet
- Wait for the transaction to be confirmed by polling the status of the transaction
Install thirdweb
Create a client
A client is required to use thirdweb SDK. Refer to this guide to learn how to create a client.
Get a Buy Quote
You can get a quote by using the
useBuyWithCryptoQuote
hook.This hook takes an object with the following properties:
- client: The Thirdweb client instance.
- fromAddress: The address of the wallet initiating the transaction.
- toTokenAddress: The contract address of the destination token. This is the token the user wants to purchase.
- fromTokenAddress: The contract address of the source token. This is the token the user wants will use to purchase the destination token.
- fromChainId: The chain id of the source token.
- toChainId: The chain id of the destination token.
- toAmount: The amount of the token you to purchase.
Send a Transaction
Once you've received a quote, you can execute a buy transaction by using the
useSendTransaction
hook and then waiting for the transaction to be confirmed using thewaitForReceipt
function.Note that buys with non-native (ERC20) tokens requires executing an approval transaction before executing the buy transaction as shown in the example below.
Listening to Transaction Status
Once you've submitted the transaction, you can listen to a transaction's status using the
useBuyWithCryptoStatus
hook.Set Up Fee Sharing
Now that you've set up Buy With Crypto, you can configure a recipient address to earn on every user transaction.