buyToken

This action is used to buy an ERC-1155 or ERC-721 token. You can also use it to buy multiple tokens of the same kind (sweeping).

You can supply an object of parameters listed below:

ParameterDescriptionRequiredExample
itemsAn array of objects representing orders to be purchased. Refer to the the execute buy api for a full list of parameterstrue[{ token: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d:1" }]
walletA valid WalletClient from viem or a ReservoirWallet generated from an adapter. Learn more about adapters.trueRefer to viem's documentation on Wallet Clients
onProgressCallback to update UI state as execution progresses. Can also be used to get the transaction hash for a given step item.true(steps) => { console.log(steps) }
expectedPriceToken price data used to protect buyer from price moves. Pass an object where the keys represent the currency and the value details the amount or/and raw amount with currency details. The raw amount will be more precise.false{ "0x0000000000000000000000000000000000000000": { amount: 10, raw: 10000000000000000000 currencyAddress: "0x0000000000000000000000000000000000000000", currencyDecimals: 18 } }
optionsSupports all of the parameters allowed in the execute buy apifalse{
source: "reservoir.market",
skipBalanceCheck: true
...
}
chainIdOverride the current active chainfalse1
precheckA boolean indicating whether to just get back the steps/path and not to execute them. This is useful for checking if marketplace approval is required before iterating over the steps.falsefalse
gasString of the gas provided for the transaction execution. Unused gas will be returned.false"1000000000000000"

Example

import { getClient, Execute } from "@reservoir0x/reservoir-sdk";
import { createWalletClient, http } from 'viem'

...

address = "0x8ba1f109551bD432803012645Ac136ddd6000000"
wallet = createWalletClient({
  account: address,
  transport: http()
})

getClient()?.actions.buyToken({
  items: [{ token: "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d:1", quantity: 1 }],
  wallet,
  onProgress: (steps: Execute['steps']) => {
    console.log(steps)
  }
})

Notes

  • expectedPrice - A feature that protects the buyer when the price changes drastically in a less desirable direction. There’s a small threshold in which it’s ok for it to go up but once it’s crossed the SDK prevents the transaction from being submitted. This check is hardcoded into the SDK and occurs before the transaction hits the blockchain.