Skip to content

Commit 215fa80

Browse files
Copilotbhavidhingra
andcommitted
Move getAccountResources to BaseCoin and rename tokenName to assetName
Co-authored-by: bhavidhingra <17147510+bhavidhingra@users.noreply.github.com>
1 parent 3fc282c commit 215fa80

5 files changed

Lines changed: 71 additions & 56 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,29 @@ export abstract class BaseCoin implements IBaseCoin {
390390
return false;
391391
}
392392

393+
/**
394+
* Get account resources for the given addresses
395+
* @param addresses - array of address strings
396+
* @param assetName - optional asset name
397+
* @returns {Promise<any>} - response from WP API
398+
*/
399+
async getAccountResources(addresses: string[], assetName?: string): Promise<any> {
400+
if (!Array.isArray(addresses)) {
401+
throw new Error('addresses must be an array');
402+
}
403+
404+
if (addresses.length === 0) {
405+
throw new Error('addresses array cannot be empty');
406+
}
407+
408+
const query: any = { addresses };
409+
if (assetName) {
410+
query.assetName = assetName;
411+
}
412+
413+
return this.bitgo.get(this.url('/accountResources')).query(query).result();
414+
}
415+
393416
/**
394417
* Check whether a coin supports signing of Typed data
395418
* @returns {boolean}

modules/sdk-core/src/bitgo/baseCoin/iBaseCoin.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,4 +654,12 @@ export interface IBaseCoin {
654654
*/
655655
assertIsValidKey({ publicKey, encryptedPrv, walletPassphrase, multiSigType }: AuditKeyParams): void;
656656
requiresWalletInitializationTransaction(): boolean;
657+
658+
/**
659+
* Get account resources for the given addresses
660+
* @param addresses - array of address strings
661+
* @param assetName - optional asset name
662+
* @returns {Promise<any>} - response from WP API
663+
*/
664+
getAccountResources(addresses: string[], assetName?: string): Promise<any>;
657665
}

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

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

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

