Transaction
Generating Signed Transaction
Install the npm library here or download the utils and the lib folders into your project to use natively.
Alternatively, you can use API Signature and Broadcast Flow to generate a signed transaction.
Creating a Transaction
Import transactionBuilder and crypto into your JavaScript file.
const CryptoUtils = require("@tronscan/client/src/utils/crypto");
const TransactionUtils = require("@tronscan/client/src/utils/transactionBuilder");
This function from the imported transactionBuilder file will create the Transaction Object.
function buildTransferTransaction(token, from, to, amount) {
if (token.toUpperCase() === 'TRX') {
let transferContract = new TransferContract();
transferContract.setToAddress(Uint8Array.from(decode58Check(to)));
transferContract.setOwnerAddress(Uint8Array.from(decode58Check(from)));
transferContract.setAmount(amount);
return buildTransferContract(
transferContract,
Transaction.Contract.ContractType.TRANSFERCONTRACT,
"TransferContract");
} else {
let transferContract = new TransferAssetContract();
transferContract.setToAddress(Uint8Array.from(decode58Check(to)));
transferContract.setOwnerAddress(Uint8Array.from(decode58Check(from)));
transferContract.setAmount(amount);
transferContract.setAssetName(encodeString(token));
return buildTransferContract(
transferContract,
Transaction.Contract.ContractType.TRANSFERASSETCONTRACT,
"TransferAssetContract");
}
}
The parameters for this function are:
Value | Data Type | Description |
---|---|---|
token | String | The asset you want to send in your transaction. Example: "TRX" |
from | String | The address that you want to send from in your transaction. Example: "TDCMWoSbAfcegQqNUaRNjGhY4tAbdCmdwi" |
to | String | The address that you want to send to in your transaction. Example: "TQ6pM81JDC2GhrUoNYtZGvPc7SvyqcemEu" |
amount | Integer | The amount you want to spend of the specified token. If you want to send "TRX", make sure you multiply this value by 1000000. Example: If you want to send 2 TRX, use 2000000 |
The buildTransferTransaction specified above will return the value of buildTransferContract, which is the function that creates the Transaction Object.
function buildTransferContract(message, contractType, typeName) {
let anyValue = new google_protobuf_any_pb.Any();
anyValue.pack(message.serializeBinary(), "protocol." + typeName);
let contract = new Transaction.Contract();
contract.setType(contractType);
contract.setParameter(anyValue);
let raw = new Transaction.raw();
raw.addContract(contract);
// raw.setTimestamp(new Date().getTime() * 1000000);
let transaction = new Transaction();
transaction.setRawData(raw);
return transaction;
}
Signing the Transaction
This function from the imported crypto file will sign the Transaction Object.
function signTransaction(priKeyBytes, transaction) {
if (typeof priKeyBytes === 'string') {
priKeyBytes = hexStr2byteArray(priKeyBytes);
}
let raw = transaction.getRawData();
let rawBytes = raw.serializeBinary();
let hashBytes = SHA256(rawBytes);
let signBytes = ECKeySign(hashBytes, priKeyBytes);
let uint8Array = new Uint8Array(signBytes);
let count = raw.getContractList().length;
for (let i = 0; i < count; i++) {
transaction.addSignature(uint8Array);
}
return {
transaction,
hex: byteArray2hexStr(transaction.serializeBinary()),
};
}
The parameters for this function are:
Value | Data Type | Description |
---|---|---|
priKeyBytes | Byte Array. You can also use a String due the function converting the value to byte array if needed. | The private key of the account. |
Transaction | Transaction Object | The bundled transaction to be sent. |
After successfully running these functions, you will have a signed transaction that can be broadcasted to the TRON blockchain.
Full Code Example
const CryptoUtils = require("@tronscan/client/src/utils/crypto");
const TransactionUtils = require("@tronscan/client/src/utils/transactionBuilder");
function transferContractTx() {
const privateKey = "b815adfd6ef133d5a878869cb3a2b31f32d4c1481132a71300c3e125be0ab1a1";
const token = "TRX";
const fromAddress = CryptoUtils.pkToAddress(privateKey);
const toAddress = "TQ6pM81JDC2GhrUoNYtZGvPc7SvyqcemEu";
const amount = 1;
let transaction = TransactionUtils.buildTransferTransaction(token, fromAddress, toAddress, amount);
let signedTransaction = CryptoUtils.signTransaction(privateKey, transaction);
}
Transaction Confirmation
The mechanism of TRON's block validation is that the current block is validated after the current block is produced and 19 different SRs produce subsequent blocks based on that block.
Whether a transaction is to be confirmed or not requires the block in which the transaction is to be confirmed. TRON provides the /walletsolidty/ interface to make it easier for users to search for confirmed transactions, the following describes how to confirm different types of transactions.
Transaction Type | Method of transaction confirmation |
---|---|
transferContract and transferAssetContract | As long as the transaction can query the results via walletsolidity/gettransactioninfobyid or walletsolidity/gettransactionbyid. |
TriggerSmartContract | There are two ways to judge: 1. Find transactionInfo.receipt.result=success via the /walletsolidity/gettransactioninfobyid interface (It is not recommended to use transactionInfo.result to judge, because for the http interface, successful transactions do not return this field by default). 2. Via /walletsolidity/gettransactionbyid The interface found transaction.ret.contractRet =success |
InternalTransaction | Query by /walletsolidity/gettransactioninfobyid. Http interface: For successful transactions, the rejected field is not returned by default. For failed transactions, rejected=true. Grpc interface: For successful transactions, rejected=false (indicating that the current internalTransaction has not been discarded), For failed transactions, rejected=true. |
Updated over 4 years ago