@btc-vision/transaction 1.0.105 → 1.0.106
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/builders/TransactionBuilder.d.ts +2 -0
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +9 -8
- package/browser/utxo/OPNetLimitedProvider.d.ts +11 -1
- package/browser/utxo/interfaces/IUTXO.d.ts +2 -1
- package/build/transaction/TransactionFactory.js +2 -1
- package/build/transaction/builders/TransactionBuilder.d.ts +2 -0
- package/build/transaction/builders/TransactionBuilder.js +13 -1
- package/build/transaction/interfaces/ITransactionParameters.d.ts +9 -8
- package/build/utxo/OPNetLimitedProvider.d.ts +11 -1
- package/build/utxo/OPNetLimitedProvider.js +49 -2
- package/build/utxo/interfaces/IUTXO.d.ts +2 -1
- package/eslint.config.js +0 -1
- package/package.json +2 -2
- package/src/transaction/TransactionFactory.ts +3 -3
- package/src/transaction/builders/TransactionBuilder.ts +24 -3
- package/src/transaction/interfaces/ITransactionParameters.ts +11 -9
- package/src/utxo/OPNetLimitedProvider.ts +86 -4
- package/src/utxo/interfaces/IUTXO.ts +2 -1
|
@@ -27,6 +27,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
|
|
|
27
27
|
protected to: Address | undefined;
|
|
28
28
|
protected from: Address;
|
|
29
29
|
protected _maximumFeeRate: number;
|
|
30
|
+
optionalOutputs: PsbtOutputExtended[] | undefined;
|
|
30
31
|
protected constructor(parameters: ITransactionParameters);
|
|
31
32
|
static getFrom(from: string | undefined, keypair: ECPairInterface, network: Network): Address;
|
|
32
33
|
static witnessStackToScriptWitness(witness: Buffer[]): Buffer;
|
|
@@ -50,6 +51,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
|
|
|
50
51
|
protected getTransactionOPNetFee(): bigint;
|
|
51
52
|
protected calculateTotalUTXOAmount(): bigint;
|
|
52
53
|
protected calculateTotalVOutAmount(): bigint;
|
|
54
|
+
protected addOptionalOutputsAndGetAmount(): bigint;
|
|
53
55
|
protected addInputsFromUTXO(): void;
|
|
54
56
|
protected internalInit(): void;
|
|
55
57
|
protected abstract buildTransaction(): Promise<void>;
|
|
@@ -4,12 +4,14 @@ import { WrappedGeneration } from '../../wbtc/WrappedGenerationParameters.js';
|
|
|
4
4
|
import { ITweakedTransactionData } from '../shared/TweakedTransaction.js';
|
|
5
5
|
import { VaultUTXOs } from '../processor/PsbtTransaction.js';
|
|
6
6
|
import { ChainId } from '../../network/ChainId.js';
|
|
7
|
+
import { PsbtOutputExtended } from './Tap.js';
|
|
7
8
|
export interface ITransactionParameters extends ITweakedTransactionData {
|
|
8
9
|
readonly from?: Address;
|
|
9
|
-
readonly to?: Address
|
|
10
|
+
readonly to?: Address;
|
|
10
11
|
utxos: UTXO[];
|
|
11
|
-
nonWitnessUtxo?: Buffer
|
|
12
|
+
nonWitnessUtxo?: Buffer;
|
|
12
13
|
estimatedFees?: bigint;
|
|
14
|
+
optionalOutputs?: PsbtOutputExtended[];
|
|
13
15
|
chainId?: ChainId;
|
|
14
16
|
readonly feeRate: number;
|
|
15
17
|
readonly priorityFee: bigint;
|
|
@@ -19,27 +21,26 @@ export interface IFundingTransactionParameters extends ITransactionParameters {
|
|
|
19
21
|
splitInputsInto?: number;
|
|
20
22
|
}
|
|
21
23
|
export interface SharedInteractionParameters extends ITransactionParameters {
|
|
22
|
-
calldata?: Buffer
|
|
24
|
+
calldata?: Buffer;
|
|
23
25
|
disableAutoRefund?: boolean;
|
|
24
26
|
readonly randomBytes?: Buffer;
|
|
25
27
|
}
|
|
26
|
-
export interface IInteractionParameters extends SharedInteractionParameters {
|
|
28
|
+
export interface IInteractionParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
27
29
|
readonly calldata: Buffer;
|
|
28
30
|
readonly to: Address;
|
|
29
31
|
}
|
|
30
|
-
export interface IWrapParameters extends SharedInteractionParameters {
|
|
32
|
+
export interface IWrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
31
33
|
readonly to?: Address;
|
|
32
34
|
readonly from: Address;
|
|
33
35
|
readonly amount: bigint;
|
|
34
36
|
readonly receiver?: Address;
|
|
35
37
|
readonly generationParameters: WrappedGeneration;
|
|
36
38
|
}
|
|
37
|
-
export interface IUnwrapParameters extends SharedInteractionParameters {
|
|
39
|
+
export interface IUnwrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
38
40
|
readonly unwrapUTXOs: VaultUTXOs[];
|
|
39
41
|
readonly amount: bigint;
|
|
40
42
|
}
|
|
41
|
-
export interface IDeploymentParameters extends ITransactionParameters {
|
|
43
|
+
export interface IDeploymentParameters extends Omit<ITransactionParameters, 'to'> {
|
|
42
44
|
readonly bytecode: Buffer;
|
|
43
|
-
readonly to?: undefined;
|
|
44
45
|
readonly randomBytes?: Buffer;
|
|
45
46
|
}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { Address } from '@btc-vision/bsi-binary';
|
|
2
|
+
import { Network } from 'bitcoinjs-lib';
|
|
3
|
+
import { Wallet } from '../opnet.js';
|
|
2
4
|
import { UnwrapGeneration } from '../wbtc/UnwrapGeneration.js';
|
|
3
5
|
import { WrappedGeneration } from '../wbtc/WrappedGenerationParameters.js';
|
|
4
6
|
import { BroadcastResponse } from './interfaces/BroadcastResponse.js';
|
|
5
|
-
import { FetchUTXOParams, FetchUTXOParamsMultiAddress, UTXO } from './interfaces/IUTXO.js';
|
|
7
|
+
import { FetchUTXOParams, FetchUTXOParamsMultiAddress, RawUTXOResponse, UTXO } from './interfaces/IUTXO.js';
|
|
8
|
+
export interface WalletUTXOs {
|
|
9
|
+
readonly confirmed: RawUTXOResponse[];
|
|
10
|
+
readonly pending: RawUTXOResponse[];
|
|
11
|
+
readonly spentTransactions: RawUTXOResponse[];
|
|
12
|
+
}
|
|
6
13
|
export declare class OPNetLimitedProvider {
|
|
7
14
|
private readonly opnetAPIUrl;
|
|
8
15
|
private readonly utxoPath;
|
|
@@ -11,6 +18,9 @@ export declare class OPNetLimitedProvider {
|
|
|
11
18
|
fetchUTXO(settings: FetchUTXOParams): Promise<UTXO[]>;
|
|
12
19
|
fetchUTXOMultiAddr(settings: FetchUTXOParamsMultiAddress): Promise<UTXO[]>;
|
|
13
20
|
broadcastTransaction(transaction: string, psbt: boolean): Promise<BroadcastResponse | undefined>;
|
|
21
|
+
splitUTXOs(wallet: Wallet, network: Network, splitInputsInto: number, amountPerUTXO: bigint): Promise<BroadcastResponse | {
|
|
22
|
+
error: string;
|
|
23
|
+
}>;
|
|
14
24
|
rpcMethod(method: string, paramsMethod: unknown[]): Promise<unknown>;
|
|
15
25
|
fetchWrapParameters(amount: bigint): Promise<WrappedGeneration | undefined>;
|
|
16
26
|
fetchUnWrapParameters(amount: bigint, receiver: Address): Promise<UnwrapGeneration | undefined>;
|
|
@@ -9,7 +9,8 @@ export interface FetchUTXOParams {
|
|
|
9
9
|
readonly address: string;
|
|
10
10
|
readonly minAmount: bigint;
|
|
11
11
|
readonly requestedAmount: bigint;
|
|
12
|
-
|
|
12
|
+
optimized?: boolean;
|
|
13
|
+
usePendingUTXO?: boolean;
|
|
13
14
|
}
|
|
14
15
|
export interface FetchUTXOParamsMultiAddress {
|
|
15
16
|
readonly addresses: string[];
|
|
@@ -115,6 +115,7 @@ export class TransactionFactory {
|
|
|
115
115
|
utxos: [newUtxo],
|
|
116
116
|
randomBytes: preTransaction.getRndBytes(),
|
|
117
117
|
nonWitnessUtxo: signedTransaction.toBuffer(),
|
|
118
|
+
optionalOutputs: []
|
|
118
119
|
};
|
|
119
120
|
const finalTransaction = new DeploymentTransaction(newParams);
|
|
120
121
|
const outTx = await finalTransaction.signTransaction();
|
|
@@ -141,7 +142,7 @@ export class TransactionFactory {
|
|
|
141
142
|
}
|
|
142
143
|
const childTransactionRequiredValue = wrapParameters.amount +
|
|
143
144
|
currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT +
|
|
144
|
-
(wrapParameters.priorityFee ||
|
|
145
|
+
(wrapParameters.priorityFee || 330n);
|
|
145
146
|
const wbtc = new wBTC(wrapParameters.network, wrapParameters.chainId);
|
|
146
147
|
const to = wbtc.getAddress();
|
|
147
148
|
const fundingParameters = {
|
|
@@ -27,6 +27,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
|
|
|
27
27
|
protected to: Address | undefined;
|
|
28
28
|
protected from: Address;
|
|
29
29
|
protected _maximumFeeRate: number;
|
|
30
|
+
optionalOutputs: PsbtOutputExtended[] | undefined;
|
|
30
31
|
protected constructor(parameters: ITransactionParameters);
|
|
31
32
|
static getFrom(from: string | undefined, keypair: ECPairInterface, network: Network): Address;
|
|
32
33
|
static witnessStackToScriptWitness(witness: Buffer[]): Buffer;
|
|
@@ -50,6 +51,7 @@ export declare abstract class TransactionBuilder<T extends TransactionType> exte
|
|
|
50
51
|
protected getTransactionOPNetFee(): bigint;
|
|
51
52
|
protected calculateTotalUTXOAmount(): bigint;
|
|
52
53
|
protected calculateTotalVOutAmount(): bigint;
|
|
54
|
+
protected addOptionalOutputsAndGetAmount(): bigint;
|
|
53
55
|
protected addInputsFromUTXO(): void;
|
|
54
56
|
protected internalInit(): void;
|
|
55
57
|
protected abstract buildTransaction(): Promise<void>;
|
|
@@ -25,6 +25,7 @@ export class TransactionBuilder extends TweakedTransaction {
|
|
|
25
25
|
this.priorityFee = parameters.priorityFee ?? 0n;
|
|
26
26
|
this.utxos = parameters.utxos;
|
|
27
27
|
this.to = parameters.to || undefined;
|
|
28
|
+
this.optionalOutputs = parameters.optionalOutputs;
|
|
28
29
|
this.from = TransactionBuilder.getFrom(parameters.from, this.signer, this.network);
|
|
29
30
|
this.totalInputAmount = this.calculateTotalUTXOAmount();
|
|
30
31
|
const totalVOut = this.calculateTotalVOutAmount();
|
|
@@ -73,6 +74,7 @@ export class TransactionBuilder extends TweakedTransaction {
|
|
|
73
74
|
priorityFee: this.priorityFee ?? 0n,
|
|
74
75
|
from: this.from,
|
|
75
76
|
amount: this.estimatedFees,
|
|
77
|
+
optionalOutputs: this.optionalOutputs,
|
|
76
78
|
};
|
|
77
79
|
}
|
|
78
80
|
setDestinationAddress(address) {
|
|
@@ -180,7 +182,7 @@ export class TransactionBuilder extends TweakedTransaction {
|
|
|
180
182
|
return outputs;
|
|
181
183
|
}
|
|
182
184
|
async addRefundOutput(amountSpent) {
|
|
183
|
-
const sendBackAmount = this.totalInputAmount - amountSpent;
|
|
185
|
+
const sendBackAmount = this.totalInputAmount - amountSpent - this.addOptionalOutputsAndGetAmount();
|
|
184
186
|
if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
|
|
185
187
|
if (AddressVerificator.isValidP2TRAddress(this.from, this.network)) {
|
|
186
188
|
await this.setFeeOutput({
|
|
@@ -231,6 +233,16 @@ export class TransactionBuilder extends TweakedTransaction {
|
|
|
231
233
|
}
|
|
232
234
|
return total;
|
|
233
235
|
}
|
|
236
|
+
addOptionalOutputsAndGetAmount() {
|
|
237
|
+
if (!this.optionalOutputs)
|
|
238
|
+
return 0n;
|
|
239
|
+
let refundedFromOptionalOutputs = 0n;
|
|
240
|
+
for (let i = 0; i < this.optionalOutputs.length; i++) {
|
|
241
|
+
this.addOutput(this.optionalOutputs[i]);
|
|
242
|
+
refundedFromOptionalOutputs += BigInt(this.optionalOutputs[i].value);
|
|
243
|
+
}
|
|
244
|
+
return refundedFromOptionalOutputs;
|
|
245
|
+
}
|
|
234
246
|
addInputsFromUTXO() {
|
|
235
247
|
if (this.utxos.length) {
|
|
236
248
|
if (this.totalInputAmount < TransactionBuilder.MINIMUM_DUST) {
|
|
@@ -4,12 +4,14 @@ import { WrappedGeneration } from '../../wbtc/WrappedGenerationParameters.js';
|
|
|
4
4
|
import { ITweakedTransactionData } from '../shared/TweakedTransaction.js';
|
|
5
5
|
import { VaultUTXOs } from '../processor/PsbtTransaction.js';
|
|
6
6
|
import { ChainId } from '../../network/ChainId.js';
|
|
7
|
+
import { PsbtOutputExtended } from './Tap.js';
|
|
7
8
|
export interface ITransactionParameters extends ITweakedTransactionData {
|
|
8
9
|
readonly from?: Address;
|
|
9
|
-
readonly to?: Address
|
|
10
|
+
readonly to?: Address;
|
|
10
11
|
utxos: UTXO[];
|
|
11
|
-
nonWitnessUtxo?: Buffer
|
|
12
|
+
nonWitnessUtxo?: Buffer;
|
|
12
13
|
estimatedFees?: bigint;
|
|
14
|
+
optionalOutputs?: PsbtOutputExtended[];
|
|
13
15
|
chainId?: ChainId;
|
|
14
16
|
readonly feeRate: number;
|
|
15
17
|
readonly priorityFee: bigint;
|
|
@@ -19,27 +21,26 @@ export interface IFundingTransactionParameters extends ITransactionParameters {
|
|
|
19
21
|
splitInputsInto?: number;
|
|
20
22
|
}
|
|
21
23
|
export interface SharedInteractionParameters extends ITransactionParameters {
|
|
22
|
-
calldata?: Buffer
|
|
24
|
+
calldata?: Buffer;
|
|
23
25
|
disableAutoRefund?: boolean;
|
|
24
26
|
readonly randomBytes?: Buffer;
|
|
25
27
|
}
|
|
26
|
-
export interface IInteractionParameters extends SharedInteractionParameters {
|
|
28
|
+
export interface IInteractionParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
27
29
|
readonly calldata: Buffer;
|
|
28
30
|
readonly to: Address;
|
|
29
31
|
}
|
|
30
|
-
export interface IWrapParameters extends SharedInteractionParameters {
|
|
32
|
+
export interface IWrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
31
33
|
readonly to?: Address;
|
|
32
34
|
readonly from: Address;
|
|
33
35
|
readonly amount: bigint;
|
|
34
36
|
readonly receiver?: Address;
|
|
35
37
|
readonly generationParameters: WrappedGeneration;
|
|
36
38
|
}
|
|
37
|
-
export interface IUnwrapParameters extends SharedInteractionParameters {
|
|
39
|
+
export interface IUnwrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
38
40
|
readonly unwrapUTXOs: VaultUTXOs[];
|
|
39
41
|
readonly amount: bigint;
|
|
40
42
|
}
|
|
41
|
-
export interface IDeploymentParameters extends ITransactionParameters {
|
|
43
|
+
export interface IDeploymentParameters extends Omit<ITransactionParameters, 'to'> {
|
|
42
44
|
readonly bytecode: Buffer;
|
|
43
|
-
readonly to?: undefined;
|
|
44
45
|
readonly randomBytes?: Buffer;
|
|
45
46
|
}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { Address } from '@btc-vision/bsi-binary';
|
|
2
|
+
import { Network } from 'bitcoinjs-lib';
|
|
3
|
+
import { Wallet } from '../opnet.js';
|
|
2
4
|
import { UnwrapGeneration } from '../wbtc/UnwrapGeneration.js';
|
|
3
5
|
import { WrappedGeneration } from '../wbtc/WrappedGenerationParameters.js';
|
|
4
6
|
import { BroadcastResponse } from './interfaces/BroadcastResponse.js';
|
|
5
|
-
import { FetchUTXOParams, FetchUTXOParamsMultiAddress, UTXO } from './interfaces/IUTXO.js';
|
|
7
|
+
import { FetchUTXOParams, FetchUTXOParamsMultiAddress, RawUTXOResponse, UTXO } from './interfaces/IUTXO.js';
|
|
8
|
+
export interface WalletUTXOs {
|
|
9
|
+
readonly confirmed: RawUTXOResponse[];
|
|
10
|
+
readonly pending: RawUTXOResponse[];
|
|
11
|
+
readonly spentTransactions: RawUTXOResponse[];
|
|
12
|
+
}
|
|
6
13
|
export declare class OPNetLimitedProvider {
|
|
7
14
|
private readonly opnetAPIUrl;
|
|
8
15
|
private readonly utxoPath;
|
|
@@ -11,6 +18,9 @@ export declare class OPNetLimitedProvider {
|
|
|
11
18
|
fetchUTXO(settings: FetchUTXOParams): Promise<UTXO[]>;
|
|
12
19
|
fetchUTXOMultiAddr(settings: FetchUTXOParamsMultiAddress): Promise<UTXO[]>;
|
|
13
20
|
broadcastTransaction(transaction: string, psbt: boolean): Promise<BroadcastResponse | undefined>;
|
|
21
|
+
splitUTXOs(wallet: Wallet, network: Network, splitInputsInto: number, amountPerUTXO: bigint): Promise<BroadcastResponse | {
|
|
22
|
+
error: string;
|
|
23
|
+
}>;
|
|
14
24
|
rpcMethod(method: string, paramsMethod: unknown[]): Promise<unknown>;
|
|
15
25
|
fetchWrapParameters(amount: bigint): Promise<WrappedGeneration | undefined>;
|
|
16
26
|
fetchUnWrapParameters(amount: bigint, receiver: Address): Promise<UnwrapGeneration | undefined>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { currentConsensusConfig } from '../consensus/ConsensusConfig.js';
|
|
2
|
+
import { TransactionFactory } from '../opnet.js';
|
|
2
3
|
import { UnwrapGeneration } from '../wbtc/UnwrapGeneration.js';
|
|
3
4
|
import { WrappedGeneration } from '../wbtc/WrappedGenerationParameters.js';
|
|
4
5
|
export class OPNetLimitedProvider {
|
|
@@ -8,6 +9,12 @@ export class OPNetLimitedProvider {
|
|
|
8
9
|
this.rpc = 'json-rpc';
|
|
9
10
|
}
|
|
10
11
|
async fetchUTXO(settings) {
|
|
12
|
+
if (settings.usePendingUTXO === undefined) {
|
|
13
|
+
settings.usePendingUTXO = true;
|
|
14
|
+
}
|
|
15
|
+
if (settings.optimized === undefined) {
|
|
16
|
+
settings.optimized = true;
|
|
17
|
+
}
|
|
11
18
|
const params = {
|
|
12
19
|
method: 'GET',
|
|
13
20
|
headers: {
|
|
@@ -20,10 +27,21 @@ export class OPNetLimitedProvider {
|
|
|
20
27
|
throw new Error(`Failed to fetch UTXO data: ${resp.statusText}`);
|
|
21
28
|
}
|
|
22
29
|
const fetchedData = (await resp.json());
|
|
23
|
-
|
|
30
|
+
const allUtxos = settings.usePendingUTXO
|
|
31
|
+
? [...fetchedData.confirmed, ...fetchedData.pending]
|
|
32
|
+
: fetchedData.confirmed;
|
|
33
|
+
const unspentUTXOs = [];
|
|
34
|
+
for (const utxo of allUtxos) {
|
|
35
|
+
if (fetchedData.spentTransactions.some((spent) => spent.transactionId === utxo.transactionId &&
|
|
36
|
+
spent.outputIndex === utxo.outputIndex)) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
unspentUTXOs.push(utxo);
|
|
40
|
+
}
|
|
41
|
+
if (unspentUTXOs.length === 0) {
|
|
24
42
|
throw new Error('No UTXO found');
|
|
25
43
|
}
|
|
26
|
-
const meetCriteria =
|
|
44
|
+
const meetCriteria = unspentUTXOs.filter((utxo) => {
|
|
27
45
|
return BigInt(utxo.value) >= settings.minAmount;
|
|
28
46
|
});
|
|
29
47
|
if (meetCriteria.length === 0) {
|
|
@@ -58,6 +76,7 @@ export class OPNetLimitedProvider {
|
|
|
58
76
|
minAmount: settings.minAmount,
|
|
59
77
|
requestedAmount: settings.requestedAmount,
|
|
60
78
|
optimized: settings.optimized,
|
|
79
|
+
usePendingUTXO: false,
|
|
61
80
|
};
|
|
62
81
|
const promise = this.fetchUTXO(params).catch(() => {
|
|
63
82
|
return [];
|
|
@@ -86,6 +105,34 @@ export class OPNetLimitedProvider {
|
|
|
86
105
|
}
|
|
87
106
|
return result;
|
|
88
107
|
}
|
|
108
|
+
async splitUTXOs(wallet, network, splitInputsInto, amountPerUTXO) {
|
|
109
|
+
const utxoSetting = {
|
|
110
|
+
addresses: [wallet.p2wpkh, wallet.p2tr],
|
|
111
|
+
minAmount: 330n,
|
|
112
|
+
requestedAmount: 1000000000000000n,
|
|
113
|
+
};
|
|
114
|
+
const utxos = await this.fetchUTXOMultiAddr(utxoSetting);
|
|
115
|
+
if (!utxos || !utxos.length)
|
|
116
|
+
return { error: 'No UTXOs found' };
|
|
117
|
+
const amount = BigInt(splitInputsInto) * amountPerUTXO;
|
|
118
|
+
const fundingTransactionParameters = {
|
|
119
|
+
amount: amount,
|
|
120
|
+
feeRate: 500,
|
|
121
|
+
from: wallet.p2tr,
|
|
122
|
+
utxos: utxos,
|
|
123
|
+
signer: wallet.keypair,
|
|
124
|
+
network,
|
|
125
|
+
to: wallet.p2tr,
|
|
126
|
+
splitInputsInto,
|
|
127
|
+
priorityFee: 330n,
|
|
128
|
+
};
|
|
129
|
+
const transactionFactory = new TransactionFactory();
|
|
130
|
+
const fundingTx = await transactionFactory.createBTCTransfer(fundingTransactionParameters);
|
|
131
|
+
const broadcastResponse = await this.broadcastTransaction(fundingTx.tx, false);
|
|
132
|
+
if (!broadcastResponse)
|
|
133
|
+
return { error: 'Could not broadcast transaction' };
|
|
134
|
+
return broadcastResponse;
|
|
135
|
+
}
|
|
89
136
|
async rpcMethod(method, paramsMethod) {
|
|
90
137
|
const params = {
|
|
91
138
|
method: 'POST',
|
|
@@ -9,7 +9,8 @@ export interface FetchUTXOParams {
|
|
|
9
9
|
readonly address: string;
|
|
10
10
|
readonly minAmount: bigint;
|
|
11
11
|
readonly requestedAmount: bigint;
|
|
12
|
-
|
|
12
|
+
optimized?: boolean;
|
|
13
|
+
usePendingUTXO?: boolean;
|
|
13
14
|
}
|
|
14
15
|
export interface FetchUTXOParamsMultiAddress {
|
|
15
16
|
readonly addresses: string[];
|
package/eslint.config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@btc-vision/transaction",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.106",
|
|
5
5
|
"author": "BlobMaster41",
|
|
6
6
|
"description": "OPNet transaction library allows you to create and sign transactions for the OPNet network.",
|
|
7
7
|
"engines": {
|
|
@@ -92,7 +92,7 @@
|
|
|
92
92
|
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
|
|
93
93
|
"@bitcoinerlab/secp256k1": "^1.1.1",
|
|
94
94
|
"@btc-vision/bsi-binary": "^1.0.42",
|
|
95
|
-
"@btc-vision/bsi-bitcoin-rpc": "^1.0.
|
|
95
|
+
"@btc-vision/bsi-bitcoin-rpc": "^1.0.29",
|
|
96
96
|
"@btc-vision/logger": "^1.0.6",
|
|
97
97
|
"@eslint/js": "^9.10.0",
|
|
98
98
|
"assert": "^2.1.0",
|
|
@@ -224,8 +224,7 @@ export class TransactionFactory {
|
|
|
224
224
|
// Initial generation
|
|
225
225
|
await preTransaction.signTransaction();
|
|
226
226
|
|
|
227
|
-
const parameters: IFundingTransactionParameters =
|
|
228
|
-
await preTransaction.getFundingTransactionParameters();
|
|
227
|
+
const parameters: IFundingTransactionParameters =await preTransaction.getFundingTransactionParameters();
|
|
229
228
|
|
|
230
229
|
const fundingTransaction: FundingTransaction = new FundingTransaction(parameters);
|
|
231
230
|
const signedTransaction: Transaction = await fundingTransaction.signTransaction();
|
|
@@ -249,6 +248,7 @@ export class TransactionFactory {
|
|
|
249
248
|
utxos: [newUtxo],
|
|
250
249
|
randomBytes: preTransaction.getRndBytes(),
|
|
251
250
|
nonWitnessUtxo: signedTransaction.toBuffer(),
|
|
251
|
+
optionalOutputs: []
|
|
252
252
|
};
|
|
253
253
|
|
|
254
254
|
const finalTransaction: DeploymentTransaction = new DeploymentTransaction(newParams);
|
|
@@ -291,7 +291,7 @@ export class TransactionFactory {
|
|
|
291
291
|
const childTransactionRequiredValue: bigint =
|
|
292
292
|
wrapParameters.amount +
|
|
293
293
|
currentConsensusConfig.UNWRAP_CONSOLIDATION_PREPAID_FEES_SAT +
|
|
294
|
-
(wrapParameters.priorityFee ||
|
|
294
|
+
(wrapParameters.priorityFee || 330n);
|
|
295
295
|
|
|
296
296
|
const wbtc: wBTC = new wBTC(wrapParameters.network, wrapParameters.chainId);
|
|
297
297
|
const to = wbtc.getAddress();
|
|
@@ -105,6 +105,9 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
105
105
|
/**
|
|
106
106
|
* @param {ITransactionParameters} parameters - The transaction parameters
|
|
107
107
|
*/
|
|
108
|
+
|
|
109
|
+
public optionalOutputs: PsbtOutputExtended[] | undefined;
|
|
110
|
+
|
|
108
111
|
protected constructor(parameters: ITransactionParameters) {
|
|
109
112
|
super(parameters);
|
|
110
113
|
|
|
@@ -119,6 +122,8 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
119
122
|
this.utxos = parameters.utxos;
|
|
120
123
|
this.to = parameters.to || undefined;
|
|
121
124
|
|
|
125
|
+
this.optionalOutputs = parameters.optionalOutputs;
|
|
126
|
+
|
|
122
127
|
this.from = TransactionBuilder.getFrom(
|
|
123
128
|
parameters.from,
|
|
124
129
|
this.signer as ECPairInterface,
|
|
@@ -195,6 +200,7 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
195
200
|
priorityFee: this.priorityFee ?? 0n,
|
|
196
201
|
from: this.from,
|
|
197
202
|
amount: this.estimatedFees,
|
|
203
|
+
optionalOutputs: this.optionalOutputs,
|
|
198
204
|
};
|
|
199
205
|
}
|
|
200
206
|
|
|
@@ -409,8 +415,8 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
409
415
|
*/
|
|
410
416
|
protected async addRefundOutput(amountSpent: bigint): Promise<void> {
|
|
411
417
|
/** Add the refund output */
|
|
412
|
-
const sendBackAmount: bigint =
|
|
413
|
-
|
|
418
|
+
const sendBackAmount: bigint =
|
|
419
|
+
this.totalInputAmount - amountSpent - this.addOptionalOutputsAndGetAmount();
|
|
414
420
|
if (sendBackAmount >= TransactionBuilder.MINIMUM_DUST) {
|
|
415
421
|
if (AddressVerificator.isValidP2TRAddress(this.from, this.network)) {
|
|
416
422
|
await this.setFeeOutput({
|
|
@@ -479,7 +485,6 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
479
485
|
for (const utxo of this.utxos) {
|
|
480
486
|
total += utxo.value;
|
|
481
487
|
}
|
|
482
|
-
|
|
483
488
|
return total;
|
|
484
489
|
}
|
|
485
490
|
|
|
@@ -496,6 +501,22 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
496
501
|
|
|
497
502
|
return total;
|
|
498
503
|
}
|
|
504
|
+
/**
|
|
505
|
+
* @description Adds optional outputs to transaction and returns their total value in satoshi to calculate refund transaction
|
|
506
|
+
* @protected
|
|
507
|
+
* @returns {bigint}
|
|
508
|
+
*/
|
|
509
|
+
protected addOptionalOutputsAndGetAmount(): bigint {
|
|
510
|
+
if (!this.optionalOutputs) return 0n;
|
|
511
|
+
|
|
512
|
+
let refundedFromOptionalOutputs = 0n;
|
|
513
|
+
|
|
514
|
+
for (let i = 0; i < this.optionalOutputs.length; i++) {
|
|
515
|
+
this.addOutput(this.optionalOutputs[i]);
|
|
516
|
+
refundedFromOptionalOutputs += BigInt(this.optionalOutputs[i].value);
|
|
517
|
+
}
|
|
518
|
+
return refundedFromOptionalOutputs;
|
|
519
|
+
}
|
|
499
520
|
|
|
500
521
|
/**
|
|
501
522
|
* @description Adds the inputs from the utxos
|
|
@@ -4,15 +4,17 @@ import { WrappedGeneration } from '../../wbtc/WrappedGenerationParameters.js';
|
|
|
4
4
|
import { ITweakedTransactionData } from '../shared/TweakedTransaction.js';
|
|
5
5
|
import { VaultUTXOs } from '../processor/PsbtTransaction.js';
|
|
6
6
|
import { ChainId } from '../../network/ChainId.js';
|
|
7
|
-
|
|
7
|
+
import { PsbtOutputExtended } from './Tap.js';
|
|
8
8
|
export interface ITransactionParameters extends ITweakedTransactionData {
|
|
9
9
|
readonly from?: Address;
|
|
10
|
-
readonly to?: Address
|
|
10
|
+
readonly to?: Address;
|
|
11
11
|
utxos: UTXO[];
|
|
12
12
|
|
|
13
|
-
nonWitnessUtxo?: Buffer
|
|
13
|
+
nonWitnessUtxo?: Buffer;
|
|
14
14
|
estimatedFees?: bigint;
|
|
15
15
|
|
|
16
|
+
optionalOutputs?: PsbtOutputExtended[];
|
|
17
|
+
|
|
16
18
|
chainId?: ChainId;
|
|
17
19
|
|
|
18
20
|
readonly feeRate: number;
|
|
@@ -26,19 +28,20 @@ export interface IFundingTransactionParameters extends ITransactionParameters {
|
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
export interface SharedInteractionParameters extends ITransactionParameters {
|
|
29
|
-
calldata?: Buffer
|
|
31
|
+
calldata?: Buffer;
|
|
30
32
|
disableAutoRefund?: boolean;
|
|
31
33
|
|
|
32
34
|
readonly randomBytes?: Buffer;
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
export interface IInteractionParameters
|
|
37
|
+
export interface IInteractionParameters
|
|
38
|
+
extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
36
39
|
readonly calldata: Buffer;
|
|
37
40
|
|
|
38
41
|
readonly to: Address;
|
|
39
42
|
}
|
|
40
43
|
|
|
41
|
-
export interface IWrapParameters extends SharedInteractionParameters {
|
|
44
|
+
export interface IWrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
42
45
|
readonly to?: Address;
|
|
43
46
|
|
|
44
47
|
readonly from: Address;
|
|
@@ -48,14 +51,13 @@ export interface IWrapParameters extends SharedInteractionParameters {
|
|
|
48
51
|
readonly generationParameters: WrappedGeneration;
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
export interface IUnwrapParameters extends SharedInteractionParameters {
|
|
54
|
+
export interface IUnwrapParameters extends Omit<SharedInteractionParameters, 'optionalOutputs'> {
|
|
52
55
|
readonly unwrapUTXOs: VaultUTXOs[];
|
|
53
56
|
readonly amount: bigint;
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
export interface IDeploymentParameters extends ITransactionParameters {
|
|
59
|
+
export interface IDeploymentParameters extends Omit<ITransactionParameters, 'to'> {
|
|
57
60
|
readonly bytecode: Buffer;
|
|
58
61
|
|
|
59
|
-
readonly to?: undefined;
|
|
60
62
|
readonly randomBytes?: Buffer;
|
|
61
63
|
}
|