Skip to content

Commit 449452c

Browse files
bhavidhingraclaude
andcommitted
feat(examples): add TRX DelegateResource example script
Add an example script demonstrating how to build and sign a TRX DelegateResource transaction via the BitGo platform API. This allows delegating frozen ENERGY or BANDWIDTH resources to another address. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 826061a commit 449452c

File tree

5 files changed

+238
-9
lines changed

5 files changed

+238
-9
lines changed

examples/ts/list-wallet-addresses-by-balance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function main() {
2222
const addresses = await wallet.addressesByBalance({
2323
token: 'tapt:usdt',
2424
});
25-
console.log(JSON.stringify(addresses.addresses));
25+
console.log(JSON.stringify(addresses.addresses, null, 2));
2626
}
2727

2828
main().catch((e) => console.error(e));
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Build and sign a TRX DelegateResource transaction via the BitGo platform.
3+
*
4+
* DelegateResource allows a TRX holder who has frozen TRX (via FreezeBalanceV2)
5+
* to delegate the resulting BANDWIDTH or ENERGY resources to another address,
6+
* without transferring TRX itself.
7+
*
8+
* Prerequisites:
9+
* - Valid BitGo access token
10+
* - TRX wallet with frozen balance (via FreezeBalanceV2)
11+
* - Wallet passphrase for signing
12+
*
13+
* Copyright 2026, BitGo, Inc. All Rights Reserved.
14+
*/
15+
import { WalletCoinSpecific } from 'bitgo';
16+
import {BitGoAPI} from "@bitgo/sdk-api";
17+
import {Ttrx} from "@bitgo/sdk-coin-trx";
18+
require('dotenv').config({ path: '../../../.env' });
19+
20+
// TODO: change to 'production' for mainnet
21+
const bitgo = new BitGoAPI({
22+
accessToken: process.env.TESTNET_ACCESS_TOKEN,
23+
env: 'test',
24+
});
25+
26+
const coin = 'ttrx';
27+
bitgo.register(coin, Ttrx.createInstance);
28+
29+
// TODO: set your wallet id
30+
const walletId = '';
31+
32+
// TODO: set your wallet passphrase
33+
const walletPassphrase = '';
34+
35+
// TODO: set OTP code
36+
const otp = '000000';
37+
38+
// TODO: set the receiver address (the address that will use the delegated resources)
39+
const receiverAddress = '';
40+
41+
// TODO: set the amount of frozen TRX to delegate, in SUN (1 TRX = 1,000,000 SUN)
42+
const amountSun = '1000000';
43+
44+
// TODO: set the resource type to delegate: 'ENERGY' or 'BANDWIDTH'
45+
const resource = 'BANDWIDTH';
46+
47+
async function main() {
48+
49+
const wallet = await bitgo.coin(coin).wallets().getWallet({ id: walletId });
50+
const coinSpecific = wallet.coinSpecific() as WalletCoinSpecific;
51+
const ownerAddress = coinSpecific.rootAddress;
52+
53+
console.log('Wallet ID:', wallet.id());
54+
console.log('Owner Address:', ownerAddress);
55+
56+
// Unlock the session for signing
57+
const unlock = await bitgo.unlock({ otp, duration: 3600 });
58+
if (!unlock) {
59+
throw new Error('error unlocking session');
60+
}
61+
62+
// Build, sign, and send the transaction in one step
63+
// The SDK handles prebuild, user half-sign, platform co-signing, and broadcasting
64+
const result = await wallet.sendMany({
65+
type: 'delegateResource',
66+
stakingParams: {
67+
owner_address: ownerAddress,
68+
receiver_address: receiverAddress,
69+
amount: amountSun,
70+
resource,
71+
},
72+
recipients: [
73+
{
74+
address: receiverAddress,
75+
amount: '0',
76+
},
77+
],
78+
walletPassphrase,
79+
});
80+
81+
console.log('Transaction sent successfully!');
82+
console.log('TX ID:', result.txid);
83+
console.log('Result:', JSON.stringify(result, null, 2));
84+
}
85+
86+
main().catch((e) => console.error(e));

examples/ts/trx/get-account-resources.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
*
77
* Copyright 2026, BitGo, Inc. All Rights Reserved.
88
*/
9-
import { BitGo } from 'bitgo';
9+
import { BitGoAPI } from "@bitgo/sdk-api";
10+
import { Ttrx } from "@bitgo/sdk-coin-trx";
11+
require('dotenv').config({ path: '../../../.env' });
1012

1113
// TODO: change to 'production' for mainnet
12-
const env = 'test';
13-
const bitgo = new BitGo({ env });
14+
const bitgo = new BitGoAPI({
15+
accessToken: process.env.TESTNET_ACCESS_TOKEN,
16+
env: 'test',
17+
});
1418

1519
// TODO: change to 'trx' for mainnet or 'ttrx:<token>' for testnet token
1620
const coin = 'ttrx';
21+
bitgo.register(coin, Ttrx.createInstance);
1722

1823
// TODO: set your wallet id
1924
const walletId = '';
2025

21-
// TODO: set your access token here
22-
// You can get this from User Settings > Developer Options > Add Access Token
23-
const accessToken = '';
24-
2526
// TODO: set the addresses to query
2627
// Note: To get energy deficit for a token transfer, make sure the token exists in the address.
2728
const addresses = [''];
2829

