@ledgerhq/coin-evm 2.1.0-windows-certificate.0 → 2.1.0

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 (204) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +76 -11
  3. package/lib/__tests__/coin-tester/indexer.d.ts +4 -6
  4. package/lib/__tests__/coin-tester/indexer.d.ts.map +1 -1
  5. package/lib/__tests__/coin-tester/indexer.js +562 -119
  6. package/lib/__tests__/coin-tester/indexer.js.map +1 -1
  7. package/lib/__tests__/coin-tester/scenarios/ethereum.d.ts.map +1 -1
  8. package/lib/__tests__/coin-tester/scenarios/ethereum.js +15 -10
  9. package/lib/__tests__/coin-tester/scenarios/ethereum.js.map +1 -1
  10. package/lib/__tests__/coin-tester/scenarios/polygon.d.ts.map +1 -1
  11. package/lib/__tests__/coin-tester/scenarios/polygon.js +13 -8
  12. package/lib/__tests__/coin-tester/scenarios/polygon.js.map +1 -1
  13. package/lib/__tests__/fixtures/synchronization.fixtures.d.ts +3 -7
  14. package/lib/__tests__/fixtures/synchronization.fixtures.d.ts.map +1 -1
  15. package/lib/__tests__/fixtures/transaction.fixtures.d.ts +4 -4
  16. package/lib/__tests__/unit/api/node/ledger.unit.test.js +69 -0
  17. package/lib/__tests__/unit/api/node/ledger.unit.test.js.map +1 -1
  18. package/lib/__tests__/unit/api/node/rpc.unit.test.js +51 -3
  19. package/lib/__tests__/unit/api/node/rpc.unit.test.js.map +1 -1
  20. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js +1 -0
  21. package/lib/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
  22. package/lib/__tests__/unit/getTransactionStatus.unit.test.js +4 -2
  23. package/lib/__tests__/unit/getTransactionStatus.unit.test.js.map +1 -1
  24. package/lib/__tests__/unit/logic.unit.test.js +46 -5
  25. package/lib/__tests__/unit/logic.unit.test.js.map +1 -1
  26. package/lib/__tests__/unit/preload.unit.test.js +4 -4
  27. package/lib/__tests__/unit/preload.unit.test.js.map +1 -1
  28. package/lib/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
  29. package/lib/__tests__/unit/synchronization.unit.test.js.map +1 -1
  30. package/lib/__tests__/unit/transaction.unit.test.js +95 -28
  31. package/lib/__tests__/unit/transaction.unit.test.js.map +1 -1
  32. package/lib/abis/scrollGasPriceOracle.abi.json +265 -0
  33. package/lib/api/gasTracker/ledger.d.ts +3 -6
  34. package/lib/api/gasTracker/ledger.d.ts.map +1 -1
  35. package/lib/api/gasTracker/ledger.js +3 -1
  36. package/lib/api/gasTracker/ledger.js.map +1 -1
  37. package/lib/api/node/ledger.d.ts +10 -0
  38. package/lib/api/node/ledger.d.ts.map +1 -1
  39. package/lib/api/node/ledger.js +52 -2
  40. package/lib/api/node/ledger.js.map +1 -1
  41. package/lib/api/node/rpc.common.d.ts +10 -0
  42. package/lib/api/node/rpc.common.d.ts.map +1 -1
  43. package/lib/api/node/rpc.common.js +51 -10
  44. package/lib/api/node/rpc.common.js.map +1 -1
  45. package/lib/api/node/types.d.ts +1 -0
  46. package/lib/api/node/types.d.ts.map +1 -1
  47. package/lib/api/node/types.js.map +1 -1
  48. package/lib/bridge/js.d.ts +1 -1
  49. package/lib/bridge/js.d.ts.map +1 -1
  50. package/lib/bridge/js.js +1 -1
  51. package/lib/bridge/js.js.map +1 -1
  52. package/lib/config.d.ts +1 -1
  53. package/lib/config.d.ts.map +1 -1
  54. package/lib/editTransaction/getEditTransactionPatch.d.ts.map +1 -1
  55. package/lib/editTransaction/getEditTransactionPatch.js +4 -4
  56. package/lib/editTransaction/getEditTransactionPatch.js.map +1 -1
  57. package/lib/getTransactionStatus.d.ts +3 -3
  58. package/lib/getTransactionStatus.d.ts.map +1 -1
  59. package/lib/getTransactionStatus.js +1 -0
  60. package/lib/getTransactionStatus.js.map +1 -1
  61. package/lib/hw-getAddress.d.ts.map +1 -1
  62. package/lib/hw-getAddress.js +6 -2
  63. package/lib/hw-getAddress.js.map +1 -1
  64. package/lib/logic.d.ts +11 -11
  65. package/lib/logic.d.ts.map +1 -1
  66. package/lib/logic.js +6 -0
  67. package/lib/logic.js.map +1 -1
  68. package/lib/nftResolvers.d.ts +2 -2
  69. package/lib/prepareTransaction.d.ts +1 -1
  70. package/lib/prepareTransaction.d.ts.map +1 -1
  71. package/lib/signOperation.d.ts +2 -2
  72. package/lib/signOperation.d.ts.map +1 -1
  73. package/lib/signOperation.js +1 -1
  74. package/lib/signOperation.js.map +1 -1
  75. package/lib/specs.d.ts.map +1 -1
  76. package/lib/specs.js +2 -0
  77. package/lib/specs.js.map +1 -1
  78. package/lib/synchronization.d.ts +2 -2
  79. package/lib/synchronization.d.ts.map +1 -1
  80. package/lib/synchronization.js +3 -3
  81. package/lib/synchronization.js.map +1 -1
  82. package/lib/transaction.d.ts +5 -3
  83. package/lib/transaction.d.ts.map +1 -1
  84. package/lib/transaction.js +14 -3
  85. package/lib/transaction.js.map +1 -1
  86. package/lib/types/transaction.d.ts +14 -10
  87. package/lib/types/transaction.d.ts.map +1 -1
  88. package/lib-es/__tests__/coin-tester/indexer.d.ts +4 -6
  89. package/lib-es/__tests__/coin-tester/indexer.d.ts.map +1 -1
  90. package/lib-es/__tests__/coin-tester/indexer.js +559 -116
  91. package/lib-es/__tests__/coin-tester/indexer.js.map +1 -1
  92. package/lib-es/__tests__/coin-tester/scenarios/ethereum.d.ts.map +1 -1
  93. package/lib-es/__tests__/coin-tester/scenarios/ethereum.js +15 -10
  94. package/lib-es/__tests__/coin-tester/scenarios/ethereum.js.map +1 -1
  95. package/lib-es/__tests__/coin-tester/scenarios/polygon.d.ts.map +1 -1
  96. package/lib-es/__tests__/coin-tester/scenarios/polygon.js +13 -8
  97. package/lib-es/__tests__/coin-tester/scenarios/polygon.js.map +1 -1
  98. package/lib-es/__tests__/fixtures/synchronization.fixtures.d.ts +3 -7
  99. package/lib-es/__tests__/fixtures/synchronization.fixtures.d.ts.map +1 -1
  100. package/lib-es/__tests__/fixtures/transaction.fixtures.d.ts +4 -4
  101. package/lib-es/__tests__/unit/api/node/ledger.unit.test.js +69 -0
  102. package/lib-es/__tests__/unit/api/node/ledger.unit.test.js.map +1 -1
  103. package/lib-es/__tests__/unit/api/node/rpc.unit.test.js +51 -3
  104. package/lib-es/__tests__/unit/api/node/rpc.unit.test.js.map +1 -1
  105. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js +1 -0
  106. package/lib-es/__tests__/unit/deviceTransactionConfig.unit.test.js.map +1 -1
  107. package/lib-es/__tests__/unit/getTransactionStatus.unit.test.js +4 -2
  108. package/lib-es/__tests__/unit/getTransactionStatus.unit.test.js.map +1 -1
  109. package/lib-es/__tests__/unit/logic.unit.test.js +47 -6
  110. package/lib-es/__tests__/unit/logic.unit.test.js.map +1 -1
  111. package/lib-es/__tests__/unit/preload.unit.test.js +4 -4
  112. package/lib-es/__tests__/unit/preload.unit.test.js.map +1 -1
  113. package/lib-es/__tests__/unit/prepareTransaction.unit.test.js.map +1 -1
  114. package/lib-es/__tests__/unit/synchronization.unit.test.js.map +1 -1
  115. package/lib-es/__tests__/unit/transaction.unit.test.js +69 -2
  116. package/lib-es/__tests__/unit/transaction.unit.test.js.map +1 -1
  117. package/lib-es/abis/scrollGasPriceOracle.abi.json +265 -0
  118. package/lib-es/api/gasTracker/ledger.d.ts +3 -6
  119. package/lib-es/api/gasTracker/ledger.d.ts.map +1 -1
  120. package/lib-es/api/gasTracker/ledger.js +3 -1
  121. package/lib-es/api/gasTracker/ledger.js.map +1 -1
  122. package/lib-es/api/node/ledger.d.ts +10 -0
  123. package/lib-es/api/node/ledger.d.ts.map +1 -1
  124. package/lib-es/api/node/ledger.js +50 -1
  125. package/lib-es/api/node/ledger.js.map +1 -1
  126. package/lib-es/api/node/rpc.common.d.ts +10 -0
  127. package/lib-es/api/node/rpc.common.d.ts.map +1 -1
  128. package/lib-es/api/node/rpc.common.js +49 -9
  129. package/lib-es/api/node/rpc.common.js.map +1 -1
  130. package/lib-es/api/node/types.d.ts +1 -0
  131. package/lib-es/api/node/types.d.ts.map +1 -1
  132. package/lib-es/api/node/types.js.map +1 -1
  133. package/lib-es/bridge/js.d.ts +1 -1
  134. package/lib-es/bridge/js.d.ts.map +1 -1
  135. package/lib-es/bridge/js.js +1 -1
  136. package/lib-es/bridge/js.js.map +1 -1
  137. package/lib-es/config.d.ts +1 -1
  138. package/lib-es/config.d.ts.map +1 -1
  139. package/lib-es/editTransaction/getEditTransactionPatch.d.ts.map +1 -1
  140. package/lib-es/editTransaction/getEditTransactionPatch.js +4 -4
  141. package/lib-es/editTransaction/getEditTransactionPatch.js.map +1 -1
  142. package/lib-es/getTransactionStatus.d.ts +3 -3
  143. package/lib-es/getTransactionStatus.d.ts.map +1 -1
  144. package/lib-es/getTransactionStatus.js +1 -0
  145. package/lib-es/getTransactionStatus.js.map +1 -1
  146. package/lib-es/hw-getAddress.d.ts.map +1 -1
  147. package/lib-es/hw-getAddress.js +6 -2
  148. package/lib-es/hw-getAddress.js.map +1 -1
  149. package/lib-es/logic.d.ts +11 -11
  150. package/lib-es/logic.d.ts.map +1 -1
  151. package/lib-es/logic.js +6 -0
  152. package/lib-es/logic.js.map +1 -1
  153. package/lib-es/nftResolvers.d.ts +2 -2
  154. package/lib-es/prepareTransaction.d.ts +1 -1
  155. package/lib-es/prepareTransaction.d.ts.map +1 -1
  156. package/lib-es/signOperation.d.ts +2 -2
  157. package/lib-es/signOperation.d.ts.map +1 -1
  158. package/lib-es/signOperation.js +1 -1
  159. package/lib-es/signOperation.js.map +1 -1
  160. package/lib-es/specs.d.ts.map +1 -1
  161. package/lib-es/specs.js +2 -0
  162. package/lib-es/specs.js.map +1 -1
  163. package/lib-es/synchronization.d.ts +2 -2
  164. package/lib-es/synchronization.d.ts.map +1 -1
  165. package/lib-es/synchronization.js +3 -3
  166. package/lib-es/synchronization.js.map +1 -1
  167. package/lib-es/transaction.d.ts +5 -3
  168. package/lib-es/transaction.d.ts.map +1 -1
  169. package/lib-es/transaction.js +10 -1
  170. package/lib-es/transaction.js.map +1 -1
  171. package/lib-es/types/transaction.d.ts +14 -10
  172. package/lib-es/types/transaction.d.ts.map +1 -1
  173. package/package.json +17 -15
  174. package/src/__tests__/coin-tester/indexer.ts +706 -148
  175. package/src/__tests__/coin-tester/scenarios/ethereum.ts +18 -14
  176. package/src/__tests__/coin-tester/scenarios/polygon.ts +16 -12
  177. package/src/__tests__/unit/api/node/ledger.unit.test.ts +79 -0
  178. package/src/__tests__/unit/api/node/rpc.unit.test.ts +69 -3
  179. package/src/__tests__/unit/deviceTransactionConfig.unit.test.ts +1 -0
  180. package/src/__tests__/unit/editTransaction/__snapshots__/getEditTransactionPatch.test.ts.snap +0 -72
  181. package/src/__tests__/unit/editTransaction/__snapshots__/getFormattedFeeFields.test.ts.snap +720 -0
  182. package/src/__tests__/unit/getTransactionStatus.unit.test.ts +17 -17
  183. package/src/__tests__/unit/logic.unit.test.ts +53 -6
  184. package/src/__tests__/unit/preload.unit.test.ts +4 -4
  185. package/src/__tests__/unit/prepareTransaction.unit.test.ts +1 -1
  186. package/src/__tests__/unit/synchronization.unit.test.ts +1 -1
  187. package/src/__tests__/unit/transaction.unit.test.ts +98 -21
  188. package/src/abis/scrollGasPriceOracle.abi.json +265 -0
  189. package/src/api/gasTracker/ledger.ts +3 -1
  190. package/src/api/node/ledger.ts +67 -1
  191. package/src/api/node/rpc.common.ts +61 -6
  192. package/src/api/node/types.ts +4 -0
  193. package/src/bridge/js.ts +2 -2
  194. package/src/config.ts +1 -1
  195. package/src/editTransaction/getEditTransactionPatch.ts +0 -6
  196. package/src/getTransactionStatus.ts +11 -13
  197. package/src/hw-getAddress.ts +5 -4
  198. package/src/logic.ts +7 -1
  199. package/src/signOperation.ts +3 -16
  200. package/src/specs.ts +2 -0
  201. package/src/synchronization.ts +22 -20
  202. package/src/transaction.ts +24 -2
  203. package/src/types/transaction.ts +14 -10
  204. package/tsconfig.json +2 -1
