@atomiqlabs/chain-evm 1.0.0-dev.22

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.
Files changed (146) hide show
  1. package/LICENSE +201 -0
  2. package/dist/chains/citrea/CitreaChainType.d.ts +13 -0
  3. package/dist/chains/citrea/CitreaChainType.js +2 -0
  4. package/dist/chains/citrea/CitreaInitializer.d.ts +30 -0
  5. package/dist/chains/citrea/CitreaInitializer.js +120 -0
  6. package/dist/evm/btcrelay/BtcRelayAbi.d.ts +198 -0
  7. package/dist/evm/btcrelay/BtcRelayAbi.js +261 -0
  8. package/dist/evm/btcrelay/BtcRelayTypechain.d.ts +172 -0
  9. package/dist/evm/btcrelay/BtcRelayTypechain.js +2 -0
  10. package/dist/evm/btcrelay/EVMBtcRelay.d.ts +188 -0
  11. package/dist/evm/btcrelay/EVMBtcRelay.js +419 -0
  12. package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +33 -0
  13. package/dist/evm/btcrelay/headers/EVMBtcHeader.js +84 -0
  14. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +56 -0
  15. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +123 -0
  16. package/dist/evm/chain/EVMChainInterface.d.ts +51 -0
  17. package/dist/evm/chain/EVMChainInterface.js +90 -0
  18. package/dist/evm/chain/EVMModule.d.ts +9 -0
  19. package/dist/evm/chain/EVMModule.js +13 -0
  20. package/dist/evm/chain/modules/ERC20Abi.d.ts +168 -0
  21. package/dist/evm/chain/modules/ERC20Abi.js +225 -0
  22. package/dist/evm/chain/modules/EVMAddresses.d.ts +9 -0
  23. package/dist/evm/chain/modules/EVMAddresses.js +26 -0
  24. package/dist/evm/chain/modules/EVMBlocks.d.ts +20 -0
  25. package/dist/evm/chain/modules/EVMBlocks.js +64 -0
  26. package/dist/evm/chain/modules/EVMEvents.d.ts +36 -0
  27. package/dist/evm/chain/modules/EVMEvents.js +122 -0
  28. package/dist/evm/chain/modules/EVMFees.d.ts +35 -0
  29. package/dist/evm/chain/modules/EVMFees.js +73 -0
  30. package/dist/evm/chain/modules/EVMSignatures.d.ts +29 -0
  31. package/dist/evm/chain/modules/EVMSignatures.js +68 -0
  32. package/dist/evm/chain/modules/EVMTokens.d.ts +49 -0
  33. package/dist/evm/chain/modules/EVMTokens.js +105 -0
  34. package/dist/evm/chain/modules/EVMTransactions.d.ts +89 -0
  35. package/dist/evm/chain/modules/EVMTransactions.js +216 -0
  36. package/dist/evm/contract/EVMContractBase.d.ts +22 -0
  37. package/dist/evm/contract/EVMContractBase.js +34 -0
  38. package/dist/evm/contract/EVMContractModule.d.ts +8 -0
  39. package/dist/evm/contract/EVMContractModule.js +11 -0
  40. package/dist/evm/contract/modules/EVMContractEvents.d.ts +42 -0
  41. package/dist/evm/contract/modules/EVMContractEvents.js +75 -0
  42. package/dist/evm/events/EVMChainEvents.d.ts +22 -0
  43. package/dist/evm/events/EVMChainEvents.js +67 -0
  44. package/dist/evm/events/EVMChainEventsBrowser.d.ts +86 -0
  45. package/dist/evm/events/EVMChainEventsBrowser.js +294 -0
  46. package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +64 -0
  47. package/dist/evm/spv_swap/EVMSpvVaultContract.js +410 -0
  48. package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +38 -0
  49. package/dist/evm/spv_swap/EVMSpvVaultData.js +159 -0
  50. package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +19 -0
  51. package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +55 -0
  52. package/dist/evm/spv_swap/SpvVaultContractAbi.d.ts +91 -0
  53. package/dist/evm/spv_swap/SpvVaultContractAbi.js +849 -0
  54. package/dist/evm/spv_swap/SpvVaultContractTypechain.d.ts +450 -0
  55. package/dist/evm/spv_swap/SpvVaultContractTypechain.js +2 -0
  56. package/dist/evm/swaps/EVMSwapContract.d.ts +192 -0
  57. package/dist/evm/swaps/EVMSwapContract.js +373 -0
  58. package/dist/evm/swaps/EVMSwapData.d.ts +64 -0
  59. package/dist/evm/swaps/EVMSwapData.js +254 -0
  60. package/dist/evm/swaps/EVMSwapModule.d.ts +9 -0
  61. package/dist/evm/swaps/EVMSwapModule.js +11 -0
  62. package/dist/evm/swaps/EscrowManagerAbi.d.ts +120 -0
  63. package/dist/evm/swaps/EscrowManagerAbi.js +985 -0
  64. package/dist/evm/swaps/EscrowManagerTypechain.d.ts +475 -0
  65. package/dist/evm/swaps/EscrowManagerTypechain.js +2 -0
  66. package/dist/evm/swaps/handlers/IHandler.d.ts +13 -0
  67. package/dist/evm/swaps/handlers/IHandler.js +2 -0
  68. package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +10 -0
  69. package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +13 -0
  70. package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.d.ts +20 -0
  71. package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.js +39 -0
  72. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -0
  73. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +59 -0
  74. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -0
  75. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +51 -0
  76. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +21 -0
  77. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +28 -0
  78. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +48 -0
  79. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +63 -0
  80. package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -0
  81. package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.js +28 -0
  82. package/dist/evm/swaps/modules/EVMLpVault.d.ts +69 -0
  83. package/dist/evm/swaps/modules/EVMLpVault.js +131 -0
  84. package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +53 -0
  85. package/dist/evm/swaps/modules/EVMSwapClaim.js +101 -0
  86. package/dist/evm/swaps/modules/EVMSwapInit.d.ts +88 -0
  87. package/dist/evm/swaps/modules/EVMSwapInit.js +241 -0
  88. package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +62 -0
  89. package/dist/evm/swaps/modules/EVMSwapRefund.js +132 -0
  90. package/dist/evm/typechain/common.d.ts +50 -0
  91. package/dist/evm/typechain/common.js +2 -0
  92. package/dist/evm/wallet/EVMSigner.d.ts +9 -0
  93. package/dist/evm/wallet/EVMSigner.js +16 -0
  94. package/dist/index.d.ts +37 -0
  95. package/dist/index.js +53 -0
  96. package/dist/utils/Utils.d.ts +15 -0
  97. package/dist/utils/Utils.js +71 -0
  98. package/package.json +37 -0
  99. package/src/chains/citrea/CitreaChainType.ts +28 -0
  100. package/src/chains/citrea/CitreaInitializer.ts +167 -0
  101. package/src/evm/btcrelay/BtcRelayAbi.ts +258 -0
  102. package/src/evm/btcrelay/BtcRelayTypechain.ts +371 -0
  103. package/src/evm/btcrelay/EVMBtcRelay.ts +517 -0
  104. package/src/evm/btcrelay/headers/EVMBtcHeader.ts +110 -0
  105. package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +153 -0
  106. package/src/evm/chain/EVMChainInterface.ts +157 -0
  107. package/src/evm/chain/EVMModule.ts +21 -0
  108. package/src/evm/chain/modules/ERC20Abi.ts +222 -0
  109. package/src/evm/chain/modules/EVMAddresses.ts +24 -0
  110. package/src/evm/chain/modules/EVMBlocks.ts +75 -0
  111. package/src/evm/chain/modules/EVMEvents.ts +139 -0
  112. package/src/evm/chain/modules/EVMFees.ts +105 -0
  113. package/src/evm/chain/modules/EVMSignatures.ts +76 -0
  114. package/src/evm/chain/modules/EVMTokens.ts +115 -0
  115. package/src/evm/chain/modules/EVMTransactions.ts +246 -0
  116. package/src/evm/contract/EVMContractBase.ts +63 -0
  117. package/src/evm/contract/EVMContractModule.ts +16 -0
  118. package/src/evm/contract/modules/EVMContractEvents.ts +102 -0
  119. package/src/evm/events/EVMChainEvents.ts +81 -0
  120. package/src/evm/events/EVMChainEventsBrowser.ts +390 -0
  121. package/src/evm/spv_swap/EVMSpvVaultContract.ts +533 -0
  122. package/src/evm/spv_swap/EVMSpvVaultData.ts +201 -0
  123. package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +70 -0
  124. package/src/evm/spv_swap/SpvVaultContractAbi.ts +846 -0
  125. package/src/evm/spv_swap/SpvVaultContractTypechain.ts +685 -0
  126. package/src/evm/swaps/EVMSwapContract.ts +590 -0
  127. package/src/evm/swaps/EVMSwapData.ts +367 -0
  128. package/src/evm/swaps/EVMSwapModule.ts +16 -0
  129. package/src/evm/swaps/EscrowManagerAbi.ts +982 -0
  130. package/src/evm/swaps/EscrowManagerTypechain.ts +723 -0
  131. package/src/evm/swaps/handlers/IHandler.ts +17 -0
  132. package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +20 -0
  133. package/src/evm/swaps/handlers/claim/HashlockClaimHandler.ts +47 -0
  134. package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +82 -0
  135. package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +76 -0
  136. package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +46 -0
  137. package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +115 -0
  138. package/src/evm/swaps/handlers/refund/TimelockRefundHandler.ts +38 -0
  139. package/src/evm/swaps/modules/EVMLpVault.ts +153 -0
  140. package/src/evm/swaps/modules/EVMSwapClaim.ts +141 -0
  141. package/src/evm/swaps/modules/EVMSwapInit.ts +292 -0
  142. package/src/evm/swaps/modules/EVMSwapRefund.ts +198 -0
  143. package/src/evm/typechain/common.ts +131 -0
  144. package/src/evm/wallet/EVMSigner.ts +23 -0
  145. package/src/index.ts +44 -0
  146. package/src/utils/Utils.ts +81 -0
