@atomiqlabs/chain-evm 1.0.0-dev.100

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 (185) hide show
  1. package/LICENSE +201 -0
  2. package/dist/chains/botanix/BotanixChainType.d.ts +14 -0
  3. package/dist/chains/botanix/BotanixChainType.js +2 -0
  4. package/dist/chains/botanix/BotanixInitializer.d.ts +30 -0
  5. package/dist/chains/botanix/BotanixInitializer.js +125 -0
  6. package/dist/chains/citrea/CitreaBtcRelay.d.ts +21 -0
  7. package/dist/chains/citrea/CitreaBtcRelay.js +43 -0
  8. package/dist/chains/citrea/CitreaChainType.d.ts +14 -0
  9. package/dist/chains/citrea/CitreaChainType.js +2 -0
  10. package/dist/chains/citrea/CitreaFees.d.ts +29 -0
  11. package/dist/chains/citrea/CitreaFees.js +67 -0
  12. package/dist/chains/citrea/CitreaInitializer.d.ts +30 -0
  13. package/dist/chains/citrea/CitreaInitializer.js +132 -0
  14. package/dist/chains/citrea/CitreaSpvVaultContract.d.ts +15 -0
  15. package/dist/chains/citrea/CitreaSpvVaultContract.js +74 -0
  16. package/dist/chains/citrea/CitreaSwapContract.d.ts +22 -0
  17. package/dist/chains/citrea/CitreaSwapContract.js +96 -0
  18. package/dist/chains/citrea/CitreaTokens.d.ts +9 -0
  19. package/dist/chains/citrea/CitreaTokens.js +20 -0
  20. package/dist/evm/btcrelay/BtcRelayAbi.d.ts +198 -0
  21. package/dist/evm/btcrelay/BtcRelayAbi.js +261 -0
  22. package/dist/evm/btcrelay/BtcRelayTypechain.d.ts +172 -0
  23. package/dist/evm/btcrelay/BtcRelayTypechain.js +2 -0
  24. package/dist/evm/btcrelay/EVMBtcRelay.d.ts +197 -0
  25. package/dist/evm/btcrelay/EVMBtcRelay.js +435 -0
  26. package/dist/evm/btcrelay/headers/EVMBtcHeader.d.ts +33 -0
  27. package/dist/evm/btcrelay/headers/EVMBtcHeader.js +84 -0
  28. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.d.ts +56 -0
  29. package/dist/evm/btcrelay/headers/EVMBtcStoredHeader.js +123 -0
  30. package/dist/evm/chain/EVMChainInterface.d.ts +60 -0
  31. package/dist/evm/chain/EVMChainInterface.js +107 -0
  32. package/dist/evm/chain/EVMModule.d.ts +9 -0
  33. package/dist/evm/chain/EVMModule.js +13 -0
  34. package/dist/evm/chain/modules/ERC20Abi.d.ts +168 -0
  35. package/dist/evm/chain/modules/ERC20Abi.js +225 -0
  36. package/dist/evm/chain/modules/EVMAddresses.d.ts +10 -0
  37. package/dist/evm/chain/modules/EVMAddresses.js +30 -0
  38. package/dist/evm/chain/modules/EVMBlocks.d.ts +27 -0
  39. package/dist/evm/chain/modules/EVMBlocks.js +73 -0
  40. package/dist/evm/chain/modules/EVMEvents.d.ts +46 -0
  41. package/dist/evm/chain/modules/EVMEvents.js +151 -0
  42. package/dist/evm/chain/modules/EVMFees.d.ts +36 -0
  43. package/dist/evm/chain/modules/EVMFees.js +74 -0
  44. package/dist/evm/chain/modules/EVMSignatures.d.ts +29 -0
  45. package/dist/evm/chain/modules/EVMSignatures.js +68 -0
  46. package/dist/evm/chain/modules/EVMTokens.d.ts +70 -0
  47. package/dist/evm/chain/modules/EVMTokens.js +142 -0
  48. package/dist/evm/chain/modules/EVMTransactions.d.ts +94 -0
  49. package/dist/evm/chain/modules/EVMTransactions.js +307 -0
  50. package/dist/evm/contract/EVMContractBase.d.ts +22 -0
  51. package/dist/evm/contract/EVMContractBase.js +34 -0
  52. package/dist/evm/contract/EVMContractModule.d.ts +8 -0
  53. package/dist/evm/contract/EVMContractModule.js +11 -0
  54. package/dist/evm/contract/modules/EVMContractEvents.d.ts +43 -0
  55. package/dist/evm/contract/modules/EVMContractEvents.js +76 -0
  56. package/dist/evm/events/EVMChainEvents.d.ts +22 -0
  57. package/dist/evm/events/EVMChainEvents.js +69 -0
  58. package/dist/evm/events/EVMChainEventsBrowser.d.ts +102 -0
  59. package/dist/evm/events/EVMChainEventsBrowser.js +412 -0
  60. package/dist/evm/providers/JsonRpcProviderWithRetries.d.ts +16 -0
  61. package/dist/evm/providers/JsonRpcProviderWithRetries.js +27 -0
  62. package/dist/evm/providers/ReconnectingWebSocketProvider.d.ts +22 -0
  63. package/dist/evm/providers/ReconnectingWebSocketProvider.js +91 -0
  64. package/dist/evm/providers/SocketProvider.d.ts +111 -0
  65. package/dist/evm/providers/SocketProvider.js +336 -0
  66. package/dist/evm/providers/WebSocketProviderWithRetries.d.ts +17 -0
  67. package/dist/evm/providers/WebSocketProviderWithRetries.js +23 -0
  68. package/dist/evm/spv_swap/EVMSpvVaultContract.d.ts +110 -0
  69. package/dist/evm/spv_swap/EVMSpvVaultContract.js +629 -0
  70. package/dist/evm/spv_swap/EVMSpvVaultData.d.ts +40 -0
  71. package/dist/evm/spv_swap/EVMSpvVaultData.js +184 -0
  72. package/dist/evm/spv_swap/EVMSpvWithdrawalData.d.ts +19 -0
  73. package/dist/evm/spv_swap/EVMSpvWithdrawalData.js +55 -0
  74. package/dist/evm/spv_swap/SpvVaultContractAbi.d.ts +91 -0
  75. package/dist/evm/spv_swap/SpvVaultContractAbi.js +849 -0
  76. package/dist/evm/spv_swap/SpvVaultContractTypechain.d.ts +450 -0
  77. package/dist/evm/spv_swap/SpvVaultContractTypechain.js +2 -0
  78. package/dist/evm/swaps/EVMSwapContract.d.ts +199 -0
  79. package/dist/evm/swaps/EVMSwapContract.js +394 -0
  80. package/dist/evm/swaps/EVMSwapData.d.ts +66 -0
  81. package/dist/evm/swaps/EVMSwapData.js +260 -0
  82. package/dist/evm/swaps/EVMSwapModule.d.ts +9 -0
  83. package/dist/evm/swaps/EVMSwapModule.js +11 -0
  84. package/dist/evm/swaps/EscrowManagerAbi.d.ts +120 -0
  85. package/dist/evm/swaps/EscrowManagerAbi.js +985 -0
  86. package/dist/evm/swaps/EscrowManagerTypechain.d.ts +475 -0
  87. package/dist/evm/swaps/EscrowManagerTypechain.js +2 -0
  88. package/dist/evm/swaps/handlers/IHandler.d.ts +13 -0
  89. package/dist/evm/swaps/handlers/IHandler.js +2 -0
  90. package/dist/evm/swaps/handlers/claim/ClaimHandlers.d.ts +10 -0
  91. package/dist/evm/swaps/handlers/claim/ClaimHandlers.js +13 -0
  92. package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.d.ts +20 -0
  93. package/dist/evm/swaps/handlers/claim/HashlockClaimHandler.js +39 -0
  94. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -0
  95. package/dist/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +59 -0
  96. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -0
  97. package/dist/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +51 -0
  98. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +21 -0
  99. package/dist/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +28 -0
  100. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +48 -0
  101. package/dist/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +63 -0
  102. package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -0
  103. package/dist/evm/swaps/handlers/refund/TimelockRefundHandler.js +28 -0
  104. package/dist/evm/swaps/modules/EVMLpVault.d.ts +69 -0
  105. package/dist/evm/swaps/modules/EVMLpVault.js +134 -0
  106. package/dist/evm/swaps/modules/EVMSwapClaim.d.ts +54 -0
  107. package/dist/evm/swaps/modules/EVMSwapClaim.js +137 -0
  108. package/dist/evm/swaps/modules/EVMSwapInit.d.ts +88 -0
  109. package/dist/evm/swaps/modules/EVMSwapInit.js +274 -0
  110. package/dist/evm/swaps/modules/EVMSwapRefund.d.ts +62 -0
  111. package/dist/evm/swaps/modules/EVMSwapRefund.js +167 -0
  112. package/dist/evm/typechain/common.d.ts +50 -0
  113. package/dist/evm/typechain/common.js +2 -0
  114. package/dist/evm/wallet/EVMBrowserSigner.d.ts +5 -0
  115. package/dist/evm/wallet/EVMBrowserSigner.js +11 -0
  116. package/dist/evm/wallet/EVMPersistentSigner.d.ts +29 -0
  117. package/dist/evm/wallet/EVMPersistentSigner.js +230 -0
  118. package/dist/evm/wallet/EVMSigner.d.ts +12 -0
  119. package/dist/evm/wallet/EVMSigner.js +28 -0
  120. package/dist/index.d.ts +44 -0
  121. package/dist/index.js +60 -0
  122. package/dist/utils/Utils.d.ts +19 -0
  123. package/dist/utils/Utils.js +98 -0
  124. package/package.json +39 -0
  125. package/src/chains/botanix/BotanixChainType.ts +30 -0
  126. package/src/chains/botanix/BotanixInitializer.ts +175 -0
  127. package/src/chains/citrea/CitreaBtcRelay.ts +58 -0
  128. package/src/chains/citrea/CitreaChainType.ts +30 -0
  129. package/src/chains/citrea/CitreaFees.ts +77 -0
  130. package/src/chains/citrea/CitreaInitializer.ts +182 -0
  131. package/src/chains/citrea/CitreaSpvVaultContract.ts +75 -0
  132. package/src/chains/citrea/CitreaSwapContract.ts +103 -0
  133. package/src/chains/citrea/CitreaTokens.ts +22 -0
  134. package/src/evm/btcrelay/BtcRelayAbi.ts +258 -0
  135. package/src/evm/btcrelay/BtcRelayTypechain.ts +371 -0
  136. package/src/evm/btcrelay/EVMBtcRelay.ts +537 -0
  137. package/src/evm/btcrelay/headers/EVMBtcHeader.ts +110 -0
  138. package/src/evm/btcrelay/headers/EVMBtcStoredHeader.ts +153 -0
  139. package/src/evm/chain/EVMChainInterface.ts +189 -0
  140. package/src/evm/chain/EVMModule.ts +21 -0
  141. package/src/evm/chain/modules/ERC20Abi.ts +222 -0
  142. package/src/evm/chain/modules/EVMAddresses.ts +29 -0
  143. package/src/evm/chain/modules/EVMBlocks.ts +86 -0
  144. package/src/evm/chain/modules/EVMEvents.ts +182 -0
  145. package/src/evm/chain/modules/EVMFees.ts +104 -0
  146. package/src/evm/chain/modules/EVMSignatures.ts +76 -0
  147. package/src/evm/chain/modules/EVMTokens.ts +155 -0
  148. package/src/evm/chain/modules/EVMTransactions.ts +346 -0
  149. package/src/evm/contract/EVMContractBase.ts +63 -0
  150. package/src/evm/contract/EVMContractModule.ts +16 -0
  151. package/src/evm/contract/modules/EVMContractEvents.ts +104 -0
  152. package/src/evm/events/EVMChainEvents.ts +82 -0
  153. package/src/evm/events/EVMChainEventsBrowser.ts +533 -0
  154. package/src/evm/providers/JsonRpcProviderWithRetries.ts +34 -0
  155. package/src/evm/providers/ReconnectingWebSocketProvider.ts +107 -0
  156. package/src/evm/providers/SocketProvider.ts +371 -0
  157. package/src/evm/providers/WebSocketProviderWithRetries.ts +35 -0
  158. package/src/evm/spv_swap/EVMSpvVaultContract.ts +779 -0
  159. package/src/evm/spv_swap/EVMSpvVaultData.ts +228 -0
  160. package/src/evm/spv_swap/EVMSpvWithdrawalData.ts +70 -0
  161. package/src/evm/spv_swap/SpvVaultContractAbi.ts +846 -0
  162. package/src/evm/spv_swap/SpvVaultContractTypechain.ts +685 -0
  163. package/src/evm/swaps/EVMSwapContract.ts +621 -0
  164. package/src/evm/swaps/EVMSwapData.ts +378 -0
  165. package/src/evm/swaps/EVMSwapModule.ts +16 -0
  166. package/src/evm/swaps/EscrowManagerAbi.ts +982 -0
  167. package/src/evm/swaps/EscrowManagerTypechain.ts +723 -0
  168. package/src/evm/swaps/handlers/IHandler.ts +17 -0
  169. package/src/evm/swaps/handlers/claim/ClaimHandlers.ts +20 -0
  170. package/src/evm/swaps/handlers/claim/HashlockClaimHandler.ts +47 -0
  171. package/src/evm/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +82 -0
  172. package/src/evm/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +76 -0
  173. package/src/evm/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +46 -0
  174. package/src/evm/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +115 -0
  175. package/src/evm/swaps/handlers/refund/TimelockRefundHandler.ts +38 -0
  176. package/src/evm/swaps/modules/EVMLpVault.ts +155 -0
  177. package/src/evm/swaps/modules/EVMSwapClaim.ts +173 -0
  178. package/src/evm/swaps/modules/EVMSwapInit.ts +329 -0
  179. package/src/evm/swaps/modules/EVMSwapRefund.ts +229 -0
  180. package/src/evm/typechain/common.ts +131 -0
  181. package/src/evm/wallet/EVMBrowserSigner.ts +12 -0
  182. package/src/evm/wallet/EVMPersistentSigner.ts +307 -0
  183. package/src/evm/wallet/EVMSigner.ts +35 -0
  184. package/src/index.ts +53 -0
  185. package/src/utils/Utils.ts +111 -0
