Wallet API
Inscribe transfer function

Inscribe transfer function#

1. Obtain the information required for signing#

Call the POST /api/v5/waas/transaction/get-sign-info API to query some data required for the signature, including the fee rate and other information required by the BTC exchange.

//Define your parameters
const postBody = {
  addrFrom: "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",
  addrTo: "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",
  txAmount: "0", //fill in 0 here
  chainId: "0",
  extJson: {
  }
};

//Define auxiliary functions
const sendData = async () => {
  const apiRequestUrl = getRequestUrl(
      apiBaseUrl,
      '/api/v5/waas/transaction/get-sign-info'
  );
  return fetch(apiRequestUrl, {
    method: 'POST',
    headers: headersParams,
    body: JSON.stringify(postBody),
  })
  .then((res) => res.json())
  .then((res) => {
    return res;
  });
};

const { data: sendData } = await sendData();

After calling the API, you will receive the following response:

{
  "code": 0,
  "msg": "",
  "data": [{
    "normalFeeRate": 118, // Medium
    "maxFeeRate": 135, // highest gear
    "minFeeRate": 102, // low gear
    "inscriptionOutput": 546, // inscribed output
    "minOutput": 1500, //minimum required output
    "normalCost": "1800", // Cost per transaction
    "maxCost": "3600", // Maximum cost per transaction
    "minCost": "600", // Minimum cost per transaction
  }]
}

2. Get the available UTXO#

You can call the POST /api/v5/waas/transaction/get-utxo API to get a list of available UTXOs.

Fill in the instructions for some parameters:
1. coinAmount = inscriptionOutput * inscribe_nums > minOutput ? inscriptionOutput * inscribe_nums : minOutput.
2. serviceCharge = normalCost * inscribe_nums.
inscribe_nums = commitTX + revealTxs,inscribe_nums 1 + 1 = 2 in the transfer scenario.
//Define your parameters
const postBody = {
  chainId: 0,
  utxoRequests: [{
    address: "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",
    coinAmount: 1500, // How to fill in the number of output * mint transactions. If deploy = output * 2 (commit, reveal)
    serviceCharge: 3600, // normalCost * mint transactions
    utxoType: 11
  }]
};

//Define auxiliary functions
const sendData = async () => {
  const apiRequestUrl = getRequestUrl(
      apiBaseUrl,
      '/api/v5/waas/transaction/get-utxo'
  );
  return fetch(apiRequestUrl, {
    method: 'POST',
    headers: headersParams,
    body: JSON.stringify(postBody),
  })
  .then((res) => res.json())
  .then((res) => {
    return res;
  });
};

const { data: sendData } = await sendData();

After calling the API, you will receive the following response:

{
  "code": 0,
  "msg": "",
  "data": [
  {
    "address": "bc1qjlgnuyaugzfhnnjjnhrjc2333vfk4rv8h4f05d",
    "canTransferAmount": "27627",
    "utxoType": 11,
    "utxoList": [
      {
        "txHash": "a33ea20a5d9c1106e577f920816b4349fcda004f5c74e5896c47779e95220437",
        "vout": 0,
        "coinAmount": 4743,
        "status": 1
      }
    ]
  }
]
}

3. Generate inscribed signature info#

Call the SDK, perform the transfer engraving operation, and generate signedTx (commitTx + revealTxs [])

Fill in the instructions for some parameters:
1. In the commitTxPrevOutputList of the SDK, txId in each item corresponds to the txHash of each UTXO in the list returned by get-utxo.
2. In the InscriptionRequest of the SDK, commitFeeRate and revealFeeRate fill in the normalFeeRate returned by the get-sign-info API by default. revealOutValue fills in the inscriptionOutput returned by the get-sign-info API by default.
3. ChangeAddress is the change address (usually the address of the initiator).
4. revealAddr is the address engraved on the inscription (usually the address of the initiator).
import { inscribe, networks, PrevOutput, InscriptionData, InscriptionRequest } from '@okxweb3/coin-bitcoin';

