Generate Calldata for NFT Checkout
Create transaction calldata for NFT purchases
When using OZZOBiT's NFT Checkout feature, you need to generate calldata (encoded function call data) that represents the NFT purchase transaction. This calldata is passed to OZZOBiT so we can execute the purchase on behalf of your user.
What is Calldata?
Calldata is the hex-encoded data that gets sent as part of an Ethereum (or EVM-compatible) transaction. For NFT purchases, it contains the encoded function call to your smart contract's purchase/mint function along with all required parameters.
Generating Calldata
Identify Your Contract's Purchase Function
Determine which function on your smart contract handles purchases. Common patterns include:
| Function | Use Case |
|---|---|
| mint(address to, uint256 amount) | Minting new NFTs |
| buy(uint256 tokenId) | Purchasing existing NFTs at fixed price |
| purchase(address buyer, uint256 tokenId) | Marketplace-style purchase |
| mintWithPrice(address to, uint256 amount, bytes32 proof) | Allowlist minting |
Encode the Function Call
Use ethers.js or web3.js to encode the function signature and parameters:
// Using ethers.js v6
import { ethers } from 'ethers'
async function generateNFTCalldata(
contractAddress: string,
functionName: string,
params: any[]
): Promise<string> {
// Create contract instance (ABI only needs the target function)
const abi = [
`function ${functionName}` // Full ABI fragment with parameter types
]
const contract = new ethers.Contract(contractAddress, abi)
// Encode the function call
const calldata = await contract[functionName].encodeFunctionData(...params)
return calldata
}
// Example: Fixed price mint
const calldata = await generateNFTCalldata(
'0xYourContractAddress...', // Your verified NFT contract
'mint(address,uint256)', // Function signature
[
'0xUserWalletAddress...', // Recipient address
1 // Quantity
]
)
console.log('Calldata:', calldata)
// Output: 0xa0712d680000000000... (hex string)Pass Calldata to OZZOBiT
Include the generated calldata in your NFT checkout configuration:
// Build the nftCheckoutData object
const nftCheckoutData = {
nftContractAddress: '0xYourContractAddress...',
nftTokenId: '42',
nftNetwork: 'ethereum',
nftPrice: ethers.parseEther('0.5'), // Price in wei
nftQuantity: 1,
exchangeCallData: calldata, // The generated calldata!
}
// Pass to widget URL or SDK
const params = new URLSearchParams({
apiKey: 'YOUR_KEY',
productsAvailed: 'NFT',
nftCheckoutData: JSON.stringify(nftCheckoutData),
})
const widgetUrl = `https://OZZOBiT.com/global?${params.toString()}`Complete Example
// utils/nft-calldata.ts
import { ethers } from 'ethers'
interface NFTPurchaseConfig {
contractAddress: string
network: string
tokenId: string | number
price: string // In ETH (e.g., "0.5")
quantity?: number
buyerAddress: string
}
export async function createNFTCheckoutUrl(config: NFTPurchaseConfig): Promise<string> {
// 1. Define the ABI for your purchase function
const abi = ['function buy(uint256 tokenId) payable']
// 2. Create interface and encode
const iface = new ethers.Interface(abi)
const calldata = iface.encodeFunctionData('buy', [config.tokenId])
// 3. Convert price to wei
const priceWei = ethers.parseEther(config.price)
// 4. Build checkout data
const nftCheckoutData = {
nftContractAddress: config.contractAddress,
nftTokenId: String(config.tokenId),
nftNetwork: config.network,
nftPrice: priceWei.toString(),
nftQuantity: config.quantity || 1,
exchangeCallData: calldata,
}
// 5. Construct widget URL
const params = new URLSearchParams({
apiKey: process.env.OZZOBiT_API_KEY!,
productsAvailed: 'NFT',
walletAddress: config.buyerAddress,
nftCheckoutData: JSON.stringify(nftCheckoutData),
})
return `https://OZZOBiT.com/global?${params.toString()}`
}
// Usage:
const url = await createNFTCheckoutUrl({
contractAddress: '0x1234...abcd',
network: 'ethereum',
tokenId: 42,
price: '0.15',
quantity: 1,
buyerAddress: '0x742d35Cc6634C0532925a3b844Bc9e7595f8bD21',
})
console.log('NFT Checkout URL:', url)- Your smart contract must be verified on the blockchain explorer
- The contract must be whitelisted by OZZOBiT before use
- Calldata must exactly match your contract's expected function signature
- Test thoroughly on testnets (Goerli/Mumbai) before mainnet use
If you need assistance generating calldata for your specific contract, contact our support team through the Partner Support Hub.