API Signature and Broadcast Flow

Introduction

The TRON network has a variety of API calls to allow users to interact with the blockchain. These API calls can be found in the API reference page, which lists API calls from Full Node, Solidity Node, and TronWeb. While some of the API calls serve as stand-alone requests to get individual pieces of information, there are also many API calls which modify the user TRX wallet, resulting in a need to sign and broadcast the transaction. This guide walks the user through a TRX Balance staking example to show the API signature and broadcast process flow.

Stake Balance Example

One of the most commonly used APIs is the /wallet/freezebalance call. This API call stakes the TRX balance within a user-specified wallet address, and provides either bandwidth OR energy and TRON Power (voting rights) to the wallet owner. This API call takes in the four input parameters of owner_address, frozen_balance, frozen_duration, and resource:

  • owner_address is the TRX wallet's address in string format.
  • frozen_balance is the amount of TRX staked in denominations of Sun, in integer format.
  • frozen_duration is the duration in days to be staked, in integer format.
  • resource is the type of resource staking for. This can be either ENERGY or BANDWIDTH, in string format.
  1. Make a Transaction: stake balance by making the API call to get the JSON data:

Below is the sample JSON output. It lists the various attributes associated with the stake balance transaction. This JSON output will be used to sign the transaction.

{  
   "transaction":{  
      "txID":"454f156bf1256587ff6ccdbc56e64ad0c51e4f8efea5490dcbc720ee606bc7b8",
      "raw_data":{  
         "contract":[  
            {  
               "parameter":{  
                  "value":{  
                     "amount":1000,
                     "owner_address":"41e552f6487585c2b58bc2c9bb4492bc1f17132cd0",
                     "to_address":"41d1e7a6bc354106cb410e65ff8b181c600ff14292"
                  },
                  "type_url":"type.googleapis.com/protocol.TransferContract"
               },
               "type":"TransferContract"
            }
         ],
         "ref_block_bytes":"267e",
         "ref_block_hash":"9a447d222e8de9f2",
         "expiration":1530893064000,
         "timestamp":1530893006233
      }
   },
   "privateKey":"your private key"
}
  1. Sign the Transaction: The /wallet/gettransactionsign API call takes in two parameters. One is the transaction parameters, which takes in the JSON output from the previous step. The other is the privateKey parameter, which requires the private key associated with the staked TRX address to sign the transaction.

Below is the sample JSON output, with the signature ID:

{  
   "signature":[  
      "8e6582cead9ef92d7731e356b0131dca2dfe18d701bdaecb5591781af5493391127d10b4864cf45a0f56b10ed97af102864cff8205e14c8bf29b0e50d85f681801"
   ],
   "txID":"ddcbaf061eaa2454975ae8faefbeb0b410329ef9e5bb43b64d4065a7d66720c7",
   "raw_data":{  
      "contract":[  
         {  
            "parameter":{  
               "value":{  
                  "resource":"ENERGY",
                  "frozen_duration":3,
                  "frozen_balance":1000000,
                  "owner_address":"41928c9af0651632157ef27a2cf17ca72c575a4d21"
               },
               "type_url":"type.googleapis.com/protocol.FreezeBalanceContract"
            },
            "type":"FreezeBalanceContract"
         }
      ],
      "ref_block_bytes":"ee08",
      "ref_block_hash":"7b2480cc92edd8a2",
      "expiration":1540253364000,
      "timestamp":1540253304828
   }
}
  1. Broadcast Transaction: The /wallet/broadcasttransaction API call takes in one parameter, which is the JSON output data from signing the transaction.

Below is the sample JSON Output, confirming successful transaction broadcast.

{"result": true}