|
| 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