Skip to content

Commit da4c125

Browse files
feat(awm): fix comments
1 parent a3f6fb1 commit da4c125

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/__tests__/api/master/consolidateUnspents.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,58 @@ describe('POST /api/:coin/wallet/:walletId/consolidateunspents', () => {
236236
sinon.assert.calledOnce(consolidateUnspentsStub);
237237
});
238238

239+
it('should fail when consolidateUnspents returns array with more than one element', async () => {
240+
const walletGetNock = nock(bitgoApiUrl)
241+
.get(`/api/v2/${coin}/wallet/${walletId}`)
242+
.matchHeader('authorization', `Bearer ${accessToken}`)
243+
.reply(200, mockWalletData);
244+
245+
const keychainGetNock = nock(bitgoApiUrl)
246+
.get(`/api/v2/${coin}/key/user-key-id`)
247+
.matchHeader('authorization', `Bearer ${accessToken}`)
248+
.reply(200, mockUserKeychain);
249+
250+
const mockArrayResult = [
251+
{
252+
txid: 'first-tx-id',
253+
tx: '01000000000102first...',
254+
status: 'signed',
255+
},
256+
{
257+
txid: 'second-tx-id',
258+
tx: '01000000000102second...',
259+
status: 'signed',
260+
},
261+
];
262+
263+
const consolidateUnspentsStub = sinon
264+
.stub(Wallet.prototype, 'consolidateUnspents')
265+
.resolves(mockArrayResult);
266+
267+
const requestPayload = {
268+
pubkey: mockUserKeychain.pub,
269+
source: 'user' as const,
270+
feeRate: 1000,
271+
};
272+
273+
const response = await agent
274+
.post(`/api/${coin}/wallet/${walletId}/consolidateunspents`)
275+
.set('Authorization', `Bearer ${accessToken}`)
276+
.send(requestPayload);
277+
278+
response.status.should.equal(500);
279+
response.body.should.have.property('error', 'Internal Server Error');
280+
response.body.should.have.property('name', 'Error');
281+
response.body.should.have.property(
282+
'details',
283+
'Expected single consolidation result, but received 2 results',
284+
);
285+
286+
walletGetNock.done();
287+
keychainGetNock.done();
288+
sinon.assert.calledOnce(consolidateUnspentsStub);
289+
});
290+
239291
it('should succeed in consolidating unspents with all optional parameters', async () => {
240292
const walletGetNock = nock(bitgoApiUrl)
241293
.get(`/api/v2/${coin}/wallet/${walletId}`)

src/masterBitgoExpress/handlers/handleConsolidateUnspents.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ export async function handleConsolidateUnspents(
4040
// Send consolidate unspents
4141
let result = await wallet.consolidateUnspents(consolidationParams);
4242

43-
if (Array.isArray(result) && result.length > 0) {
44-
result = result[0];
43+
if (Array.isArray(result)) {
44+
if (result.length === 1) {
45+
result = result[0];
46+
} else if (result.length > 1) {
47+
throw new Error(
48+
`Expected single consolidation result, but received ${result.length} results`,
49+
);
50+
}
4551
}
4652

4753
return result;

0 commit comments

Comments
 (0)