2930
async function main() {
30-
bitgo.authenticateWithAccessToken({ accessToken });
3131

3232
const wallet = await bitgo.coin(coin).wallets().getWallet({ id: walletId });
3333

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Get resource delegations for a TRX wallet at BitGo.
3+
*
4+
* This tool will help you see how to use the BitGo API to query
5+
* outgoing and incoming ENERGY/BANDWIDTH delegations for a wallet.
6+
*
7+
* Prerequisites:
8+
* - Valid BitGo access token
9+
* - TRX wallet ID
10+
*
11+
* Copyright 2026, BitGo, Inc. All Rights Reserved.
12+
*/
13+
import { BitGoAPI } from '@bitgo/sdk-api';
14+
import { Ttrx } from '@bitgo/sdk-coin-trx';
15+
require('dotenv').config({ path: '../../../.env' });
16+
17+
// TODO: change to 'production' for mainnet
18+
const bitgo = new BitGoAPI({
19+
accessToken: process.env.TESTNET_ACCESS_TOKEN,
20+
env: 'test',
21+
});
22+
23+
// TODO: change to 'trx' for mainnet
24+
const coin = 'ttrx';
25+
bitgo.register(coin, Ttrx.createInstance);
26+
27+
// TODO: set your wallet id
28+
const walletId = '';
29+
30+
// TODO: (optional) filter by delegation type
31+
const type: 'outgoing' | 'incoming' | undefined = undefined;
32+
33+
// TODO: (optional) filter by resource type: 'ENERGY' or 'BANDWIDTH'
34+
const resourceType: 'ENERGY' | 'BANDWIDTH' | undefined = undefined;
35+
36+
// TODO: (optional) maximum number of results to return
37+
const limit: number | undefined = undefined;
38+
39+
async function main() {
40+
const queryParams: Record<string, string | number> = {};
41+
if (type !== undefined) queryParams.type = type;
42+
if (resourceType !== undefined) queryParams.resourceType = resourceType;
43+
if (limit !== undefined) queryParams.limit = limit;
44+
45+
const result = await bitgo
46+
.get(bitgo.url(`/${coin}/wallet/${walletId}/resourcedelegations`, 2))
47+
.query(queryParams)
48+
.result();
49+
50+
console.log('Wallet Address:', result.address);
51+
console.log('Coin:', result.coin);
52+
console.log('Outgoing Delegations:', result.delegations.outgoing.length);
53+
console.log('Incoming Delegations:', result.delegations.incoming.length);
54+
console.log('Resource Delegations:', JSON.stringify(result, null, 2));
55+
}
56+
57+
main().catch((e) => console.error(e));
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Build and sign a TRX UndelegateResource transaction via the BitGo platform.
3+
*
4+
* DelegateResource allows a TRX holder who has frozen TRX (via FreezeBalanceV2)
5+
* to delegate the resulting BANDWIDTH or ENERGY resources to another address,
6+
* without transferring TRX itself.
7+
*
8+
* Prerequisites:
9+
* - Valid BitGo access token
10+
* - TRX wallet with frozen balance (via FreezeBalanceV2)
11+
* - Wallet passphrase for signing
12+
*
13+
* Copyright 2026, BitGo, Inc. All Rights Reserved.
14+
*/
15+
import { WalletCoinSpecific } from 'bitgo';
16+
import {BitGoAPI} from "@bitgo/sdk-api";
17+
import {Ttrx} from "@bitgo/sdk-coin-trx";
18+
require('dotenv').config({ path: '../../../.env' });
19+
20+
// TODO: change to 'production' for mainnet
21+
const bitgo = new BitGoAPI({
22+
accessToken: process.env.TESTNET_ACCESS_TOKEN,
23+
env: 'test',
24+
});
25+
26+
const coin = 'ttrx';
27+
bitgo.register(coin, Ttrx.createInstance);
28+
29+
// TODO: set your wallet id
30+
const walletId = '';
31+
32+
// TODO: set your wallet passphrase
33+
const walletPassphrase = '';
34+
35+
// TODO: set OTP code
36+
const otp = '000000';
37+
38+
// TODO: set the receiver address (the address that will use the delegated resources)
39+
const receiverAddress = '';
40+
41+
// TODO: set the amount of frozen TRX to delegate, in SUN (1 TRX = 1,000,000 SUN)
42+
const amountSun = '1000000';
43+
44+
// TODO: set the resource type to delegate: 'ENERGY' or 'BANDWIDTH'
45+
const resource = 'BANDWIDTH';
46+
47+
async function main() {
48+
49+
const wallet = await bitgo.coin(coin).wallets().getWallet({ id: walletId });
50+
const coinSpecific = wallet.coinSpecific() as WalletCoinSpecific;
51+
const ownerAddress = coinSpecific.rootAddress;
52+
53+
console.log('Wallet ID:', wallet.id());
54+
console.log('Owner Address:', ownerAddress);
55+
56+
// Unlock the session for signing
57+
const unlock = await bitgo.unlock({ otp, duration: 3600 });
58+
if (!unlock) {
59+
throw new Error('error unlocking session');
60+
}
61+
62+
// Build, sign, and send the transaction in one step
63+
// The SDK handles prebuild, user half-sign, platform co-signing, and broadcasting
64+
const result = await wallet.sendMany({
65+
type: 'undelegateResource',
66+
stakingParams: {
67+
owner_address: ownerAddress,
68+
receiver_address: receiverAddress,
69+
amount: amountSun,
70+
resource,
71+
},
72+
recipients: [
73+
{
74+
address: receiverAddress,
75+
amount: '0',
76+
},
77+
],
78+
walletPassphrase,
79+
});
80+
81+
console.log('Transaction sent successfully!');
82+
console.log('TX ID:', result.txid);
83+
console.log('Result:', JSON.stringify(result, null, 2));
84+
}
85+
86+
main().catch((e) => console.error(e));

0 commit comments

Comments
 (0)