Skip to content

Commit 459e760

Browse files
fixup! fix(wallet): getCollateral callback now passes empty array to wallet if no colateral found
1 parent 80097e4 commit 459e760

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

packages/util-dev/src/mockProviders/mockData.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,51 @@ export const utxosWithLowCoins: Cardano.Utxo[] = [
128128
]
129129
];
130130

131+
export const impureUtxos: Cardano.Utxo[] = [
132+
[
133+
{
134+
address: Cardano.PaymentAddress(
135+
'addr_test1qzs0umu0s2ammmpw0hea0w2crtcymdjvvlqngpgqy76gpfnuzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475qp3y3vz'
136+
),
137+
index: 1,
138+
txId: Cardano.TransactionId('c7c0973c6bbf1a04a9f306da7814b4fa564db649bf48b0bd93c273bd03143547')
139+
},
140+
{
141+
address: Cardano.PaymentAddress(
142+
'addr_test1qq585l3hyxgj3nas2v3xymd23vvartfhceme6gv98aaeg9muzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475q2g7k3g'
143+
),
144+
value: {
145+
assets: new Map([
146+
[AssetId.PXL, 5n],
147+
[AssetId.TSLA, 10n]
148+
]),
149+
coins: 4_027_026_465n
150+
}
151+
}
152+
],
153+
[
154+
{
155+
address: Cardano.PaymentAddress(
156+
'addr_test1qzs0umu0s2ammmpw0hea0w2crtcymdjvvlqngpgqy76gpfnuzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475qp3y3vz'
157+
),
158+
index: 2,
159+
txId: Cardano.TransactionId('c7c0973c6bbf1a04a9f306da7814b4fa564db649bf48b0bd93c273bd03143547')
160+
},
161+
{
162+
address: Cardano.PaymentAddress(
163+
'addr_test1qq585l3hyxgj3nas2v3xymd23vvartfhceme6gv98aaeg9muzcjqw982pcftgx53fu5527z2cj2tkx2h8ux2vxsg475q2g7k3g'
164+
),
165+
value: {
166+
assets: new Map([
167+
[AssetId.PXL, 5n],
168+
[AssetId.TSLA, 10n]
169+
]),
170+
coins: 4_027_026_465n
171+
}
172+
}
173+
]
174+
];
175+
131176
export const utxosWithLowCoinsAndMixedAssets: Cardano.Utxo[] = [
132177
...utxosWithLowCoins,
133178
[

packages/wallet/src/cip30.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ const getCollateralCallback = async (
230230
callback: GetCollateralCallback,
231231
logger: Logger
232232
) => {
233+
if (availableUtxos.length === 0) return null;
233234
const availableUtxosWithoutAssets = getUtxosWithoutAssets(availableUtxos);
234235
try {
235236
// Send the amount and filtered available UTxOs to the callback

packages/wallet/test/integration/cip30mapping.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const {
4646
utxo: mockUtxo,
4747
utxosWithLowCoins,
4848
utxosWithLowCoinsAndMixedAssets,
49-
sortedUtxosWithLowCoins
49+
sortedUtxosWithLowCoins,
50+
impureUtxos
5051
} = mocks;
5152

5253
type TestProviders = Required<Pick<Providers, 'txSubmitProvider' | 'networkInfoProvider'>>;
@@ -269,6 +270,7 @@ describe('cip30', () => {
269270
});
270271
});
271272

273+
// eslint-disable-next-line max-statements
272274
describe('api.getCollateral', () => {
273275
// Wallet 2
274276
let wallet2: BaseWallet;
@@ -294,6 +296,14 @@ describe('cip30', () => {
294296
let wallet7: BaseWallet;
295297
let api7: WithSenderContext<WalletApi>;
296298

299+
// Wallet 8
300+
let wallet8: BaseWallet;
301+
let api8: WithSenderContext<WalletApi>;
302+
303+
// Wallet 9
304+
let wallet9: BaseWallet;
305+
let api9: WithSenderContext<WalletApi>;
306+
297307
beforeAll(async () => {
298308
// CREATE A WALLET WITH LOW COINS UTxOs
299309
({ wallet: wallet2, api: api2 } = await createWalletAndApiWithStores(utxosWithLowCoins));
@@ -330,6 +340,24 @@ describe('cip30', () => {
330340
true,
331341
[]
332342
));
343+
344+
// WALLET WITH CALLBACK FOR GET COLLATERAL (BRAND NEW WALLET, NO UTXOS)
345+
({ wallet: wallet8, api: api8 } = await createWalletAndApiWithStores(
346+
[],
347+
providers,
348+
mockCollateralCallback,
349+
true,
350+
[]
351+
));
352+
353+
// WALLET WITH CALLBACK FOR GET COLLATERAL (ONLY IMPURE UTXOs)
354+
({ wallet: wallet9, api: api9 } = await createWalletAndApiWithStores(
355+
[],
356+
providers,
357+
mockCollateralCallback,
358+
true,
359+
impureUtxos
360+
));
333361
});
334362

335363
afterAll(() => {
@@ -387,6 +415,27 @@ describe('cip30', () => {
387415
wallet7.shutdown();
388416
});
389417

418+
it('does not execute collateral callback and returns null if brand new wallet (no UTXOS)', async () => {
419+
await expect(api8.getCollateral(context)).resolves.toBeNull();
420+
expect(mockCollateralCallback).not.toHaveBeenCalled();
421+
wallet8.shutdown();
422+
});
423+
424+
it('does executes collateral callback with empty array if wallet has only impure UTXOS', async () => {
425+
await expect(api9.getCollateral(context)).resolves.not.toBeNull();
426+
expect(mockCollateralCallback).toHaveBeenCalledWith({
427+
data: {
428+
amount: 5_000_000n,
429+
utxos: []
430+
},
431+
sender: {
432+
url: 'https://lace.io'
433+
},
434+
type: 'get_collateral'
435+
});
436+
wallet9.shutdown();
437+
});
438+
390439
it('does not execute collateral callback if not provided', async () => {
391440
await expect(api2.getCollateral(context)).rejects.toThrow(ApiError);
392441
expect(mockCollateralCallback).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)