Skip to content

Commit 3fc282c

Browse files
Copilotbhavidhingra
andcommitted
Add getAccountResources method to wallet class with tests
Co-authored-by: bhavidhingra <17147510+bhavidhingra@users.noreply.github.com>
1 parent 8c0ae42 commit 3fc282c

3 files changed

Lines changed: 145 additions & 0 deletions

File tree

modules/sdk-core/src/bitgo/wallet/iWallet.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ export interface ForwarderBalanceOptions {
539539
maximumBalance?: number;
540540
}
541541

542+
export interface GetAccountResourcesOptions {
543+
addresses: string[];
544+
tokenName?: string;
545+
}
546+
542547
export type CreateAddressFormat = 'base58' | 'cashaddr';
543548

544549
export interface CreateAddressOptions {
@@ -988,6 +993,7 @@ export interface IWallet {
988993
getAddress(params?: GetAddressOptions): Promise<any>;
989994
createAddress(params?: CreateAddressOptions): Promise<any>;
990995
updateAddress(params?: UpdateAddressOptions): Promise<any>;
996+
getAccountResources(addresses: string[], tokenName?: string): Promise<any>;
991997
listWebhooks(params?: PaginationOptions): Promise<any>;
992998
simulateWebhook(params?: SimulateWebhookOptions): Promise<any>;
993999
addWebhook(params?: ModifyWebhookOptions): Promise<any>;

modules/sdk-core/src/bitgo/wallet/wallet.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,29 @@ export class Wallet implements IWallet {
14651465
return this.bitgo.put(url).send(putParams).result();
14661466
}
14671467

1468+
/**
1469+
* Get account resources for the given addresses
1470+
* @param addresses - array of address strings
1471+
* @param tokenName - optional token name
1472+
* @returns {Promise<any>} - response from WP API
1473+
*/
1474+
async getAccountResources(addresses: string[], tokenName?: string): Promise<any> {
1475+
if (!Array.isArray(addresses)) {
1476+
throw new Error('addresses must be an array');
1477+
}
1478+
1479+
if (addresses.length === 0) {
1480+
throw new Error('addresses array cannot be empty');
1481+
}
1482+
1483+
const query: any = { addresses };
1484+
if (tokenName) {
1485+
query.tokenName = tokenName;
1486+
}
1487+
1488+
return this.bitgo.get(this.url('/accountResources')).query(query).result();
1489+
}
1490+
14681491
async updateWalletBuildDefaults(params: UpdateBuildDefaultOptions): Promise<unknown> {
14691492
common.validateParams(params, [], ['minFeeRate', 'changeAddressType', 'txFormat']);
14701493
return this.updateWallet({
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import * as assert from 'assert';
2+
import * as sinon from 'sinon';
3+
import 'should';
4+
import { Wallet } from '../../../../src/bitgo/wallet/wallet';
5+
6+
describe('Wallet - getAccountResources', function () {
7+
let wallet: Wallet;
8+
let mockBitGo: any;
9+
let mockBaseCoin: any;
10+
let mockWalletData: any;
11+
12+
beforeEach(function () {
13+
mockBitGo = {
14+
get: sinon.stub(),
15+
};
16+
17+
mockBaseCoin = {
18+
url: sinon.stub().returns('/test/coin'),
19+
};
20+
21+
mockWalletData = {
22+
id: 'test-wallet-id',
23+
keys: ['user-key', 'backup-key', 'bitgo-key'],
24+
};
25+
26+
wallet = new Wallet(mockBitGo, mockBaseCoin, mockWalletData);
27+
});
28+
29+
afterEach(function () {
30+
sinon.restore();
31+
});
32+
33+
describe('getAccountResources', function () {
34+
it('should call WP API with addresses parameter', async function () {
35+
const mockResponse = {
36+
resources: [
37+
{ address: 'address1', balance: 100 },
38+
{ address: 'address2', balance: 200 },
39+
],
40+
};
41+
42+
mockBitGo.get.returns({
43+
query: sinon.stub().returns({
44+
result: sinon.stub().resolves(mockResponse),
45+
}),
46+
});
47+
48+
const addresses = ['address1', 'address2'];
49+
const result = await wallet.getAccountResources(addresses);
50+
51+
result.should.deepEqual(mockResponse);
52+
mockBitGo.get.should.have.been.calledOnce;
53+
const queryStub = mockBitGo.get.returnValues[0].query;
54+
queryStub.should.have.been.calledWith({ addresses });
55+
});
56+
57+
it('should call WP API with addresses and tokenName parameters', async function () {
58+
const mockResponse = {
59+
resources: [
60+
{ address: 'address1', balance: 100, token: 'USDT' },
61+
],
62+
};
63+
64+
mockBitGo.get.returns({
65+
query: sinon.stub().returns({
66+
result: sinon.stub().resolves(mockResponse),
67+
}),
68+
});
69+
70+
const addresses = ['address1'];
71+
const tokenName = 'USDT';
72+
const result = await wallet.getAccountResources(addresses, tokenName);
73+
74+
result.should.deepEqual(mockResponse);
75+
mockBitGo.get.should.have.been.calledOnce;
76+
const queryStub = mockBitGo.get.returnValues[0].query;
77+
queryStub.should.have.been.calledWith({ addresses, tokenName });
78+
});
79+
80+
it('should throw error if addresses is not an array', async function () {
81+
try {
82+
await wallet.getAccountResources('not-an-array' as any);
83+
assert.fail('Should have thrown error');
84+
} catch (error) {
85+
error.message.should.equal('addresses must be an array');
86+
}
87+
});
88+
89+
it('should throw error if addresses array is empty', async function () {
90+
try {
91+
await wallet.getAccountResources([]);
92+
assert.fail('Should have thrown error');
93+
} catch (error) {
94+
error.message.should.equal('addresses array cannot be empty');
95+
}
96+
});
97+
98+
it('should not include tokenName in query if not provided', async function () {
99+
const mockResponse = { resources: [] };
100+
101+
mockBitGo.get.returns({
102+
query: sinon.stub().returns({
103+
result: sinon.stub().resolves(mockResponse),
104+
}),
105+
});
106+
107+
const addresses = ['address1'];
108+
await wallet.getAccountResources(addresses);
109+
110+
const queryStub = mockBitGo.get.returnValues[0].query;
111+
const queryArg = queryStub.firstCall.args[0];
112+
queryArg.should.deepEqual({ addresses });
113+
queryArg.should.not.have.property('tokenName');
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)