Skip to content

Commit 0721b96

Browse files
committed
fix: fee calculations
1 parent 4a049be commit 0721b96

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

packages/sdk/src/mintlayer-connect-sdk.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,9 +1986,10 @@ class Client {
19861986

19871987
const BINRepresentation = this.getTransactionBINrepresentation(JSONRepresentation, 1);
19881988

1989-
const transaction_size = BigInt(Math.ceil(BINRepresentation.transactionsize));
1990-
const feerate = BigInt('100000000000'); // TODO: Get the current feerate from the network
1991-
const nextPreciseFee = (transaction_size * feerate) / 1000n;
1989+
const transaction_size_in_bytes = BigInt(Math.ceil(BINRepresentation.transactionsize));
1990+
const fee_amount_per_kb = BigInt('100000000000'); // TODO: Get the current feerate from the network
1991+
1992+
const nextPreciseFee = ((fee_amount_per_kb * transaction_size_in_bytes) + BigInt(999)) / BigInt(1000)
19921993

19931994
if (nextPreciseFee === preciseFee || nextPreciseFee === previousFee) {
19941995
const transaction = encode_transaction(
@@ -2072,7 +2073,7 @@ class Client {
20722073
return encode_input_for_fill_order(
20732074
input.order_id,
20742075
Amount.from_atoms(input.fill_atoms.toString()),
2075-
input.destination,
2076+
'tmt1q8vjp3lrvezlnkwhhv944f2vw2k9get4ty0fkyn3',
20762077
BigInt(input.nonce.toString()),
20772078
network,
20782079
);
@@ -2130,6 +2131,8 @@ class Client {
21302131

21312132
const inputsArray = [...inputCommands, ...inputsIds].filter((x): x is NonNullable<typeof x> => x !== undefined);
21322133

2134+
console.log('inputsArray', inputsArray);
2135+
21332136
const outputsArrayItems = transactionJSONrepresentation.outputs.map((output) => {
21342137
if (output.type === 'Transfer') {
21352138
if (output.value.type === 'TokenV1') {
@@ -2261,6 +2264,7 @@ class Client {
22612264
}
22622265
});
22632266
const outputsArray = outputsArrayItems.filter((x): x is NonNullable<typeof x> => x !== undefined);
2267+
console.log('outputsArray', outputsArray);
22642268

22652269
const inputAddresses: string[] = (transactionJSONrepresentation.inputs as UtxoInput[])
22662270
.filter(({ input }) => input.input_type === 'UTXO')
@@ -2271,6 +2275,14 @@ class Client {
22712275
// @ts-ignore
22722276
inputAddresses.push(transactionJSONrepresentation.outputs[0].destination);
22732277
}
2278+
// @ts-ignore
2279+
if (transactionJSONrepresentation.inputs[0].input.input_type === 'AccountCommand') {
2280+
// @ts-ignore
2281+
if (transactionJSONrepresentation.inputs[0].input.destination) {
2282+
// @ts-ignore
2283+
inputAddresses.push(transactionJSONrepresentation.inputs[0].input.destination);
2284+
}
2285+
}
22742286

22752287
const transactionsize = estimate_transaction_size(
22762288
mergeUint8Arrays(inputsArray),
@@ -2279,6 +2291,8 @@ class Client {
22792291
network,
22802292
);
22812293

2294+
console.log('transactionsize', transactionsize);
2295+
22822296
return {
22832297
inputs: inputsArray,
22842298
outputs: outputsArray,

packages/sdk/tests/orders.test.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ test('fill order', async () => {
234234

235235
expect(result.JSONRepresentation).toStrictEqual({
236236
"fee": {
237-
"atoms": "30100000000",
238-
"decimal": "0.301",
237+
"atoms": "40400000000",
238+
"decimal": "0.404",
239239
},
240-
"id": "4bcaf078df908c0fa7fa73419289b5c4e2ea4f89a2f9a4ef877d8a04cc8650c5",
240+
"id": "ee22e819eba9b30b3b3ecd21322b566d41973d346a922eb9a685121554ad3be1",
241241
"inputs": [
242242
{
243243
"input": {
@@ -288,8 +288,8 @@ test('fill order', async () => {
288288
"value": {
289289
"type": "Coin",
290290
"amount": {
291-
"atoms": "1702175504300000",
292-
"decimal": "17021.755043"
291+
"atoms": "1702165204300000",
292+
"decimal": "17021.652043"
293293
}
294294
},
295295
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6"
@@ -310,6 +310,9 @@ test('fill order that fail on call', async () => {
310310
});
311311

312312
const result = await spy.mock.results[0]?.value;
313+
314+
console.log(JSON.stringify(result, null, 2));
315+
313316
expect(result.JSONRepresentation.fee.decimal).toBe('0.399');
314317
});
315318

@@ -371,10 +374,10 @@ test('conclude order - snapshot', async () => {
371374

372375
expect(result.JSONRepresentation).toStrictEqual({
373376
"fee": {
374-
"atoms": "30200000000",
375-
"decimal": "0.302",
377+
"atoms": "40500000000",
378+
"decimal": "0.405",
376379
},
377-
"id": "bc6c7df6b4cb0f337b16cca4e3ed5c5105ae12c2c32d08f8d5186adbac5e5f22",
380+
"id": "a0e027bed40136528b3c5462b198a7e9781fad6659ab6c0d22b8c2a499148964",
378381
"inputs": [
379382
{
380383
"input": {
@@ -435,8 +438,8 @@ test('conclude order - snapshot', async () => {
435438
"value": {
436439
"type": "Coin",
437440
"amount": {
438-
"atoms": "1703175404300000",
439-
"decimal": "17031.754043"
441+
"atoms": "1703165104300000",
442+
"decimal": "17031.651043"
440443
}
441444
},
442445
"destination": "tmt1qxrwc3gy2lgf4kvqwwfa388vn3cavgrqyyrgswe6"

0 commit comments

Comments
 (0)