Follow the steps below to make purchase of any order(s).

Fetch order info from X2Y2 server

You can use Fetch open orders API to filter and get any open orders.

const params: Record<string, string> = {
    contract: tokenAddress,
    token_id: tokenId,
    network_id: '1', // 1: Mainnet; 5: Goerli
}
const orderReponse = await get('https://api.x2y2.org/v1/orders', {
    params, 
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'X-API-KEY': X2Y2_API_KEY,
    }
}
// Extract order info from orderReponse…

Sign order info via X2Y2 server

The order info needs to be signed by X2Y2 server via Sign order info. If the order is cancelled or expired, the signning will fail.

const signResponse = await post('https://api.x2y2.org/api/orders/sign', JSON.stringify({
    caller: buyerAddress,
    op: 1,
    amountToEth: '0',
    amountToWeth: '0',
    items: [{
        orderId: sellOrder.id, 
        currency: sellOrder.currency, 
        price: sellOrder.price,
        tokenId // Only required when accepting collection offer
    }],
    check: true,
}), {
    headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'X-API-KEY': X2Y2_API_KEY,
    }
}
                                
// Extract signed info(input) from signResponse…

Decode signed order info

The signed order info needs to be decoded by ABI before it could be submitted via buyer's wallet.

const orderInput = ethers.utils.defaultAbiCoder.decode(
    [`tuple(tuple(uint256 salt, address user, uint256 network, uint256 intent, uint256 delegateType, uint256 deadline, address currency, bytes dataMask, tuple(uint256 price, bytes data)[] items, bytes32 r, bytes32 s, uint8 v, uint8 signVersion)[] orders, tuple(uint8 op, uint256 orderIdx, uint256 itemIdx, uint256 price, bytes32 itemHash, address executionDelegate, bytes dataReplacement, uint256 bidIncentivePct, uint256 aucMinIncrementPct, uint256 aucIncDurationSecs, tuple(uint256 percentage, address to)[] fees)[] details, tuple(uint256 salt, uint256 deadline, uint256 amountToEth, uint256 amountToWeth, address user, bool canFail) shared, bytes32 r, bytes32 s, uint8 v)`],
    input.input
)[0]

Verify signed data

It is recommended to verify the signed data obtained above. And when making purchases of multiple orders, it would be necessary to iterate each orders and calculate the total payment value.

Submit the data to user's wallet

Submit the orderInput decoded to buyer's wallet to finish the purchase.

const marketContract = '0x74312363e45DCaBA76c59ec49a7Aa8A65a67EeD3' // X2Y2's market address on Mainnet. Use 0x1891EcD5F7b1E751151d857265D6e6D08ae8989e for Goerli testnet
const abi = [] // JSON format of X2Y2's market contract ABI, available at `https://etherscan.io/address/0x6d7812d41a08bc2a910b562d8b56411964a4ed88#code`
const market = new ethers.Contract(marketContract, abi, web3Provider)
const tx = await market.run(orderInput, { value: price_value }) // Pass 0 as value when accepting offer