549544
export interface CreateAddressOptions {
@@ -993,7 +988,6 @@ export interface IWallet {
993988
getAddress(params?: GetAddressOptions): Promise<any>;
994989
createAddress(params?: CreateAddressOptions): Promise<any>;
995990
updateAddress(params?: UpdateAddressOptions): Promise<any>;
996-
getAccountResources(addresses: string[], tokenName?: string): Promise<any>;
997991
listWebhooks(params?: PaginationOptions): Promise<any>;
998992
simulateWebhook(params?: SimulateWebhookOptions): Promise<any>;
999993
addWebhook(params?: ModifyWebhookOptions): Promise<any>;

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,29 +1465,6 @@ 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-
14911468
async updateWalletBuildDefaults(params: UpdateBuildDefaultOptions): Promise<unknown> {
14921469
common.validateParams(params, [], ['minFeeRate', 'changeAddressType', 'txFormat']);
14931470
return this.updateWallet({

modules/sdk-core/test/unit/bitgo/wallet/getAccountResources.ts renamed to modules/sdk-core/test/unit/bitgo/baseCoin/getAccountResources.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
import * as assert from 'assert';
22
import * as sinon from 'sinon';
33
import 'should';
4-
import { Wallet } from '../../../../src/bitgo/wallet/wallet';
5-
6-
describe('Wallet - getAccountResources', function () {
7-
let wallet: Wallet;
4+
import { BaseCoin } from '../../../../src/bitgo/baseCoin/baseCoin';
5+
import { BitGoBase } from '../../../../src/bitgo/bitgoBase';
6+
7+
// Create a concrete test implementation of BaseCoin
8+
class TestBaseCoin extends BaseCoin {
9+
getChain(): string {
10+
return 'test';
11+
}
12+
getFamily(): string {
13+
return 'test';
14+
}
15+
getFullName(): string {
16+
return 'Test Coin';
17+
}
18+
getBaseFactor(): string | number {
19+
return 1e8;
20+
}
21+
isValidAddress(address: string): boolean {
22+
return true;
23+
}
24+
static createInstance(bitgo: BitGoBase): BaseCoin {
25+
return new TestBaseCoin(bitgo);
26+
}
27+
}
28+
29+
describe('BaseCoin - getAccountResources', function () {
30+
let baseCoin: BaseCoin;
831
let mockBitGo: any;
9-
let mockBaseCoin: any;
10-
let mockWalletData: any;
1132

1233
beforeEach(function () {
1334
mockBitGo = {
1435
get: sinon.stub(),
36+
url: sinon.stub().returns('/api/v2/'),
1537
};
1638

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);
39+
baseCoin = TestBaseCoin.createInstance(mockBitGo);
2740
});
2841

2942
afterEach(function () {
@@ -46,18 +59,18 @@ describe('Wallet - getAccountResources', function () {
4659
});
4760

4861
const addresses = ['address1', 'address2'];
49-
const result = await wallet.getAccountResources(addresses);
62+
const result = await baseCoin.getAccountResources(addresses);
5063

5164
result.should.deepEqual(mockResponse);
5265
mockBitGo.get.should.have.been.calledOnce;
5366
const queryStub = mockBitGo.get.returnValues[0].query;
5467
queryStub.should.have.been.calledWith({ addresses });
5568
});
5669

57-
it('should call WP API with addresses and tokenName parameters', async function () {
70+
it('should call WP API with addresses and assetName parameters', async function () {
5871
const mockResponse = {
5972
resources: [
60-
{ address: 'address1', balance: 100, token: 'USDT' },
73+
{ address: 'address1', balance: 100, asset: 'USDT' },
6174
],
6275
};
6376

@@ -68,18 +81,18 @@ describe('Wallet - getAccountResources', function () {
6881
});
6982

7083
const addresses = ['address1'];
71-
const tokenName = 'USDT';
72-
const result = await wallet.getAccountResources(addresses, tokenName);
84+
const assetName = 'USDT';
85+
const result = await baseCoin.getAccountResources(addresses, assetName);
7386

7487
result.should.deepEqual(mockResponse);
7588
mockBitGo.get.should.have.been.calledOnce;
7689
const queryStub = mockBitGo.get.returnValues[0].query;
77-
queryStub.should.have.been.calledWith({ addresses, tokenName });
90+
queryStub.should.have.been.calledWith({ addresses, assetName });
7891
});
7992

8093
it('should throw error if addresses is not an array', async function () {
8194
try {
82-
await wallet.getAccountResources('not-an-array' as any);
95+
await baseCoin.getAccountResources('not-an-array' as any);
8396
assert.fail('Should have thrown error');
8497
} catch (error) {
8598
error.message.should.equal('addresses must be an array');
@@ -88,14 +101,14 @@ describe('Wallet - getAccountResources', function () {
88101

89102
it('should throw error if addresses array is empty', async function () {
90103
try {
91-
await wallet.getAccountResources([]);
104+
await baseCoin.getAccountResources([]);
92105
assert.fail('Should have thrown error');
93106
} catch (error) {
94107
error.message.should.equal('addresses array cannot be empty');
95108
}
96109
});
97110

98-
it('should not include tokenName in query if not provided', async function () {
111+
it('should not include assetName in query if not provided', async function () {
99112
const mockResponse = { resources: [] };
100113

101114
mockBitGo.get.returns({
@@ -105,12 +118,12 @@ describe('Wallet - getAccountResources', function () {
105118
});
106119

107120
const addresses = ['address1'];
108-
await wallet.getAccountResources(addresses);
121+
await baseCoin.getAccountResources(addresses);
109122

110123
const queryStub = mockBitGo.get.returnValues[0].query;
111124
const queryArg = queryStub.firstCall.args[0];
112125
queryArg.should.deepEqual({ addresses });
113-
queryArg.should.not.have.property('tokenName');
126+
queryArg.should.not.have.property('assetName');
114127
});
115128
});
116129
});

0 commit comments

Comments
 (0)