@atomiqlabs/chain-evm 1.0.0-dev.75 → 1.0.0-dev.77
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/LICENSE +201 -201
- package/dist/chains/botanix/BotanixChainType.d.ts +13 -13
- package/dist/chains/botanix/BotanixChainType.js +2 -2
- package/dist/chains/botanix/BotanixInitializer.d.ts +30 -30
- package/dist/chains/botanix/BotanixInitializer.js +122 -122
- package/dist/chains/citrea/CitreaBtcRelay.d.ts +21 -21
- package/dist/chains/citrea/CitreaBtcRelay.js +43 -43
- package/dist/chains/citrea/CitreaChainType.d.ts +13 -13
- package/dist/chains/citrea/CitreaChainType.js +2 -2
- package/dist/chains/citrea/CitreaFees.d.ts +29 -29
- package/dist/chains/citrea/CitreaFees.js +67 -67
- package/dist/chains/citrea/CitreaInitializer.d.ts +30 -30
- package/dist/chains/citrea/CitreaInitializer.js +129 -129
- package/dist/chains/citrea/CitreaSpvVaultContract.d.ts +15 -15
- package/dist/chains/citrea/CitreaSpvVaultContract.js +74 -74
- package/dist/chains/citrea/CitreaSwapContract.d.ts +22 -22
- package/dist/chains/citrea/CitreaSwapContract.js +96 -96
- package/dist/chains/citrea/CitreaTokens.d.ts +9 -9
- package/dist/chains/citrea/CitreaTokens.js +20 -20
- package/dist/evm/btcrelay/BtcRelayAbi.d.ts +198 -198
- package/dist/evm/btcrelay/BtcRelayAbi.js +261 -261
- package/dist/evm/btcrelay/BtcRelayTypechain.d.ts +172 -172
- package/dist/evm/btcrelay/BtcRelayTypechain.js +2 -2
- package/dist/evm/btcrelay/EVMBtcRelay.d.ts +197 -197
- package/dist/evm/btcrelay/EVMBtcRelay.js +435 -435
- package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +33 -33
- package/dist/evm/btcrelay/headers/EVMBtcHeader.js +84 -84
- package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +56 -56
- package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +123 -123
- package/dist/evm/chain/EVMChainInterface.d.ts +51 -51
- package/dist/evm/chain/EVMChainInterface.js +89 -89
- package/dist/evm/chain/EVMModule.d.ts +9 -9
- package/dist/evm/chain/EVMModule.js +13 -13
- package/dist/evm/chain/modules/ERC20Abi.d.ts +168 -168
- package/dist/evm/chain/modules/ERC20Abi.js +225 -225
- package/dist/evm/chain/modules/EVMAddresses.d.ts +10 -10
- package/dist/evm/chain/modules/EVMAddresses.js +30 -30
- package/dist/evm/chain/modules/EVMBlocks.d.ts +20 -20
- package/dist/evm/chain/modules/EVMBlocks.js +64 -64
- package/dist/evm/chain/modules/EVMEvents.d.ts +36 -36
- package/dist/evm/chain/modules/EVMEvents.js +122 -122
- package/dist/evm/chain/modules/EVMFees.d.ts +36 -36
- package/dist/evm/chain/modules/EVMFees.js +74 -74
- package/dist/evm/chain/modules/EVMSignatures.d.ts +29 -29
- package/dist/evm/chain/modules/EVMSignatures.js +68 -68
- package/dist/evm/chain/modules/EVMTokens.d.ts +70 -70
- package/dist/evm/chain/modules/EVMTokens.js +142 -142
- package/dist/evm/chain/modules/EVMTransactions.d.ts +94 -94
- package/dist/evm/chain/modules/EVMTransactions.js +286 -286
- package/dist/evm/contract/EVMContractBase.d.ts +22 -22
- package/dist/evm/contract/EVMContractBase.js +34 -34
- package/dist/evm/contract/EVMContractModule.d.ts +8 -8
- package/dist/evm/contract/EVMContractModule.js +11 -11
- package/dist/evm/contract/modules/EVMContractEvents.d.ts +42 -42
- package/dist/evm/contract/modules/EVMContractEvents.js +75 -75
- package/dist/evm/events/EVMChainEvents.d.ts +22 -22
- package/dist/evm/events/EVMChainEvents.js +69 -69
- package/dist/evm/events/EVMChainEventsBrowser.d.ts +102 -102
- package/dist/evm/events/EVMChainEventsBrowser.js +413 -404
- package/dist/evm/providers/JsonRpcProviderWithRetries.d.ts +15 -15
- package/dist/evm/providers/JsonRpcProviderWithRetries.js +19 -19
- package/dist/evm/providers/ReconnectingWebSocketProvider.d.ts +22 -22
- package/dist/evm/providers/ReconnectingWebSocketProvider.js +87 -87
- package/dist/evm/providers/SocketProvider.d.ts +111 -111
- package/dist/evm/providers/SocketProvider.js +334 -334
- package/dist/evm/providers/WebSocketProviderWithRetries.d.ts +17 -17
- package/dist/evm/providers/WebSocketProviderWithRetries.js +19 -19
- package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +79 -79
- package/dist/evm/spv_swap/EVMSpvVaultContract.js +482 -482
- package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +39 -39
- package/dist/evm/spv_swap/EVMSpvVaultData.js +0 -180
- package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +19 -19
- package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +55 -55
- package/dist/evm/spv_swap/SpvVaultContractAbi.d.ts +91 -91
- package/dist/evm/spv_swap/SpvVaultContractAbi.js +849 -849
- package/dist/evm/spv_swap/SpvVaultContractTypechain.d.ts +450 -450
- package/dist/evm/spv_swap/SpvVaultContractTypechain.js +2 -2
- package/dist/evm/swaps/EVMSwapContract.d.ts +193 -193
- package/dist/evm/swaps/EVMSwapContract.js +378 -378
- package/dist/evm/swaps/EVMSwapData.d.ts +66 -66
- package/dist/evm/swaps/EVMSwapData.js +260 -260
- package/dist/evm/swaps/EVMSwapModule.d.ts +9 -9
- package/dist/evm/swaps/EVMSwapModule.js +11 -11
- package/dist/evm/swaps/EscrowManagerAbi.d.ts +120 -120
- package/dist/evm/swaps/EscrowManagerAbi.js +985 -985
- package/dist/evm/swaps/EscrowManagerTypechain.d.ts +475 -475
- package/dist/evm/swaps/EscrowManagerTypechain.js +2 -2
- package/dist/evm/swaps/handlers/IHandler.d.ts +13 -13
- package/dist/evm/swaps/handlers/IHandler.js +2 -2
- package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +10 -10
- package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +13 -13
- package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.d.ts +20 -20
- package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.js +39 -39
- package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -24
- package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +59 -59
- package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -25
- package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +51 -51
- package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +21 -21
- package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +28 -28
- package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +48 -48
- package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +63 -63
- package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -17
- package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.js +28 -28
- package/dist/evm/swaps/modules/EVMLpVault.d.ts +69 -69
- package/dist/evm/swaps/modules/EVMLpVault.js +134 -134
- package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +54 -54
- package/dist/evm/swaps/modules/EVMSwapClaim.js +137 -137
- package/dist/evm/swaps/modules/EVMSwapInit.d.ts +88 -88
- package/dist/evm/swaps/modules/EVMSwapInit.js +274 -274
- package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +62 -62
- package/dist/evm/swaps/modules/EVMSwapRefund.js +167 -167
- package/dist/evm/typechain/common.d.ts +50 -50
- package/dist/evm/typechain/common.js +2 -2
- package/dist/evm/wallet/EVMBrowserSigner.d.ts +5 -5
- package/dist/evm/wallet/EVMBrowserSigner.js +11 -11
- package/dist/evm/wallet/EVMPersistentSigner.d.ts +29 -29
- package/dist/evm/wallet/EVMPersistentSigner.js +222 -222
- package/dist/evm/wallet/EVMSigner.d.ts +11 -11
- package/dist/evm/wallet/EVMSigner.js +24 -24
- package/dist/index.d.ts +44 -44
- package/dist/index.js +60 -60
- package/dist/utils/Utils.d.ts +17 -17
- package/dist/utils/Utils.js +81 -81
- package/package.json +39 -39
- package/src/chains/botanix/BotanixChainType.ts +28 -28
- package/src/chains/botanix/BotanixInitializer.ts +171 -171
- package/src/chains/citrea/CitreaBtcRelay.ts +57 -57
- package/src/chains/citrea/CitreaChainType.ts +28 -28
- package/src/chains/citrea/CitreaFees.ts +77 -77
- package/src/chains/citrea/CitreaInitializer.ts +178 -178
- package/src/chains/citrea/CitreaSpvVaultContract.ts +75 -75
- package/src/chains/citrea/CitreaSwapContract.ts +102 -102
- package/src/chains/citrea/CitreaTokens.ts +21 -21
- package/src/evm/btcrelay/BtcRelayAbi.ts +258 -258
- package/src/evm/btcrelay/BtcRelayTypechain.ts +371 -371
- package/src/evm/btcrelay/EVMBtcRelay.ts +537 -537
- package/src/evm/btcrelay/headers/EVMBtcHeader.ts +109 -109
- package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +152 -152
- package/src/evm/chain/EVMChainInterface.ts +155 -155
- package/src/evm/chain/EVMModule.ts +21 -21
- package/src/evm/chain/modules/ERC20Abi.ts +222 -222
- package/src/evm/chain/modules/EVMAddresses.ts +28 -28
- package/src/evm/chain/modules/EVMBlocks.ts +75 -75
- package/src/evm/chain/modules/EVMEvents.ts +139 -139
- package/src/evm/chain/modules/EVMFees.ts +104 -104
- package/src/evm/chain/modules/EVMSignatures.ts +76 -76
- package/src/evm/chain/modules/EVMTokens.ts +155 -155
- package/src/evm/chain/modules/EVMTransactions.ts +325 -325
- package/src/evm/contract/EVMContractBase.ts +63 -63
- package/src/evm/contract/EVMContractModule.ts +16 -16
- package/src/evm/contract/modules/EVMContractEvents.ts +102 -102
- package/src/evm/events/EVMChainEvents.ts +82 -82
- package/src/evm/events/EVMChainEventsBrowser.ts +534 -525
- package/src/evm/providers/JsonRpcProviderWithRetries.ts +24 -24
- package/src/evm/providers/ReconnectingWebSocketProvider.ts +101 -101
- package/src/evm/providers/SocketProvider.ts +368 -368
- package/src/evm/providers/WebSocketProviderWithRetries.ts +27 -27
- package/src/evm/spv_swap/EVMSpvVaultContract.ts +615 -615
- package/src/evm/spv_swap/EVMSpvVaultData.ts +224 -224
- package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +70 -70
- package/src/evm/spv_swap/SpvVaultContractAbi.ts +846 -846
- package/src/evm/spv_swap/SpvVaultContractTypechain.ts +685 -685
- package/src/evm/swaps/EVMSwapContract.ts +600 -600
- package/src/evm/swaps/EVMSwapData.ts +378 -378
- package/src/evm/swaps/EVMSwapModule.ts +16 -16
- package/src/evm/swaps/EscrowManagerAbi.ts +982 -982
- package/src/evm/swaps/EscrowManagerTypechain.ts +723 -723
- package/src/evm/swaps/handlers/IHandler.ts +17 -17
- package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +20 -20
- package/src/evm/swaps/handlers/claim/HashlockClaimHandler.ts +46 -46
- package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +82 -82
- package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +76 -76
- package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +46 -46
- package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +115 -115
- package/src/evm/swaps/handlers/refund/TimelockRefundHandler.ts +37 -37
- package/src/evm/swaps/modules/EVMLpVault.ts +154 -154
- package/src/evm/swaps/modules/EVMSwapClaim.ts +172 -172
- package/src/evm/swaps/modules/EVMSwapInit.ts +328 -328
- package/src/evm/swaps/modules/EVMSwapRefund.ts +229 -229
- package/src/evm/typechain/common.ts +131 -131
- package/src/evm/wallet/EVMBrowserSigner.ts +11 -11
- package/src/evm/wallet/EVMPersistentSigner.ts +298 -298
- package/src/evm/wallet/EVMSigner.ts +31 -31
- package/src/index.ts +53 -53
- package/src/utils/Utils.ts +92 -92
|
@@ -1,378 +1,378 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EVMSwapContract = void 0;
|
|
4
|
-
const base_1 = require("@atomiqlabs/base");
|
|
5
|
-
const buffer_1 = require("buffer");
|
|
6
|
-
const TimelockRefundHandler_1 = require("./handlers/refund/TimelockRefundHandler");
|
|
7
|
-
const ClaimHandlers_1 = require("./handlers/claim/ClaimHandlers");
|
|
8
|
-
const sha2_1 = require("@noble/hashes/sha2");
|
|
9
|
-
const EVMContractBase_1 = require("../contract/EVMContractBase");
|
|
10
|
-
const EVMSwapData_1 = require("./EVMSwapData");
|
|
11
|
-
const EscrowManagerAbi_1 = require("./EscrowManagerAbi");
|
|
12
|
-
const ethers_1 = require("ethers");
|
|
13
|
-
const EVMLpVault_1 = require("./modules/EVMLpVault");
|
|
14
|
-
const EVMSwapInit_1 = require("./modules/EVMSwapInit");
|
|
15
|
-
const EVMSwapRefund_1 = require("./modules/EVMSwapRefund");
|
|
16
|
-
const EVMSwapClaim_1 = require("./modules/EVMSwapClaim");
|
|
17
|
-
const ESCROW_STATE_COMMITTED = 1;
|
|
18
|
-
const ESCROW_STATE_CLAIMED = 2;
|
|
19
|
-
const ESCROW_STATE_REFUNDED = 3;
|
|
20
|
-
class EVMSwapContract extends EVMContractBase_1.EVMContractBase {
|
|
21
|
-
constructor(chainInterface, btcRelay, contractAddress, handlerAddresses) {
|
|
22
|
-
super(chainInterface, contractAddress, EscrowManagerAbi_1.EscrowManagerAbi);
|
|
23
|
-
this.supportsInitWithoutClaimer = true;
|
|
24
|
-
////////////////////////
|
|
25
|
-
//// Timeouts
|
|
26
|
-
this.claimWithSecretTimeout = 180;
|
|
27
|
-
this.claimWithTxDataTimeout = 180;
|
|
28
|
-
this.refundTimeout = 180;
|
|
29
|
-
this.claimGracePeriod = 10 * 60;
|
|
30
|
-
this.refundGracePeriod = 10 * 60;
|
|
31
|
-
this.authGracePeriod = 30;
|
|
32
|
-
////////////////////////
|
|
33
|
-
//// Handlers
|
|
34
|
-
this.claimHandlersByAddress = {};
|
|
35
|
-
this.claimHandlersBySwapType = {};
|
|
36
|
-
this.refundHandlersByAddress = {};
|
|
37
|
-
this.chainId = chainInterface.chainId;
|
|
38
|
-
this.Init = new EVMSwapInit_1.EVMSwapInit(chainInterface, this);
|
|
39
|
-
this.Refund = new EVMSwapRefund_1.EVMSwapRefund(chainInterface, this);
|
|
40
|
-
this.Claim = new EVMSwapClaim_1.EVMSwapClaim(chainInterface, this);
|
|
41
|
-
this.LpVault = new EVMLpVault_1.EVMLpVault(chainInterface, this);
|
|
42
|
-
this.btcRelay = btcRelay;
|
|
43
|
-
ClaimHandlers_1.claimHandlersList.forEach(handlerCtor => {
|
|
44
|
-
const handler = new handlerCtor(handlerAddresses.claim[handlerCtor.type]);
|
|
45
|
-
this.claimHandlersByAddress[handler.address.toLowerCase()] = handler;
|
|
46
|
-
this.claimHandlersBySwapType[handlerCtor.type] = handler;
|
|
47
|
-
});
|
|
48
|
-
this.timelockRefundHandler = new TimelockRefundHandler_1.TimelockRefundHandler(handlerAddresses.refund.timelock);
|
|
49
|
-
this.refundHandlersByAddress[this.timelockRefundHandler.address.toLowerCase()] = this.timelockRefundHandler;
|
|
50
|
-
}
|
|
51
|
-
async start() {
|
|
52
|
-
}
|
|
53
|
-
////////////////////////////////////////////
|
|
54
|
-
//// Signatures
|
|
55
|
-
preFetchForInitSignatureVerification() {
|
|
56
|
-
return this.Init.preFetchForInitSignatureVerification();
|
|
57
|
-
}
|
|
58
|
-
getInitSignature(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate) {
|
|
59
|
-
return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout);
|
|
60
|
-
}
|
|
61
|
-
isValidInitAuthorization(sender, swapData, { timeout, prefix, signature }, feeRate, preFetchedData) {
|
|
62
|
-
return this.Init.isSignatureValid(sender, swapData, timeout, prefix, signature, preFetchedData);
|
|
63
|
-
}
|
|
64
|
-
getInitAuthorizationExpiry(swapData, { timeout, prefix, signature }, preFetchedData) {
|
|
65
|
-
return this.Init.getSignatureExpiry(timeout);
|
|
66
|
-
}
|
|
67
|
-
isInitAuthorizationExpired(swapData, { timeout, prefix, signature }) {
|
|
68
|
-
return this.Init.isSignatureExpired(timeout);
|
|
69
|
-
}
|
|
70
|
-
getRefundSignature(signer, swapData, authorizationTimeout) {
|
|
71
|
-
return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
|
|
72
|
-
}
|
|
73
|
-
isValidRefundAuthorization(swapData, { timeout, prefix, signature }) {
|
|
74
|
-
return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
|
|
75
|
-
}
|
|
76
|
-
getDataSignature(signer, data) {
|
|
77
|
-
return this.Chain.Signatures.getDataSignature(signer, data);
|
|
78
|
-
}
|
|
79
|
-
isValidDataSignature(data, signature, publicKey) {
|
|
80
|
-
return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
|
|
81
|
-
}
|
|
82
|
-
////////////////////////////////////////////
|
|
83
|
-
//// Swap data utils
|
|
84
|
-
/**
|
|
85
|
-
* Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
|
|
86
|
-
*
|
|
87
|
-
* @param signer
|
|
88
|
-
* @param data
|
|
89
|
-
*/
|
|
90
|
-
async isClaimable(signer, data) {
|
|
91
|
-
if (!data.isClaimer(signer))
|
|
92
|
-
return false;
|
|
93
|
-
if (await this.isExpired(signer, data))
|
|
94
|
-
return false;
|
|
95
|
-
return await this.isCommited(data);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
|
|
99
|
-
*
|
|
100
|
-
* @param swapData
|
|
101
|
-
*/
|
|
102
|
-
async isCommited(swapData) {
|
|
103
|
-
const data = await this.contract.getHashState("0x" + swapData.getEscrowHash());
|
|
104
|
-
return Number(data.state) === ESCROW_STATE_COMMITTED;
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
|
|
108
|
-
* the swap expires a bit sooner than it should've & for the offerer it expires a bit later
|
|
109
|
-
*
|
|
110
|
-
* @param signer
|
|
111
|
-
* @param data
|
|
112
|
-
*/
|
|
113
|
-
isExpired(signer, data) {
|
|
114
|
-
let currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
115
|
-
if (data.isClaimer(signer))
|
|
116
|
-
currentTimestamp = currentTimestamp + BigInt(this.claimGracePeriod);
|
|
117
|
-
if (data.isOfferer(signer))
|
|
118
|
-
currentTimestamp = currentTimestamp - BigInt(this.refundGracePeriod);
|
|
119
|
-
return Promise.resolve(data.getExpiry() < currentTimestamp);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
|
|
123
|
-
* is still commited
|
|
124
|
-
*
|
|
125
|
-
* @param signer
|
|
126
|
-
* @param data
|
|
127
|
-
*/
|
|
128
|
-
async isRequestRefundable(signer, data) {
|
|
129
|
-
//Swap can only be refunded by the offerer
|
|
130
|
-
if (!data.isOfferer(signer))
|
|
131
|
-
return false;
|
|
132
|
-
if (!(await this.isExpired(signer, data)))
|
|
133
|
-
return false;
|
|
134
|
-
return await this.isCommited(data);
|
|
135
|
-
}
|
|
136
|
-
getHashForTxId(txId, confirmations) {
|
|
137
|
-
return buffer_1.Buffer.from(this.claimHandlersBySwapType[base_1.ChainSwapType.CHAIN_TXID].getCommitment({
|
|
138
|
-
txId,
|
|
139
|
-
confirmations,
|
|
140
|
-
btcRelay: this.btcRelay
|
|
141
|
-
}).slice(2), "hex");
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Get the swap payment hash to be used for an on-chain swap, uses poseidon hash of the value
|
|
145
|
-
*
|
|
146
|
-
* @param outputScript output script required to claim the swap
|
|
147
|
-
* @param amount sats sent required to claim the swap
|
|
148
|
-
* @param confirmations
|
|
149
|
-
* @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
|
|
150
|
-
*/
|
|
151
|
-
getHashForOnchain(outputScript, amount, confirmations, nonce) {
|
|
152
|
-
let result;
|
|
153
|
-
if (nonce == null || nonce === 0n) {
|
|
154
|
-
result = this.claimHandlersBySwapType[base_1.ChainSwapType.CHAIN].getCommitment({
|
|
155
|
-
output: outputScript,
|
|
156
|
-
amount,
|
|
157
|
-
confirmations,
|
|
158
|
-
btcRelay: this.btcRelay
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
result = this.claimHandlersBySwapType[base_1.ChainSwapType.CHAIN_NONCED].getCommitment({
|
|
163
|
-
output: outputScript,
|
|
164
|
-
amount,
|
|
165
|
-
nonce,
|
|
166
|
-
confirmations,
|
|
167
|
-
btcRelay: this.btcRelay
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
return buffer_1.Buffer.from(result.slice(2), "hex");
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Get the swap payment hash to be used for a lightning htlc swap, uses poseidon hash of the sha256 hash of the preimage
|
|
174
|
-
*
|
|
175
|
-
* @param paymentHash payment hash of the HTLC
|
|
176
|
-
*/
|
|
177
|
-
getHashForHtlc(paymentHash) {
|
|
178
|
-
return buffer_1.Buffer.from(this.claimHandlersBySwapType[base_1.ChainSwapType.HTLC].getCommitment(paymentHash).slice(2), "hex");
|
|
179
|
-
}
|
|
180
|
-
getExtraData(outputScript, amount, confirmations, nonce) {
|
|
181
|
-
if (nonce == null)
|
|
182
|
-
nonce = 0n;
|
|
183
|
-
const txoHash = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
|
|
184
|
-
base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
|
|
185
|
-
outputScript
|
|
186
|
-
])));
|
|
187
|
-
return buffer_1.Buffer.concat([
|
|
188
|
-
txoHash,
|
|
189
|
-
base_1.BigIntBufferUtils.toBuffer(nonce, "be", 8),
|
|
190
|
-
base_1.BigIntBufferUtils.toBuffer(BigInt(confirmations), "be", 2)
|
|
191
|
-
]);
|
|
192
|
-
}
|
|
193
|
-
////////////////////////////////////////////
|
|
194
|
-
//// Swap data getters
|
|
195
|
-
/**
|
|
196
|
-
* Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
|
|
197
|
-
* if swap is refundable)
|
|
198
|
-
*
|
|
199
|
-
* @param signer
|
|
200
|
-
* @param data
|
|
201
|
-
*/
|
|
202
|
-
async getCommitStatus(signer, data) {
|
|
203
|
-
const escrowHash = data.getEscrowHash();
|
|
204
|
-
const stateData = await this.contract.getHashState("0x" + escrowHash);
|
|
205
|
-
const state = Number(stateData.state);
|
|
206
|
-
const blockHeight = Number(stateData.finishBlockheight);
|
|
207
|
-
switch (state) {
|
|
208
|
-
case ESCROW_STATE_COMMITTED:
|
|
209
|
-
if (data.isOfferer(signer) && await this.isExpired(signer, data))
|
|
210
|
-
return { type: base_1.SwapCommitStateType.REFUNDABLE };
|
|
211
|
-
return { type: base_1.SwapCommitStateType.COMMITED };
|
|
212
|
-
case ESCROW_STATE_CLAIMED:
|
|
213
|
-
return {
|
|
214
|
-
type: base_1.SwapCommitStateType.PAID,
|
|
215
|
-
getTxBlock: async () => {
|
|
216
|
-
return {
|
|
217
|
-
blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
|
|
218
|
-
blockHeight: blockHeight
|
|
219
|
-
};
|
|
220
|
-
},
|
|
221
|
-
getClaimResult: async () => {
|
|
222
|
-
const events = await this.Events.getContractBlockEvents(["Claim"], [null, null, "0x" + escrowHash], blockHeight, blockHeight);
|
|
223
|
-
return events.length === 0 ? null : events[0].args.witnessResult;
|
|
224
|
-
},
|
|
225
|
-
getClaimTxId: async () => {
|
|
226
|
-
const events = await this.Events.getContractBlockEvents(["Claim"], [null, null, "0x" + escrowHash], blockHeight, blockHeight);
|
|
227
|
-
return events.length === 0 ? null : events[0].transactionHash;
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
default:
|
|
231
|
-
return {
|
|
232
|
-
type: await this.isExpired(signer, data) ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
|
|
233
|
-
getTxBlock: async () => {
|
|
234
|
-
return {
|
|
235
|
-
blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
|
|
236
|
-
blockHeight: blockHeight
|
|
237
|
-
};
|
|
238
|
-
},
|
|
239
|
-
getRefundTxId: async () => {
|
|
240
|
-
const events = await this.Events.getContractBlockEvents(["Refund"], [null, null, "0x" + escrowHash], blockHeight, blockHeight);
|
|
241
|
-
return events.length === 0 ? null : events[0].transactionHash;
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Returns the data committed for a specific payment hash, or null if no data is currently commited for
|
|
248
|
-
* the specific swap
|
|
249
|
-
*
|
|
250
|
-
* @param paymentHashHex
|
|
251
|
-
*/
|
|
252
|
-
async getCommitedData(paymentHashHex) {
|
|
253
|
-
//TODO: Noop
|
|
254
|
-
return null;
|
|
255
|
-
}
|
|
256
|
-
////////////////////////////////////////////
|
|
257
|
-
//// Swap data initializer
|
|
258
|
-
createSwapData(type, offerer, claimer, token, amount, paymentHash, sequence, expiry, payIn, payOut, securityDeposit, claimerBounty, depositToken = this.Chain.Tokens.getNativeCurrencyAddress()) {
|
|
259
|
-
return Promise.resolve(new EVMSwapData_1.EVMSwapData(offerer, claimer, token, this.timelockRefundHandler.address, this.claimHandlersBySwapType?.[type]?.address, payOut, payIn, payIn, //For now track reputation for all payIn swaps
|
|
260
|
-
sequence, "0x" + paymentHash, (0, ethers_1.hexlify)(base_1.BigIntBufferUtils.toBuffer(expiry, "be", 32)), amount, depositToken, securityDeposit, claimerBounty, type, null));
|
|
261
|
-
}
|
|
262
|
-
////////////////////////////////////////////
|
|
263
|
-
//// Utils
|
|
264
|
-
async getBalance(signer, tokenAddress, inContract) {
|
|
265
|
-
if (inContract)
|
|
266
|
-
return await this.getIntermediaryBalance(signer, tokenAddress);
|
|
267
|
-
return await this.Chain.getBalance(signer, tokenAddress);
|
|
268
|
-
}
|
|
269
|
-
getIntermediaryData(address, token) {
|
|
270
|
-
return this.LpVault.getIntermediaryData(address, token);
|
|
271
|
-
}
|
|
272
|
-
getIntermediaryReputation(address, token) {
|
|
273
|
-
return this.LpVault.getIntermediaryReputation(address, token);
|
|
274
|
-
}
|
|
275
|
-
getIntermediaryBalance(address, token) {
|
|
276
|
-
return this.LpVault.getIntermediaryBalance(address, token);
|
|
277
|
-
}
|
|
278
|
-
////////////////////////////////////////////
|
|
279
|
-
//// Transaction initializers
|
|
280
|
-
async txsClaimWithSecret(signer, swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck) {
|
|
281
|
-
return this.Claim.txsClaimWithSecret(typeof (signer) === "string" ? signer : signer.getAddress(), swapData, secret, checkExpiry, feeRate);
|
|
282
|
-
}
|
|
283
|
-
async txsClaimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, feeRate) {
|
|
284
|
-
return this.Claim.txsClaimWithTxData(typeof (signer) === "string" ? signer : signer.getAddress(), swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, feeRate);
|
|
285
|
-
}
|
|
286
|
-
txsRefund(signer, swapData, check, initAta, feeRate) {
|
|
287
|
-
return this.Refund.txsRefund(signer, swapData, check, feeRate);
|
|
288
|
-
}
|
|
289
|
-
txsRefundWithAuthorization(signer, swapData, { timeout, prefix, signature }, check, initAta, feeRate) {
|
|
290
|
-
return this.Refund.txsRefundWithAuthorization(signer, swapData, timeout, prefix, signature, check, feeRate);
|
|
291
|
-
}
|
|
292
|
-
txsInit(signer, swapData, { timeout, prefix, signature }, skipChecks, feeRate) {
|
|
293
|
-
return this.Init.txsInit(signer, swapData, timeout, prefix, signature, skipChecks, feeRate);
|
|
294
|
-
}
|
|
295
|
-
txsWithdraw(signer, token, amount, feeRate) {
|
|
296
|
-
return this.LpVault.txsWithdraw(signer, token, amount, feeRate);
|
|
297
|
-
}
|
|
298
|
-
txsDeposit(signer, token, amount, feeRate) {
|
|
299
|
-
return this.LpVault.txsDeposit(signer, token, amount, feeRate);
|
|
300
|
-
}
|
|
301
|
-
////////////////////////////////////////////
|
|
302
|
-
//// Executors
|
|
303
|
-
async claimWithSecret(signer, swapData, secret, checkExpiry, initAta, txOptions) {
|
|
304
|
-
const result = await this.Claim.txsClaimWithSecret(signer.getAddress(), swapData, secret, checkExpiry, txOptions?.feeRate);
|
|
305
|
-
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
306
|
-
return signature;
|
|
307
|
-
}
|
|
308
|
-
async claimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, txOptions) {
|
|
309
|
-
const txs = await this.Claim.txsClaimWithTxData(signer.getAddress(), swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, txOptions?.feeRate);
|
|
310
|
-
if (txs === null)
|
|
311
|
-
throw new Error("Btc relay not synchronized to required blockheight!");
|
|
312
|
-
const txHashes = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
313
|
-
return txHashes[txHashes.length - 1];
|
|
314
|
-
}
|
|
315
|
-
async refund(signer, swapData, check, initAta, txOptions) {
|
|
316
|
-
let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
|
|
317
|
-
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
318
|
-
return signature;
|
|
319
|
-
}
|
|
320
|
-
async refundWithAuthorization(signer, swapData, signature, check, initAta, txOptions) {
|
|
321
|
-
let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
|
|
322
|
-
const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
323
|
-
return txSignature;
|
|
324
|
-
}
|
|
325
|
-
async init(signer, swapData, signature, skipChecks, txOptions) {
|
|
326
|
-
if (swapData.isPayIn()) {
|
|
327
|
-
if (!swapData.isOfferer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress()))
|
|
328
|
-
throw new Error("Invalid signer provided!");
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
if (!swapData.isClaimer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress()))
|
|
332
|
-
throw new Error("Invalid signer provided!");
|
|
333
|
-
}
|
|
334
|
-
let result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
|
|
335
|
-
const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
336
|
-
return txHashes[txHashes.length - 1];
|
|
337
|
-
}
|
|
338
|
-
async withdraw(signer, token, amount, txOptions) {
|
|
339
|
-
const txs = await this.LpVault.txsWithdraw(signer.getAddress(), token, amount, txOptions?.feeRate);
|
|
340
|
-
const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
341
|
-
return txId;
|
|
342
|
-
}
|
|
343
|
-
async deposit(signer, token, amount, txOptions) {
|
|
344
|
-
const txs = await this.LpVault.txsDeposit(signer.getAddress(), token, amount, txOptions?.feeRate);
|
|
345
|
-
const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
346
|
-
return txId;
|
|
347
|
-
}
|
|
348
|
-
////////////////////////////////////////////
|
|
349
|
-
//// Fees
|
|
350
|
-
getInitPayInFeeRate(offerer, claimer, token, paymentHash) {
|
|
351
|
-
return this.Chain.Fees.getFeeRate();
|
|
352
|
-
}
|
|
353
|
-
getInitFeeRate(offerer, claimer, token, paymentHash) {
|
|
354
|
-
return this.Chain.Fees.getFeeRate();
|
|
355
|
-
}
|
|
356
|
-
getRefundFeeRate(swapData) {
|
|
357
|
-
return this.Chain.Fees.getFeeRate();
|
|
358
|
-
}
|
|
359
|
-
getClaimFeeRate(signer, swapData) {
|
|
360
|
-
return this.Chain.Fees.getFeeRate();
|
|
361
|
-
}
|
|
362
|
-
getClaimFee(signer, swapData, feeRate) {
|
|
363
|
-
return this.Claim.getClaimFee(swapData, feeRate);
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Get the estimated fee of the commit transaction
|
|
367
|
-
*/
|
|
368
|
-
getCommitFee(signer, swapData, feeRate) {
|
|
369
|
-
return this.Init.getInitFee(swapData, feeRate);
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Get the estimated transaction fee of the refund transaction
|
|
373
|
-
*/
|
|
374
|
-
getRefundFee(signer, swapData, feeRate) {
|
|
375
|
-
return this.Refund.getRefundFee(swapData, feeRate);
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
exports.EVMSwapContract = EVMSwapContract;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EVMSwapContract = void 0;
|
|
4
|
+
const base_1 = require("@atomiqlabs/base");
|
|
5
|
+
const buffer_1 = require("buffer");
|
|
6
|
+
const TimelockRefundHandler_1 = require("./handlers/refund/TimelockRefundHandler");
|
|
7
|
+
const ClaimHandlers_1 = require("./handlers/claim/ClaimHandlers");
|
|
8
|
+
const sha2_1 = require("@noble/hashes/sha2");
|
|
9
|
+
const EVMContractBase_1 = require("../contract/EVMContractBase");
|
|
10
|
+
const EVMSwapData_1 = require("./EVMSwapData");
|
|
11
|
+
const EscrowManagerAbi_1 = require("./EscrowManagerAbi");
|
|
12
|
+
const ethers_1 = require("ethers");
|
|
13
|
+
const EVMLpVault_1 = require("./modules/EVMLpVault");
|
|
14
|
+
const EVMSwapInit_1 = require("./modules/EVMSwapInit");
|
|
15
|
+
const EVMSwapRefund_1 = require("./modules/EVMSwapRefund");
|
|
16
|
+
const EVMSwapClaim_1 = require("./modules/EVMSwapClaim");
|
|
17
|
+
const ESCROW_STATE_COMMITTED = 1;
|
|
18
|
+
const ESCROW_STATE_CLAIMED = 2;
|
|
19
|
+
const ESCROW_STATE_REFUNDED = 3;
|
|
20
|
+
class EVMSwapContract extends EVMContractBase_1.EVMContractBase {
|
|
21
|
+
constructor(chainInterface, btcRelay, contractAddress, handlerAddresses) {
|
|
22
|
+
super(chainInterface, contractAddress, EscrowManagerAbi_1.EscrowManagerAbi);
|
|
23
|
+
this.supportsInitWithoutClaimer = true;
|
|
24
|
+
////////////////////////
|
|
25
|
+
//// Timeouts
|
|
26
|
+
this.claimWithSecretTimeout = 180;
|
|
27
|
+
this.claimWithTxDataTimeout = 180;
|
|
28
|
+
this.refundTimeout = 180;
|
|
29
|
+
this.claimGracePeriod = 10 * 60;
|
|
30
|
+
this.refundGracePeriod = 10 * 60;
|
|
31
|
+
this.authGracePeriod = 30;
|
|
32
|
+
////////////////////////
|
|
33
|
+
//// Handlers
|
|
34
|
+
this.claimHandlersByAddress = {};
|
|
35
|
+
this.claimHandlersBySwapType = {};
|
|
36
|
+
this.refundHandlersByAddress = {};
|
|
37
|
+
this.chainId = chainInterface.chainId;
|
|
38
|
+
this.Init = new EVMSwapInit_1.EVMSwapInit(chainInterface, this);
|
|
39
|
+
this.Refund = new EVMSwapRefund_1.EVMSwapRefund(chainInterface, this);
|
|
40
|
+
this.Claim = new EVMSwapClaim_1.EVMSwapClaim(chainInterface, this);
|
|
41
|
+
this.LpVault = new EVMLpVault_1.EVMLpVault(chainInterface, this);
|
|
42
|
+
this.btcRelay = btcRelay;
|
|
43
|
+
ClaimHandlers_1.claimHandlersList.forEach(handlerCtor => {
|
|
44
|
+
const handler = new handlerCtor(handlerAddresses.claim[handlerCtor.type]);
|
|
45
|
+
this.claimHandlersByAddress[handler.address.toLowerCase()] = handler;
|
|
46
|
+
this.claimHandlersBySwapType[handlerCtor.type] = handler;
|
|
47
|
+
});
|
|
48
|
+
this.timelockRefundHandler = new TimelockRefundHandler_1.TimelockRefundHandler(handlerAddresses.refund.timelock);
|
|
49
|
+
this.refundHandlersByAddress[this.timelockRefundHandler.address.toLowerCase()] = this.timelockRefundHandler;
|
|
50
|
+
}
|
|
51
|
+
async start() {
|
|
52
|
+
}
|
|
53
|
+
////////////////////////////////////////////
|
|
54
|
+
//// Signatures
|
|
55
|
+
preFetchForInitSignatureVerification() {
|
|
56
|
+
return this.Init.preFetchForInitSignatureVerification();
|
|
57
|
+
}
|
|
58
|
+
getInitSignature(signer, swapData, authorizationTimeout, preFetchedBlockData, feeRate) {
|
|
59
|
+
return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout);
|
|
60
|
+
}
|
|
61
|
+
isValidInitAuthorization(sender, swapData, { timeout, prefix, signature }, feeRate, preFetchedData) {
|
|
62
|
+
return this.Init.isSignatureValid(sender, swapData, timeout, prefix, signature, preFetchedData);
|
|
63
|
+
}
|
|
64
|
+
getInitAuthorizationExpiry(swapData, { timeout, prefix, signature }, preFetchedData) {
|
|
65
|
+
return this.Init.getSignatureExpiry(timeout);
|
|
66
|
+
}
|
|
67
|
+
isInitAuthorizationExpired(swapData, { timeout, prefix, signature }) {
|
|
68
|
+
return this.Init.isSignatureExpired(timeout);
|
|
69
|
+
}
|
|
70
|
+
getRefundSignature(signer, swapData, authorizationTimeout) {
|
|
71
|
+
return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
|
|
72
|
+
}
|
|
73
|
+
isValidRefundAuthorization(swapData, { timeout, prefix, signature }) {
|
|
74
|
+
return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
|
|
75
|
+
}
|
|
76
|
+
getDataSignature(signer, data) {
|
|
77
|
+
return this.Chain.Signatures.getDataSignature(signer, data);
|
|
78
|
+
}
|
|
79
|
+
isValidDataSignature(data, signature, publicKey) {
|
|
80
|
+
return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
|
|
81
|
+
}
|
|
82
|
+
////////////////////////////////////////////
|
|
83
|
+
//// Swap data utils
|
|
84
|
+
/**
|
|
85
|
+
* Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
|
|
86
|
+
*
|
|
87
|
+
* @param signer
|
|
88
|
+
* @param data
|
|
89
|
+
*/
|
|
90
|
+
async isClaimable(signer, data) {
|
|
91
|
+
if (!data.isClaimer(signer))
|
|
92
|
+
return false;
|
|
93
|
+
if (await this.isExpired(signer, data))
|
|
94
|
+
return false;
|
|
95
|
+
return await this.isCommited(data);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
|
|
99
|
+
*
|
|
100
|
+
* @param swapData
|
|
101
|
+
*/
|
|
102
|
+
async isCommited(swapData) {
|
|
103
|
+
const data = await this.contract.getHashState("0x" + swapData.getEscrowHash());
|
|
104
|
+
return Number(data.state) === ESCROW_STATE_COMMITTED;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
|
|
108
|
+
* the swap expires a bit sooner than it should've & for the offerer it expires a bit later
|
|
109
|
+
*
|
|
110
|
+
* @param signer
|
|
111
|
+
* @param data
|
|
112
|
+
*/
|
|
113
|
+
isExpired(signer, data) {
|
|
114
|
+
let currentTimestamp = BigInt(Math.floor(Date.now() / 1000));
|
|
115
|
+
if (data.isClaimer(signer))
|
|
116
|
+
currentTimestamp = currentTimestamp + BigInt(this.claimGracePeriod);
|
|
117
|
+
if (data.isOfferer(signer))
|
|
118
|
+
currentTimestamp = currentTimestamp - BigInt(this.refundGracePeriod);
|
|
119
|
+
return Promise.resolve(data.getExpiry() < currentTimestamp);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
|
|
123
|
+
* is still commited
|
|
124
|
+
*
|
|
125
|
+
* @param signer
|
|
126
|
+
* @param data
|
|
127
|
+
*/
|
|
128
|
+
async isRequestRefundable(signer, data) {
|
|
129
|
+
//Swap can only be refunded by the offerer
|
|
130
|
+
if (!data.isOfferer(signer))
|
|
131
|
+
return false;
|
|
132
|
+
if (!(await this.isExpired(signer, data)))
|
|
133
|
+
return false;
|
|
134
|
+
return await this.isCommited(data);
|
|
135
|
+
}
|
|
136
|
+
getHashForTxId(txId, confirmations) {
|
|
137
|
+
return buffer_1.Buffer.from(this.claimHandlersBySwapType[base_1.ChainSwapType.CHAIN_TXID].getCommitment({
|
|
138
|
+
txId,
|
|
139
|
+
confirmations,
|
|
140
|
+
btcRelay: this.btcRelay
|
|
141
|
+
}).slice(2), "hex");
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Get the swap payment hash to be used for an on-chain swap, uses poseidon hash of the value
|
|
145
|
+
*
|
|
146
|
+
* @param outputScript output script required to claim the swap
|
|
147
|
+
* @param amount sats sent required to claim the swap
|
|
148
|
+
* @param confirmations
|
|
149
|
+
* @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
|
|
150
|
+
*/
|
|
151
|
+
getHashForOnchain(outputScript, amount, confirmations, nonce) {
|
|
152
|
+
let result;
|
|
153
|
+
if (nonce == null || nonce === 0n) {
|
|
154
|
+
result = this.claimHandlersBySwapType[base_1.ChainSwapType.CHAIN].getCommitment({
|
|
155
|
+
output: outputScript,
|
|
156
|
+
amount,
|
|
157
|
+
confirmations,
|
|
158
|
+
btcRelay: this.btcRelay
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
result = this.claimHandlersBySwapType[base_1.ChainSwapType.CHAIN_NONCED].getCommitment({
|
|
163
|
+
output: outputScript,
|
|
164
|
+
amount,
|
|
165
|
+
nonce,
|
|
166
|
+
confirmations,
|
|
167
|
+
btcRelay: this.btcRelay
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return buffer_1.Buffer.from(result.slice(2), "hex");
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get the swap payment hash to be used for a lightning htlc swap, uses poseidon hash of the sha256 hash of the preimage
|
|
174
|
+
*
|
|
175
|
+
* @param paymentHash payment hash of the HTLC
|
|
176
|
+
*/
|
|
177
|
+
getHashForHtlc(paymentHash) {
|
|
178
|
+
return buffer_1.Buffer.from(this.claimHandlersBySwapType[base_1.ChainSwapType.HTLC].getCommitment(paymentHash).slice(2), "hex");
|
|
179
|
+
}
|
|
180
|
+
getExtraData(outputScript, amount, confirmations, nonce) {
|
|
181
|
+
if (nonce == null)
|
|
182
|
+
nonce = 0n;
|
|
183
|
+
const txoHash = buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
|
|
184
|
+
base_1.BigIntBufferUtils.toBuffer(amount, "le", 8),
|
|
185
|
+
outputScript
|
|
186
|
+
])));
|
|
187
|
+
return buffer_1.Buffer.concat([
|
|
188
|
+
txoHash,
|
|
189
|
+
base_1.BigIntBufferUtils.toBuffer(nonce, "be", 8),
|
|
190
|
+
base_1.BigIntBufferUtils.toBuffer(BigInt(confirmations), "be", 2)
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
////////////////////////////////////////////
|
|
194
|
+
//// Swap data getters
|
|
195
|
+
/**
|
|
196
|
+
* Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
|
|
197
|
+
* if swap is refundable)
|
|
198
|
+
*
|
|
199
|
+
* @param signer
|
|
200
|
+
* @param data
|
|
201
|
+
*/
|
|
202
|
+
async getCommitStatus(signer, data) {
|
|
203
|
+
const escrowHash = data.getEscrowHash();
|
|
204
|
+
const stateData = await this.contract.getHashState("0x" + escrowHash);
|
|
205
|
+
const state = Number(stateData.state);
|
|
206
|
+
const blockHeight = Number(stateData.finishBlockheight);
|
|
207
|
+
switch (state) {
|
|
208
|
+
case ESCROW_STATE_COMMITTED:
|
|
209
|
+
if (data.isOfferer(signer) && await this.isExpired(signer, data))
|
|
210
|
+
return { type: base_1.SwapCommitStateType.REFUNDABLE };
|
|
211
|
+
return { type: base_1.SwapCommitStateType.COMMITED };
|
|
212
|
+
case ESCROW_STATE_CLAIMED:
|
|
213
|
+
return {
|
|
214
|
+
type: base_1.SwapCommitStateType.PAID,
|
|
215
|
+
getTxBlock: async () => {
|
|
216
|
+
return {
|
|
217
|
+
blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
|
|
218
|
+
blockHeight: blockHeight
|
|
219
|
+
};
|
|
220
|
+
},
|
|
221
|
+
getClaimResult: async () => {
|
|
222
|
+
const events = await this.Events.getContractBlockEvents(["Claim"], [null, null, "0x" + escrowHash], blockHeight, blockHeight);
|
|
223
|
+
return events.length === 0 ? null : events[0].args.witnessResult;
|
|
224
|
+
},
|
|
225
|
+
getClaimTxId: async () => {
|
|
226
|
+
const events = await this.Events.getContractBlockEvents(["Claim"], [null, null, "0x" + escrowHash], blockHeight, blockHeight);
|
|
227
|
+
return events.length === 0 ? null : events[0].transactionHash;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
default:
|
|
231
|
+
return {
|
|
232
|
+
type: await this.isExpired(signer, data) ? base_1.SwapCommitStateType.EXPIRED : base_1.SwapCommitStateType.NOT_COMMITED,
|
|
233
|
+
getTxBlock: async () => {
|
|
234
|
+
return {
|
|
235
|
+
blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
|
|
236
|
+
blockHeight: blockHeight
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
getRefundTxId: async () => {
|
|
240
|
+
const events = await this.Events.getContractBlockEvents(["Refund"], [null, null, "0x" + escrowHash], blockHeight, blockHeight);
|
|
241
|
+
return events.length === 0 ? null : events[0].transactionHash;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Returns the data committed for a specific payment hash, or null if no data is currently commited for
|
|
248
|
+
* the specific swap
|
|
249
|
+
*
|
|
250
|
+
* @param paymentHashHex
|
|
251
|
+
*/
|
|
252
|
+
async getCommitedData(paymentHashHex) {
|
|
253
|
+
//TODO: Noop
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
////////////////////////////////////////////
|
|
257
|
+
//// Swap data initializer
|
|
258
|
+
createSwapData(type, offerer, claimer, token, amount, paymentHash, sequence, expiry, payIn, payOut, securityDeposit, claimerBounty, depositToken = this.Chain.Tokens.getNativeCurrencyAddress()) {
|
|
259
|
+
return Promise.resolve(new EVMSwapData_1.EVMSwapData(offerer, claimer, token, this.timelockRefundHandler.address, this.claimHandlersBySwapType?.[type]?.address, payOut, payIn, payIn, //For now track reputation for all payIn swaps
|
|
260
|
+
sequence, "0x" + paymentHash, (0, ethers_1.hexlify)(base_1.BigIntBufferUtils.toBuffer(expiry, "be", 32)), amount, depositToken, securityDeposit, claimerBounty, type, null));
|
|
261
|
+
}
|
|
262
|
+
////////////////////////////////////////////
|
|
263
|
+
//// Utils
|
|
264
|
+
async getBalance(signer, tokenAddress, inContract) {
|
|
265
|
+
if (inContract)
|
|
266
|
+
return await this.getIntermediaryBalance(signer, tokenAddress);
|
|
267
|
+
return await this.Chain.getBalance(signer, tokenAddress);
|
|
268
|
+
}
|
|
269
|
+
getIntermediaryData(address, token) {
|
|
270
|
+
return this.LpVault.getIntermediaryData(address, token);
|
|
271
|
+
}
|
|
272
|
+
getIntermediaryReputation(address, token) {
|
|
273
|
+
return this.LpVault.getIntermediaryReputation(address, token);
|
|
274
|
+
}
|
|
275
|
+
getIntermediaryBalance(address, token) {
|
|
276
|
+
return this.LpVault.getIntermediaryBalance(address, token);
|
|
277
|
+
}
|
|
278
|
+
////////////////////////////////////////////
|
|
279
|
+
//// Transaction initializers
|
|
280
|
+
async txsClaimWithSecret(signer, swapData, secret, checkExpiry, initAta, feeRate, skipAtaCheck) {
|
|
281
|
+
return this.Claim.txsClaimWithSecret(typeof (signer) === "string" ? signer : signer.getAddress(), swapData, secret, checkExpiry, feeRate);
|
|
282
|
+
}
|
|
283
|
+
async txsClaimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, feeRate) {
|
|
284
|
+
return this.Claim.txsClaimWithTxData(typeof (signer) === "string" ? signer : signer.getAddress(), swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, feeRate);
|
|
285
|
+
}
|
|
286
|
+
txsRefund(signer, swapData, check, initAta, feeRate) {
|
|
287
|
+
return this.Refund.txsRefund(signer, swapData, check, feeRate);
|
|
288
|
+
}
|
|
289
|
+
txsRefundWithAuthorization(signer, swapData, { timeout, prefix, signature }, check, initAta, feeRate) {
|
|
290
|
+
return this.Refund.txsRefundWithAuthorization(signer, swapData, timeout, prefix, signature, check, feeRate);
|
|
291
|
+
}
|
|
292
|
+
txsInit(signer, swapData, { timeout, prefix, signature }, skipChecks, feeRate) {
|
|
293
|
+
return this.Init.txsInit(signer, swapData, timeout, prefix, signature, skipChecks, feeRate);
|
|
294
|
+
}
|
|
295
|
+
txsWithdraw(signer, token, amount, feeRate) {
|
|
296
|
+
return this.LpVault.txsWithdraw(signer, token, amount, feeRate);
|
|
297
|
+
}
|
|
298
|
+
txsDeposit(signer, token, amount, feeRate) {
|
|
299
|
+
return this.LpVault.txsDeposit(signer, token, amount, feeRate);
|
|
300
|
+
}
|
|
301
|
+
////////////////////////////////////////////
|
|
302
|
+
//// Executors
|
|
303
|
+
async claimWithSecret(signer, swapData, secret, checkExpiry, initAta, txOptions) {
|
|
304
|
+
const result = await this.Claim.txsClaimWithSecret(signer.getAddress(), swapData, secret, checkExpiry, txOptions?.feeRate);
|
|
305
|
+
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
306
|
+
return signature;
|
|
307
|
+
}
|
|
308
|
+
async claimWithTxData(signer, swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, initAta, txOptions) {
|
|
309
|
+
const txs = await this.Claim.txsClaimWithTxData(signer.getAddress(), swapData, tx, requiredConfirmations, vout, commitedHeader, synchronizer, txOptions?.feeRate);
|
|
310
|
+
if (txs === null)
|
|
311
|
+
throw new Error("Btc relay not synchronized to required blockheight!");
|
|
312
|
+
const txHashes = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
313
|
+
return txHashes[txHashes.length - 1];
|
|
314
|
+
}
|
|
315
|
+
async refund(signer, swapData, check, initAta, txOptions) {
|
|
316
|
+
let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
|
|
317
|
+
const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
318
|
+
return signature;
|
|
319
|
+
}
|
|
320
|
+
async refundWithAuthorization(signer, swapData, signature, check, initAta, txOptions) {
|
|
321
|
+
let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
|
|
322
|
+
const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
323
|
+
return txSignature;
|
|
324
|
+
}
|
|
325
|
+
async init(signer, swapData, signature, skipChecks, txOptions) {
|
|
326
|
+
if (swapData.isPayIn()) {
|
|
327
|
+
if (!swapData.isOfferer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress()))
|
|
328
|
+
throw new Error("Invalid signer provided!");
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
if (!swapData.isClaimer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress()))
|
|
332
|
+
throw new Error("Invalid signer provided!");
|
|
333
|
+
}
|
|
334
|
+
let result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
|
|
335
|
+
const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
|
|
336
|
+
return txHashes[txHashes.length - 1];
|
|
337
|
+
}
|
|
338
|
+
async withdraw(signer, token, amount, txOptions) {
|
|
339
|
+
const txs = await this.LpVault.txsWithdraw(signer.getAddress(), token, amount, txOptions?.feeRate);
|
|
340
|
+
const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
341
|
+
return txId;
|
|
342
|
+
}
|
|
343
|
+
async deposit(signer, token, amount, txOptions) {
|
|
344
|
+
const txs = await this.LpVault.txsDeposit(signer.getAddress(), token, amount, txOptions?.feeRate);
|
|
345
|
+
const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
|
|
346
|
+
return txId;
|
|
347
|
+
}
|
|
348
|
+
////////////////////////////////////////////
|
|
349
|
+
//// Fees
|
|
350
|
+
getInitPayInFeeRate(offerer, claimer, token, paymentHash) {
|
|
351
|
+
return this.Chain.Fees.getFeeRate();
|
|
352
|
+
}
|
|
353
|
+
getInitFeeRate(offerer, claimer, token, paymentHash) {
|
|
354
|
+
return this.Chain.Fees.getFeeRate();
|
|
355
|
+
}
|
|
356
|
+
getRefundFeeRate(swapData) {
|
|
357
|
+
return this.Chain.Fees.getFeeRate();
|
|
358
|
+
}
|
|
359
|
+
getClaimFeeRate(signer, swapData) {
|
|
360
|
+
return this.Chain.Fees.getFeeRate();
|
|
361
|
+
}
|
|
362
|
+
getClaimFee(signer, swapData, feeRate) {
|
|
363
|
+
return this.Claim.getClaimFee(swapData, feeRate);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Get the estimated fee of the commit transaction
|
|
367
|
+
*/
|
|
368
|
+
getCommitFee(signer, swapData, feeRate) {
|
|
369
|
+
return this.Init.getInitFee(swapData, feeRate);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Get the estimated transaction fee of the refund transaction
|
|
373
|
+
*/
|
|
374
|
+
getRefundFee(signer, swapData, feeRate) {
|
|
375
|
+
return this.Refund.getRefundFee(swapData, feeRate);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
exports.EVMSwapContract = EVMSwapContract;
|