市场 API

快速开始#

在本指南中,我们将向你展示如何在 OKX 上创建铭文挂单。注意:降价时同样可以执行以下步骤。此过程包括:

  • 获取有效铭文
  • 获取 UTXO 信息
  • 获取 PSBT
  • 挂单铭文

1. 获取有效铭文#

首先,你需要获取钱包地址下可用的铭文资产,以便在 OKX 平台上挂单。详情见获取铭文资产列表

步骤 1:构建请求

const apiBaseUrl = 'https://www.okx.com';
const requestUrl = '/api/v5/mktplace/nft/ordinals/get-valid-inscriptions'; //
const originalMessage = JSON.stringify({
    slug: 'BTC Ordinals',
    walletAddress: 'bc1qek7lal8ghnmus3dw897atlpgvmv27zgdgqfawx',
    limit: '10',
    isBrc20: false // true for brc20 tokens.  Note: Default value is true if not included
});

const apiRequestUrl = getRequestUrl(apiBaseUrl, requestUrl);

const headersParams = {
  'Content-Type': 'application/json',
  'OK-ACCESS-KEY': apiKey,
  'OK-ACCESS-SIGN': cryptoJS.enc.Base64.stringify(
    cryptoJS.HmacSHA256(timestamp + 'POST' + '/api/v5/mktplace/nft/ordinals/get-valid-inscriptions' + originalMessage , secretKey)
  ),
  'OK-ACCESS-TIMESTAMP': timestamp,
  'OK-ACCESS-PASSPHRASE': passphrase,
};

步骤 2:发出请求

return fetch(apiRequestUrl, {method: 'post', headers: headersParams, body: originalMessage})
    .then((res) => {
        console.log(res)
        return res.json()
    }
).then((res) => {
    console.log(JSON.stringify(res))
    return res;
});
{
    "code": 0,
    "data": {
        "cursor": "MTY5MDQ1NzA3NTE2OToxNjgwMzUxMDg4MDMwO...3Z3Z2xjbWo4dGg0cXF6amVxbGtweXVxbGp3Y3AyMDh4MzM3ZmV4cDI2cDBteHA4cXQ4MjB4ZA==",
        "inscriptionInfos": [
            {
                "amount": "1",
                "inscriptionId": "08ea6e794016c8f11ddb262...15ba29a6f54d3c75fd184a9i0",
                "nftId": "1651480...458",
                "ticker": "",
                "tickerId": ""
            },
            {
                "amount": "1",
                "inscriptionId": "0c3020593597d1f58188...23cbcb91d9b7fb8e3e0cefa0b2dfi0",
                "nftId": "2105134...741490",
                "ticker": "",
                "tickerId": ""
            }
        ]
    },
    "msg": ""
}

2. 获取 UTXO 信息#

为了获取交易的 PSBT,需要获取铭文的 UTXO。因此,你将需要 inscriptionId 来查询 UTXO。详情见查询 UTXO

步骤 1:构建新的请求参数去获取 UTXO 信息

const apiBaseUrl = 'https://www.okx.com';
const requestUrl = '/api/v5/waas/transaction/get-utxo'; // this endpoint is to get UTXO information of the inscription
const utxoRequestBody = JSON.stringify({
    chainId:0,
    utxoRequests: [
        {
            address: "bc1p9npkvwglc...ljwcp208x337fexp26p0mxp8qt820xd", // the wallet that holds the inscription
            coinAmount: 0,  // use default value 0
            serviceCharge: 0, // use default value 0
            utxoType: 2, // default type 2 for utxo inscription check
            nftId: "1803359c...cfeb15d1aa6ade40i0" // this is inscription ID
        }
    ]
})
const apiRequestUrl = getRequestUrl(apiBaseUrl, requestUrl);

const headersParams = {
  'Content-Type': 'application/json',
  'OK-ACCESS-KEY': apiKey,
  'OK-ACCESS-SIGN': cryptoJS.enc.Base64.stringify(
    cryptoJS.HmacSHA256(timestamp + 'POST' + requestUrl + utxoRequestBody , secretKey)
  ),
  'OK-ACCESS-TIMESTAMP': timestamp,
  'OK-ACCESS-PASSPHRASE': passphrase,
};

步骤 2:获取 UTXO 信息

return fetch(apiRequestUrl, {method: 'post', headers: headersParams, body: utxoRequestBody})
    .then((res) => {
        console.log(res)
        return res.json()
    }
).then((res) => {
    console.log(JSON.stringify(res))
    return res;
});

请求返回

