Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/ts/trx/get-account-resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Get account resources for a Tron wallet at BitGo.
*
* This tool will help you see how to use the BitGo API to easily get
* account resources information for a wallet.
*
* Copyright 2026, BitGo, Inc. All Rights Reserved.
*/
import { BitGo } from 'bitgo';

// TODO: change to 'production' for mainnet
const env = 'test';
const bitgo = new BitGo({ env });

// TODO: change to 'trx' for mainnet or 'ttrx:<token>' for testnet token
const coin = 'ttrx';

// 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 });

console.log('Wallet ID:', wallet.id());

const resources = await wallet.getAccountResources({ addresses });
console.log('Account Resources:', JSON.stringify(resources, null, 2));
}

main().catch((e) => console.error(e));
6 changes: 6 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/iWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ export interface ForwarderBalanceOptions {
maximumBalance?: number;
}

export interface GetAccountResourcesOptions {
addresses: string[];
destinationAddress?: string;
}

export type CreateAddressFormat = 'base58' | 'cashaddr';

export interface CreateAddressOptions {
Expand Down Expand Up @@ -988,6 +993,7 @@ export interface IWallet {
getAddress(params?: GetAddressOptions): Promise<any>;
createAddress(params?: CreateAddressOptions): Promise<any>;
updateAddress(params?: UpdateAddressOptions): Promise<any>;
getAccountResources(params: GetAccountResourcesOptions): Promise<any>;
listWebhooks(params?: PaginationOptions): Promise<any>;
simulateWebhook(params?: SimulateWebhookOptions): Promise<any>;
addWebhook(params?: ModifyWebhookOptions): Promise<any>;
Expand Down
25 changes: 25 additions & 0 deletions modules/sdk-core/src/bitgo/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import {
WalletType,
BuildTokenApprovalResponse,
WalletInitResult,
GetAccountResourcesOptions,
} from './iWallet';

const debug = require('debug')('bitgo:v2:wallet');
Expand Down Expand Up @@ -1465,6 +1466,30 @@ export class Wallet implements IWallet {
return this.bitgo.put(url).send(putParams).result();
}

/**
* Get account resources for the given addresses
* @param params - options object containing addresses and optional destinationAddress
* @returns {Promise<any>} - response from WP API
*/
async getAccountResources(params: GetAccountResourcesOptions): Promise<any> {
const { addresses, destinationAddress } = params;

if (!Array.isArray(addresses)) {
throw new Error('addresses must be an array');
}

if (addresses.length === 0) {
throw new Error('addresses array cannot be empty');
}

const query: GetAccountResourcesOptions = { addresses };
if (destinationAddress) {
query.destinationAddress = destinationAddress;
}

return this.bitgo.get(this.url('/getAccountResources')).query(query).result();
}

async updateWalletBuildDefaults(params: UpdateBuildDefaultOptions): Promise<unknown> {
common.validateParams(params, [], ['minFeeRate', 'changeAddressType', 'txFormat']);
return this.updateWallet({
Expand Down
115 changes: 115 additions & 0 deletions modules/sdk-core/test/unit/bitgo/wallet/getAccountResources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import 'should';
import { Wallet } from '../../../../src';

describe('Wallet - getAccountResources', function () {
let wallet: Wallet;
let mockBitGo: any;
let mockBaseCoin: any;
let mockWalletData: any;

beforeEach(function () {
mockBitGo = {
get: sinon.stub(),
};

mockBaseCoin = {
url: sinon.stub().returns('/test/coin'),
supportsTss: sinon.stub().returns(false),
};

mockWalletData = {
id: 'test-wallet-id',
keys: ['user-key', 'backup-key', 'bitgo-key'],
};

wallet = new Wallet(mockBitGo, mockBaseCoin, mockWalletData);
});

afterEach(function () {
sinon.restore();
});

describe('getAccountResources', function () {
it('should call WP API with addresses parameter', async function () {
const mockResponse = {
resources: [
{ address: 'address1', balance: 100 },
{ address: 'address2', balance: 200 },
],
};

mockBitGo.get.returns({
query: sinon.stub().returns({
result: sinon.stub().resolves(mockResponse),
}),
});

const addresses = ['address1', 'address2'];
const result = await wallet.getAccountResources({ addresses });

result.should.deepEqual(mockResponse);
sinon.assert.calledOnce(mockBitGo.get);
const queryStub = mockBitGo.get.returnValues[0].query;
sinon.assert.calledWith(queryStub, { addresses });
});

it('should call WP API with addresses and destinationAddress parameters', async function () {
const mockResponse = {
resources: [{ address: 'address1', balance: 100 }],
};

mockBitGo.get.returns({
query: sinon.stub().returns({
result: sinon.stub().resolves(mockResponse),
}),
});

const addresses = ['address1'];
const destinationAddress = 'TDestAddress123';
const result = await wallet.getAccountResources({ addresses, destinationAddress });

result.should.deepEqual(mockResponse);
sinon.assert.calledOnce(mockBitGo.get);
const queryStub = mockBitGo.get.returnValues[0].query;
sinon.assert.calledWith(queryStub, { addresses, destinationAddress });
});

it('should throw error if addresses is not an array', async function () {
try {
await wallet.getAccountResources({ addresses: 'not-an-array' as any });
assert.fail('Should have thrown error');
} catch (error) {
error.message.should.equal('addresses must be an array');
}
});

it('should throw error if addresses array is empty', async function () {
try {
await wallet.getAccountResources({ addresses: [] });
assert.fail('Should have thrown error');
} catch (error) {
error.message.should.equal('addresses array cannot be empty');
}
});

it('should not include destinationAddress in query if not provided', async function () {
const mockResponse = { resources: [] };

mockBitGo.get.returns({
query: sinon.stub().returns({
result: sinon.stub().resolves(mockResponse),
}),
});

const addresses = ['address1'];
await wallet.getAccountResources({ addresses });

const queryStub = mockBitGo.get.returnValues[0].query;
const queryArg = queryStub.firstCall.args[0];
queryArg.should.deepEqual({ addresses });
queryArg.should.not.have.property('destinationAddress');
});
});
});