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