@@ -12,14 +12,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.indexBlocks = exports.getLogs = exports.clearExplorerAppendix = exports.explorerAppendixByAddress = exports.setBlock = void 0;
15
+ exports.initMswHandlers = exports.indexBlocks = exports.setBlock = exports.resetIndexer = void 0;
16
+ const bignumber_js_1 = __importDefault(require("bignumber.js"));
16
17
  const node_1 = require("msw/node");
18
+ const bluebird_1 = __importDefault(require("bluebird"));
17
19
  const msw_1 = require("msw");
18
20
  const ethers_1 = require("ethers");
19
21
  const erc1155_abi_json_1 = __importDefault(require("../../abis/erc1155.abi.json"));
20
22
  const erc721_abi_json_1 = __importDefault(require("../../abis/erc721.abi.json"));
21
23
  const erc20_abi_json_1 = __importDefault(require("../../abis/erc20.abi.json"));
22
- const provider = new ethers_1.ethers.providers.StaticJsonRpcProvider("http://127.0.0.1:8545");
24
+ const logic_1 = require("../../logic");
25
+ const MAX_BLOCK_RANGE = 1024;
23
26
  const ERC20Interface = new ethers_1.utils.Interface(erc20_abi_json_1.default);
24
27
  const ERC721Interface = new ethers_1.utils.Interface(erc721_abi_json_1.default);