//Construct the unspent input list used for inscription. Unspent can be queried through the backend api.
const commitTxPrevOutputList: PrevOutput[] = [];
commitTxPrevOutputList.push({
  txId: "a33ea20a5d9c1106e577f920816b4349fcda004f5c74e5896c47779e95220437",//unspent transaction ID
  vOut: 0,// unspent output index
  amount: 4743,// Unspent balance
  address: "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",//unspent address
  privateKey: "***sXSA1tzG9Hyc2S2ZQLsbR3o8pvzQyz9DYewrRQDcFC2vhcwGT***",//unspent WIF encoded private key
});

//Construct the data that needs to be engraved, and batch engraving only needs to loop the construction list.
const inscriptionDataList: InscriptionData[] = [];
inscriptionDataList.push({
  contentType: "text/plain; charset = utf-8",//inscription type, represented by MIME type
  body: `{"p": "brc-20", "op": "transfer", "tick": "tokb", "amt": "1 "}`,// inscription content, utf-8 string or binary buffer
  revealAddr: "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",//Inscription inscription (binding) address
});

//Custom information, including rate, change address, inscription output amount
const request: InscriptionRequest = {
  commitTxPrevOutputList,
  commitFeeRate: 118, // Please use the current network rate to avoid pending transactions
  revealFeeRate: 118,
  revealOutValue: 546,
  inscriptionDataList,
  changeAddress: "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",
};

//Return the list of transactions to be broadcast, which can be broadcast through the backend API
const txs = inscribe(networks.testnet, request);

//txs structure:
{
  commitTx: '02000000000104a5093589e4b99dab2789d5f45d19e139f30ad8ed422ed068162cb0d291b4cd3604000000171600145c005c5532ce810ddf20f9d1d939631b47089ecdfdffffff2a4127c4ad36d98bf6ed209c88247d41c84997f0aa553e2d7d4251d12c59793d0000000000fdffffff8dbe51b56c29000af450fd949363c3a2062a04941a19d9bf5db9d8bf8a76f583000000006b483045022100c19038816660c17f8eaa07682f3d4a1238b2bec6d6fe50601644110ee4eb90be02201c7b7c09b5f47dc762f69a558f1b6d73a7cf915b72b4792ee6cf1033c91af8fd01210357bbb2d4a9cb8a2357633f201b9c518c2795ded682b7913c6beef3fe23bd6d2ffdffffff417cf5848813e9612ef70de69f5a62d5694b60d1b20ef2f6497508fc2bf983850000000000fdffffff0550030000000000002251206ff0ac47ccff79fc3eaab0cd0047c28dead95cd35c6c695dfe33010b8807d16c3c03000000000000225120845a93ad3f2f36750672201709a48e6ad458cc0a42455f0786cf3bbbe42a6d183803000000000000225120be60aa4826e2e3a3245158c0e7b36543ed7ead2ed40a541c4583b80d4b3762003803000000000000225120e7ff49e9dee3ddaf3a811f12954a9c66cc98bf01c4eccb1ec093acf04ee2d1ffba6d110000000000225120b7ee7f83a6a7fdb513040856c56778aa3abea9a451e0c9bb012f22a77ed99b2102483045022100f5604773d87ce23082bf44ef299519b7cab37414a4c2973e20baa9d4ac140ab402202c2319337194675b2a7de9ad4f1ca2d2ecb1ccc34dd0ec9647bc58431ac414b401210357bbb2d4a9cb8a2357633f201b9c518c2795ded682b7913c6beef3fe23bd6d2f024830450221009d0e54b2d6818768e36c88f913c4b257d7a2fec5c9612335aa21d5cf5a933db80220404b227c68effd2e5e1c6c79cef6df5fd7c0da25afcef230ae9eec3c98da861e01210357bbb2d4a9cb8a2357633f201b9c518c2795ded682b7913c6beef3fe23bd6d2f00014016fc622bacdaeb38aae055900b226597ae4149f32f1349f5f9d2b8f642b2c1a9e171c0c228f66eef28be0bcad551ed31b2c48a9aa6380b7dcaba97e72cede86500000000',
  revealTxs: [
    '0200000000010195cf7a2aacbb6604682f9462c6ff54377adbc8842e19e29037079a09ff70c8630000000000fdffffff012202000000000000225120b7ee7f83a6a7fdb513040856c56778aa3abea9a451e0c9bb012f22a77ed99b2103405c2655b788f453c1fdba7cab2fe1be693ff7fa8b12451ceab5b4b3e8d7981713f0ad9cc0f6d547d89a77a8b8fdcd6c284b8a6d82ae2093f65873581563edd1c57a2057bbb2d4a9cb8a2357633f201b9c518c2795ded682b7913c6beef3fe23bd6d2fac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800347b2270223a226272632d3230222c226f70223a226d696e74222c227469636b223a2278637662222c22616d74223a22313030227d6821c057bbb2d4a9cb8a2357633f201b9c518c2795ded682b7913c6beef3fe23bd6d2f00000000'
  ],
  commitTxFee: 1180,
  revealTxFees: [ 302],
  commitAddrs: ['tb1pdlc2c37vlaulc042krxsq37z3h4djhxnt3kxjh07xvqshzq869kqz5sgrc']
}

