@btc-vision/transaction 1.0.108 → 1.0.110
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/browser/index.js +1 -1
- package/browser/transaction/TransactionFactory.d.ts +1 -0
- package/build/transaction/TransactionFactory.d.ts +1 -0
- package/build/transaction/TransactionFactory.js +18 -5
- package/build/transaction/builders/FundingTransaction.js +3 -12
- package/package.json +1 -1
- package/src/transaction/TransactionFactory.ts +23 -6
- package/src/transaction/builders/FundingTransaction.ts +3 -12
- package/src/transaction/builders/TransactionBuilder.ts +1 -1
|
@@ -4,6 +4,7 @@ import { CustomScriptTransaction, } from './builders/CustomScriptTransaction.js'
|
|
|
4
4
|
import { DeploymentTransaction } from './builders/DeploymentTransaction.js';
|
|
5
5
|
import { FundingTransaction } from './builders/FundingTransaction.js';
|
|
6
6
|
import { InteractionTransaction } from './builders/InteractionTransaction.js';
|
|
7
|
+
import { TransactionBuilder } from './builders/TransactionBuilder.js';
|
|
7
8
|
import { UnwrapSegwitTransaction } from './builders/UnwrapSegwitTransaction.js';
|
|
8
9
|
import { UnwrapTransaction } from './builders/UnwrapTransaction.js';
|
|
9
10
|
import { WrapTransaction } from './builders/WrapTransaction.js';
|
|
@@ -24,7 +25,8 @@ export class TransactionFactory {
|
|
|
24
25
|
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
25
26
|
parameters.utxos = interactionParameters.utxos;
|
|
26
27
|
parameters.amount =
|
|
27
|
-
(await preTransaction.estimateTransactionFees()) +
|
|
28
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
29
|
+
this.getPriorityFee(interactionParameters);
|
|
28
30
|
const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
|
|
29
31
|
if (!feeEstimationFundingTransaction) {
|
|
30
32
|
throw new Error('Could not sign funding transaction.');
|
|
@@ -65,7 +67,8 @@ export class TransactionFactory {
|
|
|
65
67
|
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
66
68
|
parameters.utxos = interactionParameters.utxos;
|
|
67
69
|
parameters.amount =
|
|
68
|
-
(await preTransaction.estimateTransactionFees()) +
|
|
70
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
71
|
+
this.getPriorityFee(interactionParameters);
|
|
69
72
|
const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
|
|
70
73
|
if (!feeEstimationFundingTransaction) {
|
|
71
74
|
throw new Error('Could not sign funding transaction.');
|
|
@@ -95,6 +98,9 @@ export class TransactionFactory {
|
|
|
95
98
|
const preTransaction = new DeploymentTransaction(deploymentParameters);
|
|
96
99
|
await preTransaction.signTransaction();
|
|
97
100
|
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
101
|
+
parameters.amount =
|
|
102
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
103
|
+
this.getPriorityFee(deploymentParameters);
|
|
98
104
|
const fundingTransaction = new FundingTransaction(parameters);
|
|
99
105
|
const signedTransaction = await fundingTransaction.signTransaction();
|
|
100
106
|
if (!signedTransaction) {
|
|
@@ -115,7 +121,7 @@ export class TransactionFactory {
|
|
|
115
121
|
utxos: [newUtxo],
|
|
116
122
|
randomBytes: preTransaction.getRndBytes(),
|
|
117
123
|
nonWitnessUtxo: signedTransaction.toBuffer(),
|
|
118
|
-
optionalOutputs: []
|
|
124
|
+
optionalOutputs: [],
|
|
119
125
|
};
|
|
120
126
|
const finalTransaction = new DeploymentTransaction(newParams);
|
|
121
127
|
const outTx = await finalTransaction.signTransaction();
|
|
@@ -142,7 +148,7 @@ export class TransactionFactory {
|
|
|
142
148
|
}
|
|
143
149
|
const childTransactionRequiredValue = wrapParameters.amount +
|
|
144
150
|
currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT +
|
|
145
|
-
(wrapParameters
|
|
151
|
+
this.getPriorityFee(wrapParameters);
|
|
146
152
|
const wbtc = new wBTC(wrapParameters.network, wrapParameters.chainId);
|
|
147
153
|
const to = wbtc.getAddress();
|
|
148
154
|
const fundingParameters = {
|
|
@@ -247,7 +253,8 @@ export class TransactionFactory {
|
|
|
247
253
|
const parameters = await preTransaction.getFundingTransactionParameters();
|
|
248
254
|
parameters.utxos = fundingParameters.utxos;
|
|
249
255
|
parameters.amount =
|
|
250
|
-
(await preTransaction.estimateTransactionFees()) +
|
|
256
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
257
|
+
this.getPriorityFee(unwrapParameters);
|
|
251
258
|
const signedTransaction = await this.createFundTransaction(parameters);
|
|
252
259
|
if (!signedTransaction) {
|
|
253
260
|
throw new Error('Could not sign funding transaction.');
|
|
@@ -340,6 +347,12 @@ export class TransactionFactory {
|
|
|
340
347
|
header.writeUInt8(currentConsensus, 1);
|
|
341
348
|
return Buffer.concat([header, buf]).toString('hex');
|
|
342
349
|
}
|
|
350
|
+
getPriorityFee(params) {
|
|
351
|
+
if (params.priorityFee < TransactionBuilder.MINIMUM_DUST) {
|
|
352
|
+
return TransactionBuilder.MINIMUM_DUST;
|
|
353
|
+
}
|
|
354
|
+
return params.priorityFee;
|
|
355
|
+
}
|
|
343
356
|
getUTXOAsTransaction(tx, to, index) {
|
|
344
357
|
if (!tx.outs[index])
|
|
345
358
|
return [];
|
|
@@ -13,25 +13,16 @@ export class FundingTransaction extends TransactionBuilder {
|
|
|
13
13
|
throw new Error('Recipient address is required');
|
|
14
14
|
}
|
|
15
15
|
this.addInputsFromUTXO();
|
|
16
|
-
let amountSpent = this.amount;
|
|
17
|
-
if (this.getTransactionOPNetFee() === TransactionBuilder.MINIMUM_DUST) {
|
|
18
|
-
if (amountSpent < TransactionBuilder.MINIMUM_DUST) {
|
|
19
|
-
amountSpent += TransactionBuilder.MINIMUM_DUST;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
amountSpent += this.getTransactionOPNetFee();
|
|
24
|
-
}
|
|
25
16
|
if (this.splitInputsInto > 1) {
|
|
26
|
-
this.splitInputs(
|
|
17
|
+
this.splitInputs(this.amount);
|
|
27
18
|
}
|
|
28
19
|
else {
|
|
29
20
|
this.addOutput({
|
|
30
|
-
value: Number(
|
|
21
|
+
value: Number(this.amount),
|
|
31
22
|
address: this.to,
|
|
32
23
|
});
|
|
33
24
|
}
|
|
34
|
-
await this.addRefundOutput(
|
|
25
|
+
await this.addRefundOutput(this.amount);
|
|
35
26
|
}
|
|
36
27
|
splitInputs(amountSpent) {
|
|
37
28
|
if (!this.to) {
|
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
IDeploymentParameters,
|
|
21
21
|
IFundingTransactionParameters,
|
|
22
22
|
IInteractionParameters,
|
|
23
|
+
ITransactionParameters,
|
|
23
24
|
IUnwrapParameters,
|
|
24
25
|
IWrapParameters,
|
|
25
26
|
} from './interfaces/ITransactionParameters.js';
|
|
@@ -102,7 +103,8 @@ export class TransactionFactory {
|
|
|
102
103
|
|
|
103
104
|
parameters.utxos = interactionParameters.utxos;
|
|
104
105
|
parameters.amount =
|
|
105
|
-
(await preTransaction.estimateTransactionFees()) +
|
|
106
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
107
|
+
this.getPriorityFee(interactionParameters);
|
|
106
108
|
|
|
107
109
|
const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
|
|
108
110
|
if (!feeEstimationFundingTransaction) {
|
|
@@ -170,7 +172,8 @@ export class TransactionFactory {
|
|
|
170
172
|
|
|
171
173
|
parameters.utxos = interactionParameters.utxos;
|
|
172
174
|
parameters.amount =
|
|
173
|
-
(await preTransaction.estimateTransactionFees()) +
|
|
175
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
176
|
+
this.getPriorityFee(interactionParameters);
|
|
174
177
|
|
|
175
178
|
const feeEstimationFundingTransaction = await this.createFundTransaction({ ...parameters });
|
|
176
179
|
if (!feeEstimationFundingTransaction) {
|
|
@@ -224,7 +227,12 @@ export class TransactionFactory {
|
|
|
224
227
|
// Initial generation
|
|
225
228
|
await preTransaction.signTransaction();
|
|
226
229
|
|
|
227
|
-
const parameters: IFundingTransactionParameters =
|
|
230
|
+
const parameters: IFundingTransactionParameters =
|
|
231
|
+
await preTransaction.getFundingTransactionParameters();
|
|
232
|
+
|
|
233
|
+
parameters.amount =
|
|
234
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
235
|
+
this.getPriorityFee(deploymentParameters);
|
|
228
236
|
|
|
229
237
|
const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
|
|
230
238
|
const signedTransaction: Transaction = await fundingTransaction.signTransaction();
|
|
@@ -248,7 +256,7 @@ export class TransactionFactory {
|
|
|
248
256
|
utxos: [newUtxo],
|
|
249
257
|
randomBytes: preTransaction.getRndBytes(),
|
|
250
258
|
nonWitnessUtxo: signedTransaction.toBuffer(),
|
|
251
|
-
optionalOutputs: []
|
|
259
|
+
optionalOutputs: [],
|
|
252
260
|
};
|
|
253
261
|
|
|
254
262
|
const finalTransaction: DeploymentTransaction = new DeploymentTransaction(newParams);
|
|
@@ -291,7 +299,7 @@ export class TransactionFactory {
|
|
|
291
299
|
const childTransactionRequiredValue: bigint =
|
|
292
300
|
wrapParameters.amount +
|
|
293
301
|
currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT +
|
|
294
|
-
(wrapParameters
|
|
302
|
+
this.getPriorityFee(wrapParameters);
|
|
295
303
|
|
|
296
304
|
const wbtc: wBTC = new wBTC(wrapParameters.network, wrapParameters.chainId);
|
|
297
305
|
const to = wbtc.getAddress();
|
|
@@ -458,7 +466,8 @@ export class TransactionFactory {
|
|
|
458
466
|
|
|
459
467
|
parameters.utxos = fundingParameters.utxos;
|
|
460
468
|
parameters.amount =
|
|
461
|
-
(await preTransaction.estimateTransactionFees()) +
|
|
469
|
+
(await preTransaction.estimateTransactionFees()) +
|
|
470
|
+
this.getPriorityFee(unwrapParameters);
|
|
462
471
|
|
|
463
472
|
const signedTransaction = await this.createFundTransaction(parameters);
|
|
464
473
|
if (!signedTransaction) {
|
|
@@ -596,6 +605,14 @@ export class TransactionFactory {
|
|
|
596
605
|
return Buffer.concat([header, buf]).toString('hex');
|
|
597
606
|
}
|
|
598
607
|
|
|
608
|
+
private getPriorityFee(params: ITransactionParameters): bigint {
|
|
609
|
+
if (params.priorityFee < TransactionBuilder.MINIMUM_DUST) {
|
|
610
|
+
return TransactionBuilder.MINIMUM_DUST;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return params.priorityFee;
|
|
614
|
+
}
|
|
615
|
+
|
|
599
616
|
private getUTXOAsTransaction(tx: Transaction, to: Address, index: number): UTXO[] {
|
|
600
617
|
if (!tx.outs[index]) return [];
|
|
601
618
|
|
|
@@ -25,25 +25,16 @@ export class FundingTransaction extends TransactionBuilder<TransactionType.FUNDI
|
|
|
25
25
|
|
|
26
26
|
this.addInputsFromUTXO();
|
|
27
27
|
|
|
28
|
-
let amountSpent: bigint = this.amount;
|
|
29
|
-
if (this.getTransactionOPNetFee() === TransactionBuilder.MINIMUM_DUST) {
|
|
30
|
-
if (amountSpent < TransactionBuilder.MINIMUM_DUST) {
|
|
31
|
-
amountSpent += TransactionBuilder.MINIMUM_DUST;
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
amountSpent += this.getTransactionOPNetFee();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
28
|
if (this.splitInputsInto > 1) {
|
|
38
|
-
this.splitInputs(
|
|
29
|
+
this.splitInputs(this.amount);
|
|
39
30
|
} else {
|
|
40
31
|
this.addOutput({
|
|
41
|
-
value: Number(
|
|
32
|
+
value: Number(this.amount),
|
|
42
33
|
address: this.to,
|
|
43
34
|
});
|
|
44
35
|
}
|
|
45
36
|
|
|
46
|
-
await this.addRefundOutput(
|
|
37
|
+
await this.addRefundOutput(this.amount);
|
|
47
38
|
}
|
|
48
39
|
|
|
49
40
|
protected splitInputs(amountSpent: bigint): void {
|
|
@@ -365,7 +365,7 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
365
365
|
const fee: number = this.feeRate * size;
|
|
366
366
|
|
|
367
367
|
this.estimatedFees = BigInt(Math.ceil(fee) + 1);
|
|
368
|
-
|
|
368
|
+
|
|
369
369
|
return this.estimatedFees;
|
|
370
370
|
} else {
|
|
371
371
|
throw new Error(
|