@instadapp/interop-x 0.0.0-dev.75809ae → 0.0.0-dev.7913ed9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (277) hide show
  1. package/.env.example +2 -1
  2. package/.github/workflows/ci.yml +19 -0
  3. package/dist/package.json +76 -0
  4. package/dist/src/abi/connectors/index.js +36 -0
  5. package/dist/src/abi/connectors/v1/aave.js +148 -0
  6. package/dist/src/abi/connectors/v1/aave_claim.js +4 -0
  7. package/dist/src/abi/connectors/v1/aave_migrate.js +109 -0
  8. package/dist/src/abi/connectors/v1/aave_polygon_migrate.js +110 -0
  9. package/dist/src/abi/connectors/v1/aave_stake.js +4 -0
  10. package/dist/src/abi/connectors/v1/aave_v1_import.js +54 -0
  11. package/dist/src/abi/connectors/v1/aave_v2.js +230 -0
  12. package/dist/src/abi/connectors/v1/aave_v2_import.js +59 -0
  13. package/dist/src/abi/connectors/v1/authority.js +100 -0
  14. package/dist/src/abi/connectors/v1/basic.js +136 -0
  15. package/dist/src/abi/connectors/v1/chi.js +36 -0
  16. package/dist/src/abi/connectors/v1/comp.js +4 -0
  17. package/dist/src/abi/connectors/v1/compound.js +4 -0
  18. package/dist/src/abi/connectors/v1/compoundImport.js +69 -0
  19. package/dist/src/abi/connectors/v1/compoundImport_v2.js +4 -0
  20. package/dist/src/abi/connectors/v1/compound_old.js +448 -0
  21. package/dist/src/abi/connectors/v1/curve.js +140 -0
  22. package/dist/src/abi/connectors/v1/curve_claim.js +63 -0
  23. package/dist/src/abi/connectors/v1/curve_gauge.js +158 -0
  24. package/dist/src/abi/connectors/v1/curve_sbtc.js +140 -0
  25. package/dist/src/abi/connectors/v1/curve_susd.js +140 -0
  26. package/dist/src/abi/connectors/v1/curve_three.js +79 -0
  27. package/dist/src/abi/connectors/v1/curve_y.js +140 -0
  28. package/dist/src/abi/connectors/v1/dsa_migrate_v1_to_v2.js +4 -0
  29. package/dist/src/abi/connectors/v1/dydx.js +148 -0
  30. package/dist/src/abi/connectors/v1/dydx_flash.js +52 -0
  31. package/dist/src/abi/connectors/v1/fee.js +50 -0
  32. package/dist/src/abi/connectors/v1/gelato.js +1138 -0
  33. package/dist/src/abi/connectors/v1/index.js +58 -0
  34. package/dist/src/abi/connectors/v1/instapool.js +439 -0
  35. package/dist/src/abi/connectors/v1/instapool_v2.js +126 -0
  36. package/dist/src/abi/connectors/v1/kyber.js +117 -0
  37. package/dist/src/abi/connectors/v1/maker.js +480 -0
  38. package/dist/src/abi/connectors/v1/maker_old.js +300 -0
  39. package/dist/src/abi/connectors/v1/math.js +43 -0
  40. package/dist/src/abi/connectors/v1/migrate.js +46 -0
  41. package/dist/src/abi/connectors/v1/oasis.js +198 -0
  42. package/dist/src/abi/connectors/v1/oneInch.js +160 -0
  43. package/dist/src/abi/connectors/v1/polygon_bridge.js +4 -0
  44. package/dist/src/abi/connectors/v1/refinance.js +4 -0
  45. package/dist/src/abi/connectors/v1/staking.js +220 -0
  46. package/dist/src/abi/connectors/v1/swerve.js +179 -0
  47. package/dist/src/abi/connectors/v1/uniswap.js +297 -0
  48. package/dist/src/abi/connectors/v2/1INCH-A.js +4 -0
  49. package/dist/src/abi/connectors/v2/1INCH-B.js +4 -0
  50. package/dist/src/abi/connectors/v2/AAVE-CLAIM-A.js +4 -0
  51. package/dist/src/abi/connectors/v2/AAVE-CLAIM-B.js +166 -0
  52. package/dist/src/abi/connectors/v2/AAVE-STAKE-A.js +4 -0
  53. package/dist/src/abi/connectors/v2/AAVE-V1-A.js +130 -0
  54. package/dist/src/abi/connectors/v2/AAVE-V1-IMPORT-A.js +4 -0
  55. package/dist/src/abi/connectors/v2/AAVE-V2-A.js +230 -0
  56. package/dist/src/abi/connectors/v2/AAVE-V2-IMPORT-A.js +4 -0
  57. package/dist/src/abi/connectors/v2/AAVE-V2-IMPORT-B.js +225 -0
  58. package/dist/src/abi/connectors/v2/AUTHORITY-A.js +100 -0
  59. package/dist/src/abi/connectors/v2/B-COMPOUND-A.js +4 -0
  60. package/dist/src/abi/connectors/v2/B-LIQUITY-A.js +4 -0
  61. package/dist/src/abi/connectors/v2/B-MAKERDAO-A.js +4 -0
  62. package/dist/src/abi/connectors/v2/BASIC-A.js +136 -0
  63. package/dist/src/abi/connectors/v2/BASIC-B.js +4 -0
  64. package/dist/src/abi/connectors/v2/BASIC-C.js +4 -0
  65. package/dist/src/abi/connectors/v2/COMP-A.js +4 -0
  66. package/dist/src/abi/connectors/v2/COMPOUND-A.js +4 -0
  67. package/dist/src/abi/connectors/v2/COMPOUND-IMPORT-A.js +4 -0
  68. package/dist/src/abi/connectors/v2/COMPOUND-IMPORT-B.js +195 -0
  69. package/dist/src/abi/connectors/v2/G-UNISWAP-A.js +4 -0
  70. package/dist/src/abi/connectors/v2/GELATO-AAVE-A.js +4 -0
  71. package/dist/src/abi/connectors/v2/INST-A.js +4 -0
  72. package/dist/src/abi/connectors/v2/INST-LM-A.js +4 -0
  73. package/dist/src/abi/connectors/v2/INST-STAKING-A.js +4 -0
  74. package/dist/src/abi/connectors/v2/INST-STAKING-B.js +4 -0
  75. package/dist/src/abi/connectors/v2/INSTAPOOL-A.js +4 -0
  76. package/dist/src/abi/connectors/v2/LIQUITY-A.js +4 -0
  77. package/dist/src/abi/connectors/v2/MAKERDAO-A.js +4 -0
  78. package/dist/src/abi/connectors/v2/MAKERDAO-CLAIM-A.js +136 -0
  79. package/dist/src/abi/connectors/v2/PARASWAP-A.js +4 -0
  80. package/dist/src/abi/connectors/v2/POLYGON-BRIDGE-A.js +4 -0
  81. package/dist/src/abi/connectors/v2/REFINANCE-A.js +4 -0
  82. package/dist/src/abi/connectors/v2/REFLEXER-A.js +4 -0
  83. package/dist/src/abi/connectors/v2/STAKE-ERC20-A.js +4 -0
  84. package/dist/src/abi/connectors/v2/UNISWAP-A.js +297 -0
  85. package/dist/src/abi/connectors/v2/UNISWAP-V2-A.js +4 -0
  86. package/dist/src/abi/connectors/v2/UNISWAP-V3-A.js +4 -0
  87. package/dist/src/abi/connectors/v2/UNISWAP-V3-STAKE-A.js +4 -0
  88. package/dist/src/abi/connectors/v2/WETH-A.js +4 -0
  89. package/dist/src/abi/connectors/v2/YEARN-VAULT-A.js +4 -0
  90. package/dist/src/abi/connectors/v2/index.js +89 -0
  91. package/dist/src/abi/erc20.json +350 -0
  92. package/dist/src/abi/gnosisSafe.json +747 -0
  93. package/dist/src/abi/index.js +15 -0
  94. package/dist/src/abi/interopX.json +1436 -0
  95. package/dist/src/alias.js +10 -0
  96. package/dist/src/api/index.js +36 -0
  97. package/dist/src/config/index.js +31 -0
  98. package/dist/src/constants/addresses.js +17 -0
  99. package/dist/src/constants/blockConfirmations.js +8 -0
  100. package/dist/{constants → src/constants}/index.js +2 -0
  101. package/dist/src/constants/tokens.js +130 -0
  102. package/dist/{db → src/db}/index.js +0 -0
  103. package/dist/{db → src/db}/models/index.js +1 -1
  104. package/dist/src/db/models/transaction.js +72 -0
  105. package/dist/{db → src/db}/sequelize.js +2 -1
  106. package/dist/src/gnosis/actions/aaveV2/index.js +11 -0
  107. package/dist/src/gnosis/actions/aaveV2/source.js +73 -0
  108. package/dist/src/gnosis/actions/aaveV2/target.js +8 -0
  109. package/dist/src/gnosis/actions/index.js +9 -0
  110. package/dist/src/gnosis/index.js +20 -0
  111. package/dist/src/index.js +119 -0
  112. package/dist/{logger → src/logger}/index.js +0 -0
  113. package/dist/{net → src/net}/index.js +0 -0
  114. package/dist/{net → src/net}/peer/index.js +13 -8
  115. package/dist/{net → src/net}/pool/index.js +34 -11
  116. package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +1 -1
  117. package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +21 -14
  118. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
  119. package/dist/{net → src/net}/protocol/index.js +54 -4
  120. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  121. package/dist/{tasks → src/tasks}/BaseTask.js +14 -6
  122. package/dist/src/tasks/InteropX/ProcessSubmitSubmitEvents.js +180 -0
  123. package/dist/src/tasks/InteropX/SyncLogSubmitEvents.js +86 -0
  124. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +58 -0
  125. package/dist/src/tasks/index.js +37 -0
  126. package/dist/src/typechain/Erc20.js +2 -0
  127. package/dist/src/typechain/GnosisSafe.js +2 -0
  128. package/dist/src/typechain/InteropX.js +2 -0
  129. package/dist/src/typechain/common.js +2 -0
  130. package/dist/src/typechain/factories/Erc20__factory.js +367 -0
  131. package/dist/src/typechain/factories/GnosisSafe__factory.js +1174 -0
  132. package/dist/src/typechain/factories/InteropX__factory.js +1928 -0
  133. package/dist/src/typechain/factories/index.js +12 -0
  134. package/dist/src/typechain/index.js +33 -0
  135. package/dist/{types.js → src/types.js} +0 -0
  136. package/dist/src/utils/index.js +223 -0
  137. package/package.json +33 -16
  138. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  139. package/src/abi/connectors/index.ts +14 -0
  140. package/src/abi/connectors/v1/aave.ts +147 -0
  141. package/src/abi/connectors/v1/aave_claim.ts +3 -0
  142. package/src/abi/connectors/v1/aave_migrate.ts +108 -0
  143. package/src/abi/connectors/v1/aave_polygon_migrate.ts +109 -0
  144. package/src/abi/connectors/v1/aave_stake.ts +3 -0
  145. package/src/abi/connectors/v1/aave_v1_import.ts +53 -0
  146. package/src/abi/connectors/v1/aave_v2.ts +229 -0
  147. package/src/abi/connectors/v1/aave_v2_import.ts +58 -0
  148. package/src/abi/connectors/v1/authority.ts +99 -0
  149. package/src/abi/connectors/v1/basic.ts +135 -0
  150. package/src/abi/connectors/v1/chi.ts +35 -0
  151. package/src/abi/connectors/v1/comp.ts +3 -0
  152. package/src/abi/connectors/v1/compound.ts +3 -0
  153. package/src/abi/connectors/v1/compoundImport.ts +68 -0
  154. package/src/abi/connectors/v1/compoundImport_v2.ts +3 -0
  155. package/src/abi/connectors/v1/compound_old.ts +447 -0
  156. package/src/abi/connectors/v1/curve.ts +139 -0
  157. package/src/abi/connectors/v1/curve_claim.ts +62 -0
  158. package/src/abi/connectors/v1/curve_gauge.ts +157 -0
  159. package/src/abi/connectors/v1/curve_sbtc.ts +139 -0
  160. package/src/abi/connectors/v1/curve_susd.ts +139 -0
  161. package/src/abi/connectors/v1/curve_three.ts +78 -0
  162. package/src/abi/connectors/v1/curve_y.ts +139 -0
  163. package/src/abi/connectors/v1/dsa_migrate_v1_to_v2.ts +3 -0
  164. package/src/abi/connectors/v1/dydx.ts +147 -0
  165. package/src/abi/connectors/v1/dydx_flash.ts +51 -0
  166. package/src/abi/connectors/v1/fee.ts +49 -0
  167. package/src/abi/connectors/v1/gelato.ts +1137 -0
  168. package/src/abi/connectors/v1/index.ts +42 -0
  169. package/src/abi/connectors/v1/instapool.ts +438 -0
  170. package/src/abi/connectors/v1/instapool_v2.ts +125 -0
  171. package/src/abi/connectors/v1/kyber.ts +116 -0
  172. package/src/abi/connectors/v1/maker.ts +479 -0
  173. package/src/abi/connectors/v1/maker_old.ts +299 -0
  174. package/src/abi/connectors/v1/math.ts +42 -0
  175. package/src/abi/connectors/v1/migrate.ts +45 -0
  176. package/src/abi/connectors/v1/oasis.ts +197 -0
  177. package/src/abi/connectors/v1/oneInch.ts +159 -0
  178. package/src/abi/connectors/v1/polygon_bridge.ts +3 -0
  179. package/src/abi/connectors/v1/refinance.ts +3 -0
  180. package/src/abi/connectors/v1/staking.ts +219 -0
  181. package/src/abi/connectors/v1/swerve.ts +178 -0
  182. package/src/abi/connectors/v1/uniswap.ts +297 -0
  183. package/src/abi/connectors/v2/1INCH-A.ts +3 -0
  184. package/src/abi/connectors/v2/1INCH-B.ts +3 -0
  185. package/src/abi/connectors/v2/AAVE-CLAIM-A.ts +3 -0
  186. package/src/abi/connectors/v2/AAVE-CLAIM-B.ts +165 -0
  187. package/src/abi/connectors/v2/AAVE-STAKE-A.ts +3 -0
  188. package/src/abi/connectors/v2/AAVE-V1-A.ts +130 -0
  189. package/src/abi/connectors/v2/AAVE-V1-IMPORT-A.ts +3 -0
  190. package/src/abi/connectors/v2/AAVE-V2-A.ts +229 -0
  191. package/src/abi/connectors/v2/AAVE-V2-IMPORT-A.ts +3 -0
  192. package/src/abi/connectors/v2/AAVE-V2-IMPORT-B.ts +224 -0
  193. package/src/abi/connectors/v2/AUTHORITY-A.ts +99 -0
  194. package/src/abi/connectors/v2/B-COMPOUND-A.ts +3 -0
  195. package/src/abi/connectors/v2/B-LIQUITY-A.ts +3 -0
  196. package/src/abi/connectors/v2/B-MAKERDAO-A.ts +3 -0
  197. package/src/abi/connectors/v2/BASIC-A.ts +135 -0
  198. package/src/abi/connectors/v2/BASIC-B.ts +3 -0
  199. package/src/abi/connectors/v2/BASIC-C.ts +3 -0
  200. package/src/abi/connectors/v2/COMP-A.ts +3 -0
  201. package/src/abi/connectors/v2/COMPOUND-A.ts +3 -0
  202. package/src/abi/connectors/v2/COMPOUND-IMPORT-A.ts +3 -0
  203. package/src/abi/connectors/v2/COMPOUND-IMPORT-B.ts +194 -0
  204. package/src/abi/connectors/v2/G-UNISWAP-A.ts +3 -0
  205. package/src/abi/connectors/v2/GELATO-AAVE-A.ts +3 -0
  206. package/src/abi/connectors/v2/INST-A.ts +3 -0
  207. package/src/abi/connectors/v2/INST-LM-A.ts +3 -0
  208. package/src/abi/connectors/v2/INST-STAKING-A.ts +3 -0
  209. package/src/abi/connectors/v2/INST-STAKING-B.ts +3 -0
  210. package/src/abi/connectors/v2/INSTAPOOL-A.ts +3 -0
  211. package/src/abi/connectors/v2/LIQUITY-A.ts +3 -0
  212. package/src/abi/connectors/v2/MAKERDAO-A.ts +3 -0
  213. package/src/abi/connectors/v2/MAKERDAO-CLAIM-A.ts +135 -0
  214. package/src/abi/connectors/v2/PARASWAP-A.ts +3 -0
  215. package/src/abi/connectors/v2/POLYGON-BRIDGE-A.ts +3 -0
  216. package/src/abi/connectors/v2/REFINANCE-A.ts +3 -0
  217. package/src/abi/connectors/v2/REFLEXER-A.ts +3 -0
  218. package/src/abi/connectors/v2/STAKE-ERC20-A.ts +3 -0
  219. package/src/abi/connectors/v2/UNISWAP-A.ts +297 -0
  220. package/src/abi/connectors/v2/UNISWAP-V2-A.ts +3 -0
  221. package/src/abi/connectors/v2/UNISWAP-V3-A.ts +3 -0
  222. package/src/abi/connectors/v2/UNISWAP-V3-STAKE-A.ts +3 -0
  223. package/src/abi/connectors/v2/WETH-A.ts +3 -0
  224. package/src/abi/connectors/v2/YEARN-VAULT-A.ts +3 -0
  225. package/src/abi/connectors/v2/index.ts +87 -0
  226. package/src/abi/erc20.json +350 -0
  227. package/src/abi/gnosisSafe.json +747 -0
  228. package/src/abi/index.ts +11 -0
  229. package/src/abi/interopX.json +1436 -0
  230. package/src/alias.ts +6 -0
  231. package/src/api/index.ts +36 -0
  232. package/src/config/index.ts +18 -2
  233. package/src/constants/addresses.ts +10 -6
  234. package/src/constants/blockConfirmations.ts +5 -0
  235. package/src/constants/index.ts +2 -0
  236. package/src/constants/tokens.ts +127 -0
  237. package/src/db/index.ts +1 -1
  238. package/src/db/models/index.ts +1 -1
  239. package/src/db/models/transaction.ts +136 -0
  240. package/src/db/sequelize.ts +2 -1
  241. package/src/gnosis/actions/aaveV2/index.ts +9 -0
  242. package/src/gnosis/actions/aaveV2/source.ts +118 -0
  243. package/src/gnosis/actions/aaveV2/target.ts +13 -0
  244. package/src/gnosis/actions/index.ts +5 -0
  245. package/src/gnosis/index.ts +19 -0
  246. package/src/index.ts +118 -7
  247. package/src/net/peer/index.ts +12 -10
  248. package/src/net/pool/index.ts +43 -13
  249. package/src/net/protocol/dial/BaseDialProtocol.ts +1 -1
  250. package/src/net/protocol/dial/SignatureDialProtocol.ts +25 -17
  251. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  252. package/src/net/protocol/index.ts +70 -4
  253. package/src/tasks/AutoUpdateTask.ts +82 -0
  254. package/src/tasks/BaseTask.ts +16 -7
  255. package/src/tasks/InteropX/ProcessSubmitSubmitEvents.ts +270 -0
  256. package/src/tasks/InteropX/SyncLogSubmitEvents.ts +138 -0
  257. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +70 -0
  258. package/src/tasks/index.ts +21 -2
  259. package/src/typechain/Erc20.ts +491 -0
  260. package/src/typechain/GnosisSafe.ts +1728 -0
  261. package/src/typechain/InteropX.ts +1216 -0
  262. package/src/typechain/common.ts +44 -0
  263. package/src/typechain/factories/Erc20__factory.ts +368 -0
  264. package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
  265. package/src/typechain/factories/InteropX__factory.ts +1932 -0
  266. package/src/typechain/factories/index.ts +6 -0
  267. package/src/typechain/index.ts +10 -0
  268. package/src/types.ts +2 -2
  269. package/src/utils/index.ts +285 -36
  270. package/tsconfig.json +8 -0
  271. package/dist/config/index.js +0 -17
  272. package/dist/constants/addresses.js +0 -13
  273. package/dist/db/models/execution.js +0 -38
  274. package/dist/index.js +0 -34
  275. package/dist/tasks/index.js +0 -19
  276. package/dist/utils/index.js +0 -89
  277. package/src/db/models/execution.ts +0 -57