4. Generate txHash#

Call the SDK to generate the corresponding txHash for all signedTx (commit + revealTxs [])

Fill in the instructions for some parameters:
1. The data in the calcTxHashParams of the SDK corresponds to the generated signedTx, and the generated txId is txHash.
import { inscribe, networks, PrevOutput, InscriptionData, InscriptionRequest } from '@okxweb3/coin-bitcoin';

let calcTxHashParams = {
  data: "02000000000101110d0b153f3060700c20e3bf704b5a97d52012c8256963c543b41239ccbb6bac0000000000ffffffff02502d190000000000225120b7ee7f83a6a7fdb513040856c56778aa3abea9a451e0c9bb012f22a77ed99b216419000000000000225120b7ee7f83a6a7fdb513040856c56778aa3abea9a451e0c9bb012f22a77ed99b2101406137f12926ae863937630db5719445a45d868a14198431f3c5eb16df0c45382ee30f5bc083e7b9228728376a860585173b12ddd3b56b07502fd7479c4dd2d77c00000000"
};
let txId = await new BtcWallet().calcTxHash(calcTxHashParams);
console.info(txId);

5. Bath transaction to inscribe transfer inscription#

Through the batch transaction API to achieve engraved transfer, call POST /api/v5/waas/transaction/send-transaction-batch API to achieve the engraved transfer function of BRC-20 tokens.

broadcast_from_to_cn

Fill in the instructions for some parameters:
1. For the serviceCharge in the transaction, get the commitTxFee and revealTxFees [index] in the return value of the inscribe method of the SDK, respectively.
2. Under commitTx, use the address from step 3’s commitAddr for the addrTo field.
    Under revealTxs, use the address from step 3’s commitAddr for the addrFrom field.
