BNB Chain Staking API

1 Stake

Staking BNB is supported on BNB Beacon Chain network ONLY.

Staking BNB through the InfStones Safe Stake SDK involves a straightforward process: users stake BNB and are provided with an activity ID. This activity ID can be utilized to access the raw transaction which users can then sign and broadcast to complete the stake process seamlessly.

1.1 Stake

Through stake API, users can stake a specified amount of BNB and get the activity ID.

Request

curl -X 'POST' \
  'https://stakingapi.infstones.com/bsc/stake' \
  -H 'x-api-key: <access_token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "wallet": "<your_bnb_wallet_address>",
  "amount": "<stake_amount>"
}'

📘

<access_token> should be asked from InfStones. For more details, please check https://docs.infstones.com/docs/get-started#1-create-access-token.

Response

{
  "data": "3c6d736d-c238-4a91-b077-720135f24429"
}

1.2 Get Raw Transaction

Open GraphQL Playground, users can retrieve the raw transaction associated with the activity ID from 1.1 Stake.

Step 1: Input your access token in the Headers section.

Step 2: Input the activity ID and run the command, you will get the response with raw_tx.

Request

{
  get_activity(id:"3c6d736d-c238-4a91-b077-720135f24429") {
    activity {
      wallet
      amount
      raw_tx
      activity_status
    }
  }
}

Response

{
  "data": {
    "get_activity": {
      "activity": [
        {
          "wallet": "bnb13yy3xqnt30t9pcesa58vugtcpv46pd25la2xu9",
          "amount": "1",
          "raw_tx": "{\"accountNumber\":0,\"chainId\":\"Binance-Chain-Tigris\",\"baseMsg\":{\"delegator_addr\":\"bnb13yy3xqnt30t9pcesa58vugtcpv46pd25la2xu9\",\"validator_addr\":\"bva1a7n56r44h4tlgrk3mazxx6cavhgpkrm7a8p6g4\",\"delegation\":{\"denom\":\"BNB\",\"amount\":100000000},\"side_chain_id\":\"bsc\"},\"type\":\"BscDelegateMsg\",\"sequence\":0,\"memo\":\"InfStones Delegate\"}",
          "activity_status": "completed"
        }
      ]
    }
  }
}

📘

The activity status completed means the transaction details are fully prepared and ready for the next step Sign Transaction and Broadcast.

1.3 Sign Transaction and Broadcast

Users can sign the raw transaction using their own wallet's private key and broadcast it to complete the staking process.

import * as sdk from "@binance-chain/javascript-sdk"
import axios from "axios"

let privateKey = '<your_bsc_wallet_private_key>';
let rawTxStr = '<raw_tx_from_infstones_api>'; //retrieved from activity

try {
  const api = "https://dex.binance.org/"
  const httpClient = axios.create({ baseURL: api })
  const client = new sdk.BncClient(api)
  client.chooseNetwork("mainnet")
  client.initChain()

  const rawTx = JSON.parse(rawTxStr);
  let baseMsg;
  if (rawTx.type === "BscDelegateMsg") {
    baseMsg = new sdk.types.BscDelegateMsg(rawTx.baseMsg);
  } else if (rawTx.type === "BscUndelegateMsg") {
    baseMsg = new sdk.types.BscUndelegateMsg(rawTx.baseMsg);
  } else {
    throw new Error("Unsupported transaction type: " + rawTx.type);
  }
  rawTx.baseMsg = baseMsg;

  await httpClient.get(`${api}api/v1/account/${rawTx.baseMsg.delegator_addr}`)
    .then((res) =>{
      rawTx.accountNumber = res.data.account_number;
      rawTx.sequence = res.data.sequence;
  })

  const tx = new sdk.Transaction(rawTx)
  const signedBz = tx.sign(privateKey).serialize()
  client.sendRawTransaction(signedBz, true)
    .then((result) => {
      console.log(`Transaction successfully sent with hash: ${result.result[0].hash}`)
  })
} catch (error) {
    console.log(error)
}

