Skip to content

Commit e40f6d0

Browse files
committed
fix: error message for over available tokens to mint or melt
1 parent f99f5f3 commit e40f6d0

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

__tests__/integration/hathorwallet_facade.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,7 +1981,8 @@ describe('mintTokens', () => {
19811981
const { hash: tokenUid } = await createTokenHelper(hWallet, 'Token to Mint', 'TMINT', 100);
19821982

19831983
// Should not mint more tokens than the HTR funds allow
1984-
await expect(hWallet.mintTokens(tokenUid, 9000)).rejects.toThrow('HTR tokens');
1984+
await expect(hWallet.mintTokens(tokenUid, 9000))
1985+
.rejects.toThrow(/^Not enough HTR tokens for deposit: 90 required, \d+ available$/);
19851986

19861987
// Minting more of the tokens
19871988
const mintAmount = getRandomInt(100, 50);
@@ -2183,7 +2184,8 @@ describe('meltTokens', () => {
21832184
const { hash: tokenUid } = await createTokenHelper(hWallet, 'Token to Melt', 'TMELT', 500);
21842185

21852186
// Should not melt more than there is available
2186-
await expect(hWallet.meltTokens(tokenUid, 999)).rejects.toThrow('Not enough tokens to melt');
2187+
await expect(hWallet.meltTokens(tokenUid, 999))
2188+
.rejects.toThrow('Not enough tokens to melt: 999 requested, 500 available');
21872189

21882190
// Melting some tokens
21892191
const meltAmount = getRandomInt(99, 10);

__tests__/new/hathorwallet.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,40 @@ describe('prepare transactions without signature', () => {
10661066
);
10671067
});
10681068

1069+
test('prepareMintTokensData with over available tokens amount', async () => {
1070+
const amountAvailable = 1;
1071+
const amountOverAvailable = 1000;
1072+
// fake stuff to support the test
1073+
const fakeMintAuthority = [
1074+
{
1075+
txId: '002abde4018935e1bbde9600ef79c637adf42385fb1816ec284d702b7bb9ef5f',
1076+
index: 0,
1077+
value: 1,
1078+
token: '01',
1079+
address: fakeAddress.base58,
1080+
authorities: TOKEN_MINT_MASK,
1081+
timelock: null,
1082+
locked: false,
1083+
},
1084+
];
1085+
1086+
// wallet and mocks
1087+
const hWallet = new FakeHathorWallet();
1088+
hWallet.storage = getStorage({
1089+
readOnly: false,
1090+
currentAddress: fakeAddress.base58,
1091+
selectUtxos: generateSelectUtxos({ ...fakeTokenToDepositUtxo, value: amountAvailable }),
1092+
});
1093+
jest.spyOn(hWallet, 'getMintAuthority').mockReturnValue(fakeMintAuthority);
1094+
1095+
// prepare mint
1096+
await expect(hWallet.prepareMintTokensData('01', amountOverAvailable, {
1097+
address: fakeAddress.base58,
1098+
pinCode: '1234',
1099+
signTx: false, // skip the signature
1100+
})).rejects.toThrow('Not enough HTR tokens for deposit: 10 required, 1 available');
1101+
});
1102+
10691103
test('prepareMeltTokensData', async () => {
10701104
// fake stuff to support the test
10711105
const fakeTokenToMeltUtxo = {
@@ -1279,6 +1313,50 @@ describe('prepare transactions without signature', () => {
12791313
])
12801314
);
12811315
});
1316+
1317+
test('prepareMeltTokensData with over available tokens amount', async () => {
1318+
const availableToken = 10;
1319+
const amountOverAvailable = 100;
1320+
// fake stuff to support the test
1321+
const fakeTokenToMeltUtxo = {
1322+
txId: '002abde4018935e1bbde9600ef79c637adf42385fb1816ec284d702b7bb9ef5f',
1323+
index: 0,
1324+
value: availableToken,
1325+
token: '01',
1326+
address: fakeAddress.base58,
1327+
authorities: 0,
1328+
timelock: null,
1329+
locked: false,
1330+
};
1331+
const fakeMeltAuthority = [
1332+
{
1333+
txId: '002abde4018935e1bbde9600ef79c637adf42385fb1816ec284d702b7bb9ef5f',
1334+
index: 0,
1335+
value: 1,
1336+
token: '01',
1337+
address: fakeAddress.base58,
1338+
authorities: TOKEN_MELT_MASK,
1339+
timelock: null,
1340+
locked: false,
1341+
},
1342+
];
1343+
1344+
// wallet and mocks
1345+
const hWallet = new FakeHathorWallet();
1346+
hWallet.storage = getStorage({
1347+
readOnly: false,
1348+
currentAddress: fakeAddress.base58,
1349+
selectUtxos: generateSelectUtxos(fakeTokenToMeltUtxo),
1350+
});
1351+
jest.spyOn(hWallet, 'getMeltAuthority').mockReturnValue(fakeMeltAuthority);
1352+
1353+
// prepare melt
1354+
await expect(hWallet.prepareMeltTokensData('01', amountOverAvailable, {
1355+
address: fakeAddress.base58,
1356+
pinCode: '1234',
1357+
signTx: false, // skip the signature
1358+
})).rejects.toThrow('Not enough tokens to melt: 100 requested, 10 available');
1359+
});
12821360
});
12831361

12841362
test('setExternalTxSigningMethod', async () => {

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ export type UtxoSelectionAlgorithm = (
362362
storage: IStorage,
363363
token: string,
364364
amount: OutputValueType
365-
) => Promise<{ utxos: IUtxo[]; amount: OutputValueType }>;
365+
) => Promise<{ utxos: IUtxo[]; amount: OutputValueType; available?: OutputValueType }>;
366366

367367
export interface IUtxoSelectionOptions {
368368
token?: string;

src/utils/tokens.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ const tokens = {
360360
}
361361

362362
if (foundAmount < depositAmount) {
363+
const availableAmount = selectedUtxos.available;
363364
throw new InsufficientFundsError(
364-
`Not enough HTR tokens for deposit: ${depositAmount} required, ${foundAmount} available`
365+
`Not enough HTR tokens for deposit: ${depositAmount} required, ${availableAmount} available`
365366
);
366367
}
367368

@@ -510,8 +511,9 @@ const tokens = {
510511
}
511512

512513
if (foundAmount < amount) {
514+
const availableAmount = selectedUtxos.available;
513515
throw new InsufficientFundsError(
514-
`Not enough tokens to melt: ${amount} requested, ${foundAmount} available`
516+
`Not enough tokens to melt: ${amount} requested, ${availableAmount} available`
515517
);
516518
}
517519

src/utils/utxo.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function fastUtxoSelection(
4949
storage: IStorage,
5050
token: string,
5151
amount: OutputValueType
52-
): Promise<{ utxos: IUtxo[]; amount: OutputValueType }> {
52+
): Promise<{ utxos: IUtxo[]; amount: OutputValueType; available?: OutputValueType }> {
5353
const utxos: IUtxo[] = [];
5454
let utxosAmount = 0;
5555

@@ -72,6 +72,7 @@ export async function fastUtxoSelection(
7272
return {
7373
utxos: [],
7474
amount: 0,
75+
available: utxosAmount,
7576
};
7677
}
7778

@@ -93,7 +94,7 @@ export async function bestUtxoSelection(
9394
storage: IStorage,
9495
token: string,
9596
amount: OutputValueType
96-
): Promise<{ utxos: IUtxo[]; amount: OutputValueType }> {
97+
): Promise<{ utxos: IUtxo[]; amount: OutputValueType; available?: OutputValueType }> {
9798
const utxos: IUtxo[] = [];
9899
let utxosAmount = 0;
99100
let selectedUtxo: IUtxo | null = null;
@@ -150,6 +151,7 @@ export async function bestUtxoSelection(
150151
return {
151152
utxos: [],
152153
amount: 0,
154+
available: utxosAmount,
153155
};
154156
}
155157
// We need to ensure we use the smallest number of utxos and avoid hitting the maximum number of inputs

0 commit comments

Comments
 (0)