//Define your parameters
const postBody = {
  "txList": [
    {
      //commitTx
      "signedTx": "0200000000010195614884b5657bbe721450c41cf684d8e90211bb1fedee3c41111c49cdc359f30000000000fdffffff02bc47000000000000225120a321ae29298f753883a789b703353f00488d54aa9b25dab581cdda92e8e5d655e8f700000000000022512084c74a9f1f1453548b52d866ea64029298adf21679d4e09d2c053b8886995e740140c4308e98ad18155e0a044e083a0e02d7ca4aeae0fdd2c487ff53406f91843fad228e1ddb7e7fee2bcb550bfbd14a479ab937aba1dea8fdefc3ceb56c53c0a27800000000",
      "walletId": "13886e05-1265-4b79-8ac3-b7ab46211004",
      "addrFrom": "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",
      "addrTo": "bc1p5vs6u2ff3a6n3qa83xmsxdflqpyg6492nvja4dvpehdf96896e2shx5a0w", // commitAddrs
      "txHash": "5c710aeac5567439926b80b6e6e4ccc503abb3db6478cb4bb004d53cfc2cd5e0",
      "txAmount": 0,
      "chainId": 0,
      "txType": "BRC20_INSCRIBE",
      "serviceCharge": 18172,
      "tokenAddress": "btc-brc20-tokb",
      "extJson": {
        "broadcastType": 1,
        "dependTx": [],
        "feeRate": "118",
        "itemId": "commitTx"
      }
    },
    {
      //revealTx
      "signedTx": "02000000000101e0d52cfc3cd504b04bcb7864dbb3ab03c5cce4e6b6806b92397456c5ea0a715c0000000000fdffffff01220200000000000022512084c74a9f1f1453548b52d866ea64029298adf21679d4e09d2c053b8886995e740340f3e8d15354fb0370522d046f85c981625c4230ed14ddc6e092dea9359ee4733a58713795eb014731ae919c49af4aab4257c53576c8741a5d853cb9765810d42e7c20da459477b18d59be71a3ab0c0565abf3ca79b3e34f4ea66accdfa749c900f163ac0063036f7264010118746578742f706c61696e3b636861727365743d7574662d3800367b2270223a226272632d3230222c226f70223a227472616e73666572222c227469636b223a22746f6b62222c22616d74223a2231227d6821c0da459477b18d59be71a3ab0c0565abf3ca79b3e34f4ea66accdfa749c900f16300000000",
      "walletId": "13886e05-1265-4b79-8ac3-b7ab46211004",
      "addrFrom": "bc1p5vs6u2ff3a6n3qa83xmsxdflqpyg6492nvja4dvpehdf96896e2shx5a0w", // commitAddrs
      "addrTo": "bc1psnr548clz3f4fz6jmpnw5eqzj2v2musk082wp8fvq5ac3p5ete6qg05u8u",
      "txHash": "a1a41ccfe62ba715a085059ee455f164f649b5321ebb361ef6a4d65b24b047a0",
      "txAmount": 0,
      "chainId": 0,
      "txType": "BRC20_INSCRIBE",
      "serviceCharge": 17818,
      "tokenAddress": "btc-brc20-tokb",
      "extJson": {
        "broadcastType": 1,
        "dependTx": [
          "5c710aeac5567439926b80b6e6e4ccc503abb3db6478cb4bb004d53cfc2cd5e0"
        ],
        "feeRate": "118",
        "itemId": "revealTx0"
      }
    }
  ]
};

//Define auxiliary functions
const sendTransactionData = async () => {
  const apiRequestUrl = getRequestUrl(
      apiBaseUrl,
      '/api/v5/waas/transaction/send-transaction-batch'
  );
  return fetch(apiRequestUrl, {
    method: 'POST',
    headers: headersParams,
    body: JSON.stringify(postBody),
  })
  .then((res) => res.json())
  .then((res) => {
    return res;
  });
};

const { data: sendTransactionData } = await sendTransactionData();

After calling the API, you will receive the following response:

{
  "code": 0,
  "msg": "success",
  "data": [{
    "txHashList": [
      {
        "itemId": "commitTx",
        "txHash": "5c710aeac5567439926b80b6e6e4ccc503abb3db6478cb4bb004d53cfc2cd5e0"
      },
      {
        "itemId": "revealTx0",
        "txHash": "a1a41ccfe62ba715a085059ee455f164f649b5321ebb361ef6a4d65b24b047a0"
      }
    ]
  }]
}

6. Wait for push notifications or query transaction history#

Wait for the push notification, or take the initiative to check the transaction history (these two steps are already mentioned in the wallet quick start, not here). After the transaction is successful, proceed to the next step of transferring tokens.