@@ -0,0 +1,29 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import { EVMSigner } from "../../wallet/EVMSigner";
4
+ import { EVMModule } from "../EVMModule";
5
+ import { EVMChainInterface } from "../EVMChainInterface";
6
+ import { TypedDataField } from "ethers";
7
+ export declare class EVMSignatures extends EVMModule<any> {
8
+ private readonly domainName;
9
+ constructor(root: EVMChainInterface<any>, domainName?: string);
10
+ signTypedMessage(contract: string, signer: EVMSigner, type: TypedDataField[], typeName: string, message: object): Promise<string>;
11
+ isValidSignature(contract: string, signature: string, address: string, type: TypedDataField[], typeName: string, message: object): Promise<boolean>;
12
+ /**
13
+ * Produces a signature over the sha256 of a specified data Buffer, only works with providers which
14
+ * expose their private key (i.e. backend based, not browser wallet based)
15
+ *
16
+ * @param signer
17
+ * @param data data to sign
18
+ */
19
+ getDataSignature(signer: EVMSigner, data: Buffer): Promise<string>;
20
+ /**
21
+ * Checks whether a signature is a valid signature produced by the account over a data message (computes
22
+ * sha256 hash of the message)
23
+ *
24
+ * @param data signed data
25
+ * @param signature data signature
26
+ * @param address public key of the signer
27
+ */
28
+ isValidDataSignature(data: Buffer, signature: string, address: string): Promise<boolean>;
29
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EVMSignatures = void 0;
4
+ const EVMModule_1 = require("../EVMModule");
5
+ const ethers_1 = require("ethers");
6
+ const DataHash = [
7
+ { name: "dataHash", type: "bytes32" }
8
+ ];
9
+ class EVMSignatures extends EVMModule_1.EVMModule {
10
+ constructor(root, domainName = "atomiq.exchange") {
11
+ super(root);
12
+ this.domainName = domainName;
13
+ }
14
+ async signTypedMessage(contract, signer, type, typeName, message) {
15
+ return signer.account.signTypedData({
16
+ name: this.domainName,
17
+ version: "1",
18
+ chainId: BigInt(this.root.evmChainId),
19
+ verifyingContract: contract
20
+ }, { [typeName]: type }, message);
21
+ }
22
+ async isValidSignature(contract, signature, address, type, typeName, message) {
23
+ return Promise.resolve(address === (0, ethers_1.verifyTypedData)({
24
+ name: this.domainName,
25
+ version: "1",
26
+ chainId: BigInt(this.root.evmChainId),
27
+ verifyingContract: contract
28
+ }, { [typeName]: type }, message, signature));
29
+ }
30
+ ///////////////////
31
+ //// Data signatures
32
+ /**
33
+ * Produces a signature over the sha256 of a specified data Buffer, only works with providers which
34
+ * expose their private key (i.e. backend based, not browser wallet based)
35
+ *
36
+ * @param signer
37
+ * @param data data to sign
38
+ */
39
+ getDataSignature(signer, data) {
40
+ return signer.account.signTypedData({
41
+ name: this.domainName,
42
+ version: "1",
43
+ chainId: BigInt(this.root.evmChainId),
44
+ verifyingContract: "0x0000000000000000000000000000000000000000"
45
+ }, { DataHash }, {
46
+ dataHash: (0, ethers_1.sha256)(data)
47
+ });
48
+ }
49
+ /**
50
+ * Checks whether a signature is a valid signature produced by the account over a data message (computes
51
+ * sha256 hash of the message)
52
+ *
53
+ * @param data signed data
54
+ * @param signature data signature
55
+ * @param address public key of the signer
56
+ */
57
+ isValidDataSignature(data, signature, address) {
58
+ return Promise.resolve(address === (0, ethers_1.verifyTypedData)({
59
+ name: this.domainName,
60
+ version: "1",
61
+ chainId: BigInt(this.root.evmChainId),
62
+ verifyingContract: "0x0000000000000000000000000000000000000000"
63
+ }, { DataHash }, {
64
+ dataHash: (0, ethers_1.sha256)(data)
65
+ }, signature));
66
+ }
67
+ }
68
+ exports.EVMSignatures = EVMSignatures;
@@ -0,0 +1,70 @@
1
+ import { EVMModule } from "../EVMModule";
2
+ import { TransactionRequest } from "ethers";
3
+ export declare class EVMTokens extends EVMModule<any> {
4
+ static readonly ETH_ADDRESS = "0x0000000000000000000000000000000000000000";
5
+ static readonly GasCosts: {
6
+ TRANSFER: number;
7
+ APPROVE: number;
8
+ };
9
+ private getContract;
10
+ /**
11
+ * Checks if the provided string is a valid starknet token
12
+ *
13
+ * @param token
14
+ */
15
+ isValidToken(token: string): boolean;
16
+ /**
17
+ * Returns the token balance of the address
18
+ *
19
+ * @param address
20
+ * @param token
21
+ */
22
+ getTokenBalance(address: string, token: string): Promise<bigint>;
23
+ /**
24
+ * Returns the authorized allowance of specific address towards a spender contract
25
+ *
26
+ * @param spender A contract trying to spend user's erc20 balance
27
+ * @param address Wallet address
28
+ * @param token ERC-20 token
29
+ */
30
+ getTokenAllowance(spender: string, address: string, token: string): Promise<bigint>;
31
+ /**
32
+ * Returns the native currency address
33
+ */
34
+ getNativeCurrencyAddress(): string;
35
+ /**
36
+ * Creates transactions for sending the over the tokens
37
+ *
38
+ * @param signer
39
+ * @param token token to send
40
+ * @param amount amount of the token to send
41
+ * @param recipient recipient's address
42
+ * @param feeRate fee rate to use for the transactions
43
+ * @private
44
+ */
45
+ Transfer(signer: string, token: string, amount: bigint, recipient: string, feeRate?: string): Promise<TransactionRequest>;
46
+ /**
47
+ * Creates transactions for approving spending of tokens
48
+ *
49
+ * @param signer
50
+ * @param token token to send
51
+ * @param amount amount of the token to send
52
+ * @param spender recipient's address
53
+ * @param feeRate fee rate to use for the transactions
54
+ * @private
55
+ */
56
+ Approve(signer: string, token: string, amount: bigint, spender: string, feeRate?: string): Promise<TransactionRequest>;
57
+ /**
58
+ * Checks whether an approve transaction is required for a given token and either returns the tx
59
+ * or null in case the approve is not required
60
+ *
61
+ * @param signer
62
+ * @param token token to approve for
63
+ * @param amount amount of the token to send
64
+ * @param spender spending contract address
65
+ * @param feeRate fee rate to use for the transactions
66
+ */
67
+ checkAndGetApproveTx(signer: string, token: string, amount: bigint, spender: string, feeRate?: string): Promise<TransactionRequest | null>;
68
+ getApproveFee(feeRate?: string): Promise<bigint>;
69
+ getTransferFee(feeRate?: string): Promise<bigint>;
70
+ }
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EVMTokens = void 0;
4
+ const EVMModule_1 = require("../EVMModule");
5
+ const ethers_1 = require("ethers");
6
+ const ERC20Abi_1 = require("./ERC20Abi");
7
+ const EVMAddresses_1 = require("./EVMAddresses");
8
+ const EVMFees_1 = require("./EVMFees");
9
+ class EVMTokens extends EVMModule_1.EVMModule {
10
+ getContract(address) {
11
+ return new ethers_1.Contract(address, ERC20Abi_1.ERC20Abi, this.root.provider);
12
+ }
13
+ ///////////////////
14
+ //// Tokens
15
+ /**
16
+ * Checks if the provided string is a valid starknet token
17
+ *
18
+ * @param token
19
+ */
20
+ isValidToken(token) {
21
+ return EVMAddresses_1.EVMAddresses.isValidAddress(token);
22
+ }
23
+ /**
24
+ * Returns the token balance of the address
25
+ *
26
+ * @param address
27
+ * @param token
28
+ */
29
+ async getTokenBalance(address, token) {
30
+ let balance;
31
+ if (token === "0x0000000000000000000000000000000000000000") {
32
+ balance = await this.provider.getBalance(address);
33
+ }
34
+ else {
35
+ const erc20 = this.getContract(token);
36
+ balance = await erc20.balanceOf(address);
37
+ }
38
+ this.logger.debug("getTokenBalance(): token balance fetched, token: " + token +
39
+ " address: " + address + " amount: " + balance.toString(10));
40
+ return balance;
41
+ }
42
+ /**
43
+ * Returns the authorized allowance of specific address towards a spender contract
44
+ *
45
+ * @param spender A contract trying to spend user's erc20 balance
46
+ * @param address Wallet address
47
+ * @param token ERC-20 token
48
+ */
49
+ async getTokenAllowance(spender, address, token) {
50
+ if (token === "0x0000000000000000000000000000000000000000")
51
+ return 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn;
52
+ const erc20 = this.getContract(token);
53
+ return await erc20.allowance(address, spender);
54
+ }
55
+ /**
56
+ * Returns the native currency address
57
+ */
58
+ getNativeCurrencyAddress() {
59
+ return "0x0000000000000000000000000000000000000000";
60
+ }
61
+ ///////////////////
62
+ //// Transfers
63
+ /**
64
+ * Creates transactions for sending the over the tokens
65
+ *
66
+ * @param signer
67
+ * @param token token to send
68
+ * @param amount amount of the token to send
69
+ * @param recipient recipient's address
70
+ * @param feeRate fee rate to use for the transactions
71
+ * @private
72
+ */
73
+ async Transfer(signer, token, amount, recipient, feeRate) {
74
+ let tx;
75
+ if (token === this.getNativeCurrencyAddress()) {
76
+ tx = {
77
+ to: recipient,
78
+ value: amount
79
+ };
80
+ }
81
+ else {
82
+ tx = await this.getContract(token).transfer.populateTransaction(recipient, amount);
83
+ }
84
+ tx.from = signer;
85
+ EVMFees_1.EVMFees.applyFeeRate(tx, EVMTokens.GasCosts.TRANSFER, feeRate ?? await this.root.Fees.getFeeRate());
86
+ this.logger.debug("txsTransfer(): transfer TX created, recipient: " + recipient.toString() +
87
+ " token: " + token.toString() + " amount: " + amount.toString(10));
88
+ return tx;
89
+ }
90
+ ///////////////////
91
+ //// Approval
92
+ /**
93
+ * Creates transactions for approving spending of tokens
94
+ *
95
+ * @param signer
96
+ * @param token token to send
97
+ * @param amount amount of the token to send
98
+ * @param spender recipient's address
99
+ * @param feeRate fee rate to use for the transactions
100
+ * @private
101
+ */
102
+ async Approve(signer, token, amount, spender, feeRate) {
103
+ if (token === this.getNativeCurrencyAddress())
104
+ return null;
105
+ const tx = await this.getContract(token).approve.populateTransaction(spender, amount);
106
+ tx.from = signer;
107
+ EVMFees_1.EVMFees.applyFeeRate(tx, EVMTokens.GasCosts.APPROVE, feeRate ?? await this.root.Fees.getFeeRate());
108
+ this.logger.debug("txsTransfer(): approve TX created, spender: " + spender.toString() +
109
+ " token: " + token.toString() + " amount: " + amount.toString(10));
110
+ return tx;
111
+ }
112
+ /**
113
+ * Checks whether an approve transaction is required for a given token and either returns the tx
114
+ * or null in case the approve is not required
115
+ *
116
+ * @param signer
117
+ * @param token token to approve for
118
+ * @param amount amount of the token to send
119
+ * @param spender spending contract address
120
+ * @param feeRate fee rate to use for the transactions
121
+ */
122
+ async checkAndGetApproveTx(signer, token, amount, spender, feeRate) {
123
+ const alreadyApproved = await this.getTokenAllowance(spender, signer, token);
124
+ if (alreadyApproved >= amount)
125
+ return null;
126
+ return await this.Approve(signer, token, amount, spender, feeRate);
127
+ }
128
+ async getApproveFee(feeRate) {
129
+ feeRate ?? (feeRate = await this.root.Fees.getFeeRate());
130
+ return EVMFees_1.EVMFees.getGasFee(EVMTokens.GasCosts.APPROVE, feeRate);
131
+ }
132
+ async getTransferFee(feeRate) {
133
+ feeRate ?? (feeRate = await this.root.Fees.getFeeRate());
134
+ return EVMFees_1.EVMFees.getGasFee(EVMTokens.GasCosts.APPROVE, feeRate);
135
+ }
136
+ }
137
+ exports.EVMTokens = EVMTokens;
138
+ EVMTokens.ETH_ADDRESS = "0x0000000000000000000000000000000000000000";
139
+ EVMTokens.GasCosts = {
140
+ TRANSFER: 80000 + 21000,
141
+ APPROVE: 80000 + 21000
142
+ };
@@ -0,0 +1,94 @@
1
+ import { EVMModule } from "../EVMModule";
2
+ import { Transaction, TransactionRequest } from "ethers";
3
+ import { EVMSigner } from "../../wallet/EVMSigner";
4
+ export type EVMTx = TransactionRequest;
5
+ export type EVMTxTrace = {
6
+ from: string;
7
+ gas: string;
8
+ gasused: string;
9
+ to: string;
10
+ input: string;
11
+ output: string;
12
+ error: string;
13
+ revertReason: string;
14
+ calls: EVMTxTrace[];
15
+ type: "CREATE" | "CALL" | "STATICCALL";
16
+ };
17
+ export declare class EVMTransactions extends EVMModule<any> {
18
+ private readonly latestConfirmedNonces;
19
+ private readonly latestPendingNonces;
20
+ private readonly latestSignedNonces;
21
+ readonly _cbksBeforeTxReplace: ((oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>)[];
22
+ private readonly cbksBeforeTxSigned;
23
+ private cbkSendTransaction;
24
+ readonly _knownTxSet: Set<string>;
25
+ /**
26
+ * Waits for transaction confirmation using HTTP polling
27
+ *
28
+ * @param tx EVM transaction to wait for confirmation for
29
+ * @param abortSignal signal to abort waiting for tx confirmation
30
+ * @private
31
+ */
32
+ private confirmTransaction;
33
+ /**
34
+ * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
35
+ *
36
+ * @param signer
37
+ * @param txs
38
+ * @private
39
+ */
40
+ private prepareTransactions;
41
+ /**
42
+ * Sends out a signed transaction to the RPC
43
+ *
44
+ * @param tx EVM tx to send
45
+ * @param onBeforePublish a callback called before every transaction is published
46
+ * @private
47
+ */
48
+ private sendSignedTransaction;
49
+ /**
50
+ * Prepares, signs, sends (in parallel or sequentially) & optionally waits for confirmation
51
+ * of a batch of EVM transactions
52
+ *
53
+ * @param signer
54
+ * @param txs transactions to send
55
+ * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
56
+ * are re-sent at regular intervals)
57
+ * @param abortSignal abort signal to abort waiting for transaction confirmations
58
+ * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
59
+ * are executed in order)
60
+ * @param onBeforePublish a callback called before every transaction is published, NOTE: callback is not called when using browser-based wallet!
61
+ */
62
+ sendAndConfirm(signer: EVMSigner, txs: TransactionRequest[], waitForConfirmation?: boolean, abortSignal?: AbortSignal, parallel?: boolean, onBeforePublish?: (txId: string, rawTx: string) => Promise<void>): Promise<string[]>;
63
+ /**
64
+ * Serializes the signed EVM transaction
65
+ *
66
+ * @param tx
67
+ */
68
+ serializeTx(tx: Transaction): Promise<string>;
69
+ /**
70
+ * Deserializes signed EVM transaction
71
+ *
72
+ * @param txData
73
+ */
74
+ deserializeTx(txData: string): Promise<Transaction>;
75
+ /**
76
+ * Gets the status of the raw starknet transaction
77
+ *
78
+ * @param tx
79
+ */
80
+ getTxStatus(tx: string): Promise<"pending" | "success" | "not_found" | "reverted">;
81
+ /**
82
+ * Gets the status of the EVM transaction with a specific txId
83
+ *
84
+ * @param txId
85
+ */
86
+ getTxIdStatus(txId: string): Promise<"pending" | "success" | "not_found" | "reverted">;
87
+ onBeforeTxSigned(callback: (tx: TransactionRequest) => Promise<void>): void;
88
+ offBeforeTxSigned(callback: (tx: TransactionRequest) => Promise<void>): boolean;
89
+ onSendTransaction(callback: (tx: string) => Promise<string>): void;
90
+ offSendTransaction(callback: (tx: string) => Promise<string>): boolean;
91
+ onBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): void;
92
+ offBeforeTxReplace(callback: (oldTx: string, oldTxId: string, newTx: string, newTxId: string) => Promise<void>): boolean;
93
+ traceTransaction(txId: string): Promise<EVMTxTrace>;
94
+ }