25
28
  const ERC1155Interface = new ethers_1.utils.Interface(erc1155_abi_json_1.default);
@@ -28,44 +31,402 @@ const TRANSFER_EVENTS_TOPICS = {
28
31
  ERC721: ERC721Interface.getEventTopic("Transfer"),
29
32
  ERC1155: ERC1155Interface.getEventTopic("TransferSingle"),
30
33
  };
31
- let fromBlock;
32
- const setBlock = () => __awaiter(void 0, void 0, void 0, function* () {
33
- fromBlock = yield provider.getBlockNumber();
34
- });
35
- exports.setBlock = setBlock;
36
- exports.explorerAppendixByAddress = {};
37
- const clearExplorerAppendix = () => {
38
- for (const address in exports.explorerAppendixByAddress) {
39
- delete exports.explorerAppendixByAddress[address];
34
+ const explorerEtherscanOperationByAddress = {};
35
+ const explorerEtherscanERC20EventsByAddress = {};
36
+ const explorerEtherscanERC721EventsByAddress = {};
37
+ const explorerEtherscanERC1155EventsByAddress = {};
38
+ const explorerEtherscanInternalByAddress = {};
39
+ const explorerLedgerOperationByAddress = {};
40
+ const resetIndexer = () => {
41
+ (0, exports.setBlock)(0);
42
+ server.close();
43
+ for (const address in explorerEtherscanOperationByAddress) {
44
+ delete explorerEtherscanOperationByAddress[address];
45
+ }
46
+ for (const address in explorerEtherscanERC20EventsByAddress) {
47
+ delete explorerEtherscanERC20EventsByAddress[address];
48
+ }
49
+ for (const address in explorerEtherscanERC721EventsByAddress) {
50
+ delete explorerEtherscanERC721EventsByAddress[address];
51
+ }
52
+ for (const address in explorerEtherscanERC1155EventsByAddress) {
53
+ delete explorerEtherscanERC1155EventsByAddress[address];
54
+ }
55
+ for (const address in explorerEtherscanInternalByAddress) {
56
+ delete explorerEtherscanInternalByAddress[address];
57
+ }
58
+ for (const address in explorerLedgerOperationByAddress) {
59
+ delete explorerLedgerOperationByAddress[address];
40
60
  }
41
61
  };
42
- exports.clearExplorerAppendix = clearExplorerAppendix;
43
- const getLogs = () => __awaiter(void 0, void 0, void 0, function* () {
44
- if (!fromBlock) {
45
- yield (0, exports.setBlock)();
62
+ exports.resetIndexer = resetIndexer;
63
+ const handleLog = (log, provider) => __awaiter(void 0, void 0, void 0, function* () {
64
+ const contractDecimals = yield provider
65
+ .call({ to: log.address, data: ERC20Interface.encodeFunctionData("decimals") })
66
+ .then(res => (!res || res === "0x" ? false : true));
67
+ const isERC20 = log.topics[0] === TRANSFER_EVENTS_TOPICS.ERC20 && contractDecimals;
68
+ const isERC721 = log.topics[0] === TRANSFER_EVENTS_TOPICS.ERC721 && !contractDecimals;
69
+ const isERC1155 = log.topics[0] === TRANSFER_EVENTS_TOPICS.ERC1155;
70
+ if (isERC20) {
71
+ return handleERC20Log(log, provider);
46
72
  }
47
- const toBlock = yield provider.getBlockNumber();
48
- const logs = yield provider.getLogs({
49
- fromBlock,
50
- toBlock,
51
- topics: [
52
- [TRANSFER_EVENTS_TOPICS.ERC20, TRANSFER_EVENTS_TOPICS.ERC721, TRANSFER_EVENTS_TOPICS.ERC1155],
73
+ else if (isERC721) {
74
+ return handleERC721Log(log, provider);
75
+ }
76
+ else if (isERC1155) {
77
+ return handleERC1155Log(log, provider);
78
+ }
79
+ });
80
+ const handleERC20Log = (log, provider) => __awaiter(void 0, void 0, void 0, function* () {
81
+ var _a, _b;
82
+ const [name, ticker, decimals, block, tx, receipt] = yield Promise.all([
83
+ provider
84
+ .call({ to: log.address, data: ERC20Interface.encodeFunctionData("name") })
85
+ .then(res => ethers_1.ethers.utils.defaultAbiCoder.decode(["string"], res)[0]),
86
+ provider
87
+ .call({ to: log.address, data: ERC20Interface.encodeFunctionData("symbol") })
88
+ .then(res => ethers_1.ethers.utils.defaultAbiCoder.decode(["string"], res)[0]),
89
+ provider
90
+ .call({ to: log.address, data: ERC20Interface.encodeFunctionData("decimals") })
91
+ .then(res => new bignumber_js_1.default(res).toString()),
92
+ provider.getBlock(log.blockHash),
93
+ provider.getTransaction(log.transactionHash),
94
+ provider.getTransactionReceipt(log.transactionHash),
95
+ ]);
96
+ const from = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[1])[0]);
97
+ const to = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[2])[0]);
98
+ const amount = ethers_1.ethers.BigNumber.from(log.data === "0x" ? 0 : log.data).toString();
99
+ const etherscanErc20Event = {
100
+ blockNumber: block.number.toString(),
101
+ timeStamp: block.timestamp.toString(),
102
+ hash: log.transactionHash,
103
+ nonce: tx.nonce.toString(),
104
+ blockHash: block.hash,
105
+ from,
106
+ to,
107
+ value: amount,
108
+ tokenName: name,
109
+ tokenSymbol: ticker,
110
+ tokenDecimal: decimals,
111
+ transactionIndex: block.transactions.indexOf(log.transactionHash).toString(),
112
+ gas: tx.gasLimit.toString(),
113
+ gasPrice: ((_a = tx.gasPrice) === null || _a === void 0 ? void 0 : _a.toString()) || "",
114
+ cumulativeGasUsed: receipt.cumulativeGasUsed.toString(),
115
+ gasUsed: ((_b = receipt === null || receipt === void 0 ? void 0 : receipt.gasUsed) === null || _b === void 0 ? void 0 : _b.toString()) || "0",
116
+ input: tx.data,
117
+ confirmations: tx.confirmations.toString(),
118
+ contractAddress: tx.to.toLowerCase(),
119
+ };
120
+ if (!explorerEtherscanERC20EventsByAddress[from]) {
121
+ explorerEtherscanERC20EventsByAddress[from] = new Map();
122
+ }
123
+ if (!explorerEtherscanERC20EventsByAddress[to]) {
124
+ explorerEtherscanERC20EventsByAddress[to] = new Map();
125
+ }
126
+ explorerEtherscanERC20EventsByAddress[from].set(etherscanErc20Event.hash, etherscanErc20Event);
127
+ explorerEtherscanERC20EventsByAddress[to].set(etherscanErc20Event.hash, etherscanErc20Event);
128
+ if (!explorerLedgerOperationByAddress[from]) {
129
+ explorerLedgerOperationByAddress[from] = new Map();
130
+ }
131
+ if (!explorerLedgerOperationByAddress[to]) {
132
+ explorerLedgerOperationByAddress[to] = new Map();
133
+ }
134
+ const alreadyExistingOperation = explorerLedgerOperationByAddress[from].get(tx.hash) ||
135
+ explorerLedgerOperationByAddress[to].get(tx.hash);
136
+ const ledgerOperation = alreadyExistingOperation
137
+ ? Object.assign(Object.assign({}, alreadyExistingOperation), { transfer_events: [
138
+ {
139
+ contract: log.address,
140
+ count: amount,
141
+ from,
142
+ to,
143
+ },
144
+ ] }) : {
145
+ hash: log.transactionHash,
146
+ transaction_type: receipt.type,
147
+ nonce: "",
148
+ nonce_value: -1,
149
+ value: tx.value.toString(),
150
+ gas: tx.gasLimit.toString(),
151
+ gas_price: receipt.effectiveGasPrice.toString(),
152
+ max_fee_per_gas: tx.type === 2 ? tx.maxFeePerGas.toString() : null,
153
+ max_priority_fee_per_gas: tx.type === 2 ? tx.maxPriorityFeePerGas.toString() : null,
154
+ from: tx.from,
155
+ to: tx.to,
156
+ transfer_events: [
157
+ {
158
+ contract: log.address,
159
+ count: amount,
160
+ from,
161
+ to,
162
+ },
53
163
  ],
164
+ erc721_transfer_events: [],
165
+ erc1155_transfer_events: [],
166
+ approval_events: [],
167
+ actions: [],
168
+ confirmations: tx.confirmations,
169
+ input: null,
170
+ gas_used: receipt.gasUsed.toString(),
171
+ cumulative_gas_used: receipt.cumulativeGasUsed.toString(),
172
+ status: receipt.status,
173
+ received_at: new Date(block.timestamp * 1000).toISOString(),
174
+ block: {
175
+ hash: log.blockHash,
176
+ height: log.blockNumber,
177
+ time: new Date(block.timestamp * 1000).toISOString(),
178
+ },
179
+ };
180
+ explorerLedgerOperationByAddress[from].set(ledgerOperation.hash, ledgerOperation);
181
+ explorerLedgerOperationByAddress[to].set(ledgerOperation.hash, ledgerOperation);
182
+ });
183
+ const handleERC721Log = (log, provider) => __awaiter(void 0, void 0, void 0, function* () {
184
+ var _c, _d;
185
+ const [block, tx, receipt] = yield Promise.all([
186
+ provider.getBlock(log.blockHash),
187
+ provider.getTransaction(log.transactionHash),
188
+ provider.getTransactionReceipt(log.transactionHash),
189
+ ]);
190
+ const from = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[1])[0]);
191
+ const to = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[2])[0]);
192
+ const tokenID = ethers_1.ethers.utils.defaultAbiCoder.decode(["uint256"], log.topics[3])[0].toString();
193
+ const erc721Event = {
194
+ blockNumber: block.number.toString(),
195
+ timeStamp: block.timestamp.toString(),
196
+ hash: tx.hash,
197
+ nonce: tx.nonce.toString(),
198
+ blockHash: block.hash,
199
+ from,
200
+ to,
201
+ transactionIndex: block.transactions.indexOf(log.transactionHash).toString(),
202
+ gas: tx.gasLimit.toString(),
203
+ gasPrice: ((_c = tx.gasPrice) === null || _c === void 0 ? void 0 : _c.toString()) || "",
204
+ cumulativeGasUsed: receipt.cumulativeGasUsed.toString(),
205
+ gasUsed: ((_d = receipt === null || receipt === void 0 ? void 0 : receipt.gasUsed) === null || _d === void 0 ? void 0 : _d.toString()) || "0",
206
+ input: tx.data,
207
+ confirmations: tx.confirmations.toString(),
208
+ contractAddress: tx.to,
209
+ tokenID,
210
+ tokenName: "tokenName",
211
+ tokenSymbol: "tokenSymbol",
212
+ tokenDecimal: "0",
213
+ };
214
+ if (!explorerEtherscanERC721EventsByAddress[from]) {
215
+ explorerEtherscanERC721EventsByAddress[from] = new Map();
216
+ }
217
+ if (!explorerEtherscanERC721EventsByAddress[to]) {
218
+ explorerEtherscanERC721EventsByAddress[to] = new Map();
219
+ }
220
+ explorerEtherscanERC721EventsByAddress[from].set(erc721Event.hash, erc721Event);
221
+ explorerEtherscanERC721EventsByAddress[to].set(erc721Event.hash, erc721Event);
222
+ if (!explorerLedgerOperationByAddress[from]) {
223
+ explorerLedgerOperationByAddress[from] = new Map();
224
+ }
225
+ if (!explorerLedgerOperationByAddress[to]) {
226
+ explorerLedgerOperationByAddress[to] = new Map();
227
+ }
228
+ const alreadyExistingOperation = explorerLedgerOperationByAddress[from].get(tx.hash) ||
229
+ explorerLedgerOperationByAddress[to].get(tx.hash);
230
+ const ledgerOperation = alreadyExistingOperation
231
+ ? Object.assign(Object.assign({}, alreadyExistingOperation), { erc721_transfer_events: [
232
+ {
233
+ contract: log.address,
234
+ sender: from,
235
+ receiver: to,
236
+ token_id: tokenID,
237
+ },
238
+ ] }) : {
239
+ hash: log.transactionHash,
240
+ transaction_type: receipt.type,
241
+ nonce: "",
242
+ nonce_value: -1,
243
+ value: tx.value.toString(),
244
+ gas: tx.gasLimit.toString(),
245
+ gas_price: receipt.effectiveGasPrice.toString(),
246
+ max_fee_per_gas: tx.type === 2 ? tx.maxFeePerGas.toString() : null,
247
+ max_priority_fee_per_gas: tx.type === 2 ? tx.maxPriorityFeePerGas.toString() : null,
248
+ from: tx.from,
249
+ to: tx.to,
250
+ transfer_events: [],
251
+ erc721_transfer_events: [
252
+ {
253
+ contract: log.address,
254
+ sender: from,
255
+ receiver: to,
256
+ token_id: tokenID,
257
+ },
258
+ ],
259
+ erc1155_transfer_events: [],
260
+ approval_events: [],
261
+ actions: [],
262
+ confirmations: tx.confirmations,
263
+ input: null,
264
+ gas_used: receipt.gasUsed.toString(),
265
+ cumulative_gas_used: receipt.cumulativeGasUsed.toString(),
266
+ status: receipt.status,
267
+ received_at: new Date(block.timestamp * 1000).toISOString(),
268
+ block: {
269
+ hash: log.blockHash,
270
+ height: log.blockNumber,
271
+ time: new Date(block.timestamp * 1000).toISOString(),
272
+ },
273
+ };
274
+ explorerLedgerOperationByAddress[from].set(ledgerOperation.hash, ledgerOperation);
275
+ explorerLedgerOperationByAddress[to].set(ledgerOperation.hash, ledgerOperation);
276
+ });
277
+ const handleERC1155Log = (log, provider) => __awaiter(void 0, void 0, void 0, function* () {
278
+ const [block, tx, receipt] = yield Promise.all([
279
+ provider.getBlock(log.blockHash),
280
+ provider.getTransaction(log.transactionHash),
281
+ provider.getTransactionReceipt(log.transactionHash),
282
+ ]);
283
+ const from = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[2])[0]);
284
+ const to = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[3])[0]);
285
+ const operator = (0, logic_1.safeEncodeEIP55)(ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[1])[0]);
286
+ const transfersMap = ethers_1.ethers.utils.defaultAbiCoder
287
+ .decode(["uint256", "uint256"], log.data)
288
+ .map((value, index) => [index === 0 ? "id" : "value", value.toString()]);
289
+ const etherscanERC1155Events = transfersMap.map(([id, value]) => {
290
+ var _a, _b;
291
+ return ({
292
+ blockNumber: block.number.toString(),
293
+ timeStamp: block.timestamp.toString(),
294
+ hash: tx.hash,
295
+ nonce: tx.nonce.toString(),
296
+ blockHash: block.hash,
297
+ from,
298
+ to,
299
+ transactionIndex: block.transactions.indexOf(log.transactionHash).toString(),
300
+ gas: tx.gasLimit.toString(),
301
+ gasPrice: ((_a = tx.gasPrice) === null || _a === void 0 ? void 0 : _a.toString()) || "",
302
+ cumulativeGasUsed: receipt.cumulativeGasUsed.toString(),
303
+ gasUsed: ((_b = receipt === null || receipt === void 0 ? void 0 : receipt.gasUsed) === null || _b === void 0 ? void 0 : _b.toString()) || "0",
304
+ input: tx.data,
305
+ confirmations: tx.confirmations.toString(),
306
+ contractAddress: tx.to,
307
+ tokenID: id,
308
+ tokenValue: value,
309
+ tokenName: "tokenName",
310
+ tokenSymbol: "tokenSymbol",
311
+ });
54
312
  });