@@ -4,8 +4,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.peerPool = exports.PeerPool = void 0;
7
- const types_1 = require("../../types");
8
- const config_1 = __importDefault(require("../../config"));
7
+ const types_1 = require("@/types");
8
+ const config_1 = __importDefault(require("@/config"));
9
+ const logger_1 = __importDefault(require("@/logger"));
10
+ const utils_1 = require("ethers/lib/utils");
11
+ const utils_2 = require("@/utils");
12
+ const chalk_1 = __importDefault(require("chalk"));
13
+ const logger = new logger_1.default('PeerPool');
9
14
  class PeerPool {
10
15
  constructor() {
11
16
  this.PEERS_CLEANUP_TIME_LIMIT = 1;
@@ -62,10 +67,14 @@ class PeerPool {
62
67
  * @emits {@link Event.POOL_PEER_ADDED}
63
68
  */
64
69
  add(peer) {
65
- if (peer && peer.id && !this.pool.get(peer.id)) {
70
+ if (peer && peer.id) {
71
+ const newPeer = !this.pool.get(peer.id);
66
72
  this.pool.set(peer.id, peer);
67
73
  peer.pooled = true;
68
- config_1.default.events.emit(types_1.Event.POOL_PEER_ADDED, peer);
74
+ if (newPeer) {
75
+ config_1.default.events.emit(types_1.Event.POOL_PEER_ADDED, peer);
76
+ logger.info(`Peer ${chalk_1.default.bold((0, utils_2.shortenHash)(peer.id, 16))} with address ${chalk_1.default.bold((0, utils_2.shortenHash)(peer.publicAddress))} added to pool`);
77
+ }
69
78
  }
70
79
  }
71
80
  /**
@@ -78,6 +87,7 @@ class PeerPool {
78
87
  if (this.pool.delete(peer.id)) {
79
88
  peer.pooled = false;
80
89
  config_1.default.events.emit(types_1.Event.POOL_PEER_REMOVED, peer);
90
+ logger.info(`Peer ${chalk_1.default.bold((0, utils_2.shortenHash)(peer.id, 16))} with address ${chalk_1.default.bold((0, utils_2.shortenHash)(peer.publicAddress))} removed from pool`);
81
91
  }
82
92
  }
83
93
  }
@@ -93,14 +103,27 @@ class PeerPool {
93
103
  get activePeerIds() {
94
104
  return this.activePeers.map((p) => p.id);
95
105
  }
106
+ getPeer(id) {
107
+ return this.pool.get(id);
108
+ }
109
+ isLeadNode(id) {
110
+ const peer = this.pool.get(id);
111
+ if (!peer) {
112
+ return false;
113
+ }
114
+ return (0, utils_1.getAddress)(peer.publicAddress) === (0, utils_1.getAddress)(config_1.default.leadNodeAddress);
115
+ }
116
+ getLeadPeer() {
117
+ return this.peers.find((p) => this.isLeadNode(p.id));
118
+ }
96
119
  cleanup() {
97
- let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60;
98
- this.peers.forEach((peerInfo) => {
99
- if (peerInfo.updated.getTime() < compDate) {
100
- console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`);
101
- this.remove(peerInfo);
102
- }
103
- });
120
+ // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
121
+ // this.peers.forEach((peerInfo) => {
122
+ // if (peerInfo.updated.getTime() < compDate) {
123
+ // console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
124
+ // this.remove(peerInfo)
125
+ // }
126
+ // })
104
127
  }
105
128
  }
106
129
  exports.PeerPool = PeerPool;
@@ -13,7 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.BaseDialProtocol = void 0;
14
14
  const it_pipe_1 = __importDefault(require("it-pipe"));
15
15
  const peer_id_1 = __importDefault(require("peer-id"));
16
- const utils_1 = require("../../../utils");
16
+ const utils_1 = require("@/utils");
17
17
  const waait_1 = __importDefault(require("waait"));
18
18
  class BaseDialProtocol {
19
19
  constructor(libp2p, protocol) {
@@ -4,44 +4,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SignatureDialProtocol = void 0;
7
- const utils_1 = require("../../../utils");
8
7
  const BaseDialProtocol_1 = require("./BaseDialProtocol");
9
8
  const waait_1 = __importDefault(require("waait"));
10
- const config_1 = __importDefault(require("config"));
11
- const constants_1 = require("../../../constants");
12
- const db_1 = require("db");
9
+ const config_1 = __importDefault(require("@/config"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
+ const gnosis_1 = require("@/gnosis");
13
14
  class SignatureDialProtocol extends BaseDialProtocol_1.BaseDialProtocol {
14
15
  constructor(libp2p) {
15
- super(libp2p, '/signatures');
16
+ super(libp2p, '/interop-x/signatures');
16
17
  this.timeout = 30000;
17
18
  }
18
19
  async response(data) {
20
+ console.log({
21
+ tag: 'SignatureDialProtocol',
22
+ data
23
+ });
19
24
  const signer = config_1.default.wallet;
20
- let event;
25
+ let transaction;
21
26
  let maxTimeout = 20000;
22
27
  do {
23
- event = await db_1.Execution.findOne({ where: { vnonce: data.vnonce.toString() } });
24
- if (!event) {
28
+ transaction = await db_1.Transaction.findOne({ where: { transactionHash: data.transactionHash } });
29
+ if (!transaction) {
25
30
  await (0, waait_1.default)(1000);
26
31
  maxTimeout -= 1000;
27
32
  }
28
- } while (!event && maxTimeout > 0);
29
- if (!event) {
33
+ } while (!transaction && maxTimeout > 0);
34
+ if (!transaction) {
30
35
  return {
31
36
  signer: signer.address,
32
37
  data: null,
33
38
  error: 'Event not found'
34
39
  };
35
40
  }
41
+ const { data: gnosisData } = await (0, gnosis_1.buildGnosisAction)(transaction, data.type);
36
42
  const signedData = await (0, utils_1.signGnosisSafeTx)({
37
- to: constants_1.addresses[event.chainId].multisend,
38
- data: 'TODO',
39
- chainId: event.chainId,
43
+ to: constants_1.addresses[data.chainId].multisend,
44
+ data: gnosisData,
45
+ chainId: data.chainId,
40
46
  safeTxGas: data.safeTxGas,
47
+ nonce: data.safeNonce,
41
48
  }, { signer });
42
49
  return {
43
50
  signer: signer.address,
44
- data: signedData,
51
+ data: signedData
45
52
  };
46
53
  }
47
54
  }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionStatusDialProtocol = void 0;
4
+ const BaseDialProtocol_1 = require("./BaseDialProtocol");
5
+ const db_1 = require("@/db");
6
+ class TransactionStatusDialProtocol extends BaseDialProtocol_1.BaseDialProtocol {
7
+ constructor(libp2p) {
8
+ super(libp2p, '/interop-x/transaction-status');
9
+ this.timeout = 30000;
10
+ }
11
+ async response(transactionHash) {
12
+ const transaction = await db_1.Transaction.findOne({ where: { transactionHash } });
13
+ if (!transaction) {
14
+ return null;
15
+ }
16
+ return {
17
+ transactionHash: transaction.transactionHash,
18
+ sourceStatus: transaction.sourceStatus,
19
+ sourceTransactionHash: transaction.sourceTransactionHash,
20
+ sourceErrors: transaction.sourceErrors,
21
+ sourceLogs: transaction.sourceLogs,
22
+ targetStatus: transaction.targetStatus,
23
+ targetTransactionHash: transaction.targetTransactionHash,
24
+ targetErrors: transaction.targetErrors,
25
+ targetLogs: transaction.targetLogs,
26
+ status: transaction.status,
27
+ };
28
+ }
29
+ }
30
+ exports.TransactionStatusDialProtocol = TransactionStatusDialProtocol;
@@ -8,15 +8,16 @@ const stream_1 = require("stream");
8
8
  const ethereumjs_util_1 = require("ethereumjs-util");
9
9
  const SignatureDialProtocol_1 = require("./dial/SignatureDialProtocol");
10
10
  const __1 = require("..");
11
- const config_1 = __importDefault(require("config"));
12
- const types_1 = require("types");
11
+ const config_1 = __importDefault(require("@/config"));
12
+ const types_1 = require("@/types");
13
+ const TransactionStatusDialProtocol_1 = require("./dial/TransactionStatusDialProtocol");
13
14
  class Protocol extends stream_1.EventEmitter {
14
15
  constructor() {
15
16
  super(...arguments);
16
17
  this.protocolMessages = [
17
18
  {
18
19
  name: 'PeerInfo',
19
- code: 0x09,
20
+ code: 0x01,
20
21
  encode: (info) => [
21
22
  Buffer.from(info.publicAddress),
22
23
  ],
@@ -24,11 +25,45 @@ class Protocol extends stream_1.EventEmitter {
24
25
  publicAddress: publicAddress.toString(),
25
26
  }),
26
27
  },
28
+ {
29
+ name: 'TransactionStatus',
30
+ code: 0x02,
31
+ encode: (transaction) => [
32
+ Buffer.from(transaction.transactionHash),
33
+ Buffer.from(transaction.sourceStatus),
34
+ Buffer.from(transaction.sourceTransactionHash || ''),
35
+ transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
36
+ transaction.sourceLogs ? transaction.sourceLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
37
+ Buffer.from(transaction.targetStatus),
38
+ Buffer.from(transaction.targetTransactionHash || ''),
39
+ transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
40
+ transaction.targetLogs ? transaction.targetLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
41
+ Buffer.from(transaction.status),
42
+ ],
43
+ decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, sourceLogs, targetStatus, targetTransactionHash, targetErrors, targetLogs, status]) => ({
44
+ transactionHash: transactionHash.toString(),
45
+ sourceStatus: sourceStatus.toString(),
46
+ sourceTransactionHash: sourceTransactionHash.toString() || null,
47
+ sourceErrors: sourceErrors.map((e) => e.toString()),
48
+ sourceLogs: sourceLogs.map(e => ({
49
+ type: e[0].toString(),
50
+ message: e[1].toString(),
51
+ })),
52
+ targetStatus: targetStatus.toString(),
53
+ targetTransactionHash: targetTransactionHash.toString() || null,
54
+ targetErrors: targetErrors.map((e) => e.toString()),
55
+ targetLogs: targetLogs.map(e => ({
56
+ type: e[0].toString(),
57
+ message: e[1].toString(),
58
+ })),
59
+ status: status.toString(),
60
+ }),
61
+ },
27
62
  ];
28
63
  }
29
64
  start({ libp2p, topic = null, }) {
30
65
  this.libp2p = libp2p;
31
- this.topic = topic || 'protocol';
66
+ this.topic = topic || 'itnerop-x-protocol';
32
67
  if (this.libp2p.isStarted())
33
68
  this.init();
34
69
  this.on('PeerInfo', (payload) => {
@@ -40,6 +75,7 @@ class Protocol extends stream_1.EventEmitter {
40
75
  });
41
76
  });
42
77
  this.signature = new SignatureDialProtocol_1.SignatureDialProtocol(this.libp2p);
78
+ this.transactionStatus = new TransactionStatusDialProtocol_1.TransactionStatusDialProtocol(this.libp2p);
43
79
  }
44
80
  init() {
45
81
  this.libp2p.pubsub.subscribe(this.topic);
@@ -75,6 +111,11 @@ class Protocol extends stream_1.EventEmitter {
75
111
  const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(data)]);
76
112
  this.libp2p.pubsub.publish(this.topic, encoded);
77
113
  }
114
+ sendTransaction(transaction) {
115
+ const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus');
116
+ const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(transaction)]);
117
+ this.libp2p.pubsub.publish(this.topic, encoded);
118
+ }
78
119
  async requestSignatures(data, peerIds) {
79
120
  try {
80
121
  peerIds = peerIds || __1.peerPool.activePeerIds;
@@ -88,5 +129,14 @@ class Protocol extends stream_1.EventEmitter {
88
129
  return [];
89
130
  }
90
131
  }
132
+ async requestTransactionStatus(transactionHash, peerId) {
133
+ try {
134
+ return await this.transactionStatus.send(transactionHash, peerId);
135
+ }
136
+ catch (error) {
137
+ console.log(error);
138
+ return null;
139
+ }
140
+ }
91
141
  }
92
142
  exports.protocol = new Protocol();
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("./BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const await_spawn_1 = __importDefault(require("await-spawn"));
9
+ const child_process_1 = require("child_process");
10
+ const config_1 = __importDefault(require("@/config"));
11
+ const waait_1 = __importDefault(require("waait"));
12
+ const package_json_1 = __importDefault(require("../../package.json"));
13
+ const currentVersion = package_json_1.default.version;
14
+ const tag = config_1.default.staging ? 'dev' : 'latest';
15
+ class AutoUpdateTask extends BaseTask_1.BaseTask {
16
+ constructor() {
17
+ super({
18
+ logger: new logger_1.default("AutoUpdateTask"),
19
+ });
20
+ this.pollIntervalMs = 60 * 10 * 1000;
21
+ }
22
+ prePollHandler() {
23
+ return config_1.default.autoUpdate && !config_1.default.isLeadNode();
24
+ }
25
+ async getInstalledVersion() {
26
+ try {
27
+ const stdout = await (0, await_spawn_1.default)('npm', ['-g', 'ls', '--depth=0', '--json']);
28
+ return JSON.parse(stdout.toString()).dependencies[package_json_1.default.name].version;
29
+ }
30
+ catch (error) {
31
+ this.logger.error(error);
32
+ return currentVersion;
33
+ }
34
+ }
35
+ async getLatestVersion() {
36
+ try {
37
+ const stdout = await (0, await_spawn_1.default)('npm', ['view', `${package_json_1.default.name}@${tag}`, 'version']);
38
+ return stdout.toString().trim();
39
+ }
40
+ catch (error) {
41
+ this.logger.error(error);
42
+ return currentVersion;
43
+ }
44
+ }
45
+ async pollHandler() {
46
+ const version = await this.getLatestVersion();
47
+ if (version === currentVersion) {
48
+ return;
49
+ }
50
+ this.logger.warn(`New version ${version} available.`);
51
+ this.logger.info('Updating...');
52
+ await (0, await_spawn_1.default)('npm', ['-g', 'install', `@instadapp/interop-x@${tag}`, '-f']);
53
+ await (0, waait_1.default)(5000);
54
+ if (version !== await this.getInstalledVersion()) {
55
+ this.logger.warn(`failed to install ${version}, retrying in 5 minutes`);
56
+ return;
57
+ }
58
+ this.logger.warn(`Installed version ${version}`);
59
+ this.logger.warn(`Restarting...`);
60
+ // TODO: its restarting in the bg, but it should be in the fg
61
+ const subprocess = (0, child_process_1.spawn)(process.argv[0], process.argv.slice(1), {
62
+ cwd: process.cwd(),
63
+ stdio: "inherit",
64
+ // shell: process.env.SHELL,
65
+ });
66
+ subprocess.unref();
67
+ process.exit();
68
+ }
69
+ }
70
+ exports.default = AutoUpdateTask;
@@ -4,16 +4,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.BaseTask = void 0;
7
- const config_1 = __importDefault(require("config"));
7
+ const config_1 = __importDefault(require("@/config"));
8
8
  const events_1 = __importDefault(require("events"));
9
9
  const waait_1 = __importDefault(require("waait"));
10
- const logger_1 = __importDefault(require("../logger"));
10
+ const logger_1 = __importDefault(require("@/logger"));
11
11
  class BaseTask extends events_1.default {
12
12
  constructor({ logger }) {
13
13
  super();
14
14
  this.started = false;
15
15
  this.pollIntervalMs = 10 * 1000;
16
16
  this.leadNodeOnly = false;
17
+ this.exceptLeadNode = false;
17
18
  this.logger = logger !== null && logger !== void 0 ? logger : new logger_1.default('BaseTask');
18
19
  }
19
20
  async pollCheck() {
@@ -28,16 +29,23 @@ class BaseTask extends events_1.default {
28
29
  }
29
30
  }
30
31
  catch (err) {
31
- this.logger.error(`poll check error: ${err.message}\ntrace: ${err.stack}`);
32
+ this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`);
32
33
  }
33
34
  await this.postPollHandler();
34
35
  }
35
36
  }
36
37
  prePollHandler() {
37
- if (!this.leadNodeOnly) {
38
- return true;
38
+ if (config_1.default.isMaintenanceMode()) {
39
+ this.logger.warn('Maintenance mode is enabled. Skipping task.');
40
+ return false;
39
41
  }
40
- return config_1.default.isLeadNode();
42
+ if (this.exceptLeadNode) {
43
+ return !config_1.default.isLeadNode();
44
+ }
45
+ if (this.leadNodeOnly) {
46
+ return config_1.default.isLeadNode();
47
+ }
48
+ return true;
41
49
  }
42
50
  async pollHandler() {
43
51
  this.logger.warn('pollHandler not implemented');
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("../BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const ethers_1 = require("ethers");
9
+ const abi_1 = __importDefault(require("@/abi"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
+ const config_1 = __importDefault(require("@/config"));
14
+ const moment_1 = __importDefault(require("moment"));
15
+ const sequelize_1 = require("sequelize");
16
+ const gnosis_1 = require("@/gnosis");
17
+ const net_1 = require("@/net");
18
+ const waait_1 = __importDefault(require("waait"));
19
+ class ProcessSubmitSubmitEvents extends BaseTask_1.BaseTask {
20
+ constructor({ chainId }) {
21
+ super({
22
+ logger: new logger_1.default("InteropX::ProcessSubmitSubmitEvents"),
23
+ });
24
+ this.leadNodeOnly = true;
25
+ this.blockConfirmationsCount = 12;
26
+ this.chainId = chainId;
27
+ }
28
+ async pollHandler() {
29
+ var _a;
30
+ const currentBlockNumber = await this.sourceProvider.getBlockNumber();
31
+ const transaction = await db_1.Transaction.findOne({
32
+ where: {
33
+ status: "pending",
34
+ sourceStatus: "pending",
35
+ sourceChainId: this.chainId,
36
+ submitBlockNumber: {
37
+ [sequelize_1.Op.lt]: currentBlockNumber - this.blockConfirmationsCount,
38
+ },
39
+ sourceDelayUntil: {
40
+ [sequelize_1.Op.or]: {
41
+ [sequelize_1.Op.is]: null,
42
+ [sequelize_1.Op.lt]: new Date(),
43
+ },
44
+ },
45
+ submitEvent: { $ne: null },
46
+ createdAt: {
47
+ [sequelize_1.Op.gt]: (0, moment_1.default)().subtract({ hours: 12 }).toDate(),
48
+ },
49
+ },
50
+ });
51
+ if (!transaction) {
52
+ return;
53
+ }
54
+ this.logger.debug(`Processing transaction ${transaction.transactionHash}`);
55
+ transaction.sourceStatus = "proccessing";
56
+ await transaction.save();
57
+ const ownersThreshold = await this.sourceGnosisContract.getThreshold();
58
+ await (0, waait_1.default)(10000);
59
+ this.logger.debug(`Build gnosis action for ${transaction.transactionHash}`);
60
+ let data, logs = [];
61
+ try {
62
+ ({ data, logs } = await (0, gnosis_1.buildGnosisAction)(transaction, "source"));
63
+ }
64
+ catch (error) {
65
+ if (error instanceof utils_1.LiquidityError) {
66
+ await transaction.save();
67
+ transaction.sourceDelayUntil = new Date(Date.now() + 60 * 5 * 1000);
68
+ transaction.sourceStatus = "pending";
69
+ await transaction.save();
70
+ throw error;
71
+ return;
72
+ }
73
+ transaction.sourceStatus = "failed";
74
+ transaction.sourceErrors = [error.message];
75
+ transaction.targetStatus = "failed";
76
+ transaction.status = "failed";
77
+ await transaction.save();
78
+ net_1.protocol.sendTransaction(transaction);
79
+ return;
80
+ }
81
+ this.logger.debug(`Generating gnosis tx for ${transaction.transactionHash}`);
82
+ let gnosisTx = await (0, utils_1.generateGnosisTransaction)({
83
+ baseGas: "0",
84
+ data,
85
+ gasPrice: "0",
86
+ gasToken: "0x0000000000000000000000000000000000000000",
87
+ nonce: "0",
88
+ operation: "1",
89
+ refundReceiver: "0x0000000000000000000000000000000000000000",
90
+ safeAddress: this.sourceGnosisContract.address,
91
+ safeTxGas: "79668",
92
+ to: constants_1.addresses[transaction.sourceChainId].multisend,
93
+ value: "0",
94
+ }, this.sourceGnosisContract);
95
+ const owners = await this.sourceGnosisContract
96
+ .getOwners()
97
+ .then((owners) => owners.map((owner) => owner.toLowerCase()));
98
+ const ownerPeerIds = net_1.peerPool.activePeers
99
+ .filter((peer) => owners.includes(peer.publicAddress.toLowerCase()))
100
+ .map((peer) => peer.id);
101
+ console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
102
+ console.log(ownerPeerIds);
103
+ const signatures = await net_1.protocol.requestSignatures({
104
+ type: "source",
105
+ transactionHash: transaction.transactionHash,
106
+ safeTxGas: gnosisTx.safeTxGas,
107
+ safeNonce: gnosisTx.nonce,
108
+ chainId: this.chainId,
109
+ }, ownerPeerIds);
110
+ const validSignatures = signatures.filter((s) => !!s.data && s.data !== "0x");
111
+ console.log({
112
+ signatures,
113
+ validSignatures,
114
+ ownersThreshold: ownersThreshold.toString(),
115
+ });
116
+ if (validSignatures.length === 0 ||
117
+ ownersThreshold.gt(validSignatures.length)) {
118
+ await transaction.save();
119
+ transaction.sourceDelayUntil = new Date(Date.now() + 30 * 1000);
120
+ transaction.sourceStatus = "pending";
121
+ await transaction.save();
122
+ const errorMessage = (_a = signatures.find((s) => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
123
+ throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ""));
124
+ }
125
+ console.log(`Executing transaction for execution ${transaction.transactionHash}`);
126
+ const { data: txData } = await this.sourceGnosisContract.populateTransaction.execTransaction(gnosisTx.to, gnosisTx.value, gnosisTx.data, gnosisTx.operation, gnosisTx.safeTxGas, gnosisTx.baseGas, gnosisTx.gasPrice, gnosisTx.gasToken, gnosisTx.refundReceiver, (0, utils_1.buildSignatureBytes)(validSignatures));
127
+ const [gasPrice, gasLimit] = await Promise.all([
128
+ this.sourceProvider.getGasPrice(),
129
+ this.sourceProvider.estimateGas({
130
+ from: this.sourceWallet.address,
131
+ to: this.sourceGnosisContract.address,
132
+ data: txData,
133
+ }),
134
+ ]);
135
+ const txSent = await this.sourceWallet.sendTransaction({
136
+ from: this.sourceWallet.address,
137
+ to: this.sourceGnosisContract.address,
138
+ gasPrice: gasPrice.mul(120).div(100),
139
+ gasLimit: gasLimit.mul(120).div(100),
140
+ data: txData,
141
+ });
142
+ console.log(txSent);
143
+ const receipt = await txSent.wait();
144
+ const parsedLogs = [];
145
+ receipt.logs.forEach((log) => {
146
+ try {
147
+ parsedLogs.push(this.sourceGnosisContract.interface.parseLog(log));
148
+ }
149
+ catch (e) { }
150
+ });
151
+ if (parsedLogs.find((e) => e.name === "ExecutionSuccess")) {
152
+ console.log("ExecutionSuccess");
153
+ transaction.sourceStatus = "success";
154
+ if (txSent.blockNumber)
155
+ transaction.sourceBlockNumber = txSent.blockNumber;
156
+ transaction.sourceTransactionHash = txSent.hash;
157
+ transaction.sourceLogs = logs;
158
+ await transaction.save();
159
+ }
160
+ else {
161
+ console.log("ExecutionFailure");
162
+ transaction.sourceStatus = "failed";
163
+ if (txSent.blockNumber)
164
+ transaction.sourceBlockNumber = txSent.blockNumber;
165
+ transaction.sourceTransactionHash = txSent.hash;
166
+ transaction.sourceTransactionHash = txSent.hash;
167
+ transaction.status = "failed";
168
+ await transaction.save();
169
+ }
170
+ net_1.protocol.sendTransaction(transaction);
171
+ }
172
+ async start() {
173
+ this.blockConfirmationsCount = constants_1.blockConfirmations[this.chainId] + 1;
174
+ this.sourceProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
175
+ this.sourceWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, this.sourceProvider);
176
+ this.sourceGnosisContract = (0, utils_1.getContract)(constants_1.addresses[this.chainId].gnosisSafe, abi_1.default.gnosisSafe, this.sourceWallet);
177
+ await super.start();
178
+ }
179
+ }
180
+ exports.default = ProcessSubmitSubmitEvents;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("../BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const ethers_1 = require("ethers");
9
+ const abi_1 = __importDefault(require("@/abi"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
+ const config_1 = __importDefault(require("@/config"));
14
+ class SyncLogSubmitEvents extends BaseTask_1.BaseTask {
15
+ constructor({ chainId }) {
16
+ super({
17
+ logger: new logger_1.default("InteropX::SyncLogSubmitEvents"),
18
+ });
19
+ this.chainId = chainId;
20
+ }
21
+ async pollHandler() {
22
+ const currentBlock = await this.provider.getBlockNumber();
23
+ const events = await this.contract.queryFilter(this.contract.filters.LogSubmit(), currentBlock - 2000, currentBlock);
24
+ let processedEvents = 0;
25
+ for (const event of events) {
26
+ try {
27
+ if (!event.args) {
28
+ continue;
29
+ }
30
+ const { position, actionId, actionIdHashHash, sourceSender, sourceDsaId, targetDsaId, sourceChainId, targetChainId, vnonce, metadata, } = event.args;
31
+ const uniqueIdentifier = {
32
+ actionId,
33
+ vnonce: vnonce.toString(),
34
+ sourceSender: sourceSender.toString(),
35
+ sourceChainId: sourceChainId.toNumber(),
36
+ targetChainId: targetChainId.toNumber(),
37
+ sourceDsaId: sourceDsaId.toString(),
38
+ targetDsaId: targetDsaId.toString(),
39
+ };
40
+ let transactionHash = (0, utils_1.generateInteropTransactionHash)(uniqueIdentifier);
41
+ const transaction = await db_1.Transaction.findOne({
42
+ where: { transactionHash },
43
+ });
44
+ if (transaction) {
45
+ continue;
46
+ }
47
+ await db_1.Transaction.create(Object.assign(Object.assign({ transactionHash }, uniqueIdentifier), { submitChainId: this.chainId, submitTransactionHash: event.transactionHash, submitBlockNumber: event.blockNumber, submitCreatedAt: new Date(), submitEvent: {
48
+ actionId,
49
+ actionIdHashHash,
50
+ vnonce: vnonce.toString(),
51
+ position: {
52
+ withdraw: position.withdraw.map((v) => ({
53
+ sourceToken: v.sourceToken,
54
+ targetToken: v.targetToken,
55
+ amount: v.amount.toString(),
56
+ })),
57
+ supply: position.supply.map((v) => ({
58
+ sourceToken: v.sourceToken,
59
+ targetToken: v.targetToken,
60
+ amount: v.amount.toString(),
61
+ })),
62
+ },
63
+ sourceChainId: sourceChainId.toNumber(),
64
+ targetChainId: targetChainId.toNumber(),
65
+ sourceSender,
66
+ sourceDsaId: sourceDsaId.toString(),
67
+ targetDsaId: targetDsaId.toString(),
68
+ metadata,
69
+ } }));
70
+ this.logger.info(`New InteropX tranaction: ${transactionHash} `);
71
+ }
72
+ catch (error) {
73
+ this.logger.error(error);
74
+ }
75
+ }
76
+ if (processedEvents > 0)
77
+ this.logger.info(`${processedEvents} events processed`);
78
+ }
79
+ async start() {
80
+ this.contractAddress = constants_1.addresses[this.chainId].interopX;
81
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
82
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopX, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
83
+ await super.start();
84
+ }
85
+ }
86
+ exports.default = SyncLogSubmitEvents;