Cross-chain assets swap

After the full node has synchronized all the blocks with the whole network, we can use the command line tool (hbtccli) to interact with the HBTC Chain and can run the following command to check all the commands.

hbtccli help

We provide a brief introduction to the processes of cross-chain asset depositing, transferring and withdrawing coins/tokens. We will use two users ALICE and BOB as an example to show the full interactive processes. (Assuming our binary runs in the local node directory, ALICE and BOB are two different users).

1. Create and manage HBTC chain custodian units

hbtccli keys add alice --home node/hbtccli

With above command we created an ALICE custodian unit in the specified node directory (you need to enter your password to encrypt the custodian unit key during the creation process), and it was successfully created as follows:

{
  "name": "alice",
  "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
  "pubkey": "hbtpub1addwnpepqdp4ac6l2f7tjtwssvdra7fy2xfrl8th4ltcx4dgy60z2a7ffx5lcu2ja8v",
}

In this way, ALICE created an account for herself offline, and likewise, BOB creates an account following the same operation:

{
  "name": "bob",
  "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
  "pubkey": "hbtpub1addwnpepqddz9uqphxcphs50j9ewqeccymkl66n2nqkes3h4w8xrna39qxm0jwk688c",
}

After the completion of this operation, you need to apply for a certain hbc from HBTC Chain for testing, please refer to the FAQ for details.

2. Create cross-chain custodian addresses for Ethereum

First of all, create a btc cross-chain custodian address for the ALICE custodian unit with the following command:

hbtccli tx keygen keygen alice btc HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q  --chain-id hbtc-testnet --home node/hbtccli

With above command we created a cross-chain custodian address of btc for ALICE, and after waiting for 2-3 blocks after the command is sent and executed (it takes a certain amount of time for distributed key generation), we can get the cross-chain custodian address of btc by querying the corresponding custodian information with the following command:

hbtccli query cu cuinfo HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q --chain-id hbtc-testnet --home node/hbtccli

The response to the query should be as follows:

{
  "type": "hbtcchain/CustodianUnit",
  "value": {
    "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
    "assets": [
      {
        "denom": "btc",
        "address": "12ayvTjKBxZnScRoEZ67seVySsqD5JboaA",
        "enable_sendtx": true
      }
    ],
  }
}

According to the response, we have created a cross-chain custodian address of btc for the custodian unit (mtZQH3wjTGu8FgPrS7B6gicay4kcPVa1wa), and the private key to this address is kept privately by each validator nodes holding one slice of private key shares. With the same processes, bob creates a cross-chain custodian address of eth/USDT for himself with the following data query:

{
  "type": "hbtcchain/CustodianUnit",
  "value": {
    "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
    "assets": [
      {
        "denom": "usdt",
        "address": "0xB48fEe1Df0Ae85415Eb286E66b262DA70BD94693",
        "nonce": "0",
        "enable_sendtx": true
      },
      {
        "denom": "eth",
        "address": "0xB48fEe1Df0Ae85415Eb286E66b262DA70BD94693",
        "nonce": "0",
        "enable_sendtx": true
      }
    ],
  }
}

3. Deposit cross-chain assets

Through the above steps, ALICE and BOB have created btc/eth related cross-chain addresses respectively, ALICE can deposit a certain amount of btc into her own cross-chain btc address (12ayvTjKBxZnScRoEZ67seVySsqD5JboaA) through her own wallet, BOB can deposit a certain amount of eth and btc into his own eth cross-chain address (0xB48fEe1Df0Ae85415Eb286E66b262DA70BD94693), after the deposit is complete, HBTC Chain will automatically scan the cross-chain address of the custodian unit and credit the corresponding deposit to the corresponding custodian unit of HBTC Chain. For example, BOB deposits 0.5eth and 100tusdt and the query for the corresponding custodian unit is as follows:

{
  "type": "hbtcchain/CustodianUnit",
  "value": {
    "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
    "coins": [
      {
        "denom": "eth",
        "amount": "500000000000000000"
      },
      {
        "denom": "usdt",
        "amount": "100000000"
      }
    ],
    "assets": [
      {
        "denom": "tusdt",
        "address": "0xB48fEe1Df0Ae85415Eb286E66b262DA70BD94693",
        "nonce": "0",
        "enable_sendtx": true
      },
      {
        "denom": "eth",
        "address": "0xB48fEe1Df0Ae85415Eb286E66b262DA70BD94693",
        "nonce": "2",
        "enable_sendtx": true
      }
    ],
}

It can be clearly seen that BOB’s custodian unit already has the corresponding assets, and in the same way, ALICE’s account will also have the corresponding btc assets to be deposited.

4. Cross-chain assets swap

ALICE and BOB have respectively deposited the assets of the corresponding btc and eth/USDT, after the deposit is completed (the assets on the HBTC Chain can be queried and verified), ALICE decided to conduct an asset exchange with BOB, using ALICE’s btc for BOB’s USDT, the asset exchange process is as follows:

1. Build swap transaction

ALICE and BOB can each build the same exchange transaction locally, and ALICE does so with an exchange of 0.05btc to 100 USDT from BOB, with the following transaction construction command:

hbtccli tx transfer multisend HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q 5000000btc HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH 100000000usdt 0 --output-document swapmsg.json --home node/hbtccli

After the command is executed, a file of swapmsg.json is generated in the current execution directory with the corresponding transaction content.

2.Transaction signature

After the transaction is constructed, ALICE and BOB each sign the transaction with the following command:

hbtccli tx sign swapmsg.json  --from HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q --home node/hbtccli --output-document alicesign.json

The signed transaction is stored in the alicesign.json, which exists in the current directory only, with the following contents:

{
  "type": "hbtcchain/StdTx",
  "value": {
    "msg": [
      {
        "type": "hbtcchain/transfer/MsgMultiSend",
        "value": {
          "inputs": [
            {
              "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
              "coins": [
                {
                  "denom": "btc",
                  "amount": "5000000"
                }
              ]
            },
            {
              "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
              "coins": [
                {
                  "denom": "usdt",
                  "amount": "100000000"
                }
              ]
            }
          ],
          "outputs": [
            {
              "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
              "coins": [
                {
                  "denom": "usdt",
                  "amount": "100000000"
                }
              ]
            },
            {
              "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
              "coins": [
                {
                  "denom": "btc",
                  "amount": "5000000"
                }
              ]
            }
          ],
          "max_height": "0"
        }
      }
    ],
    "fee": {
      "amount": [],
      "gas": "200000"
    },
    "signatures": [
      {
        "pub_key": {
          "type": "tendermint/PubKeySecp256k1",
          "value": "A0Ne419SfLkt0IMaPvkkUZI/nXev14NVqCaeJXfJSan8"
        },
        "signature": "jVd+LPWQLSUhKYCo2Rsn24NizEv5GwDafvtFZM5nuNEPZelRCJ5CbjeIv4DtsUL5HRd5Iy3GMrnjJhmSKMM5zA=="
      }
    ],
    "memo": ""
  }
}

Similarly, bob sign the transaction by a command as follows:

hbtccli tx sign exchangemsg.json  --from HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH --home node/hbtccli --output-document bobsign.json

After bob and alice sign separately, bob sends the signature information to alice, alice assembles bob’s signature information into the transaction, put into the transaction signatures, once the transaction is assembled it should be as follows:

{
  "type": "hbtcchain/StdTx",
  "value": {
    "msg": [
      {
        "type": "hbtcchain/transfer/MsgMultiSend",
        "value": {
          "inputs": [
            {
              "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
              "coins": [
                {
                  "denom": "btc",
                  "amount": "5000000"
                }
              ]
            },
            {
              "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
              "coins": [
                {
                  "denom": "usdt",
                  "amount": "100000000"
                }
              ]
            }
          ],
          "outputs": [
            {
              "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
              "coins": [
                {
                  "denom": "usdt",
                  "amount": "100000000"
                }
              ]
            },
            {
              "address": "HBCcUCWJNattE5PYm6xQKbaCoRCG2SaVdHvH",
              "coins": [
                {
                  "denom": "btc",
                  "amount": "5000000"
                }
              ]
            }
          ],
          "max_height": "0"
        }
      }
    ],
    "fee": {
      "amount": [],
      "gas": "200000"
    },
    "signatures": [
      {
        "pub_key": {
          "type": "tendermint/PubKeySecp256k1",
          "value": "A0Ne419SfLkt0IMaPvkkUZI/nXev14NVqCaeJXfJSan8"
        },
        "signature": "jVd+LPWQLSUhKYCo2Rsn24NizEv5GwDafvtFZM5nuNEPZelRCJ5CbjeIv4DtsUL5HRd5Iy3GMrnjJhmSKMM5zA=="
      },
      {
        "pub_key": {
          "type": "tendermint/PubKeySecp256k1",
          "value": "A1oi8AG5sBvCj5Fy4GcYJu39amqYLZhG9XHMOfYlAbb5"
        },
        "signature": "/W9RJqOvx3RImGFzq7p02bw1oUW9S2L61tOCoF9TwwFAlHAKChf2HIwrwZjLUf3d81t6T4pR0L7ph4PiS2yKXw=="
      }
    ],
    "memo": ""
  }
}

After the transaction is assembled, it is saved as swaptx.json.

3. Broadcast transaction

hbtccli tx broadcast swaptx.json

With this command we completed the transaction between ALICE and BOB, ALICE’s 0.05btc was exchanged to BOB’s 100USDT, after the transaction was completed you can view the assets of the corresponding custodian unit, for example ALICE’s assets are shown as:

{
  "type": "hbtcchain/CustodianUnit",
  "value": {
    "address": "HBCPNuLKXuQSVgGe4kfF3JxJZb1Uytkajb3Q",
    "coins": [
      {
        "denom": "usdt",
        "amount": "100000000"
      }
    ],
}

As you can see, ALICE successfully swapped to 100 USDT, thus we completed the asset swap.

4. Transaction cancellation

There is a situation where ALICE sends the signed information to BOB after the signature is completed, but for some reasons ALICE does not want to continue the transaction, and if BOB does not send the signed assembled transaction to the chain, ALICE can actively send the following transaction to cancel the transaction with the following command:

hbtccli tx transfer cancel-uncommitted-tx alice --home node/hbtccli

In addition, we can specify a maximum transaction height (the fifth parameter of the transaction) when assembling the swap transaction, and if the transaction is sent to the chain after the height is exceeded, the transaction will not be settled (if 0, the transaction is not limited to the height).

5. Withdraw cross-chain assets

After the transaction between ALICE and BOB is completed, if ALICE wants to claim the 100 USDT obtained by the transaction into her own wallet, she can execute the following command:

hbtccli tx transfer withdrawal alice 0xC933C741416151dAcFE9428d39222f747e2b45EB 100000000usdt 21000000000000000 --chain-id hbtc-testnet --home node/hbtccli

With above command, ALICE withdraws the 100 USDT that was obtained through the transaction to her own eth account (0xC933C741416151dAcFE9428d39222f747e2b45EB non-custodian address), and after a certain block height consensus, the user can see the withdrawal received information in her wallet account. (Note: ALICE will need to deposit or buy from others to get a certain amount of eth as the gas fee if she wants to withdraw USDT).