55
- for (const log of logs) {
56
- const [receipt, tx, block, contractDecimals] = yield Promise.all([
57
- provider.getTransactionReceipt(log.transactionHash),
58
- provider.getTransaction(log.transactionHash),
59
- provider.getBlock(log.blockHash),
60
- provider
61
- .call({ to: log.address, data: ERC20Interface.encodeFunctionData("decimals") })
62
- .then(res => (!res || res === "0x" ? false : true)),
313
+ etherscanERC1155Events.forEach(erc1155Event => {
314
+ if (!explorerEtherscanERC1155EventsByAddress[from]) {
315
+ explorerEtherscanERC1155EventsByAddress[from] = new Map();
316
+ }
317
+ if (!explorerEtherscanERC1155EventsByAddress[to]) {
318
+ explorerEtherscanERC1155EventsByAddress[to] = new Map();
319
+ }
320
+ explorerEtherscanERC1155EventsByAddress[from].set(erc1155Event.hash, erc1155Event);
321
+ explorerEtherscanERC1155EventsByAddress[to].set(erc1155Event.hash, erc1155Event);
322
+ });
323
+ if (!explorerLedgerOperationByAddress[from]) {
324
+ explorerLedgerOperationByAddress[from] = new Map();
325
+ }
326
+ if (!explorerLedgerOperationByAddress[to]) {
327
+ explorerLedgerOperationByAddress[to] = new Map();
328
+ }
329
+ const alreadyExistingOperation = explorerLedgerOperationByAddress[from].get(tx.hash) ||
330
+ explorerLedgerOperationByAddress[to].get(tx.hash);
331
+ const ledgerOperation = alreadyExistingOperation
332
+ ? Object.assign(Object.assign({}, alreadyExistingOperation), { erc1155_transfer_events: [
333
+ {
334
+ contract: log.address,
335
+ sender: from,
336
+ receiver: to,
337
+ operator,
338
+ transfers: [Object.fromEntries(transfersMap)],
339
+ },
340
+ ] }) : {
341
+ hash: log.transactionHash,
342
+ transaction_type: receipt.type,
343
+ nonce: "",
344
+ nonce_value: -1,
345
+ value: tx.value.toString(),
346
+ gas: tx.gasLimit.toString(),
347
+ gas_price: receipt.effectiveGasPrice.toString(),
348
+ max_fee_per_gas: tx.type === 2 ? tx.maxFeePerGas.toString() : null,
349
+ max_priority_fee_per_gas: tx.type === 2 ? tx.maxPriorityFeePerGas.toString() : null,
350
+ from: tx.from,
351
+ to: tx.to,
352
+ transfer_events: [],
353
+ erc721_transfer_events: [],
354
+ erc1155_transfer_events: [
355
+ {
356
+ contract: log.address,
357
+ sender: from,
358
+ receiver: to,
359
+ operator,
360
+ transfers: [Object.fromEntries(transfersMap)],
361
+ },
362
+ ],
363
+ approval_events: [],
364
+ actions: [],
365
+ confirmations: tx.confirmations,
366
+ input: null,
367
+ gas_used: receipt.gasUsed.toString(),
368
+ cumulative_gas_used: receipt.cumulativeGasUsed.toString(),
369
+ status: receipt.status,
370
+ received_at: new Date(block.timestamp * 1000).toISOString(),
371
+ block: {
372
+ hash: log.blockHash,
373
+ height: log.blockNumber,
374
+ time: new Date(block.timestamp * 1000).toISOString(),
375
+ },
376
+ };
377
+ explorerLedgerOperationByAddress[from].set(ledgerOperation.hash, ledgerOperation);
378
+ explorerLedgerOperationByAddress[to].set(ledgerOperation.hash, ledgerOperation);
379
+ });
380
+ const handleBlock = (blockNumber, provider) => __awaiter(void 0, void 0, void 0, function* () {
381
+ var _e, _f, _g;
382
+ const block = yield provider.getBlockWithTransactions(blockNumber);
383
+ for (const transaction of (block === null || block === void 0 ? void 0 : block.transactions) || []) {
384
+ const [tx, receipt, traces] = yield Promise.all([
385
+ provider.getTransaction(transaction.hash),
386
+ provider.getTransactionReceipt(transaction.hash),
387
+ provider.send("trace_transaction", [transaction.hash]).catch(() => []),
63
388
  ]);
64
- const isERC20 = log.topics[0] === TRANSFER_EVENTS_TOPICS.ERC20 && contractDecimals;
65
- const isERC721 = log.topics[0] === TRANSFER_EVENTS_TOPICS.ERC721 && !contractDecimals;
66
- const isERC1155 = log.topics[0] === TRANSFER_EVENTS_TOPICS.ERC1155;
67
- const operation = {
68
- hash: log.transactionHash,
389
+ const code = transaction.to ? yield provider.getCode(transaction.to) : false;
390
+ const from = (0, logic_1.safeEncodeEIP55)(transaction.from);
391
+ const to = (0, logic_1.safeEncodeEIP55)(transaction.to || "");
392
+ const etherscanOperation = {
393
+ blockNumber: block.number.toString(),
394
+ timeStamp: block.timestamp.toString(),
395
+ hash: transaction.hash,
396
+ nonce: transaction.nonce.toString(),
397
+ blockHash: block.hash,
398
+ transactionIndex: block.transactions.indexOf(transaction).toString(),
399
+ from,
400
+ to,
401
+ value: transaction.value.toBigInt().toString(),
402
+ gas: transaction.gasLimit.toString(),
403
+ gasPrice: ((_e = transaction.gasPrice) === null || _e === void 0 ? void 0 : _e.toString()) || "",
404
+ isError: receipt.status === 1 ? "0" : "1",
405
+ txreceipt_status: receipt.status.toString(),
406
+ input: transaction.data,
407
+ contractAddress: code === "0x" ? "" : transaction.to,
408
+ cumulativeGasUsed: receipt.cumulativeGasUsed.toString(),
409
+ gasUsed: ((_f = receipt === null || receipt === void 0 ? void 0 : receipt.gasUsed) === null || _f === void 0 ? void 0 : _f.toString()) || "0",
410
+ confirmations: transaction.confirmations.toString(),
411
+ methodId: ((_g = transaction.data) === null || _g === void 0 ? void 0 : _g.length) > 10 ? transaction.data.slice(0, 10) : "",
412
+ functionName: "",
413
+ };
414
+ if (!explorerEtherscanOperationByAddress[from]) {
415
+ explorerEtherscanOperationByAddress[from] = new Map();
416
+ }
417
+ if (!explorerEtherscanOperationByAddress[to]) {
418
+ explorerEtherscanOperationByAddress[to] = new Map();
419
+ }
420
+ explorerEtherscanOperationByAddress[from].set(etherscanOperation.hash, etherscanOperation);
421
+ explorerEtherscanOperationByAddress[to].set(etherscanOperation.hash, etherscanOperation);
422
+ if (!explorerLedgerOperationByAddress[from]) {
423
+ explorerLedgerOperationByAddress[from] = new Map();
424
+ }
425
+ if (!explorerLedgerOperationByAddress[to]) {
426
+ explorerLedgerOperationByAddress[to] = new Map();
427
+ }
428
+ const ledgerOperation = {
429
+ hash: receipt.transactionHash,
69
430
  transaction_type: receipt.type,
70
431
  nonce: "",
71
432
  nonce_value: -1,
@@ -76,41 +437,9 @@ const getLogs = () => __awaiter(void 0, void 0, void 0, function* () {
76
437
  max_priority_fee_per_gas: tx.type === 2 ? tx.maxPriorityFeePerGas.toString() : null,
77
438
  from: tx.from,
78
439
  to: tx.to,
79
- transfer_events: isERC20
80
- ? [
81
- {
82
- contract: log.address,
83
- count: ethers_1.ethers.BigNumber.from(log.data).toString(),
84
- from: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[1])[0],
85
- to: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[2])[0],
86
- },
87
- ]
88
- : [],
89
- erc721_transfer_events: isERC721
90
- ? [
91
- {
92
- contract: log.address,
93
- sender: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[1])[0],
94
- receiver: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[2])[0],
95
- token_id: ethers_1.ethers.BigNumber.from(log.topics[3]).toString(),
96
- },
97
- ]
98
- : [],
99
- erc1155_transfer_events: isERC1155
100
- ? [
101
- {
102
- contract: log.address,
103
- sender: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[2])[0],
104
- receiver: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[3])[0],
105
- operator: ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], log.topics[1])[0],
106
- transfers: [
107
- Object.fromEntries(ethers_1.ethers.utils.defaultAbiCoder
108
- .decode(["uint256", "uint256"], log.data)
109
- .map((value, index) => [index === 0 ? "id" : "value", value.toString()])),
110
- ],
111
- },
112
- ]
113
- : [],
440
+ transfer_events: [],
441
+ erc721_transfer_events: [],
442
+ erc1155_transfer_events: [],
114
443
  approval_events: [],