@@ -0,0 +1,590 @@
1
+ import {
2
+ BigIntBufferUtils,
3
+ ChainSwapType,
4
+ IntermediaryReputationType,
5
+ RelaySynchronizer,
6
+ SignatureData, SwapCommitState, SwapCommitStateType,
7
+ SwapContract,
8
+ TransactionConfirmationOptions
9
+ } from "@atomiqlabs/base";
10
+ import {Buffer} from "buffer";
11
+ import {TimelockRefundHandler} from "./handlers/refund/TimelockRefundHandler";
12
+ import {claimHandlersList, IClaimHandler} from "./handlers/claim/ClaimHandlers"
13
+ import {IHandler} from "./handlers/IHandler";
14
+ import {sha256} from "@noble/hashes/sha2";
15
+ import { EVMContractBase } from "../contract/EVMContractBase";
16
+ import {EscrowManager} from "./EscrowManagerTypechain";
17
+ import { EVMSwapData } from "./EVMSwapData";
18
+ import {EVMTx} from "../chain/modules/EVMTransactions";
19
+ import { EVMSigner } from "../wallet/EVMSigner";
20
+ import {EVMChainInterface} from "../chain/EVMChainInterface";
21
+ import { EVMBtcRelay } from "../btcrelay/EVMBtcRelay";
22
+ import {EscrowManagerAbi} from "./EscrowManagerAbi";
23
+ import {hexlify} from "ethers";
24
+ import {EVMBtcStoredHeader} from "../btcrelay/headers/EVMBtcStoredHeader";
25
+ import {EVMLpVault} from "./modules/EVMLpVault";
26
+ import {EVMPreFetchVerification, EVMSwapInit} from "./modules/EVMSwapInit";
27
+ import { EVMSwapRefund } from "./modules/EVMSwapRefund";
28
+ import {EVMSwapClaim} from "./modules/EVMSwapClaim";
29
+
30
+ const ESCROW_STATE_COMMITTED = 1;
31
+ const ESCROW_STATE_CLAIMED = 2;
32
+ const ESCROW_STATE_REFUNDED = 3;
33
+
34
+ export class EVMSwapContract<ChainId extends string = string>
35
+ extends EVMContractBase<EscrowManager>
36
+ implements SwapContract<
37
+ EVMSwapData,
38
+ EVMTx,
39
+ never,
40
+ EVMPreFetchVerification,
41
+ EVMSigner,
42
+ ChainId
43
+ > {
44
+
45
+ ////////////////////////
46
+ //// Constants
47
+ readonly chainId: ChainId;
48
+
49
+ ////////////////////////
50
+ //// Timeouts
51
+ readonly claimWithSecretTimeout: number = 180;
52
+ readonly claimWithTxDataTimeout: number = 180;
53
+ readonly refundTimeout: number = 180;
54
+ readonly claimGracePeriod: number = 10*60;
55
+ readonly refundGracePeriod: number = 10*60;
56
+ readonly authGracePeriod: number = 30;
57
+
58
+ ////////////////////////
59
+ //// Services
60
+ readonly Init: EVMSwapInit;
61
+ readonly Refund: EVMSwapRefund;
62
+ readonly Claim: EVMSwapClaim;
63
+ readonly LpVault: EVMLpVault;
64
+
65
+ ////////////////////////
66
+ //// Handlers
67
+ readonly claimHandlersByAddress: {[address: string]: IClaimHandler<any, any>} = {};
68
+ readonly claimHandlersBySwapType: {[type in ChainSwapType]?: IClaimHandler<any, any>} = {};
69
+
70
+ readonly refundHandlersByAddress: {[address: string]: IHandler<any, any>} = {};
71
+ readonly timelockRefundHandler: IHandler<any, any>;
72
+
73
+ readonly btcRelay: EVMBtcRelay<any>;
74
+
75
+ constructor(
76
+ chainInterface: EVMChainInterface<ChainId>,
77
+ btcRelay: EVMBtcRelay<any>,
78
+ contractAddress: string,
79
+ handlerAddresses: {
80
+ refund: {
81
+ timelock: string
82
+ },
83
+ claim: {
84
+ [type in ChainSwapType]: string
85
+ }
86
+ }
87
+ ) {
88
+ super(chainInterface, contractAddress, EscrowManagerAbi);
89
+ this.chainId = chainInterface.chainId;
90
+ this.Init = new EVMSwapInit(chainInterface, this);
91
+ this.Refund = new EVMSwapRefund(chainInterface, this);
92
+ this.Claim = new EVMSwapClaim(chainInterface, this);
93
+ this.LpVault = new EVMLpVault(chainInterface, this);
94
+
95
+ this.btcRelay = btcRelay;
96
+
97
+ claimHandlersList.forEach(handlerCtor => {
98
+ const handler = new handlerCtor(handlerAddresses.claim[handlerCtor.type]);
99
+ this.claimHandlersByAddress[handler.address.toLowerCase()] = handler;
100
+ this.claimHandlersBySwapType[handlerCtor.type] = handler;
101
+ });
102
+
103
+ this.timelockRefundHandler = new TimelockRefundHandler(handlerAddresses.refund.timelock);
104
+ this.refundHandlersByAddress[this.timelockRefundHandler.address.toLowerCase()] = this.timelockRefundHandler;
105
+ }
106
+
107
+ async start(): Promise<void> {
108
+ }
109
+
110
+ ////////////////////////////////////////////
111
+ //// Signatures
112
+ preFetchForInitSignatureVerification(): Promise<EVMPreFetchVerification> {
113
+ return this.Init.preFetchForInitSignatureVerification();
114
+ }
115
+
116
+ getInitSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number, preFetchedBlockData?: never, feeRate?: string): Promise<SignatureData> {
117
+ return this.Init.signSwapInitialization(signer, swapData, authorizationTimeout);
118
+ }
119
+
120
+ isValidInitAuthorization(sender: string, swapData: EVMSwapData, {timeout, prefix, signature}, feeRate?: string, preFetchedData?: EVMPreFetchVerification): Promise<Buffer> {
121
+ return this.Init.isSignatureValid(sender, swapData, timeout, prefix, signature, preFetchedData);
122
+ }
123
+
124
+ getInitAuthorizationExpiry(swapData: EVMSwapData, {timeout, prefix, signature}, preFetchedData?: EVMPreFetchVerification): Promise<number> {
125
+ return this.Init.getSignatureExpiry(timeout);
126
+ }
127
+
128
+ isInitAuthorizationExpired(swapData: EVMSwapData, {timeout, prefix, signature}): Promise<boolean> {
129
+ return this.Init.isSignatureExpired(timeout);
130
+ }
131
+
132
+ getRefundSignature(signer: EVMSigner, swapData: EVMSwapData, authorizationTimeout: number): Promise<SignatureData> {
133
+ return this.Refund.signSwapRefund(signer, swapData, authorizationTimeout);
134
+ }
135
+
136
+ isValidRefundAuthorization(swapData: EVMSwapData, {timeout, prefix, signature}): Promise<Buffer> {
137
+ return this.Refund.isSignatureValid(swapData, timeout, prefix, signature);
138
+ }
139
+
140
+ getDataSignature(signer: EVMSigner, data: Buffer): Promise<string> {
141
+ return this.Chain.Signatures.getDataSignature(signer, data);
142
+ }
143
+
144
+ isValidDataSignature(data: Buffer, signature: string, publicKey: string): Promise<boolean> {
145
+ return this.Chain.Signatures.isValidDataSignature(data, signature, publicKey);
146
+ }
147
+
148
+ ////////////////////////////////////////////
149
+ //// Swap data utils
150
+ /**
151
+ * Checks whether the claim is claimable by us, that means not expired, we are claimer & the swap is commited
152
+ *
153
+ * @param signer
154
+ * @param data
155
+ */
156
+ async isClaimable(signer: string, data: EVMSwapData): Promise<boolean> {
157
+ if(!data.isClaimer(signer)) return false;
158
+ if(await this.isExpired(signer, data)) return false;
159
+ return await this.isCommited(data);
160
+ }
161
+
162
+ /**
163
+ * Checks whether a swap is commited, i.e. the swap still exists on-chain and was not claimed nor refunded
164
+ *
165
+ * @param swapData
166
+ */
167
+ async isCommited(swapData: EVMSwapData): Promise<boolean> {
168
+ const data = await this.contract.getHashState("0x"+swapData.getEscrowHash());
169
+ return Number(data.state)===ESCROW_STATE_COMMITTED;
170
+ }
171
+
172
+ /**
173
+ * Checks whether the swap is expired, takes into consideration possible on-chain time skew, therefore for claimer
174
+ * the swap expires a bit sooner than it should've & for the offerer it expires a bit later
175
+ *
176
+ * @param signer
177
+ * @param data
178
+ */
179
+ isExpired(signer: string, data: EVMSwapData): Promise<boolean> {
180
+ let currentTimestamp: bigint = BigInt(Math.floor(Date.now()/1000));
181
+ if(data.isClaimer(signer)) currentTimestamp = currentTimestamp + BigInt(this.claimGracePeriod);
182
+ if(data.isOfferer(signer)) currentTimestamp = currentTimestamp - BigInt(this.refundGracePeriod);
183
+ return Promise.resolve(data.getExpiry() < currentTimestamp);
184
+ }
185
+
186
+ /**
187
+ * Checks if the swap is refundable by us, checks if we are offerer, if the swap is already expired & if the swap
188
+ * is still commited
189
+ *
190
+ * @param signer
191
+ * @param data
192
+ */
193
+ async isRequestRefundable(signer: string, data: EVMSwapData): Promise<boolean> {
194
+ //Swap can only be refunded by the offerer
195
+ if(!data.isOfferer(signer)) return false;
196
+ if(!(await this.isExpired(signer, data))) return false;
197
+ return await this.isCommited(data);
198
+ }
199
+
200
+ getHashForTxId(txId: string, confirmations: number) {
201
+ return Buffer.from(this.claimHandlersBySwapType[ChainSwapType.CHAIN_TXID].getCommitment({
202
+ txId,
203
+ confirmations,
204
+ btcRelay: this.btcRelay
205
+ }).slice(2), "hex");
206
+ }
207
+
208
+ /**
209
+ * Get the swap payment hash to be used for an on-chain swap, uses poseidon hash of the value
210
+ *
211
+ * @param outputScript output script required to claim the swap
212
+ * @param amount sats sent required to claim the swap
213
+ * @param confirmations
214
+ * @param nonce swap nonce uniquely identifying the transaction to prevent replay attacks
215
+ */
216
+ getHashForOnchain(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
217
+ let result: string;
218
+ if(nonce==null || nonce === 0n) {
219
+ result = this.claimHandlersBySwapType[ChainSwapType.CHAIN].getCommitment({
220
+ output: outputScript,
221
+ amount,
222
+ confirmations,
223
+ btcRelay: this.btcRelay
224
+ });
225
+ } else {
226
+ result = this.claimHandlersBySwapType[ChainSwapType.CHAIN_NONCED].getCommitment({
227
+ output: outputScript,
228
+ amount,
229
+ nonce,
230
+ confirmations,
231
+ btcRelay: this.btcRelay
232
+ });
233
+ }
234
+ return Buffer.from(result.slice(2), "hex");
235
+ }
236
+
237
+ /**
238
+ * Get the swap payment hash to be used for a lightning htlc swap, uses poseidon hash of the sha256 hash of the preimage
239
+ *
240
+ * @param paymentHash payment hash of the HTLC
241
+ */
242
+ getHashForHtlc(paymentHash: Buffer): Buffer {
243
+ return Buffer.from(this.claimHandlersBySwapType[ChainSwapType.HTLC].getCommitment(paymentHash).slice(2), "hex");
244
+ }
245
+
246
+ getExtraData(outputScript: Buffer, amount: bigint, confirmations: number, nonce?: bigint): Buffer {
247
+ if(nonce==null) nonce = 0n;
248
+ const txoHash = Buffer.from(sha256(Buffer.concat([
249
+ BigIntBufferUtils.toBuffer(amount, "le", 8),
250
+ outputScript
251
+ ])));
252
+ return Buffer.concat([
253
+ txoHash,
254
+ BigIntBufferUtils.toBuffer(nonce, "be", 8),
255
+ BigIntBufferUtils.toBuffer(BigInt(confirmations), "be", 2)
256
+ ]);
257
+ }
258
+
259
+
260
+ ////////////////////////////////////////////
261
+ //// Swap data getters
262
+ /**
263
+ * Gets the status of the specific swap, this also checks if we are offerer/claimer & checks for expiry (to see
264
+ * if swap is refundable)
265
+ *
266
+ * @param signer
267
+ * @param data
268
+ */
269
+ async getCommitStatus(signer: string, data: EVMSwapData): Promise<SwapCommitState> {
270
+ const escrowHash = data.getEscrowHash();
271
+ const stateData = await this.contract.getHashState("0x"+escrowHash);
272
+ const state = Number(stateData.state);
273
+ const blockHeight = Number(stateData.finishBlockheight);
274
+ switch(state) {
275
+ case ESCROW_STATE_COMMITTED:
276
+ if(data.isOfferer(signer) && await this.isExpired(signer,data)) return {type: SwapCommitStateType.REFUNDABLE};
277
+ return {type: SwapCommitStateType.COMMITED};
278
+ case ESCROW_STATE_CLAIMED:
279
+ return {
280
+ type: SwapCommitStateType.PAID,
281
+ getTxBlock: async () => {
282
+ return {
283
+ blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
284
+ blockHeight: blockHeight
285
+ };
286
+ },
287
+ getClaimTxId: async () => {
288
+ const events = await this.Events.getContractBlockEvents(
289
+ ["Claim"],
290
+ [null, null, "0x"+escrowHash],
291
+ blockHeight, blockHeight
292
+ );
293
+ return events.length===0 ? null : events[0].transactionHash;
294
+ }
295
+ };
296
+ default:
297
+ return {
298
+ type: await this.isExpired(signer, data) ? SwapCommitStateType.EXPIRED : SwapCommitStateType.NOT_COMMITED,
299
+ getTxBlock: async () => {
300
+ return {
301
+ blockTime: await this.Chain.Blocks.getBlockTime(blockHeight),
302
+ blockHeight: blockHeight
303
+ };
304
+ },
305
+ getRefundTxId: async () => {
306
+ const events = await this.Events.getContractBlockEvents(
307
+ ["Refund"],
308
+ [null, null, "0x"+escrowHash],
309
+ blockHeight, blockHeight
310
+ );
311
+ return events.length===0 ? null : events[0].transactionHash;
312
+ }
313
+ };
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Returns the data committed for a specific payment hash, or null if no data is currently commited for
319
+ * the specific swap
320
+ *
321
+ * @param paymentHashHex
322
+ */
323
+ async getCommitedData(paymentHashHex: string): Promise<EVMSwapData> {
324
+ //TODO: Noop
325
+ return null;
326
+ }
327
+
328
+ ////////////////////////////////////////////
329
+ //// Swap data initializer
330
+ createSwapData(
331
+ type: ChainSwapType,
332
+ offerer: string,
333
+ claimer: string,
334
+ token: string,
335
+ amount: bigint,
336
+ paymentHash: string,
337
+ sequence: bigint,
338
+ expiry: bigint,
339
+ payIn: boolean,
340
+ payOut: boolean,
341
+ securityDeposit: bigint,
342
+ claimerBounty: bigint,
343
+ depositToken: string = this.Chain.Tokens.getNativeCurrencyAddress()
344
+ ): Promise<EVMSwapData> {
345
+ return Promise.resolve(new EVMSwapData(
346
+ offerer,
347
+ claimer,
348
+ token,
349
+ this.timelockRefundHandler.address,
350
+ this.claimHandlersBySwapType?.[type]?.address,
351
+ payOut,
352
+ payIn,
353
+ payIn, //For now track reputation for all payIn swaps
354
+ sequence,
355
+ "0x"+paymentHash,
356
+ hexlify(BigIntBufferUtils.toBuffer(expiry, "be", 32)),
357
+ amount,
358
+ depositToken,
359
+ securityDeposit,
360
+ claimerBounty,
361
+ type,
362
+ null
363
+ ));
364
+ }
365
+
366
+ ////////////////////////////////////////////
367
+ //// Utils
368
+ async getBalance(signer: string, tokenAddress: string, inContract?: boolean): Promise<bigint> {
369
+ if(inContract) return await this.getIntermediaryBalance(signer, tokenAddress);
370
+
371
+ return await this.Chain.getBalance(signer, tokenAddress);
372
+ }
373
+
374
+ getIntermediaryData(address: string, token: string): Promise<{
375
+ balance: bigint,
376
+ reputation: IntermediaryReputationType
377
+ }> {
378
+ return this.LpVault.getIntermediaryData(address, token);
379
+ }
380
+
381
+ getIntermediaryReputation(address: string, token: string): Promise<IntermediaryReputationType> {
382
+ return this.LpVault.getIntermediaryReputation(address, token);
383
+ }
384
+
385
+ getIntermediaryBalance(address: string, token: string): Promise<bigint> {
386
+ return this.LpVault.getIntermediaryBalance(address, token);
387
+ }
388
+
389
+ ////////////////////////////////////////////
390
+ //// Transaction initializers
391
+ async txsClaimWithSecret(
392
+ signer: string | EVMSigner,
393
+ swapData: EVMSwapData,
394
+ secret: string,
395
+ checkExpiry?: boolean,
396
+ initAta?: boolean,
397
+ feeRate?: string,
398
+ skipAtaCheck?: boolean
399
+ ): Promise<EVMTx[]> {
400
+ return this.Claim.txsClaimWithSecret(typeof(signer)==="string" ? signer : signer.getAddress(), swapData, secret, checkExpiry, feeRate)
401
+ }
402
+
403
+ async txsClaimWithTxData(
404
+ signer: string | EVMSigner,
405
+ swapData: EVMSwapData,
406
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
407
+ requiredConfirmations: number,
408
+ vout: number,
409
+ commitedHeader?: EVMBtcStoredHeader,
410
+ synchronizer?: RelaySynchronizer<EVMBtcStoredHeader, EVMTx, any>,
411
+ initAta?: boolean,
412
+ feeRate?: string
413
+ ): Promise<EVMTx[] | null> {
414
+ return this.Claim.txsClaimWithTxData(
415
+ typeof(signer)==="string" ? signer : signer.getAddress(),
416
+ swapData,
417
+ tx,
418
+ requiredConfirmations,
419
+ vout,
420
+ commitedHeader,
421
+ synchronizer,
422
+ feeRate
423
+ );
424
+ }
425
+
426
+ txsRefund(signer: string, swapData: EVMSwapData, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
427
+ return this.Refund.txsRefund(signer, swapData, check, feeRate);
428
+ }
429
+
430
+ txsRefundWithAuthorization(signer: string, swapData: EVMSwapData, {timeout, prefix, signature}, check?: boolean, initAta?: boolean, feeRate?: string): Promise<EVMTx[]> {
431
+ return this.Refund.txsRefundWithAuthorization(signer, swapData, timeout, prefix,signature, check, feeRate);
432
+ }
433
+
434
+ txsInit(signer: string, swapData: EVMSwapData, {timeout, prefix, signature}, skipChecks?: boolean, feeRate?: string): Promise<EVMTx[]> {
435
+ return this.Init.txsInit(signer, swapData, timeout, prefix, signature, skipChecks, feeRate);
436
+ }
437
+
438
+ txsWithdraw(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
439
+ return this.LpVault.txsWithdraw(signer, token, amount, feeRate);
440
+ }
441
+
442
+ txsDeposit(signer: string, token: string, amount: bigint, feeRate?: string): Promise<EVMTx[]> {
443
+ return this.LpVault.txsDeposit(signer, token, amount, feeRate);
444
+ }
445
+
446
+ ////////////////////////////////////////////
447
+ //// Executors
448
+ async claimWithSecret(
449
+ signer: EVMSigner,
450
+ swapData: EVMSwapData,
451
+ secret: string,
452
+ checkExpiry?: boolean,
453
+ initAta?: boolean,
454
+ txOptions?: TransactionConfirmationOptions
455
+ ): Promise<string> {
456
+ const result = await this.Claim.txsClaimWithSecret(signer.getAddress(), swapData, secret, checkExpiry, txOptions?.feeRate);
457
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
458
+ return signature;
459
+ }
460
+
461
+ async claimWithTxData(
462
+ signer: EVMSigner,
463
+ swapData: EVMSwapData,
464
+ tx: { blockhash: string, confirmations: number, txid: string, hex: string, height: number },
465
+ requiredConfirmations: number,
466
+ vout: number,
467
+ commitedHeader?: EVMBtcStoredHeader,
468
+ synchronizer?: RelaySynchronizer<EVMBtcStoredHeader, EVMTx, any>,
469
+ initAta?: boolean,
470
+ txOptions?: TransactionConfirmationOptions
471
+ ): Promise<string> {
472
+ const txs = await this.Claim.txsClaimWithTxData(
473
+ signer.getAddress(), swapData, tx, requiredConfirmations, vout,
474
+ commitedHeader, synchronizer, txOptions?.feeRate
475
+ );
476
+ if(txs===null) throw new Error("Btc relay not synchronized to required blockheight!");
477
+
478
+ const txHashes = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal);
479
+
480
+ return txHashes[txHashes.length - 1];
481
+ }
482
+
483
+ async refund(
484
+ signer: EVMSigner,
485
+ swapData: EVMSwapData,
486
+ check?: boolean,
487
+ initAta?: boolean,
488
+ txOptions?: TransactionConfirmationOptions
489
+ ): Promise<string> {
490
+ let result = await this.txsRefund(signer.getAddress(), swapData, check, initAta, txOptions?.feeRate);
491
+
492
+ const [signature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
493
+
494
+ return signature;
495
+ }
496
+
497
+ async refundWithAuthorization(
498
+ signer: EVMSigner,
499
+ swapData: EVMSwapData,
500
+ signature: SignatureData,
501
+ check?: boolean,
502
+ initAta?: boolean,
503
+ txOptions?: TransactionConfirmationOptions
504
+ ): Promise<string> {
505
+ let result = await this.txsRefundWithAuthorization(signer.getAddress(), swapData, signature, check, initAta, txOptions?.feeRate);
506
+
507
+ const [txSignature] = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
508
+
509
+ return txSignature;
510
+ }
511
+
512
+ async init(
513
+ signer: EVMSigner,
514
+ swapData: EVMSwapData,
515
+ signature: SignatureData,
516
+ skipChecks?: boolean,
517
+ txOptions?: TransactionConfirmationOptions
518
+ ): Promise<string> {
519
+ if(swapData.isPayIn()) {
520
+ if(!swapData.isOfferer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress())) throw new Error("Invalid signer provided!");
521
+ } else {
522
+ if(!swapData.isClaimer(signer.getAddress()) && !swapData.isOfferer(signer.getAddress())) throw new Error("Invalid signer provided!");
523
+ }
524
+
525
+ let result = await this.txsInit(signer.getAddress(), swapData, signature, skipChecks, txOptions?.feeRate);
526
+
527
+ const txHashes = await this.Chain.sendAndConfirm(signer, result, txOptions?.waitForConfirmation, txOptions?.abortSignal);
528
+
529
+ return txHashes[txHashes.length - 1];
530
+ }
531
+
532
+ async withdraw(
533
+ signer: EVMSigner,
534
+ token: string,
535
+ amount: bigint,
536
+ txOptions?: TransactionConfirmationOptions
537
+ ): Promise<string> {
538
+ const txs = await this.LpVault.txsWithdraw(signer.getAddress(), token, amount, txOptions?.feeRate);
539
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
540
+ return txId;
541
+ }
542
+
543
+ async deposit(
544
+ signer: EVMSigner,
545
+ token: string,
546
+ amount: bigint,
547
+ txOptions?: TransactionConfirmationOptions
548
+ ): Promise<string> {
549
+ const txs = await this.LpVault.txsDeposit(signer.getAddress(), token, amount, txOptions?.feeRate);
550
+ const [txId] = await this.Chain.sendAndConfirm(signer, txs, txOptions?.waitForConfirmation, txOptions?.abortSignal, false);
551
+ return txId;
552
+ }
553
+
554
+ ////////////////////////////////////////////
555
+ //// Fees
556
+ getInitPayInFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
557
+ return this.Chain.Fees.getFeeRate();
558
+ }
559
+
560
+ getInitFeeRate(offerer?: string, claimer?: string, token?: string, paymentHash?: string): Promise<string> {
561
+ return this.Chain.Fees.getFeeRate();
562
+ }
563
+
564
+ getRefundFeeRate(swapData: EVMSwapData): Promise<string> {
565
+ return this.Chain.Fees.getFeeRate();
566
+ }
567
+
568
+ getClaimFeeRate(signer: string, swapData: EVMSwapData): Promise<string> {
569
+ return this.Chain.Fees.getFeeRate();
570
+ }
571
+
572
+ getClaimFee(signer: string, swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
573
+ return this.Claim.getClaimFee(swapData, feeRate);
574
+ }
575
+
576
+ /**
577
+ * Get the estimated solana fee of the commit transaction
578
+ */
579
+ getCommitFee(swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
580
+ return this.Init.getInitFee(swapData, feeRate);
581
+ }
582
+
583
+ /**
584
+ * Get the estimated solana transaction fee of the refund transaction
585
+ */
586
+ getRefundFee(swapData: EVMSwapData, feeRate?: string): Promise<bigint> {
587
+ return this.Refund.getRefundFee(swapData, feeRate);
588
+ }
589
+
590
+ }