@hardkas/tx-builder 0.5.5-alpha → 0.6.1-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +30 -8
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -193,16 +193,29 @@ function buildPaymentPlan(request) {
|
|
|
193
193
|
if (request.outputs.length === 0) {
|
|
194
194
|
throw new Error("At least one transaction output is required.");
|
|
195
195
|
}
|
|
196
|
-
const
|
|
196
|
+
const sortedOutputs = [...request.outputs].sort((a, b) => {
|
|
197
|
+
if (a.amountSompi < b.amountSompi) return -1;
|
|
198
|
+
if (a.amountSompi > b.amountSompi) return 1;
|
|
199
|
+
if (a.address < b.address) return -1;
|
|
200
|
+
if (a.address > b.address) return 1;
|
|
201
|
+
return 0;
|
|
202
|
+
});
|
|
203
|
+
const target = sortedOutputs.reduce(
|
|
197
204
|
(sum, output) => sum + output.amountSompi,
|
|
198
205
|
0n
|
|
199
206
|
);
|
|
200
207
|
if (target <= 0n) {
|
|
201
208
|
throw new Error("Transaction amount must be positive.");
|
|
202
209
|
}
|
|
203
|
-
const sortedUtxos = [...request.availableUtxos].sort(
|
|
204
|
-
(a
|
|
205
|
-
|
|
210
|
+
const sortedUtxos = [...request.availableUtxos].sort((a, b) => {
|
|
211
|
+
if (a.amountSompi < b.amountSompi) return -1;
|
|
212
|
+
if (a.amountSompi > b.amountSompi) return 1;
|
|
213
|
+
if (a.outpoint.transactionId < b.outpoint.transactionId) return -1;
|
|
214
|
+
if (a.outpoint.transactionId > b.outpoint.transactionId) return 1;
|
|
215
|
+
if (a.outpoint.index < b.outpoint.index) return -1;
|
|
216
|
+
if (a.outpoint.index > b.outpoint.index) return 1;
|
|
217
|
+
return 0;
|
|
218
|
+
});
|
|
206
219
|
const selected = [];
|
|
207
220
|
let selectedAmount = 0n;
|
|
208
221
|
for (const utxo of sortedUtxos) {
|
|
@@ -211,7 +224,7 @@ function buildPaymentPlan(request) {
|
|
|
211
224
|
if (selectedAmount < target) continue;
|
|
212
225
|
const result = estimateTransactionMass({
|
|
213
226
|
inputCount: selected.length,
|
|
214
|
-
outputs:
|
|
227
|
+
outputs: sortedOutputs,
|
|
215
228
|
payloadBytes: request.payloadBytes ?? 0,
|
|
216
229
|
hasChange: true
|
|
217
230
|
// Optimistic assumption for the loop
|
|
@@ -226,7 +239,7 @@ function buildPaymentPlan(request) {
|
|
|
226
239
|
if (!hasActualChange) {
|
|
227
240
|
const noChangeResult = estimateTransactionMass({
|
|
228
241
|
inputCount: selected.length,
|
|
229
|
-
outputs:
|
|
242
|
+
outputs: sortedOutputs,
|
|
230
243
|
payloadBytes: request.payloadBytes ?? 0,
|
|
231
244
|
hasChange: false
|
|
232
245
|
});
|
|
@@ -234,9 +247,18 @@ function buildPaymentPlan(request) {
|
|
|
234
247
|
finalFee = finalMass * request.feeRateSompiPerMass;
|
|
235
248
|
if (selectedAmount < target + finalFee) continue;
|
|
236
249
|
}
|
|
250
|
+
const canonicalSelected = [...selected].sort((a, b) => {
|
|
251
|
+
if (a.amountSompi < b.amountSompi) return -1;
|
|
252
|
+
if (a.amountSompi > b.amountSompi) return 1;
|
|
253
|
+
if (a.outpoint.transactionId < b.outpoint.transactionId) return -1;
|
|
254
|
+
if (a.outpoint.transactionId > b.outpoint.transactionId) return 1;
|
|
255
|
+
if (a.outpoint.index < b.outpoint.index) return -1;
|
|
256
|
+
if (a.outpoint.index > b.outpoint.index) return 1;
|
|
257
|
+
return 0;
|
|
258
|
+
});
|
|
237
259
|
return {
|
|
238
|
-
inputs:
|
|
239
|
-
outputs:
|
|
260
|
+
inputs: canonicalSelected,
|
|
261
|
+
outputs: sortedOutputs,
|
|
240
262
|
change: hasActualChange ? {
|
|
241
263
|
address: request.changeAddress ?? request.fromAddress,
|
|
242
264
|
amountSompi: changeAmount
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hardkas/tx-builder",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1-alpha",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
".": "./dist/index.js"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@hardkas/core": "0.
|
|
11
|
+
"@hardkas/core": "0.6.1-alpha"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"tsup": "^8.3.5",
|