@atomiqlabs/chain-evm 2.1.12 → 2.1.14
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/README.md +75 -0
- package/dist/chains/EVMOptions.d.ts +66 -0
- package/dist/chains/EVMOptions.js +2 -0
- package/dist/chains/alpen/AlpenInitializer.d.ts +3 -30
- package/dist/chains/alpen/AlpenInitializer.js +3 -3
- package/dist/chains/botanix/BotanixInitializer.d.ts +3 -30
- package/dist/chains/botanix/BotanixInitializer.js +3 -3
- package/dist/chains/citrea/CitreaBtcRelay.d.ts +5 -0
- package/dist/chains/citrea/CitreaBtcRelay.js +7 -2
- package/dist/chains/citrea/CitreaFees.d.ts +3 -5
- package/dist/chains/citrea/CitreaFees.js +3 -5
- package/dist/chains/citrea/CitreaInitializer.d.ts +3 -29
- package/dist/chains/citrea/CitreaInitializer.js +3 -3
- package/dist/chains/citrea/CitreaSpvVaultContract.d.ts +5 -0
- package/dist/chains/citrea/CitreaSpvVaultContract.js +7 -2
- package/dist/chains/citrea/CitreaSwapContract.d.ts +7 -2
- package/dist/chains/citrea/CitreaSwapContract.js +10 -5
- package/dist/chains/citrea/CitreaTokens.d.ts +5 -0
- package/dist/chains/citrea/CitreaTokens.js +5 -0
- package/dist/chains/goat/GoatInitializer.d.ts +3 -30
- package/dist/chains/goat/GoatInitializer.js +3 -3
- package/dist/evm/btcrelay/EVMBtcRelay.d.ts +41 -10
- package/dist/evm/btcrelay/EVMBtcRelay.js +50 -18
- package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +53 -7
- package/dist/evm/btcrelay/headers/EVMBtcHeader.js +43 -5
- package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +53 -8
- package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +41 -1
- package/dist/evm/chain/EVMChainInterface.d.ts +57 -2
- package/dist/evm/chain/EVMChainInterface.js +7 -7
- package/dist/evm/chain/EVMModule.d.ts +5 -0
- package/dist/evm/chain/EVMModule.js +6 -1
- package/dist/evm/chain/modules/EVMBlocks.d.ts +7 -0
- package/dist/evm/chain/modules/EVMBlocks.js +2 -0
- package/dist/evm/chain/modules/EVMEvents.js +19 -19
- package/dist/evm/chain/modules/EVMFees.d.ts +41 -5
- package/dist/evm/chain/modules/EVMFees.js +24 -5
- package/dist/evm/chain/modules/EVMTokens.d.ts +1 -1
- package/dist/evm/chain/modules/EVMTokens.js +1 -1
- package/dist/evm/chain/modules/EVMTransactions.d.ts +20 -2
- package/dist/evm/chain/modules/EVMTransactions.js +11 -8
- package/dist/evm/contract/EVMContractBase.d.ts +28 -10
- package/dist/evm/contract/EVMContractBase.js +9 -18
- package/dist/evm/contract/EVMContractModule.d.ts +5 -0
- package/dist/evm/contract/EVMContractModule.js +5 -0
- package/dist/evm/contract/modules/EVMContractEvents.d.ts +7 -1
- package/dist/evm/contract/modules/EVMContractEvents.js +23 -3
- package/dist/evm/events/EVMChainEvents.d.ts +8 -0
- package/dist/evm/events/EVMChainEvents.js +8 -0
- package/dist/evm/events/EVMChainEventsBrowser.d.ts +87 -19
- package/dist/evm/events/EVMChainEventsBrowser.js +53 -18
- package/dist/evm/providers/JsonRpcProviderWithRetries.d.ts +9 -0
- package/dist/evm/providers/JsonRpcProviderWithRetries.js +9 -0
- package/dist/evm/providers/ReconnectingWebSocketProvider.d.ts +5 -0
- package/dist/evm/providers/ReconnectingWebSocketProvider.js +5 -0
- package/dist/evm/providers/WebSocketProviderWithRetries.d.ts +9 -0
- package/dist/evm/providers/WebSocketProviderWithRetries.js +9 -0
- package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +46 -21
- package/dist/evm/spv_swap/EVMSpvVaultContract.js +61 -23
- package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +57 -2
- package/dist/evm/spv_swap/EVMSpvVaultData.js +57 -2
- package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +12 -0
- package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +12 -0
- package/dist/evm/swaps/EVMSwapContract.d.ts +58 -13
- package/dist/evm/swaps/EVMSwapContract.js +81 -54
- package/dist/evm/swaps/EVMSwapData.d.ts +27 -6
- package/dist/evm/swaps/EVMSwapData.js +26 -0
- package/dist/evm/swaps/EVMSwapModule.d.ts +5 -0
- package/dist/evm/swaps/EVMSwapModule.js +5 -0
- package/dist/evm/swaps/handlers/IHandler.d.ts +5 -0
- package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +15 -0
- package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +5 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +5 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +10 -0
- package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +5 -0
- package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +15 -0
- package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +7 -2
- package/dist/evm/swaps/modules/EVMLpVault.d.ts +5 -0
- package/dist/evm/swaps/modules/EVMLpVault.js +9 -4
- package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +7 -2
- package/dist/evm/swaps/modules/EVMSwapClaim.js +11 -6
- package/dist/evm/swaps/modules/EVMSwapInit.d.ts +10 -0
- package/dist/evm/swaps/modules/EVMSwapInit.js +11 -6
- package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +5 -0
- package/dist/evm/swaps/modules/EVMSwapRefund.js +9 -4
- package/dist/evm/wallet/EVMBrowserSigner.d.ts +22 -2
- package/dist/evm/wallet/EVMBrowserSigner.js +40 -2
- package/dist/evm/wallet/EVMPersistentSigner.d.ts +13 -2
- package/dist/evm/wallet/EVMPersistentSigner.js +13 -1
- package/dist/evm/wallet/EVMSigner.d.ts +30 -1
- package/dist/evm/wallet/EVMSigner.js +34 -1
- package/dist/index.d.ts +71 -0
- package/dist/index.js +70 -0
- package/dist/node/index.d.ts +10 -0
- package/dist/node/index.js +15 -0
- package/dist/utils/Utils.d.ts +50 -0
- package/dist/utils/Utils.js +45 -0
- package/node/index.d.ts +1 -0
- package/node/index.js +3 -0
- package/package.json +4 -3
- package/src/chains/EVMOptions.ts +70 -0
- package/src/chains/alpen/AlpenInitializer.ts +5 -27
- package/src/chains/botanix/BotanixChainType.ts +5 -5
- package/src/chains/botanix/BotanixInitializer.ts +5 -27
- package/src/chains/citrea/CitreaBtcRelay.ts +8 -3
- package/src/chains/citrea/CitreaFees.ts +3 -6
- package/src/chains/citrea/CitreaInitializer.ts +5 -27
- package/src/chains/citrea/CitreaSpvVaultContract.ts +7 -2
- package/src/chains/citrea/CitreaSwapContract.ts +11 -6
- package/src/chains/citrea/CitreaTokens.ts +6 -1
- package/src/chains/goat/GoatChainType.ts +5 -5
- package/src/chains/goat/GoatInitializer.ts +3 -25
- package/src/evm/btcrelay/EVMBtcRelay.ts +54 -22
- package/src/evm/btcrelay/headers/EVMBtcHeader.ts +60 -13
- package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +55 -10
- package/src/evm/chain/EVMChainInterface.ts +66 -14
- package/src/evm/chain/EVMModule.ts +6 -1
- package/src/evm/chain/modules/EVMBlocks.ts +7 -0
- package/src/evm/chain/modules/EVMEvents.ts +19 -19
- package/src/evm/chain/modules/EVMFees.ts +41 -5
- package/src/evm/chain/modules/EVMTokens.ts +1 -1
- package/src/evm/chain/modules/EVMTransactions.ts +27 -8
- package/src/evm/contract/EVMContractBase.ts +29 -24
- package/src/evm/contract/EVMContractModule.ts +5 -0
- package/src/evm/contract/modules/EVMContractEvents.ts +27 -8
- package/src/evm/events/EVMChainEvents.ts +8 -0
- package/src/evm/events/EVMChainEventsBrowser.ts +103 -29
- package/src/evm/providers/JsonRpcProviderWithRetries.ts +10 -1
- package/src/evm/providers/ReconnectingWebSocketProvider.ts +6 -1
- package/src/evm/providers/WebSocketProviderWithRetries.ts +10 -1
- package/src/evm/spv_swap/EVMSpvVaultContract.ts +72 -32
- package/src/evm/spv_swap/EVMSpvVaultData.ts +57 -2
- package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +12 -0
- package/src/evm/swaps/EVMSwapContract.ts +108 -63
- package/src/evm/swaps/EVMSwapData.ts +27 -1
- package/src/evm/swaps/EVMSwapModule.ts +5 -0
- package/src/evm/swaps/handlers/IHandler.ts +5 -0
- package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +15 -0
- package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +5 -0
- package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +10 -0
- package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +5 -0
- package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +17 -2
- package/src/evm/swaps/modules/EVMLpVault.ts +10 -5
- package/src/evm/swaps/modules/EVMSwapClaim.ts +12 -7
- package/src/evm/swaps/modules/EVMSwapInit.ts +17 -7
- package/src/evm/swaps/modules/EVMSwapRefund.ts +9 -4
- package/src/evm/wallet/EVMBrowserSigner.ts +44 -5
- package/src/evm/wallet/EVMPersistentSigner.ts +14 -2
- package/src/evm/wallet/EVMSigner.ts +37 -1
- package/src/index.ts +72 -0
- package/src/node/index.ts +10 -0
- package/src/utils/Utils.ts +50 -1
|
@@ -36,6 +36,9 @@ const ESCROW_STATE_REFUNDED = 3;
|
|
|
36
36
|
const logger = getLogger("EVMSwapContract: ");
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
+
* EVM swap contract (escrow manager) representation handling PrTLC (on-chain) and HTLC (lightning)
|
|
40
|
+
* based swaps.
|
|
41
|
+
*
|
|
39
42
|
* @category Swaps
|
|
40
43
|
*/
|
|
41
44
|
export class EVMSwapContract<ChainId extends string = string>
|
|
@@ -60,26 +63,56 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
60
63
|
readonly claimWithSecretTimeout: number = 180;
|
|
61
64
|
readonly claimWithTxDataTimeout: number = 180;
|
|
62
65
|
readonly refundTimeout: number = 180;
|
|
63
|
-
readonly claimGracePeriod: number = 10*60;
|
|
64
|
-
readonly refundGracePeriod: number = 10*60;
|
|
65
|
-
|
|
66
|
+
private readonly claimGracePeriod: number = 10*60;
|
|
67
|
+
private readonly refundGracePeriod: number = 10*60;
|
|
68
|
+
/**
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
readonly _authGracePeriod: number = 30;
|
|
66
72
|
|
|
67
73
|
////////////////////////
|
|
68
74
|
//// Services
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
readonly
|
|
75
|
+
/**
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
readonly _Init: EVMSwapInit;
|
|
79
|
+
/**
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
readonly _Refund: EVMSwapRefund;
|
|
83
|
+
/**
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
readonly _Claim: EVMSwapClaim;
|
|
87
|
+
/**
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
readonly _LpVault: EVMLpVault;
|
|
73
91
|
|
|
74
92
|
////////////////////////
|
|
75
93
|
//// Handlers
|
|
76
|
-
|
|
77
|
-
|
|
94
|
+
/**
|
|
95
|
+
* @internal
|
|
96
|
+
*/
|
|
97
|
+
readonly _claimHandlersByAddress: {[address: string]: IClaimHandler<any, any>} = {};
|
|
98
|
+
/**
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
readonly _claimHandlersBySwapType: {[type in ChainSwapType]?: IClaimHandler<any, any>} = {};
|
|
78
102
|
|
|
79
|
-
|
|
80
|
-
|
|
103
|
+
/**
|
|
104
|
+
* @internal
|
|
105
|
+
*/
|
|
106
|
+
readonly _refundHandlersByAddress: {[address: string]: IHandler<any, any>} = {};
|
|
107
|
+
/**
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
readonly _timelockRefundHandler: IHandler<any, any>;
|
|
81
111
|
|
|
82
|
-
|
|
112
|
+
/**
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
readonly _btcRelay: EVMBtcRelay<any>;
|
|
83
116
|
|
|
84
117
|
constructor(
|
|
85
118
|
chainInterface: EVMChainInterface<ChainId>,
|
|
@@ -97,21 +130,21 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
97
130
|
) {
|
|
98
131
|
super(chainInterface, contractAddress, EscrowManagerAbi, contractDeploymentHeight);
|
|
99
132
|
this.chainId = chainInterface.chainId;
|
|
100
|
-
this.
|
|
101
|
-
this.
|
|
102
|
-
this.
|
|
103
|
-
this.
|
|
133
|
+
this._Init = new EVMSwapInit(chainInterface, this);
|
|
134
|
+
this._Refund = new EVMSwapRefund(chainInterface, this);
|
|
135
|
+
this._Claim = new EVMSwapClaim(chainInterface, this);
|
|
136
|
+
this._LpVault = new EVMLpVault(chainInterface, this);
|
|
104
137
|
|
|
105
|
-
this.
|
|
138
|
+
this._btcRelay = btcRelay;
|
|
106
139
|
|
|
107
140
|
claimHandlersList.forEach(handlerCtor => {
|
|
108
141
|
const handler = new handlerCtor(handlerAddresses.claim[handlerCtor.type]);
|
|
109
|
-
this.
|
|
110
|
-
this.
|
|
142
|
+
this._claimHandlersByAddress[handler.address.toLowerCase()] = handler;
|
|
143
|
+
this._claimHandlersBySwapType[handlerCtor.type] = handler;
|
|
111
144
|
});
|
|
112
145
|
|
|
113
|
-
this.
|
|
114
|
-
this.
|
|
146
|
+
this._timelockRefundHandler = new TimelockRefundHandler(handlerAddresses.refund.timelock);
|
|
147
|
+
this._refundHandlersByAddress[this._timelockRefundHandler.address.toLowerCase()] = this._timelockRefundHandler;
|
|
115
148
|
}
|
|
116
149
|
|
|
117
150
|
/**
|
|
@@ -126,49 +159,49 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
126
159
|
* @inheritDoc
|
|
127
160
|
*/
|
|
128
161
|
preFetchForInitSignatureVerification(): Promise<EVMPreFetchVerification> {
|
|
129
|
-
return this.
|
|
162
|
+
return this._Init.preFetchForInitSignatureVerification();
|
|
130
163
|
}
|
|
131
164
|
|
|
132
165
|
/**
|
|
133
166
|
* @inheritDoc
|
|
134
167
|
*/
|
|
135
168
|
getInitSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number, preFetchedBlockData?: never, feeRate?: string): Promise<SignatureData> {
|
|
136
|
-
return this.
|
|
169
|
+
return this._Init.signSwapInitialization(signer, swapData, authorizationTimeout);
|
|
137
170
|
}
|
|
138
171
|
|
|
139
172
|
/**
|
|
140
173
|
* @inheritDoc
|
|
141
174
|
*/
|
|
142
175
|
isValidInitAuthorization(sender: string, swapData: EVMSwapData, signature: SignatureData, feeRate?: string, preFetchedData?: EVMPreFetchVerification): Promise<Buffer | null> {
|
|
143
|
-
return this.
|
|
176
|
+
return this._Init.isSignatureValid(sender, swapData, signature.timeout, signature.prefix, signature.signature, preFetchedData);
|
|
144
177
|
}
|
|
145
178
|
|
|
146
179
|
/**
|
|
147
180
|
* @inheritDoc
|
|
148
181
|
*/
|
|
149
182
|
getInitAuthorizationExpiry(swapData: EVMSwapData, signature: SignatureData, preFetchedData?: EVMPreFetchVerification): Promise<number> {
|
|
150
|
-
return this.
|
|
183
|
+
return this._Init.getSignatureExpiry(signature.timeout);
|
|
151
184
|
}
|
|
152
185
|
|
|
153
186
|
/**
|
|
154
187
|
* @inheritDoc
|
|
155
188
|
*/
|
|
156
189
|
isInitAuthorizationExpired(swapData: EVMSwapData, signature: SignatureData): Promise<boolean> {
|
|
157
|
-
return this.
|
|
190
|
+
return this._Init.isSignatureExpired(signature.timeout);
|
|
158
191
|
}
|
|
159
192
|
|
|
160
193
|
/**
|
|
161
194
|
* @inheritDoc
|
|
162
195
|
*/
|
|
163
196
|
getRefundSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number): Promise<SignatureData> {
|
|
164
|
-
return this.
|
|
197
|
+
return this._Refund.signSwapRefund(signer, swapData, authorizationTimeout);
|
|
165
198
|
}
|
|
166
199
|
|
|
167
200
|
/**
|
|
168
201
|
* @inheritDoc
|
|
169
202
|
*/
|
|
170
203
|
isValidRefundAuthorization(swapData: EVMSwapData, signature: SignatureData): Promise<Buffer | null> {
|
|
171
|
-
return this.
|
|
204
|
+
return this._Refund.isSignatureValid(swapData, signature.timeout, signature.prefix, signature.signature);
|
|
172
205
|
}
|
|
173
206
|
|
|
174
207
|
/**
|
|
@@ -228,12 +261,12 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
228
261
|
* @inheritDoc
|
|
229
262
|
*/
|
|
230
263
|
getHashForTxId(txId: string, confirmations: number) {
|
|
231
|
-
const chainTxIdHandler = this.
|
|
264
|
+
const chainTxIdHandler = this._claimHandlersBySwapType[ChainSwapType.CHAIN_TXID];
|
|
232
265
|
if(chainTxIdHandler==null) throw new Error("Claim handler for CHAIN_TXID not found!");
|
|
233
266
|
return Buffer.from(chainTxIdHandler.getCommitment({
|
|
234
267
|
txId,
|
|
235
268
|
confirmations,
|
|
236
|
-
btcRelay: this.
|
|
269
|
+
btcRelay: this._btcRelay
|
|
237
270
|
}).slice(2), "hex");
|
|
238
271
|
}
|
|
239
272
|
|
|
@@ -243,23 +276,23 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
243
276
|
getHashForOnchain(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
|
|
244
277
|
let result: string;
|
|
245
278
|
if(nonce==null || nonce === 0n) {
|
|
246
|
-
const chainHandler = this.
|
|
279
|
+
const chainHandler = this._claimHandlersBySwapType[ChainSwapType.CHAIN];
|
|
247
280
|
if(chainHandler==null) throw new Error("Claim handler for CHAIN not found!");
|
|
248
281
|
result = chainHandler.getCommitment({
|
|
249
282
|
output: outputScript,
|
|
250
283
|
amount,
|
|
251
284
|
confirmations,
|
|
252
|
-
btcRelay: this.
|
|
285
|
+
btcRelay: this._btcRelay
|
|
253
286
|
});
|
|
254
287
|
} else {
|
|
255
|
-
const chainNoncedHandler = this.
|
|
288
|
+
const chainNoncedHandler = this._claimHandlersBySwapType[ChainSwapType.CHAIN_NONCED];
|
|
256
289
|
if(chainNoncedHandler==null) throw new Error("Claim handler for CHAIN_NONCED not found!");
|
|
257
290
|
result = chainNoncedHandler.getCommitment({
|
|
258
291
|
output: outputScript,
|
|
259
292
|
amount,
|
|
260
293
|
nonce,
|
|
261
294
|
confirmations,
|
|
262
|
-
btcRelay: this.
|
|
295
|
+
btcRelay: this._btcRelay
|
|
263
296
|
});
|
|
264
297
|
}
|
|
265
298
|
return Buffer.from(result.slice(2), "hex");
|
|
@@ -269,7 +302,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
269
302
|
* @inheritDoc
|
|
270
303
|
*/
|
|
271
304
|
getHashForHtlc(paymentHash: Buffer): Buffer {
|
|
272
|
-
const htlcHandler = this.
|
|
305
|
+
const htlcHandler = this._claimHandlersBySwapType[ChainSwapType.HTLC];
|
|
273
306
|
if(htlcHandler==null) throw new Error("Claim handler for HTLC not found!");
|
|
274
307
|
return Buffer.from(htlcHandler.getCommitment(paymentHash).slice(2), "hex");
|
|
275
308
|
}
|
|
@@ -315,7 +348,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
315
348
|
};
|
|
316
349
|
},
|
|
317
350
|
getClaimResult: async () => {
|
|
318
|
-
const events = await this.
|
|
351
|
+
const events = await this._Events.getContractBlockEvents(
|
|
319
352
|
["Claim"],
|
|
320
353
|
[null, null, "0x"+escrowHash],
|
|
321
354
|
blockHeight, blockHeight
|
|
@@ -324,7 +357,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
324
357
|
return events[0].args.witnessResult;
|
|
325
358
|
},
|
|
326
359
|
getClaimTxId: async () => {
|
|
327
|
-
const events = await this.
|
|
360
|
+
const events = await this._Events.getContractBlockEvents(
|
|
328
361
|
["Claim"],
|
|
329
362
|
[null, null, "0x"+escrowHash],
|
|
330
363
|
blockHeight, blockHeight
|
|
@@ -343,7 +376,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
343
376
|
};
|
|
344
377
|
},
|
|
345
378
|
getRefundTxId: async () => {
|
|
346
|
-
const events = await this.
|
|
379
|
+
const events = await this._Events.getContractBlockEvents(
|
|
347
380
|
["Refund"],
|
|
348
381
|
[null, null, "0x"+escrowHash],
|
|
349
382
|
blockHeight, blockHeight
|
|
@@ -374,7 +407,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
374
407
|
promises.push(this.getCommitStatus(signer, swapData).then(val => {
|
|
375
408
|
result[swapData.getEscrowHash()] = val;
|
|
376
409
|
}));
|
|
377
|
-
if(promises.length>=this.Chain.
|
|
410
|
+
if(promises.length>=this.Chain._config.maxParallelCalls) {
|
|
378
411
|
await Promise.all(promises);
|
|
379
412
|
promises = [];
|
|
380
413
|
}
|
|
@@ -383,6 +416,9 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
383
416
|
return result;
|
|
384
417
|
}
|
|
385
418
|
|
|
419
|
+
/**
|
|
420
|
+
* @inheritDoc
|
|
421
|
+
*/
|
|
386
422
|
async getHistoricalSwaps(signer: string, startBlockheight?: number): Promise<{
|
|
387
423
|
swaps: {
|
|
388
424
|
[escrowHash: string]: {
|
|
@@ -425,7 +461,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
425
461
|
const event = _event as TypedEventLog<EscrowManager["filters"]["Initialize"]>;
|
|
426
462
|
const claimHandlerHex = event.args.claimHandler;
|
|
427
463
|
|
|
428
|
-
const claimHandler = this.
|
|
464
|
+
const claimHandler = this._claimHandlersByAddress[claimHandlerHex.toLowerCase()];
|
|
429
465
|
if(claimHandler==null) {
|
|
430
466
|
logger.warn(`getHistoricalSwaps(): Unknown claim handler in tx ${event.transactionHash} with claim handler: `+claimHandlerHex);
|
|
431
467
|
return null;
|
|
@@ -490,13 +526,13 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
490
526
|
};
|
|
491
527
|
|
|
492
528
|
//We have to fetch separately the different directions
|
|
493
|
-
await this.
|
|
529
|
+
await this._Events.findInContractEventsForward(
|
|
494
530
|
["Initialize", "Claim", "Refund"],
|
|
495
531
|
[signer, null],
|
|
496
532
|
processor,
|
|
497
533
|
startBlockheight
|
|
498
534
|
);
|
|
499
|
-
await this.
|
|
535
|
+
await this._Events.findInContractEventsForward(
|
|
500
536
|
["Initialize", "Claim", "Refund"],
|
|
501
537
|
[null, signer],
|
|
502
538
|
processor,
|
|
@@ -542,14 +578,14 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
542
578
|
claimerBounty: bigint,
|
|
543
579
|
depositToken: string = this.Chain.Tokens.getNativeCurrencyAddress()
|
|
544
580
|
): Promise<EVMSwapData> {
|
|
545
|
-
const claimHandler = this.
|
|
581
|
+
const claimHandler = this._claimHandlersBySwapType?.[type];
|
|
546
582
|
if(claimHandler==null) throw new Error(`Claim handler unknown for swap type: ${ChainSwapType[type]}!`);
|
|
547
583
|
|
|
548
584
|
return Promise.resolve(new EVMSwapData(
|
|
549
585
|
offerer,
|
|
550
586
|
claimer,
|
|
551
587
|
token,
|
|
552
|
-
this.
|
|
588
|
+
this._timelockRefundHandler.address,
|
|
553
589
|
claimHandler.address,
|
|
554
590
|
payOut,
|
|
555
591
|
payIn,
|
|
@@ -565,8 +601,17 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
565
601
|
));
|
|
566
602
|
}
|
|
567
603
|
|
|
604
|
+
/**
|
|
605
|
+
* Recursively scans call traces and extracts swap data from `initialize(...)` calldata
|
|
606
|
+
* for the specified escrow hash.
|
|
607
|
+
*
|
|
608
|
+
* @param call Trace call node to inspect
|
|
609
|
+
* @param escrowHash Escrow hash to match
|
|
610
|
+
* @param claimHandler Claim handler used to deserialize claim-specific fields
|
|
611
|
+
* @private
|
|
612
|
+
*/
|
|
568
613
|
findInitSwapData(call: EVMTxTrace, escrowHash: string, claimHandler: IClaimHandler<any, any>): EVMSwapData | null {
|
|
569
|
-
if(call.to.toLowerCase() === this.
|
|
614
|
+
if(call.to.toLowerCase() === this._contractAddress.toLowerCase()) {
|
|
570
615
|
const _result = this.parseCalldata(call.input);
|
|
571
616
|
if(_result!=null && _result.name==="initialize") {
|
|
572
617
|
const result = _result as TypedFunctionCall<
|
|
@@ -610,18 +655,18 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
610
655
|
balance: bigint,
|
|
611
656
|
reputation: IntermediaryReputationType
|
|
612
657
|
}> {
|
|
613
|
-
return this.
|
|
658
|
+
return this._LpVault.getIntermediaryData(address, token);
|
|
614
659
|
}
|
|
615
660
|
|
|
616
661
|
/**
|
|
617
662
|
* @inheritDoc
|
|
618
663
|
*/
|
|
619
664
|
getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
|
|
620
|
-
return this.
|
|
665
|
+
return this._LpVault.getIntermediaryReputation(address, token);
|
|
621
666
|
}
|
|
622
667
|
|
|
623
|
-
getIntermediaryBalance(address: string, token: string): Promise<bigint> {
|
|
624
|
-
return this.
|
|
668
|
+
private getIntermediaryBalance(address: string, token: string): Promise<bigint> {
|
|
669
|
+
return this._LpVault.getIntermediaryBalance(address, token);
|
|
625
670
|
}
|
|
626
671
|
|
|
627
672
|
////////////////////////////////////////////
|
|
@@ -638,7 +683,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
638
683
|
feeRate?: string,
|
|
639
684
|
skipAtaCheck?: boolean
|
|
640
685
|
): Promise<EVMTx[]> {
|
|
641
|
-
return this.
|
|
686
|
+
return this._Claim.txsClaimWithSecret(typeof(signer)==="string" ? signer : signer.getAddress(), swapData, secret, checkExpiry, feeRate)
|
|
642
687
|
}
|
|
643
688
|
|
|
644
689
|
/**
|
|
@@ -655,7 +700,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
655
700
|
initAta?: boolean,
|
|
656
701
|
feeRate?: string
|
|
657
702
|
): Promise<EVMTx[]> {
|
|
658
|
-
return this.
|
|
703
|
+
return this._Claim.txsClaimWithTxData(
|
|
659
704
|
typeof(signer)==="string" ? signer : signer.getAddress(),
|
|
660
705
|
swapData,
|
|
661
706
|
tx,
|
|
@@ -671,35 +716,35 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
671
716
|
* @inheritDoc
|
|
672
717
|
*/
|
|
673
718
|
txsRefund(signer: string, swapData: EVMSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
|
|
674
|
-
return this.
|
|
719
|
+
return this._Refund.txsRefund(signer, swapData, check, feeRate);
|
|
675
720
|
}
|
|
676
721
|
|
|
677
722
|
/**
|
|
678
723
|
* @inheritDoc
|
|
679
724
|
*/
|
|
680
725
|
txsRefundWithAuthorization(signer: string, swapData: EVMSwapData, signature: SignatureData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
|
|
681
|
-
return this.
|
|
726
|
+
return this._Refund.txsRefundWithAuthorization(signer, swapData, signature.timeout, signature.prefix, signature.signature, check, feeRate);
|
|
682
727
|
}
|
|
683
728
|
|
|
684
729
|
/**
|
|
685
730
|
* @inheritDoc
|
|
686
731
|
*/
|
|
687
732
|
txsInit(signer: string, swapData: EVMSwapData, signature: SignatureData, skipChecks?: boolean, feeRate?: string): Promise<EVMTx[]> {
|
|
688
|
-
return this.
|
|
733
|
+
return this._Init.txsInit(signer, swapData, signature.timeout, signature.prefix, signature.signature, skipChecks, feeRate);
|
|
689
734
|
}
|
|
690
735
|
|
|
691
736
|
/**
|
|
692
737
|
* @inheritDoc
|
|
693
738
|
*/
|
|
694
739
|
txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
|
|
695
|
-
return this.
|
|
740
|
+
return this._LpVault.txsWithdraw(signer, token, amount, feeRate);
|
|
696
741
|
}
|
|
697
742
|
|
|
698
743
|
/**
|
|
699
744
|
* @inheritDoc
|
|
700
745
|
*/
|
|
701
746
|
txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
|
|
702
|
-
return this.
|
|
747
|
+
return this._LpVault.txsDeposit(signer, token, amount, feeRate);
|
|
703
748
|
}
|
|
704
749
|
|
|
705
750
|
////////////////////////////////////////////
|
|
@@ -715,7 +760,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
715
760
|
initAta?: boolean,
|
|
716
761
|
txOptions?: TransactionConfirmationOptions
|
|
717
762
|
): Promise<string> {
|
|
718
|
-
const result = await this.
|
|
763
|
+
const result = await this._Claim.txsClaimWithSecret(signer.getAddress(), swapData, secret, checkExpiry, txOptions?.feeRate);
|
|
719
764
|
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
720
765
|
return signature;
|
|
721
766
|
}
|
|
@@ -734,7 +779,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
734
779
|
initAta?: boolean,
|
|
735
780
|
txOptions?: TransactionConfirmationOptions
|
|
736
781
|
): Promise<string> {
|
|
737
|
-
const txs = await this.
|
|
782
|
+
const txs = await this._Claim.txsClaimWithTxData(
|
|
738
783
|
signer.getAddress(), swapData, tx, requiredConfirmations, vout,
|
|
739
784
|
commitedHeader, synchronizer, txOptions?.feeRate
|
|
740
785
|
);
|
|
@@ -812,7 +857,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
812
857
|
amount: bigint,
|
|
813
858
|
txOptions?: TransactionConfirmationOptions
|
|
814
859
|
): Promise<string> {
|
|
815
|
-
const txs = await this.
|
|
860
|
+
const txs = await this._LpVault.txsWithdraw(signer.getAddress(), token, amount, txOptions?.feeRate);
|
|
816
861
|
const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
817
862
|
return txId;
|
|
818
863
|
}
|
|
@@ -826,7 +871,7 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
826
871
|
amount: bigint,
|
|
827
872
|
txOptions?: TransactionConfirmationOptions
|
|
828
873
|
): Promise<string> {
|
|
829
|
-
const txs = await this.
|
|
874
|
+
const txs = await this._LpVault.txsDeposit(signer.getAddress(), token, amount, txOptions?.feeRate);
|
|
830
875
|
const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
831
876
|
return txId;
|
|
832
877
|
}
|
|
@@ -865,21 +910,21 @@ export class EVMSwapContract<ChainId extends string = string>
|
|
|
865
910
|
* @inheritDoc
|
|
866
911
|
*/
|
|
867
912
|
getClaimFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
|
|
868
|
-
return this.
|
|
913
|
+
return this._Claim.getClaimFee(swapData, feeRate);
|
|
869
914
|
}
|
|
870
915
|
|
|
871
916
|
/**
|
|
872
917
|
* @inheritDoc
|
|
873
918
|
*/
|
|
874
919
|
getCommitFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
|
|
875
|
-
return this.
|
|
920
|
+
return this._Init.getInitFee(swapData, feeRate);
|
|
876
921
|
}
|
|
877
922
|
|
|
878
923
|
/**
|
|
879
924
|
* @inheritDoc
|
|
880
925
|
*/
|
|
881
926
|
getRefundFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
|
|
882
|
-
return this.
|
|
927
|
+
return this._Refund.getRefundFee(swapData, feeRate);
|
|
883
928
|
}
|
|
884
929
|
|
|
885
930
|
}
|
|
@@ -9,11 +9,13 @@ const FLAG_PAY_IN: bigint = 0x02n;
|
|
|
9
9
|
const FLAG_REPUTATION: bigint = 0x04n;
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
+
* Represents swap data for executing PrTLC (on-chain) or HTLC (lightning) based swaps.
|
|
13
|
+
*
|
|
12
14
|
* @category Swaps
|
|
13
15
|
*/
|
|
14
16
|
export class EVMSwapData extends SwapData {
|
|
15
17
|
|
|
16
|
-
static toFlags(val: bigint): {payOut: boolean, payIn: boolean, reputation: boolean, sequence: bigint} {
|
|
18
|
+
private static toFlags(val: bigint): {payOut: boolean, payIn: boolean, reputation: boolean, sequence: bigint} {
|
|
17
19
|
return {
|
|
18
20
|
sequence: val >> 64n,
|
|
19
21
|
payOut: (val & FLAG_PAY_OUT) === FLAG_PAY_OUT,
|
|
@@ -401,16 +403,31 @@ export class EVMSwapData extends SwapData {
|
|
|
401
403
|
return this.offerer.toLowerCase() === address.toLowerCase();
|
|
402
404
|
}
|
|
403
405
|
|
|
406
|
+
/**
|
|
407
|
+
* Checks whether the passed address is specified as the refund handler for the swap.
|
|
408
|
+
*
|
|
409
|
+
* @param address
|
|
410
|
+
*/
|
|
404
411
|
isRefundHandler(address: string): boolean {
|
|
405
412
|
if(!address.startsWith("0x")) address = "0x"+address;
|
|
406
413
|
return this.refundHandler.toLowerCase() === address.toLowerCase();
|
|
407
414
|
}
|
|
408
415
|
|
|
416
|
+
/**
|
|
417
|
+
* Checks whether the passed address is specified as the claim handler for the swap.
|
|
418
|
+
*
|
|
419
|
+
* @param address
|
|
420
|
+
*/
|
|
409
421
|
isClaimHandler(address: string): boolean {
|
|
410
422
|
if(!address.startsWith("0x")) address = "0x"+address;
|
|
411
423
|
return this.claimHandler.toLowerCase() === address.toLowerCase();
|
|
412
424
|
}
|
|
413
425
|
|
|
426
|
+
/**
|
|
427
|
+
* Checks if the passed data matches the swap's claim data.
|
|
428
|
+
*
|
|
429
|
+
* @param data
|
|
430
|
+
*/
|
|
414
431
|
isClaimData(data: string): boolean {
|
|
415
432
|
if(!data.startsWith("0x")) data = "0x"+data;
|
|
416
433
|
return (this.claimData.startsWith("0x") ? this.claimData : "0x"+this.claimData) === data;
|
|
@@ -437,6 +454,9 @@ export class EVMSwapData extends SwapData {
|
|
|
437
454
|
other.successActionCommitment.toLowerCase() === this.successActionCommitment.toLowerCase()
|
|
438
455
|
}
|
|
439
456
|
|
|
457
|
+
/**
|
|
458
|
+
* Serializes the swap data into the EVM escrow-manager struct representation.
|
|
459
|
+
*/
|
|
440
460
|
toEscrowStruct(): EscrowDataStruct {
|
|
441
461
|
return {
|
|
442
462
|
offerer: this.offerer,
|
|
@@ -462,6 +482,12 @@ export class EVMSwapData extends SwapData {
|
|
|
462
482
|
return this.successActionCommitment !== ZeroHash;
|
|
463
483
|
}
|
|
464
484
|
|
|
485
|
+
/**
|
|
486
|
+
* Deserializes swap data from an on-chain escrow struct.
|
|
487
|
+
*
|
|
488
|
+
* @param struct Escrow struct as returned by the contract
|
|
489
|
+
* @param claimHandlerImpl Claim handler implementation used to resolve swap type
|
|
490
|
+
*/
|
|
465
491
|
static deserializeFromStruct(struct: EscrowDataStruct, claimHandlerImpl: IClaimHandler<any, any>): EVMSwapData {
|
|
466
492
|
const {payOut, payIn, reputation, sequence} = EVMSwapData.toFlags(BigInt(struct.flags));
|
|
467
493
|
|
|
@@ -3,6 +3,11 @@ import {EVMContractModule} from "../contract/EVMContractModule";
|
|
|
3
3
|
import {EVMSwapContract} from "./EVMSwapContract";
|
|
4
4
|
import {EVMChainInterface} from "../chain/EVMChainInterface";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Base class for EVM swap submodules operating on the escrow manager contract.
|
|
8
|
+
*
|
|
9
|
+
* @category Internal/Swaps
|
|
10
|
+
*/
|
|
6
11
|
export class EVMSwapModule extends EVMContractModule<EscrowManager, EVMSwapContract> {
|
|
7
12
|
|
|
8
13
|
readonly swapContract: EscrowManager;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import {EVMSwapData} from "../EVMSwapData";
|
|
2
2
|
import {EVMTx} from "../../chain/modules/EVMTransactions";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Generic swap handler interface for commitment and witness generation.
|
|
6
|
+
*
|
|
7
|
+
* @category Internal/Handlers
|
|
8
|
+
*/
|
|
4
9
|
export interface IHandler<TCommitmentData, TWitnessData> {
|
|
5
10
|
|
|
6
11
|
readonly address: string;
|
|
@@ -5,12 +5,27 @@ import {BitcoinTxIdClaimHandler} from "./btc/BitcoinTxIdClaimHandler";
|
|
|
5
5
|
import {BitcoinOutputClaimHandler} from "./btc/BitcoinOutputClaimHandler";
|
|
6
6
|
import {BitcoinNoncedOutputClaimHandler} from "./btc/BitcoinNoncedOutputClaimHandler";
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Base interface for claim handlers with explicit chain swap type.
|
|
10
|
+
*
|
|
11
|
+
* @category Internal/Handlers
|
|
12
|
+
*/
|
|
8
13
|
export interface IClaimHandler<C, W> extends IHandler<C, W> {
|
|
9
14
|
getType(): ChainSwapType;
|
|
10
15
|
}
|
|
11
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Claim handler constructor contract with static metadata used during handler registration.
|
|
19
|
+
*
|
|
20
|
+
* @category Internal/Handlers
|
|
21
|
+
*/
|
|
12
22
|
export type ClaimHandlerType = {gas: number, type: ChainSwapType} & (new (address: string) => IClaimHandler<any, any>);
|
|
13
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Supported claim handler implementations for EVM swap contract initialization.
|
|
26
|
+
*
|
|
27
|
+
* @category Internal/Handlers
|
|
28
|
+
*/
|
|
14
29
|
export const claimHandlersList: ClaimHandlerType[] = [
|
|
15
30
|
HashlockClaimHandler,
|
|
16
31
|
BitcoinTxIdClaimHandler,
|
|
@@ -7,6 +7,11 @@ import {keccak256, solidityPackedKeccak256} from "ethers";
|
|
|
7
7
|
import {EVMSwapData} from "../../../EVMSwapData";
|
|
8
8
|
import {EVMTx} from "../../../../chain/modules/EVMTransactions";
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Commitment payload for nonced output-script bitcoin claims.
|
|
12
|
+
*
|
|
13
|
+
* @category Internal/Handlers
|
|
14
|
+
*/
|
|
10
15
|
export type BitcoinNoncedOutputCommitmentData = {
|
|
11
16
|
output: Buffer,
|
|
12
17
|
amount: bigint,
|
|
@@ -6,11 +6,21 @@ import {EVMTx} from "../../../../chain/modules/EVMTransactions";
|
|
|
6
6
|
import {Transaction} from "@scure/btc-signer";
|
|
7
7
|
import {EVMSwapData} from "../../../EVMSwapData";
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Commitment payload for output-script bitcoin claims.
|
|
11
|
+
*
|
|
12
|
+
* @category Internal/Handlers
|
|
13
|
+
*/
|
|
9
14
|
export type BitcoinOutputCommitmentData = {
|
|
10
15
|
output: Buffer,
|
|
11
16
|
amount: bigint
|
|
12
17
|
};
|
|
13
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Witness input for output-script bitcoin claims.
|
|
21
|
+
*
|
|
22
|
+
* @category Internal/Handlers
|
|
23
|
+
*/
|
|
14
24
|
export type BitcoinOutputWitnessData = BitcoinWitnessData & {
|
|
15
25
|
vout: number
|
|
16
26
|
};
|
|
@@ -5,6 +5,11 @@ import {Buffer} from "buffer";
|
|
|
5
5
|
import {EVMSwapData} from "../../../EVMSwapData";
|
|
6
6
|
import {EVMTx} from "../../../../chain/modules/EVMTransactions";
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Commitment payload for txid-based bitcoin claims.
|
|
10
|
+
*
|
|
11
|
+
* @category Internal/Handlers
|
|
12
|
+
*/
|
|
8
13
|
export type BitcoinTxIdCommitmentData = {
|
|
9
14
|
txId: string
|
|
10
15
|
};
|