{
    "address": "bc1pt5w9...2m8cky8qxjn3rg",
    "canTransferAmount": "0",
    "utxoType": 2,
    "totalUtxoNum": 0,
    "utxoList": [
        {
            "txHash": "02ecbe7ef51...87a7c6e64409883f66f4e3036",
            "vout": 0,
            "coinAmount": 546,
            "confirmations": 3548,
            "utxoType": 2,
            "status": 1,
            "utxoBizStatus": 0,
            "hasNft": true,
            "hasCheckNftExist": true,
            "nftLocaltionVOs": [
                {
                    "nftId": "02ecbe7ef5143...3f3de87a7c6e64409883f66f4e3036i0",  // this is inscriptionId
                    "nftLocation": "02ecbe7ef51431...fccb83f3de87a7c6e64409883f66f4e3036:0:0",
                    "nftType": 2,
                    "brc20Nft": true
                }
            ],
            "key": "02ecbe7ef514311523d0e7f...3f3de87a7c6e64409883f66f4e3036-0",
            "dummy": false,
            "spending": false
        }
    ]
}

3. 签署 PSBT#

接下来,我们需要设置挂单价,得到挂单的 PSBT。详情见钱包 SDK

注意
降价时如果定价不低于当前价格,会返回错误。当挂单价格低于 0.00001 BTC 时也会返回错误。

步骤 1:构建 PSBT 数据

import { generateSignedListingPsbt, networks } from '@okxweb3/coin-bitcoin';
const listingData = {
  nftAddress: 'bc1pt5w9...2m8cky8qxjn3rg',
  nftUtxo: {
    txHash: '02ecbe7ef51...87a7c6e64409883f66f4e3036',
    vout: 0,
    coinAmount: 546,
    rawTransaction: undefined
  },
  receiveBtcAddress: 'bc1p9npkvwglcmj8th4q...cp208x337fexp26p0mxp8qt820xd', // your btc wallet address
  price: 10000 // total price in Satoshi unit
}

步骤 2: 使用 OKT 钱包 SDK 获取 PSBT

const psbt = generateSignedListingPsbt(listingData, privateKey, networks.bitcoin);
console.log(psbt);

4. 挂单铭文#

通过 PSBT 签名和挂单信息来请求在 OKX 挂单。

步骤1:构建挂单请求

注意
unitPrice 对于 BRC-20 或 BTC NFT 会有所不同。对于 BTC NFT,它将与 totalPrice 相同,因为 BTC NFT 的数量为 1,但对于 BRC-20,unitPrice 将是 totalPrice 除以铭文的数量。例如,totalPrice为 10000 聪,铭文数量为 50,则 unitPrice 将为 10000/50=200。
const apiBaseUrl = 'https://www.okx.com';
const requestUrl = '/api/v5/mktplace/nft/ordinals/okx/make-orders'; // this endpoint is to get UTXO information of the inscription
const submitListingBody = JSON.stringify({
    brc20: false,
    items: [
      {
        inscriptionId: inscriptionId,
        nftId: nftId, // get this data part 1 step 2 by mapping it to inscriptionId
        orderType: 2,
        totalPrice: 100000,  // this price should match part 3 step 1
        unitPrice: 100000, // this price will varies for BRC20 or btc NFT.  Check out Note on unitPrice
        psbt: 'cHNidP8BAP0GAQIAAAADAAAAAAAAAAAAAAAAAAAAAA...GJSkBSJuPsgb2gGCekW+W04EtAAAAAA=',
      }
    ],
    tickerId: ''
  })
const apiRequestUrl = getRequestUrl(apiBaseUrl, requestUrl);

const headersParams = {
  'Content-Type': 'application/json',
  'OK-ACCESS-KEY': apiKey,
  'OK-ACCESS-SIGN': cryptoJS.enc.Base64.stringify(
    cryptoJS.HmacSHA256(timestamp + 'POST' + requestUrl + submitListingBody , secretKey)
  ),
  'OK-ACCESS-TIMESTAMP': timestamp,
  'OK-ACCESS-PASSPHRASE': passphrase,
};

步骤 2:发出请求

return fetch(apiRequestUrl, {method: 'post', headers: headersParams, body: submitListingBody})
    .then((res) => {
        console.log(res)
        return res.json()
    }
).then((res) => {
    console.log(JSON.stringify(res))
    return res;
});

最后成功挂单

{
    "result": [
        {
            "nftID": 20824...473273458,  // should be the same nftId as part 4 step 1
            "errorMsg": "",
            "success": true,
        }
    ]
}