From bd6f975acde6e5d3ddd11a1b130afdcb87f2ea7f Mon Sep 17 00:00:00 2001 From: Sergey Chystiakov Date: Wed, 29 Oct 2025 21:06:54 +0100 Subject: [PATCH 1/2] fix: transaction decimal --- packages/sdk/tests/transfer.test.ts | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/packages/sdk/tests/transfer.test.ts b/packages/sdk/tests/transfer.test.ts index b35098f..f3db332 100644 --- a/packages/sdk/tests/transfer.test.ts +++ b/packages/sdk/tests/transfer.test.ts @@ -252,3 +252,37 @@ test('buildTransaction for transfer, decimal test 4', async () => { expect(result).toMatchSnapshot(); }); + +test('buildTransaction for transfer, decimal test 5', async () => { + const client = await Client.create({ network: 'testnet', autoRestore: false }); + + const spy = jest.spyOn(Client.prototype as any, 'buildTransaction'); + + await client.connect(); + + await client.transfer({ + to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc', + amount: 0.0000001, + }); + + const result = await spy.mock.results[0]?.value; + + expect(result).toMatchSnapshot(); +}); + +test('buildTransaction for transfer, decimal test 6', async () => { + const client = await Client.create({ network: 'testnet', autoRestore: false }); + + const spy = jest.spyOn(Client.prototype as any, 'buildTransaction'); + + await client.connect(); + + await client.transfer({ + to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc', + amount: 0.00000000003, + }); + + const result = await spy.mock.results[0]?.value; + + expect(result).toMatchSnapshot(); +}); From 067dad54c1baad7b73de232f78283ee9fd2220e3 Mon Sep 17 00:00:00 2001 From: Sergey Chystiakov Date: Fri, 31 Oct 2025 14:37:49 +0100 Subject: [PATCH 2/2] fix: decimals trailing zeroes --- packages/sdk/src/mintlayer-connect-sdk.ts | 21 +- .../tests/__snapshots__/transfer.test.ts.snap | 340 ++++++++++++++++++ packages/sdk/tests/transfer.test.ts | 2 + 3 files changed, 357 insertions(+), 6 deletions(-) diff --git a/packages/sdk/src/mintlayer-connect-sdk.ts b/packages/sdk/src/mintlayer-connect-sdk.ts index 918119b..98e38b0 100644 --- a/packages/sdk/src/mintlayer-connect-sdk.ts +++ b/packages/sdk/src/mintlayer-connect-sdk.ts @@ -110,12 +110,21 @@ export function atomsToDecimal(atoms: string | number, decimals: number): string } export function decimalsToAtoms(value: string | number, decimals: number): bigint { - const [intPart, fracPart = ""] = value.toString().split("."); + const v = Number(value); + const [intPart, fracPart = ""] = v.toFixed(decimals).split("."); const paddedFrac = (fracPart + "0".repeat(decimals)).slice(0, decimals); const full = intPart + paddedFrac; return BigInt(full); } +export function decimals(value: string | number, decimals: number): string { + const v = Number(value); + if (isNaN(v)) return '0'; + return v + .toFixed(decimals) + .replace(/\.?0+$/, ''); +} + type Address = { [key: string]: { receiving: string[]; @@ -1547,13 +1556,13 @@ class Client { ...(token_details ? { amount: { - decimal: params.amount!.toString(), + decimal: decimals(params.amount, token_details.number_of_decimals).toString(), atoms: decimalsToAtoms(params.amount!, token_details.number_of_decimals).toString(), }, } : { amount: { - decimal: params.amount!.toString(), + decimal: decimals(params.amount, 11).toString(), atoms: decimalsToAtoms(params.amount!, 11).toString(), }, }), @@ -1584,7 +1593,7 @@ class Client { token_id, }), amount: { - decimal: params.amount!.toString(), + decimal: decimals(params.amount, 11).toString(), atoms: decimalsToAtoms(params.amount!, 11).toString(), }, }, @@ -1603,7 +1612,7 @@ class Client { type: 'Fixed', amount: { atoms: decimalsToAtoms(params.supply_amount!, params.number_of_decimals!).toString(), - decimal: params.supply_amount!.toString(), + decimal: decimals(params.supply_amount!, params.number_of_decimals!).toString(), }, }; } else { @@ -1669,7 +1678,7 @@ class Client { if (type === 'MintToken') { const amount = { atoms: decimalsToAtoms(params.amount!, params.token_details!.number_of_decimals).toString(), - decimal: params.amount!.toString(), + decimal: decimals(params.amount!, params.token_details!.number_of_decimals).toString(), }; inputs.push({ diff --git a/packages/sdk/tests/__snapshots__/transfer.test.ts.snap b/packages/sdk/tests/__snapshots__/transfer.test.ts.snap index 4906484..7ad91f5 100644 --- a/packages/sdk/tests/__snapshots__/transfer.test.ts.snap +++ b/packages/sdk/tests/__snapshots__/transfer.test.ts.snap @@ -849,3 +849,343 @@ exports[`buildTransaction for transfer, decimal test 4 1`] = ` "transaction_id": "95d91b8b3c9cd5973e85a397a86a803ab2b8ba8b8ad1b875c85b5c8ae7e4fc7d", } `; + +exports[`buildTransaction for transfer, decimal test 5 1`] = ` +{ + "BINRepresentation": { + "inputs": [ + Uint8Array [ + 0, + 0, + 175, + 59, + 95, + 173, + 32, + 246, + 249, + 126, + 178, + 16, + 147, + 78, + 148, + 33, + 118, + 247, + 247, + 208, + 247, + 4, + 35, + 89, + 6, + 89, + 238, + 14, + 2, + 23, + 5, + 58, + 124, + 171, + 1, + 0, + 0, + 0, + ], + ], + "outputs": [ + Uint8Array [ + 0, + 0, + 7, + 0, + 16, + 165, + 212, + 232, + 1, + 118, + 148, + 121, + 186, + 231, + 213, + 53, + 208, + 151, + 222, + 253, + 255, + 15, + 78, + 113, + 1, + 102, + 157, + 74, + 25, + ], + Uint8Array [ + 0, + 0, + 15, + 224, + 246, + 133, + 212, + 32, + 12, + 6, + 1, + 134, + 236, + 69, + 4, + 87, + 208, + 154, + 217, + 128, + 115, + 147, + 216, + 156, + 236, + 156, + 113, + 214, + 32, + 96, + 33, + ], + ], + "transactionsize": 206, + }, + "HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab01000000080000070010a5d4e801769479bae7d535d097defdff0f4e7101669d4a1900000fe0f685d4200c060186ec450457d09ad9807393d89cec9c71d6206021", + "JSONRepresentation": { + "fee": { + "atoms": "20600000000", + "decimal": "0.206", + }, + "id": "95d91b8b3c9cd5973e85a397a86a803ab2b8ba8b8ad1b875c85b5c8ae7e4fc7d", + "inputs": [ + { + "input": { + "index": 1, + "input_type": "UTXO", + "source_id": "af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab", + "source_type": "Transaction", + }, + "utxo": { + "destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6", + "type": "Transfer", + "value": { + "amount": { + "atoms": "1703205604300000", + "decimal": "17032.056043", + }, + "type": "Coin", + }, + }, + }, + ], + "outputs": [ + { + "destination": "tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc", + "type": "Transfer", + "value": { + "amount": { + "atoms": "1000000000000", + "decimal": "10", + }, + "type": "Coin", + }, + }, + { + "destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6", + "type": "Transfer", + "value": { + "amount": { + "atoms": "1702185004300000", + "decimal": "17021.850043", + }, + "type": "Coin", + }, + }, + ], + }, + "transaction_id": "95d91b8b3c9cd5973e85a397a86a803ab2b8ba8b8ad1b875c85b5c8ae7e4fc7d", +} +`; + +exports[`buildTransaction for transfer, decimal test 6 1`] = ` +{ + "BINRepresentation": { + "inputs": [ + Uint8Array [ + 0, + 0, + 175, + 59, + 95, + 173, + 32, + 246, + 249, + 126, + 178, + 16, + 147, + 78, + 148, + 33, + 118, + 247, + 247, + 208, + 247, + 4, + 35, + 89, + 6, + 89, + 238, + 14, + 2, + 23, + 5, + 58, + 124, + 171, + 1, + 0, + 0, + 0, + ], + ], + "outputs": [ + Uint8Array [ + 0, + 0, + 7, + 0, + 16, + 165, + 212, + 232, + 1, + 118, + 148, + 121, + 186, + 231, + 213, + 53, + 208, + 151, + 222, + 253, + 255, + 15, + 78, + 113, + 1, + 102, + 157, + 74, + 25, + ], + Uint8Array [ + 0, + 0, + 15, + 224, + 246, + 133, + 212, + 32, + 12, + 6, + 1, + 134, + 236, + 69, + 4, + 87, + 208, + 154, + 217, + 128, + 115, + 147, + 216, + 156, + 236, + 156, + 113, + 214, + 32, + 96, + 33, + ], + ], + "transactionsize": 206, + }, + "HEXRepresentation_unsigned": "0100040000af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab01000000080000070010a5d4e801769479bae7d535d097defdff0f4e7101669d4a1900000fe0f685d4200c060186ec450457d09ad9807393d89cec9c71d6206021", + "JSONRepresentation": { + "fee": { + "atoms": "20600000000", + "decimal": "0.206", + }, + "id": "95d91b8b3c9cd5973e85a397a86a803ab2b8ba8b8ad1b875c85b5c8ae7e4fc7d", + "inputs": [ + { + "input": { + "index": 1, + "input_type": "UTXO", + "source_id": "af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab", + "source_type": "Transaction", + }, + "utxo": { + "destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6", + "type": "Transfer", + "value": { + "amount": { + "atoms": "1703205604300000", + "decimal": "17032.056043", + }, + "type": "Coin", + }, + }, + }, + ], + "outputs": [ + { + "destination": "tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc", + "type": "Transfer", + "value": { + "amount": { + "atoms": "1000000000000", + "decimal": "10", + }, + "type": "Coin", + }, + }, + { + "destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6", + "type": "Transfer", + "value": { + "amount": { + "atoms": "1702185004300000", + "decimal": "17021.850043", + }, + "type": "Coin", + }, + }, + ], + }, + "transaction_id": "95d91b8b3c9cd5973e85a397a86a803ab2b8ba8b8ad1b875c85b5c8ae7e4fc7d", +} +`; diff --git a/packages/sdk/tests/transfer.test.ts b/packages/sdk/tests/transfer.test.ts index f3db332..104f9de 100644 --- a/packages/sdk/tests/transfer.test.ts +++ b/packages/sdk/tests/transfer.test.ts @@ -284,5 +284,7 @@ test('buildTransaction for transfer, decimal test 6', async () => { const result = await spy.mock.results[0]?.value; + console.log('result', JSON.stringify(result.JSONRepresentation.outputs[0], null, 2)); + expect(result).toMatchSnapshot(); });