Skip to content

Commit a3f6fb1

Browse files
feat(awm): fix consolidate unspent response
1 parent 998a213 commit a3f6fb1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,69 @@ describe('POST /api/:coin/wallet/:walletId/consolidateunspents', () => {
173173
sinon.assert.calledOnce(consolidateUnspentsStub);
174174
});
175175

176+
it('should handle array result from consolidateUnspents and return first element', async () => {
177+
const walletGetNock = nock(bitgoApiUrl)
178+
.get(`/api/v2/${coin}/wallet/${walletId}`)
179+
.matchHeader('authorization', `Bearer ${accessToken}`)
180+
.reply(200, mockWalletData);
181+
182+
const keychainGetNock = nock(bitgoApiUrl)
183+
.get(`/api/v2/${coin}/key/user-key-id`)
184+
.matchHeader('authorization', `Bearer ${accessToken}`)
185+
.reply(200, mockUserKeychain);
186+
187+
const mockArrayResult = [
188+
{
189+
transfer: {
190+
entries: [
191+
{ address: 'tb1qu...', value: -4000 },
192+
{ address: 'tb1qle...', value: -4000 },
193+
{ address: 'tb1qtw...', value: 2714, isChange: true },
194+
],
195+
id: 'first-transfer-id',
196+
coin: 'tbtc',
197+
wallet: '685abbf19ca95b79f88e0b41d9337109',
198+
txid: 'first-tx-id',
199+
status: 'signed',
200+
},
201+
txid: 'first-tx-id',
202+
tx: '01000000000102first...',
203+
status: 'signed',
204+
},
205+
];
206+
207+
const consolidateUnspentsStub = sinon
208+
.stub(Wallet.prototype, 'consolidateUnspents')
209+
.resolves(mockArrayResult);
210+
211+
const requestPayload = {
212+
pubkey: mockUserKeychain.pub,
213+
source: 'user' as const,
214+
feeRate: 1000,
215+
bulk: true,
216+
};
217+
218+
const response = await agent
219+
.post(`/api/${coin}/wallet/${walletId}/consolidateunspents`)
220+
.set('Authorization', `Bearer ${accessToken}`)
221+
.send(requestPayload);
222+
223+
response.status.should.equal(200);
224+
// Should return only the first element from the array
225+
response.body.should.have.property('transfer');
226+
response.body.should.have.property('txid', 'first-tx-id');
227+
response.body.should.have.property('tx', '01000000000102first...');
228+
response.body.should.have.property('status', 'signed');
229+
response.body.transfer.should.have.property('id', 'first-transfer-id');
230+
response.body.transfer.should.have.property('txid', 'first-tx-id');
231+
response.body.transfer.should.have.property('status', 'signed');
232+
response.body.transfer.should.have.property('entries').which.is.Array();
233+
234+
walletGetNock.done();
235+
keychainGetNock.done();
236+
sinon.assert.calledOnce(consolidateUnspentsStub);
237+
});
238+
176239
it('should succeed in consolidating unspents with all optional parameters', async () => {
177240
const walletGetNock = nock(bitgoApiUrl)
178241
.get(`/api/v2/${coin}/wallet/${walletId}`)

src/masterBitgoExpress/handlers/handleConsolidateUnspents.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export async function handleConsolidateUnspents(
3838
};
3939

4040
// Send consolidate unspents
41-
const result = await wallet.consolidateUnspents(consolidationParams);
41+
let result = await wallet.consolidateUnspents(consolidationParams);
42+
43+
if (Array.isArray(result) && result.length > 0) {
44+
result = result[0];
45+
}
46+
4247
return result;
4348
} catch (error) {
4449
const err = error as Error;

0 commit comments

Comments
 (0)