2 Unstake

Unstaking BNB through the InfStones Safe Stake SDK involves a straightforward process: users unstake BNB and are provided with an activity ID. This activity ID can be utilized to access the raw transaction which users can then sign and broadcast to complete the unstaking process seamlessly.

2.1 Unstake

Through unstake API, users can stake a specified amount of BNB and get the activity ID.

Request

curl -X 'POST' \
  'https://stakingapi.infstones.com/bsc/unstake' \
  -H 'x-api-key: <access_token>' \
  -H 'Content-Type: application/json' \
  -d '{
  "wallet": "<your_bnb_wallet_address>",
  "amount": "<unstake_amount>"
}'

Response

{
  "data": "53961ecf-9e65-4d9a-bfd5-12143ea645e6"
}

2.2 Get Raw Transaction

Open GraphQL Playground, users can retrieve the raw transaction associated with the activity ID from 2.1 Unstake.

Request

{
  get_activity(id:"484ed3a9-a9d0-4601-8b76-260cef48ea85") {
    activity {
      wallet
      amount
      raw_tx
      activity_status
    }
  }
}

Response

{
  "data": {
    "get_activity": {
      "activity": [
        {
          "wallet": "bnb12xmudrlgyeq9d3p3t2mlqrt0u8lmptnkvmfkdn",
          "amount": "1",
          "raw_tx": "{\"accountNumber\":0,\"chainId\":\"Binance-Chain-Tigris\",\"baseMsg\":{\"delegator_addr\":\"bnb12xmudrlgyeq9d3p3t2mlqrt0u8lmptnkvmfkdn\",\"validator_addr\":\"bva1a7n56r44h4tlgrk3mazxx6cavhgpkrm7a8p6g4\",\"amount\":{\"denom\":\"BNB\",\"amount\":100000000},\"side_chain_id\":\"bsc\"},\"type\":\"BscUndelegateMsg\",\"sequence\":0,\"memo\":\"InfStones Undelegate\"}",
          "activity_status": "completed"
        }
      ]
    }
  }
}

📘

The activity status completed means the transaction details are fully prepared and ready for the next step Sign Transaction and Broadcast.

2.3 Sign Transaction and Broadcast

Users can sign the raw transaction using their own wallet's private key and broadcast it to complete the unstaking process.

import * as sdk from "@binance-chain/javascript-sdk"
import axios from "axios"

let privateKey = '<your_bsc_wallet_private_key>';
let rawTxStr = '<raw_tx_from_infstones_api>'; //retrieved from activity

try {
  const api = "https://dex.binance.org/"
  const httpClient = axios.create({ baseURL: api })
  const client = new sdk.BncClient(api)
  client.chooseNetwork("mainnet")
  client.initChain()

  const rawTx = JSON.parse(rawTxStr);
  let baseMsg;
  if (rawTx.type === "BscDelegateMsg") {
    baseMsg = new sdk.types.BscDelegateMsg(rawTx.baseMsg);
  } else if (rawTx.type === "BscUndelegateMsg") {
    baseMsg = new sdk.types.BscUndelegateMsg(rawTx.baseMsg);
  } else {
    throw new Error("Unsupported transaction type: " + rawTx.type);
  }
  rawTx.baseMsg = baseMsg;

  await httpClient.get(`${api}api/v1/account/${rawTx.baseMsg.delegator_addr}`)
    .then((res) =>{
      rawTx.accountNumber = res.data.account_number;
      rawTx.sequence = res.data.sequence;
  })

  const tx = new sdk.Transaction(rawTx)
  const signedBz = tx.sign(privateKey).serialize()
  client.sendRawTransaction(signedBz, true)
    .then((result) => {
      console.log(`Transaction successfully sent with hash: ${result.result[0].hash}`)
  })
} catch (error) {
    console.log(error)
}