Skip to content

Instantly share code, notes, and snippets.

@zkharit
Last active June 7, 2025 16:42
Show Gist options
  • Select an option

  • Save zkharit/1b045f17d60ccd36147434cce82637e4 to your computer and use it in GitHub Desktop.

Select an option

Save zkharit/1b045f17d60ccd36147434cce82637e4 to your computer and use it in GitHub Desktop.
Create Tron Transactions
import { TronWeb } from "tronweb";
const PRIVATE_KEY = "<private key>";
const tronWeb = new TronWeb({
fullHost: "https://nile.trongrid.io/",
privateKey: PRIVATE_KEY,
});
const ownerAddress = 'THEg7RCHyDaNxaLrogF3oYu1WurunNyF4L';
const ownerPermission = { type: 0, permission_name: 'owner', threshold: 1, operations: '7fff1fc0033efb07000000000000000000000000000000000000000000000000', keys: [{ address: ownerAddress, weight: 1 }] };
const witnessAddress = 'TY1jfzP3s94oSzYECC89EFn17iA8S4imVZ'
const witnessPermission = { type: 1, id: 1, permission_name: 'witness', threshold: 2, operations: '1200000000004000000000000000000000000000000000000000000000000000', keys: [{ address: witnessAddress, weight: 1 }] };
const activePermission = { type: 2, id: 2, permission_name: 'active0', threshold: 3, operations: '7fff1fc0037e0000000000000000000000000000000000000000000000000000', keys: [{ address: ownerAddress, weight: 1 }, { address: witnessAddress, weight: 1 }] };
const accountUpdatePermissionsTx = await tronWeb.transactionBuilder.updateAccountPermissions(
ownerAddress,
ownerPermission,
witnessPermission,
[activePermission]
);
const signedAccountUpdatePermissionsTx = await tronWeb.trx.sign(accountUpdatePermissionsTx);
console.log("Signed AccountUpdatePermissions Tx:")
console.log(JSON.stringify(signedAccountUpdatePermissionsTx, null, 4))
console.log()
import { TronWeb } from "tronweb";
const PRIVATE_KEY = "<private key>";
const tronWeb = new TronWeb({
fullHost: "https://nile.trongrid.io/",
privateKey: PRIVATE_KEY,
});
const ownerAddress = 'THEg7RCHyDaNxaLrogF3oYu1WurunNyF4L';
const receiverAddress = 'TY1jfzP3s94oSzYECC89EFn17iA8S4imVZ';
const frozenBalance = 1000 * 1e6; // in SUN (1 TRX = 1e6 SUN)
const resource = 'ENERGY'; // or 'BANDWIDTH'
const lock = true; // true = locked delegation (recommended for governance participation)
const lockPeriod = 1; // length of time in blocks to lock
const delegateResourceTx = await tronWeb.transactionBuilder.delegateResource(
frozenBalance,
receiverAddress,
resource,
ownerAddress,
lock,
lockPeriod
);
const signedDelegateResourceTx = await tronWeb.trx.sign(delegateResourceTx);
console.log("Signed DelegateResourceContract Tx:")
console.log(JSON.stringify(signedDelegateResourceTx, null, 4))
console.log()
import { TronWeb } from "tronweb";
const PRIVATE_KEY = "<private key>";
const tronWeb = new TronWeb({
fullHost: "https://nile.trongrid.io/",
privateKey: PRIVATE_KEY,
});
const ownerAddress = 'THEg7RCHyDaNxaLrogF3oYu1WurunNyF4L';
const frozenBalance = 2000 * 1e6; // in SUN (1 TRX = 1e6 SUN)
const resource = 'ENERGY'; // or 'BANDWIDTH'
const freezeBalanceV2Tx = await tronWeb.transactionBuilder.freezeBalanceV2(
frozenBalance,
resource,
ownerAddress
);
const signedFreezeBalanceV2Tx = await tronWeb.trx.sign(freezeBalanceV2Tx);
console.log("Signed FreezeBalanceV2Contract Tx:")
console.log(JSON.stringify(signedFreezeBalanceV2Tx, null, 4))
console.log()
import { TronWeb } from "tronweb";
// tronweb documentation here: https://tronweb.network/docu/docs/Transfer%20Token
const tronWeb = new TronWeb({
fullHost: "https://nile.trongrid.io/",
privateKey: <private key>,
});
//////////////////////////////////
// TransferContract Transaction
const sendAddress = "THEg7RCHyDaNxaLrogF3oYu1WurunNyF4L";
const recipientAddress = "TY1jfzP3s94oSzYECC89EFn17iA8S4imVZ";
const amount = 100;
const transferContractTx = await tronWeb.transactionBuilder.sendTrx(
recipientAddress,
amount,
sendAddress
)
// The resulting raw_data_hex should be used for the unsignedTransaction parameter in the SignTransaction Activity
// Note: any addresses will be output in hex form NOT properly encoded Tron addresses with this basic print statement
console.log("Unsigned TransferContract Tx:")
console.log(JSON.stringify(transferContractTx, null, 4));
console.log()
const signedTransferContractTx = await tronWeb.trx.sign(transferContractTx)
console.log("Signed TransferContract Tx:")
console.log(JSON.stringify(signedTransferContractTx, null, 4))
console.log()
//////////////////////////////////
// TriggerSmartContract Transaction
const usdtContractAddress = 'TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf';
const parameters = [{ type: 'address', value: 'TY1jfzP3s94oSzYECC89EFn17iA8S4imVZ' }, { type: 'uint256', value: '100' }];
const options = {
feeLimit: 100000000,
callValue: 2,
tokenValue: 10,
tokenId: '1000001',
txLocal: true
};
const triggerSmartContractTx = await tronWeb.transactionBuilder.triggerSmartContract(
usdtContractAddress,
'transfer(address,uint256)',
options,
parameters
);
console.log("Unsigned TriggerSmartContract Tx:")
console.log(JSON.stringify(triggerSmartContractTx, null, 4))
console.log()
const signedtriggerSmartContractTx = await tronWeb.trx.sign(triggerSmartContractTx.transaction)
console.log("Signed TriggerSmartContract Tx:")
console.log(JSON.stringify(signedtriggerSmartContractTx, null, 4))
console.log()
//////////////////////////////////
// Broadcast transaction
// const signedTransaction = <signedTransaction result from signTransaction Activity>;
// const result = await tronWeb.trx.sendHexTransaction(signedTransaction);
// console.log(result);
import { TronWeb } from "tronweb";
const PRIVATE_KEY = "<private key>";
const tronWeb = new TronWeb({
fullHost: "https://nile.trongrid.io/",
privateKey: PRIVATE_KEY,
});
const ownerAddress = 'THEg7RCHyDaNxaLrogF3oYu1WurunNyF4L';
const receiverAddress = 'TY1jfzP3s94oSzYECC89EFn17iA8S4imVZ';
const frozenBalance = 1000 * 1e6; // in SUN (1 TRX = 1e6 SUN)
const resource = 'ENERGY'; // or 'BANDWIDTH'
const unDelegateResourceTx = await tronWeb.transactionBuilder.undelegateResource(
frozenBalance,
receiverAddress,
resource,
ownerAddress
);
const signedUnDelegateResourceTx = await tronWeb.trx.sign(unDelegateResourceTx);
console.log("Signed UnDelegateResourceContract Tx:")
console.log(JSON.stringify(signedUnDelegateResourceTx, null, 4))
console.log()
import { TronWeb } from "tronweb";
const PRIVATE_KEY = "<private key>";
const tronWeb = new TronWeb({
fullHost: "https://nile.trongrid.io/",
privateKey: PRIVATE_KEY,
});
const ownerAddress = 'THEg7RCHyDaNxaLrogF3oYu1WurunNyF4L';
const frozenBalance = 2000 * 1e6; // in SUN (1 TRX = 1e6 SUN)
const resource = 'ENERGY'; // or 'BANDWIDTH'
const unFreezeBalanceV2Tx = await tronWeb.transactionBuilder.unfreezeBalanceV2(
frozenBalance,
resource,
ownerAddress
);
const signedUnFreezeBalanceV2Tx = await tronWeb.trx.sign(unFreezeBalanceV2Tx);
console.log("Signed UnfreezeBalanceV2Contract Tx:")
console.log(JSON.stringify(signedUnFreezeBalanceV2Tx, null, 4))
console.log()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment