Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Recipe — ERC-20 approve + commit · Boson Protocol
Skip to content

Recipe: ERC-20 approve + commit

For ERC-20-priced offers, the buyer must approve the Diamond to pull tokens before committing. This is two transactions; both paid by the buyer.

const sdk = CoreSDK.fromDefaultConfig({
  web3Lib,
  envName: "production",
  configId: "production-137-0",
})
 
// 1. Approve (idempotentskip if already approved >= amount). The SDK helper
//    takes the token address and queries against the protocol Diamond by default.
const allowance = await sdk.getExchangeTokenAllowance(USDC)
if (allowance.lt(offer.price)) {
  // First arg is the token address, not the offer id.
  await (await sdk.approveExchangeToken(USDC, offer.price)).wait()
}
 
// 2. CommitcommitToOffer(offerId, overrides?); buyer is the signer.
const tx = await sdk.commitToOffer(offer.id)
const receipt = await tx.wait()
const exchangeId = sdk.getCommittedExchangeIdFromLogs(receipt.logs)

Tips

  • Approve more than price if you intend to commit to several offers in the same token. Saves one approve per commit.
  • Use MaxUint256 for "unlimited" approval — common UX pattern. Risk: if your buyer's private key is compromised, attacker can drain the approved amount.
  • For one-shot UX, use the token-auth meta-tx flow to combine approve + commit in a single signed envelope. See Recipe → Gas-less commit.

Common reverts

  • InsufficientAllowance — the buyer didn't approve enough; or they approved the wrong spender (e.g. the voucher contract instead of the Diamond).
  • InsufficientBalance — the wallet's USDC balance < price.
  • OfferVoided / OfferHasExpired — pre-flight your reads to surface this to the user.

Related