115
444
  actions: [],
116
445
  confirmations: tx.confirmations,
@@ -120,36 +449,69 @@ const getLogs = () => __awaiter(void 0, void 0, void 0, function* () {
120
449
  status: receipt.status,
121
450
  received_at: new Date(block.timestamp * 1000).toISOString(),
122
451
  block: {
123
- hash: log.blockHash,
124
- height: log.blockNumber,
452
+ hash: receipt.blockHash,
453
+ height: receipt.blockNumber,
125
454
  time: new Date(block.timestamp * 1000).toISOString(),
126
455
  },
127
456
  };
128
- const from = ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], isERC20 || isERC721 ? log.topics[1] : log.topics[2])[0];
129
- const to = ethers_1.ethers.utils.defaultAbiCoder.decode(["address"], isERC20 || isERC721 ? log.topics[2] : log.topics[3])[0];
130
- if (!exports.explorerAppendixByAddress[from]) {
131
- exports.explorerAppendixByAddress[from] = new Map();
132
- }
133
- exports.explorerAppendixByAddress[from].set(operation.hash, operation);
134
- if (!exports.explorerAppendixByAddress[to]) {
135
- exports.explorerAppendixByAddress[to] = new Map();
136
- }
137
- exports.explorerAppendixByAddress[to].set(operation.hash, operation);
138
- }
139
- return logs;
140
- });
141
- exports.getLogs = getLogs;
142
- const indexBlocks = () => __awaiter(void 0, void 0, void 0, function* () {
143
- const toBlock = yield provider.getBlockNumber();
144
- for (let blockNumber = fromBlock; blockNumber < toBlock; blockNumber++) {
145
- const block = yield provider.getBlock(blockNumber);
146
- for (const transactionHash in block.transactions) {
147
- const [receipt, tx] = yield Promise.all([
148
- provider.getTransactionReceipt(transactionHash),
149
- provider.getTransaction(transactionHash),
150
- ]);
151
- const operation = {
457
+ explorerLedgerOperationByAddress[from].set(ledgerOperation.hash, ledgerOperation);
458
+ explorerLedgerOperationByAddress[to].set(ledgerOperation.hash, ledgerOperation);
459
+ for (const { action, result, type, transactionHash, transactionPosition } of traces.filter(trace => trace.type === "call")) {
460
+ if ((action === null || action === void 0 ? void 0 : action.callType) !== "call")
461
+ continue;
462
+ const code = action.to ? yield provider.getCode(action.to) : false;
463
+ const from = (0, logic_1.safeEncodeEIP55)(action.from || "");
464
+ const to = (0, logic_1.safeEncodeEIP55)(action.to || "");
465
+ const etherscanInternalTransaction = {
466
+ blockNumber: blockNumber.toString(),
467
+ timeStamp: block.timestamp.toString(),
152
468
  hash: transactionHash,
469
+ from,
470
+ to,
471
+ value: ethers_1.ethers.BigNumber.from(action.value).toBigInt().toString(),
472
+ contractAddress: code === "0x" ? "" : action.to,
473
+ input: action.input || "0x",
474
+ type,
475
+ gas: ethers_1.ethers.BigNumber.from(action.gas).toBigInt().toString(),
476
+ gasUsed: ethers_1.ethers.BigNumber.from((result === null || result === void 0 ? void 0 : result.gasUsed) || "0")
477
+ .toBigInt()
478
+ .toString(),
479
+ traceId: transactionPosition.toString(),
480
+ isError: receipt.status === 1 ? "0" : "1",
481
+ errCode: "",
482
+ };
483
+ if (!explorerEtherscanInternalByAddress[from]) {
484
+ explorerEtherscanInternalByAddress[from] = new Map();
485
+ }
486
+ if (!explorerEtherscanInternalByAddress[to]) {
487
+ explorerEtherscanInternalByAddress[to] = new Map();
488
+ }
489
+ explorerEtherscanInternalByAddress[from].set(etherscanInternalTransaction.hash, etherscanInternalTransaction);
490
+ explorerEtherscanInternalByAddress[to].set(etherscanInternalTransaction.hash, etherscanInternalTransaction);
491
+ if (!explorerLedgerOperationByAddress[from]) {
492
+ explorerLedgerOperationByAddress[from] = new Map();
493
+ }
494
+ if (!explorerLedgerOperationByAddress[to]) {
495
+ explorerLedgerOperationByAddress[to] = new Map();
496
+ }
497
+ const alreadyExistingOperation = explorerLedgerOperationByAddress[from].get(tx.hash) ||
498
+ explorerLedgerOperationByAddress[to].get(tx.hash);
499
+ const ledgerOperation = alreadyExistingOperation
500
+ ? Object.assign(Object.assign({}, alreadyExistingOperation), { actions: [
501
+ ...alreadyExistingOperation.actions,
502
+ {
503
+ from,
504
+ to,
505
+ input: null,
506
+ value: ethers_1.ethers.BigNumber.from(action.value).toBigInt().toString(),
507
+ gas: ethers_1.ethers.BigNumber.from(action.gas).toBigInt().toString(),
508
+ gas_used: ethers_1.ethers.BigNumber.from((result === null || result === void 0 ? void 0 : result.gasUsed) || "0")
509
+ .toBigInt()
510
+ .toString(),
511
+ error: null,
512
+ },
513
+ ] }) : {
514
+ hash: receipt.transactionHash,
153
515
  transaction_type: receipt.type,
154
516
  nonce: "",
155
517
  nonce_value: -1,
@@ -164,7 +526,19 @@ const indexBlocks = () => __awaiter(void 0, void 0, void 0, function* () {
164
526
  erc721_transfer_events: [],
165
527
  erc1155_transfer_events: [],
166
528
  approval_events: [],
167
- actions: [],
529
+ actions: [
530
+ {
531
+ from,
532
+ to,
533
+ input: null,
534
+ value: ethers_1.ethers.BigNumber.from(action.value).toBigInt().toString(),
535
+ gas: ethers_1.ethers.BigNumber.from(action.gas).toBigInt().toString(),
536
+ gas_used: ethers_1.ethers.BigNumber.from((result === null || result === void 0 ? void 0 : result.gasUsed) || "0")
537
+ .toBigInt()
538
+ .toString(),
539
+ error: null,
540
+ },
541
+ ],
168
542
  confirmations: tx.confirmations,
169
543
  input: null,
170
544
  gas_used: receipt.gasUsed.toString(),
@@ -172,34 +546,103 @@ const indexBlocks = () => __awaiter(void 0, void 0, void 0, function* () {
172
546
  status: receipt.status,
173
547
  received_at: new Date(block.timestamp * 1000).toISOString(),
174
548
  block: {
175
- hash: block.hash,
176
- height: block.number,
549
+ hash: receipt.blockHash,
550
+ height: receipt.blockNumber,
177
551
  time: new Date(block.timestamp * 1000).toISOString(),
178
552
  },
179
553
  };
180
- if (!exports.explorerAppendixByAddress[tx.from]) {
181
- exports.explorerAppendixByAddress[tx.from] = new Map();
182
- }
183
- exports.explorerAppendixByAddress[tx.from].set(operation.hash, operation);
184
- if (tx.to) {
185
- if (!exports.explorerAppendixByAddress[tx.to]) {
186
- exports.explorerAppendixByAddress[tx.to] = new Map();
187
- }
188
- exports.explorerAppendixByAddress[tx.to].set(operation.hash, operation);
189
- }
554
+ explorerLedgerOperationByAddress[from].set(ledgerOperation.hash, ledgerOperation);
190
555
  }
191
556
  }
192
557
  });
558
+ let fromBlock;
559
+ const setBlock = (blockHeight) => {
560
+ fromBlock = blockHeight;
561
+ };
562
+ exports.setBlock = setBlock;
563
+ const indexBlocks = () => __awaiter(void 0, void 0, void 0, function* () {
564
+ if (!fromBlock) {
565
+ throw new Error("fromBlock is not set");
566
+ }
567
+ const provider = new ethers_1.providers.StaticJsonRpcProvider(process.env.RPC);
568
+ let latestBlockNumber = yield provider.getBlockNumber();
569
+ const toBlock = Math.min(fromBlock + MAX_BLOCK_RANGE, latestBlockNumber);
570
+ const rangeSize = toBlock - fromBlock + 1;
571
+ const blocks = rangeSize > 1
572
+ ? Array(rangeSize)
573
+ .fill("")
574
+ .map((_, index) => fromBlock + index)
575
+ .sort((a, b) => a - b)
576
+ : [latestBlockNumber];
577
+ const logs = yield provider.getLogs({
578
+ fromBlock,
579
+ toBlock,
580
+ topics: [
581
+ [TRANSFER_EVENTS_TOPICS.ERC20, TRANSFER_EVENTS_TOPICS.ERC721, TRANSFER_EVENTS_TOPICS.ERC1155],
582
+ ],
583
+ });
584
+ yield bluebird_1.default.map(blocks, (blockNumber) => __awaiter(void 0, void 0, void 0, function* () {
585
+ return Promise.all([
586
+ handleBlock(blockNumber, provider),
587
+ new Promise(resolve => setTimeout(resolve, 500)),
588
+ ]);
589
+ }), { concurrency: 10 });
590
+ yield bluebird_1.default.map(logs, (log) => __awaiter(void 0, void 0, void 0, function* () { return Promise.all([handleLog(log, provider), new Promise(resolve => setTimeout(resolve, 500))]); }), { concurrency: 10 });
591
+ latestBlockNumber = yield provider.getBlockNumber();
592
+ (0, exports.setBlock)(Math.min(toBlock, latestBlockNumber));
593
+ console.log(`Indexing completed ✓`);
594
+ });
193
595
  exports.indexBlocks = indexBlocks;
194
- const handlers = [
195
- msw_1.http.get("*/blockchain/v4/*/address/*/txs", (_a) => __awaiter(void 0, [_a], void 0, function* ({ request, params }) {
196
- const address = params["2"];
197
- const response = yield fetch((0, msw_1.bypass)(request)).then(res => res.json());
198
- const opsMap = exports.explorerAppendixByAddress[address || ""] || new Map();
199
- response.data.push(...opsMap.values());
200
- return msw_1.HttpResponse.json(response);
201
- })),
202
- ];
203
- const server = (0, node_1.setupServer)(...handlers);
204
- server.listen({ onUnhandledRequest: "bypass" });
596
+ let server;
597
+ const initMswHandlers = (currencyConfig) => {
598
+ const handlers = [];
599
+ if (currencyConfig.explorer.type === "ledger") {
600
+ handlers.push(msw_1.http.get("*.ledger.com/blockchain/v4/*/address/*/txs", (_a) => __awaiter(void 0, [_a], void 0, function* ({ request, params }) {
601
+ const address = params["2"];
602
+ const response = yield fetch((0, msw_1.bypass)(request)).then(res => res.json());
603
+ const opsMap = explorerLedgerOperationByAddress[address || ""] || new Map();
604
+ response.data.push(...opsMap.values());
605
+ return msw_1.HttpResponse.json(response);
606
+ })));
607
+ }
608
+ else {
609
+ handlers.push(msw_1.http.get(currencyConfig.explorer.uri, (_b) => __awaiter(void 0, [_b], void 0, function* ({ request }) {
610
+ const uri = new URL(request.url).searchParams;
611
+ const address = uri.get("address");
612
+ const action = uri.get("action");
613
+ const response = yield fetch((0, msw_1.bypass)(request)).then(res => res.json());
614
+ switch (action) {
615
+ case "txlist": {
616
+ const opsMap = explorerEtherscanOperationByAddress[address || ""] || new Map();
617
+ response.result.push(...opsMap.values());
618
+ break;
619
+ }
620
+ case "tokentx": {
621
+ const erc20EventsMap = explorerEtherscanERC20EventsByAddress[address || ""] || new Map();
622
+ response.result.push(...erc20EventsMap.values());
623
+ break;
624
+ }
625
+ case "tokennfttx": {
626
+ const erc721EventsMap = explorerEtherscanERC721EventsByAddress[address || ""] || new Map();
627
+ response.result.push(...erc721EventsMap.values());
628
+ break;
629
+ }
630
+ case "token1155tx": {
631
+ const erc1155EventsMap = explorerEtherscanERC1155EventsByAddress[address || ""] || new Map();
632
+ response.result.push(...erc1155EventsMap.values());
633
+ break;
634
+ }
635
+ case "txlistinternal": {
636
+ const internalMap = explorerEtherscanInternalByAddress[address || ""] || new Map();
637
+ response.result.push(...internalMap.values());
638
+ break;
639
+ }
640
+ }
641
+ return msw_1.HttpResponse.json(response);
642
+ })));
643
+ }
644
+ server = (0, node_1.setupServer)(...handlers);
645
+ server.listen({ onUnhandledRequest: "bypass" });
646
+ };
647
+ exports.initMswHandlers = initMswHandlers;
205
648
  //# sourceMappingURL=indexer.js.map