diff --git a/examples/ts/list-wallet-addresses-by-balance.ts b/examples/ts/list-wallet-addresses-by-balance.ts index b068825614..0ec87ee38e 100644 --- a/examples/ts/list-wallet-addresses-by-balance.ts +++ b/examples/ts/list-wallet-addresses-by-balance.ts @@ -22,7 +22,7 @@ async function main() { const addresses = await wallet.addressesByBalance({ token: 'tapt:usdt', }); - console.log(JSON.stringify(addresses.addresses)); + console.log(JSON.stringify(addresses.addresses, null, 2)); } main().catch((e) => console.error(e)); diff --git a/examples/ts/trx/delegate-resource.ts b/examples/ts/trx/delegate-resource.ts new file mode 100644 index 0000000000..c780568607 --- /dev/null +++ b/examples/ts/trx/delegate-resource.ts @@ -0,0 +1,85 @@ +/** + * Build and sign a TRX DelegateResource transaction via the BitGo platform. + * + * DelegateResource allows a TRX holder who has frozen TRX (via FreezeBalanceV2) + * to delegate the resulting BANDWIDTH or ENERGY resources to another address, + * without transferring TRX itself. + * + * Prerequisites: + * - Valid BitGo access token + * - TRX wallet with frozen balance (via FreezeBalanceV2) + * - Wallet passphrase for signing + * + * Copyright 2026, BitGo, Inc. All Rights Reserved. + */ +import { WalletCoinSpecific } from 'bitgo'; +import { BitGoAPI } from '@bitgo/sdk-api'; +import { Ttrx } from '@bitgo/sdk-coin-trx'; +require('dotenv').config({ path: '../../../.env' }); + +// TODO: change to 'production' for mainnet +const bitgo = new BitGoAPI({ + accessToken: process.env.TESTNET_ACCESS_TOKEN, + env: 'test', +}); + +const coin = 'ttrx'; +bitgo.register(coin, Ttrx.createInstance); + +// TODO: set your wallet id +const walletId = ''; + +// TODO: set your wallet passphrase +const walletPassphrase = ''; + +// TODO: set OTP code +const otp = '000000'; + +// TODO: set the receiver address (the address that will use the delegated resources) +const receiverAddress = ''; + +// TODO: set the amount of frozen TRX to delegate, in SUN (1 TRX = 1,000,000 SUN) +const amountSun = '1000000'; + +// TODO: set the resource type to delegate: 'ENERGY' or 'BANDWIDTH' +const resource = 'BANDWIDTH'; + +async function main() { + const wallet = await bitgo.coin(coin).wallets().getWallet({ id: walletId }); + const coinSpecific = wallet.coinSpecific() as WalletCoinSpecific; + const ownerAddress = coinSpecific.rootAddress; + + console.log('Wallet ID:', wallet.id()); + console.log('Owner Address:', ownerAddress); + + // Unlock the session for signing + const unlock = await bitgo.unlock({ otp, duration: 3600 }); + if (!unlock) { + throw new Error('error unlocking session'); + } + + // Build, sign, and send the transaction in one step + // The SDK handles prebuild, user half-sign, platform co-signing, and broadcasting + const result = await wallet.sendMany({ + type: 'delegateResource', + stakingParams: { + owner_address: ownerAddress, + receiver_address: receiverAddress, + amount: amountSun, + resource, + }, + recipients: [ + { + address: receiverAddress, + amount: '0', + }, + ], + walletPassphrase, + }); + + console.log('Transaction sent successfully!'); + console.log('TX ID:', result.txid); + console.log('Result:', JSON.stringify(result, null, 2)); +} + +main().catch((e) => console.error(e)); diff --git a/examples/ts/trx/get-account-resources.ts b/examples/ts/trx/get-account-resources.ts index 01fa1973bf..caa08fa82d 100644 --- a/examples/ts/trx/get-account-resources.ts +++ b/examples/ts/trx/get-account-resources.ts @@ -6,28 +6,28 @@ * * Copyright 2026, BitGo, Inc. All Rights Reserved. */ -import { BitGo } from 'bitgo'; +import { BitGoAPI } from "@bitgo/sdk-api"; +import { Ttrx } from "@bitgo/sdk-coin-trx"; +require('dotenv').config({ path: '../../../.env' }); // TODO: change to 'production' for mainnet -const env = 'test'; -const bitgo = new BitGo({ env }); +const bitgo = new BitGoAPI({ + accessToken: process.env.TESTNET_ACCESS_TOKEN, + env: 'test', +}); // TODO: change to 'trx' for mainnet or 'ttrx:' for testnet token const coin = 'ttrx'; +bitgo.register(coin, Ttrx.createInstance); // TODO: set your wallet id const walletId = ''; -// TODO: set your access token here -// You can get this from User Settings > Developer Options > Add Access Token -const accessToken = ''; - // TODO: set the addresses to query // Note: To get energy deficit for a token transfer, make sure the token exists in the address. const addresses = ['']; async function main() { - bitgo.authenticateWithAccessToken({ accessToken }); const wallet = await bitgo.coin(coin).wallets().getWallet({ id: walletId }); diff --git a/examples/ts/trx/get-resource-delegations.ts b/examples/ts/trx/get-resource-delegations.ts new file mode 100644 index 0000000000..7757eb1adf --- /dev/null +++ b/examples/ts/trx/get-resource-delegations.ts @@ -0,0 +1,57 @@ +/** + * Get resource delegations for a TRX wallet at BitGo. + * + * This tool will help you see how to use the BitGo API to query + * outgoing and incoming ENERGY/BANDWIDTH delegations for a wallet. + * + * Prerequisites: + * - Valid BitGo access token + * - TRX wallet ID + * + * Copyright 2026, BitGo, Inc. All Rights Reserved. + */ +import { BitGoAPI } from '@bitgo/sdk-api'; +import { Ttrx } from '@bitgo/sdk-coin-trx'; +require('dotenv').config({ path: '../../../.env' }); + +// TODO: change to 'production' for mainnet +const bitgo = new BitGoAPI({ + accessToken: process.env.TESTNET_ACCESS_TOKEN, + env: 'test', +}); + +// TODO: change to 'trx' for mainnet +const coin = 'ttrx'; +bitgo.register(coin, Ttrx.createInstance); + +// TODO: set your wallet id +const walletId = ''; + +// TODO: (optional) filter by delegation type +const type: 'outgoing' | 'incoming' | undefined = undefined; + +// TODO: (optional) filter by resource type: 'ENERGY' or 'BANDWIDTH' +const resourceType: 'ENERGY' | 'BANDWIDTH' | undefined = undefined; + +// TODO: (optional) maximum number of results to return +const limit: number | undefined = undefined; + +async function main() { + const queryParams: Record = {}; + if (type !== undefined) queryParams.type = type; + if (resourceType !== undefined) queryParams.resourceType = resourceType; + if (limit !== undefined) queryParams.limit = limit; + + const result = await bitgo + .get(bitgo.url(`/${coin}/wallet/${walletId}/resourcedelegations`, 2)) + .query(queryParams) + .result(); + + console.log('Wallet Address:', result.address); + console.log('Coin:', result.coin); + console.log('Outgoing Delegations:', result.delegations.outgoing.length); + console.log('Incoming Delegations:', result.delegations.incoming.length); + console.log('Resource Delegations:', JSON.stringify(result, null, 2)); +} + +main().catch((e) => console.error(e)); diff --git a/examples/ts/trx/undelegate-resource.ts b/examples/ts/trx/undelegate-resource.ts new file mode 100644 index 0000000000..602bc380b4 --- /dev/null +++ b/examples/ts/trx/undelegate-resource.ts @@ -0,0 +1,86 @@ +/** + * Build and sign a TRX UndelegateResource transaction via the BitGo platform. + * + * DelegateResource allows a TRX holder who has frozen TRX (via FreezeBalanceV2) + * to delegate the resulting BANDWIDTH or ENERGY resources to another address, + * without transferring TRX itself. + * + * Prerequisites: + * - Valid BitGo access token + * - TRX wallet with frozen balance (via FreezeBalanceV2) + * - Wallet passphrase for signing + * + * Copyright 2026, BitGo, Inc. All Rights Reserved. + */ +import { WalletCoinSpecific } from 'bitgo'; +import {BitGoAPI} from "@bitgo/sdk-api"; +import {Ttrx} from "@bitgo/sdk-coin-trx"; +require('dotenv').config({ path: '../../../.env' }); + +// TODO: change to 'production' for mainnet +const bitgo = new BitGoAPI({ + accessToken: process.env.TESTNET_ACCESS_TOKEN, + env: 'test', +}); + +const coin = 'ttrx'; +bitgo.register(coin, Ttrx.createInstance); + +// TODO: set your wallet id +const walletId = ''; + +// TODO: set your wallet passphrase +const walletPassphrase = ''; + +// TODO: set OTP code +const otp = '000000'; + +// TODO: set the receiver address (the address that will use the delegated resources) +const receiverAddress = ''; + +// TODO: set the amount of frozen TRX to delegate, in SUN (1 TRX = 1,000,000 SUN) +const amountSun = '1000000'; + +// TODO: set the resource type to delegate: 'ENERGY' or 'BANDWIDTH' +const resource = 'BANDWIDTH'; + +async function main() { + + const wallet = await bitgo.coin(coin).wallets().getWallet({ id: walletId }); + const coinSpecific = wallet.coinSpecific() as WalletCoinSpecific; + const ownerAddress = coinSpecific.rootAddress; + + console.log('Wallet ID:', wallet.id()); + console.log('Owner Address:', ownerAddress); + + // Unlock the session for signing + const unlock = await bitgo.unlock({ otp, duration: 3600 }); + if (!unlock) { + throw new Error('error unlocking session'); + } + + // Build, sign, and send the transaction in one step + // The SDK handles prebuild, user half-sign, platform co-signing, and broadcasting + const result = await wallet.sendMany({ + type: 'undelegateResource', + stakingParams: { + owner_address: ownerAddress, + receiver_address: receiverAddress, + amount: amountSun, + resource, + }, + recipients: [ + { + address: receiverAddress, + amount: '0', + }, + ], + walletPassphrase, + }); + + console.log('Transaction sent successfully!'); + console.log('TX ID:', result.txid); + console.log('Result:', JSON.stringify(result, null, 2)); +} + +main().catch((e) => console.error(e));