@instadapp/interop-x 0.0.0-dev.0134f74

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 (275) hide show
  1. package/.env.example +2 -0
  2. package/.github/workflows/ci.yml +19 -0
  3. package/.github/workflows/publish-dev.yml +30 -0
  4. package/README.md +21 -0
  5. package/bin/interop-x +2 -0
  6. package/dist/package.json +76 -0
  7. package/dist/src/abi/connectors/index.js +36 -0
  8. package/dist/src/abi/connectors/v1/aave.js +148 -0
  9. package/dist/src/abi/connectors/v1/aave_claim.js +4 -0
  10. package/dist/src/abi/connectors/v1/aave_migrate.js +109 -0
  11. package/dist/src/abi/connectors/v1/aave_polygon_migrate.js +110 -0
  12. package/dist/src/abi/connectors/v1/aave_stake.js +4 -0
  13. package/dist/src/abi/connectors/v1/aave_v1_import.js +54 -0
  14. package/dist/src/abi/connectors/v1/aave_v2.js +230 -0
  15. package/dist/src/abi/connectors/v1/aave_v2_import.js +59 -0
  16. package/dist/src/abi/connectors/v1/authority.js +100 -0
  17. package/dist/src/abi/connectors/v1/basic.js +136 -0
  18. package/dist/src/abi/connectors/v1/chi.js +36 -0
  19. package/dist/src/abi/connectors/v1/comp.js +4 -0
  20. package/dist/src/abi/connectors/v1/compound.js +4 -0
  21. package/dist/src/abi/connectors/v1/compoundImport.js +69 -0
  22. package/dist/src/abi/connectors/v1/compoundImport_v2.js +4 -0
  23. package/dist/src/abi/connectors/v1/compound_old.js +448 -0
  24. package/dist/src/abi/connectors/v1/curve.js +140 -0
  25. package/dist/src/abi/connectors/v1/curve_claim.js +63 -0
  26. package/dist/src/abi/connectors/v1/curve_gauge.js +158 -0
  27. package/dist/src/abi/connectors/v1/curve_sbtc.js +140 -0
  28. package/dist/src/abi/connectors/v1/curve_susd.js +140 -0
  29. package/dist/src/abi/connectors/v1/curve_three.js +79 -0
  30. package/dist/src/abi/connectors/v1/curve_y.js +140 -0
  31. package/dist/src/abi/connectors/v1/dsa_migrate_v1_to_v2.js +4 -0
  32. package/dist/src/abi/connectors/v1/dydx.js +148 -0
  33. package/dist/src/abi/connectors/v1/dydx_flash.js +52 -0
  34. package/dist/src/abi/connectors/v1/fee.js +50 -0
  35. package/dist/src/abi/connectors/v1/gelato.js +1138 -0
  36. package/dist/src/abi/connectors/v1/index.js +58 -0
  37. package/dist/src/abi/connectors/v1/instapool.js +439 -0
  38. package/dist/src/abi/connectors/v1/instapool_v2.js +126 -0
  39. package/dist/src/abi/connectors/v1/kyber.js +117 -0
  40. package/dist/src/abi/connectors/v1/maker.js +480 -0
  41. package/dist/src/abi/connectors/v1/maker_old.js +300 -0
  42. package/dist/src/abi/connectors/v1/math.js +43 -0
  43. package/dist/src/abi/connectors/v1/migrate.js +46 -0
  44. package/dist/src/abi/connectors/v1/oasis.js +198 -0
  45. package/dist/src/abi/connectors/v1/oneInch.js +160 -0
  46. package/dist/src/abi/connectors/v1/polygon_bridge.js +4 -0
  47. package/dist/src/abi/connectors/v1/refinance.js +4 -0
  48. package/dist/src/abi/connectors/v1/staking.js +220 -0
  49. package/dist/src/abi/connectors/v1/swerve.js +179 -0
  50. package/dist/src/abi/connectors/v1/uniswap.js +297 -0
  51. package/dist/src/abi/connectors/v2/1INCH-A.js +4 -0
  52. package/dist/src/abi/connectors/v2/1INCH-B.js +4 -0
  53. package/dist/src/abi/connectors/v2/AAVE-CLAIM-A.js +4 -0
  54. package/dist/src/abi/connectors/v2/AAVE-CLAIM-B.js +166 -0
  55. package/dist/src/abi/connectors/v2/AAVE-STAKE-A.js +4 -0
  56. package/dist/src/abi/connectors/v2/AAVE-V1-A.js +130 -0
  57. package/dist/src/abi/connectors/v2/AAVE-V1-IMPORT-A.js +4 -0
  58. package/dist/src/abi/connectors/v2/AAVE-V2-A.js +230 -0
  59. package/dist/src/abi/connectors/v2/AAVE-V2-IMPORT-A.js +4 -0
  60. package/dist/src/abi/connectors/v2/AAVE-V2-IMPORT-B.js +225 -0
  61. package/dist/src/abi/connectors/v2/AUTHORITY-A.js +100 -0
  62. package/dist/src/abi/connectors/v2/B-COMPOUND-A.js +4 -0
  63. package/dist/src/abi/connectors/v2/B-LIQUITY-A.js +4 -0
  64. package/dist/src/abi/connectors/v2/B-MAKERDAO-A.js +4 -0
  65. package/dist/src/abi/connectors/v2/BASIC-A.js +136 -0
  66. package/dist/src/abi/connectors/v2/BASIC-B.js +4 -0
  67. package/dist/src/abi/connectors/v2/BASIC-C.js +4 -0
  68. package/dist/src/abi/connectors/v2/COMP-A.js +4 -0
  69. package/dist/src/abi/connectors/v2/COMPOUND-A.js +4 -0
  70. package/dist/src/abi/connectors/v2/COMPOUND-IMPORT-A.js +4 -0
  71. package/dist/src/abi/connectors/v2/COMPOUND-IMPORT-B.js +195 -0
  72. package/dist/src/abi/connectors/v2/G-UNISWAP-A.js +4 -0
  73. package/dist/src/abi/connectors/v2/GELATO-AAVE-A.js +4 -0
  74. package/dist/src/abi/connectors/v2/INST-A.js +4 -0
  75. package/dist/src/abi/connectors/v2/INST-LM-A.js +4 -0
  76. package/dist/src/abi/connectors/v2/INST-STAKING-A.js +4 -0
  77. package/dist/src/abi/connectors/v2/INST-STAKING-B.js +4 -0
  78. package/dist/src/abi/connectors/v2/INSTAPOOL-A.js +4 -0
  79. package/dist/src/abi/connectors/v2/LIQUITY-A.js +4 -0
  80. package/dist/src/abi/connectors/v2/MAKERDAO-A.js +4 -0
  81. package/dist/src/abi/connectors/v2/MAKERDAO-CLAIM-A.js +136 -0
  82. package/dist/src/abi/connectors/v2/PARASWAP-A.js +4 -0
  83. package/dist/src/abi/connectors/v2/POLYGON-BRIDGE-A.js +4 -0
  84. package/dist/src/abi/connectors/v2/REFINANCE-A.js +4 -0
  85. package/dist/src/abi/connectors/v2/REFLEXER-A.js +4 -0
  86. package/dist/src/abi/connectors/v2/STAKE-ERC20-A.js +4 -0
  87. package/dist/src/abi/connectors/v2/UNISWAP-A.js +297 -0
  88. package/dist/src/abi/connectors/v2/UNISWAP-V2-A.js +4 -0
  89. package/dist/src/abi/connectors/v2/UNISWAP-V3-A.js +4 -0
  90. package/dist/src/abi/connectors/v2/UNISWAP-V3-STAKE-A.js +4 -0
  91. package/dist/src/abi/connectors/v2/WETH-A.js +4 -0
  92. package/dist/src/abi/connectors/v2/YEARN-VAULT-A.js +4 -0
  93. package/dist/src/abi/connectors/v2/index.js +89 -0
  94. package/dist/src/abi/erc20.json +350 -0
  95. package/dist/src/abi/gnosisSafe.json +747 -0
  96. package/dist/src/abi/index.js +15 -0
  97. package/dist/src/abi/interopX.json +1436 -0
  98. package/dist/src/alias.js +10 -0
  99. package/dist/src/api/index.js +36 -0
  100. package/dist/src/config/index.js +31 -0
  101. package/dist/src/constants/addresses.js +17 -0
  102. package/dist/src/constants/blockConfirmations.js +8 -0
  103. package/dist/src/constants/index.js +19 -0
  104. package/dist/src/constants/tokens.js +130 -0
  105. package/dist/src/db/index.js +17 -0
  106. package/dist/src/db/models/index.js +17 -0
  107. package/dist/src/db/models/transaction.js +72 -0
  108. package/dist/src/db/sequelize.js +23 -0
  109. package/dist/src/gnosis/actions/aaveV2/index.js +11 -0
  110. package/dist/src/gnosis/actions/aaveV2/source.js +73 -0
  111. package/dist/src/gnosis/actions/aaveV2/target.js +8 -0
  112. package/dist/src/gnosis/actions/index.js +9 -0
  113. package/dist/src/gnosis/index.js +20 -0
  114. package/dist/src/index.js +119 -0
  115. package/dist/src/logger/index.js +138 -0
  116. package/dist/src/net/index.js +19 -0
  117. package/dist/src/net/peer/index.js +109 -0
  118. package/dist/src/net/pool/index.js +130 -0
  119. package/dist/src/net/protocol/dial/BaseDialProtocol.js +106 -0
  120. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +55 -0
  121. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
  122. package/dist/src/net/protocol/index.js +142 -0
  123. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  124. package/dist/src/tasks/BaseTask.js +69 -0
  125. package/dist/src/tasks/InteropX/ProcessSubmitSubmitEvents.js +180 -0
  126. package/dist/src/tasks/InteropX/SyncLogSubmitEvents.js +86 -0
  127. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +58 -0
  128. package/dist/src/tasks/index.js +37 -0
  129. package/dist/src/typechain/Erc20.js +2 -0
  130. package/dist/src/typechain/GnosisSafe.js +2 -0
  131. package/dist/src/typechain/InteropX.js +2 -0
  132. package/dist/src/typechain/common.js +2 -0
  133. package/dist/src/typechain/factories/Erc20__factory.js +367 -0
  134. package/dist/src/typechain/factories/GnosisSafe__factory.js +1174 -0
  135. package/dist/src/typechain/factories/InteropX__factory.js +1928 -0
  136. package/dist/src/typechain/factories/index.js +12 -0
  137. package/dist/src/typechain/index.js +33 -0
  138. package/dist/src/types.js +21 -0
  139. package/dist/src/utils/index.js +223 -0
  140. package/package.json +76 -0
  141. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  142. package/src/abi/connectors/index.ts +14 -0
  143. package/src/abi/connectors/v1/aave.ts +147 -0
  144. package/src/abi/connectors/v1/aave_claim.ts +3 -0
  145. package/src/abi/connectors/v1/aave_migrate.ts +108 -0
  146. package/src/abi/connectors/v1/aave_polygon_migrate.ts +109 -0
  147. package/src/abi/connectors/v1/aave_stake.ts +3 -0
  148. package/src/abi/connectors/v1/aave_v1_import.ts +53 -0
  149. package/src/abi/connectors/v1/aave_v2.ts +229 -0
  150. package/src/abi/connectors/v1/aave_v2_import.ts +58 -0
  151. package/src/abi/connectors/v1/authority.ts +99 -0
  152. package/src/abi/connectors/v1/basic.ts +135 -0
  153. package/src/abi/connectors/v1/chi.ts +35 -0
  154. package/src/abi/connectors/v1/comp.ts +3 -0
  155. package/src/abi/connectors/v1/compound.ts +3 -0
  156. package/src/abi/connectors/v1/compoundImport.ts +68 -0
  157. package/src/abi/connectors/v1/compoundImport_v2.ts +3 -0
  158. package/src/abi/connectors/v1/compound_old.ts +447 -0
  159. package/src/abi/connectors/v1/curve.ts +139 -0
  160. package/src/abi/connectors/v1/curve_claim.ts +62 -0
  161. package/src/abi/connectors/v1/curve_gauge.ts +157 -0
  162. package/src/abi/connectors/v1/curve_sbtc.ts +139 -0
  163. package/src/abi/connectors/v1/curve_susd.ts +139 -0
  164. package/src/abi/connectors/v1/curve_three.ts +78 -0
  165. package/src/abi/connectors/v1/curve_y.ts +139 -0
  166. package/src/abi/connectors/v1/dsa_migrate_v1_to_v2.ts +3 -0
  167. package/src/abi/connectors/v1/dydx.ts +147 -0
  168. package/src/abi/connectors/v1/dydx_flash.ts +51 -0
  169. package/src/abi/connectors/v1/fee.ts +49 -0
  170. package/src/abi/connectors/v1/gelato.ts +1137 -0
  171. package/src/abi/connectors/v1/index.ts +42 -0
  172. package/src/abi/connectors/v1/instapool.ts +438 -0
  173. package/src/abi/connectors/v1/instapool_v2.ts +125 -0
  174. package/src/abi/connectors/v1/kyber.ts +116 -0
  175. package/src/abi/connectors/v1/maker.ts +479 -0
  176. package/src/abi/connectors/v1/maker_old.ts +299 -0
  177. package/src/abi/connectors/v1/math.ts +42 -0
  178. package/src/abi/connectors/v1/migrate.ts +45 -0
  179. package/src/abi/connectors/v1/oasis.ts +197 -0
  180. package/src/abi/connectors/v1/oneInch.ts +159 -0
  181. package/src/abi/connectors/v1/polygon_bridge.ts +3 -0
  182. package/src/abi/connectors/v1/refinance.ts +3 -0
  183. package/src/abi/connectors/v1/staking.ts +219 -0
  184. package/src/abi/connectors/v1/swerve.ts +178 -0
  185. package/src/abi/connectors/v1/uniswap.ts +297 -0
  186. package/src/abi/connectors/v2/1INCH-A.ts +3 -0
  187. package/src/abi/connectors/v2/1INCH-B.ts +3 -0
  188. package/src/abi/connectors/v2/AAVE-CLAIM-A.ts +3 -0
  189. package/src/abi/connectors/v2/AAVE-CLAIM-B.ts +165 -0
  190. package/src/abi/connectors/v2/AAVE-STAKE-A.ts +3 -0
  191. package/src/abi/connectors/v2/AAVE-V1-A.ts +130 -0
  192. package/src/abi/connectors/v2/AAVE-V1-IMPORT-A.ts +3 -0
  193. package/src/abi/connectors/v2/AAVE-V2-A.ts +229 -0
  194. package/src/abi/connectors/v2/AAVE-V2-IMPORT-A.ts +3 -0
  195. package/src/abi/connectors/v2/AAVE-V2-IMPORT-B.ts +224 -0
  196. package/src/abi/connectors/v2/AUTHORITY-A.ts +99 -0
  197. package/src/abi/connectors/v2/B-COMPOUND-A.ts +3 -0
  198. package/src/abi/connectors/v2/B-LIQUITY-A.ts +3 -0
  199. package/src/abi/connectors/v2/B-MAKERDAO-A.ts +3 -0
  200. package/src/abi/connectors/v2/BASIC-A.ts +135 -0
  201. package/src/abi/connectors/v2/BASIC-B.ts +3 -0
  202. package/src/abi/connectors/v2/BASIC-C.ts +3 -0
  203. package/src/abi/connectors/v2/COMP-A.ts +3 -0
  204. package/src/abi/connectors/v2/COMPOUND-A.ts +3 -0
  205. package/src/abi/connectors/v2/COMPOUND-IMPORT-A.ts +3 -0
  206. package/src/abi/connectors/v2/COMPOUND-IMPORT-B.ts +194 -0
  207. package/src/abi/connectors/v2/G-UNISWAP-A.ts +3 -0
  208. package/src/abi/connectors/v2/GELATO-AAVE-A.ts +3 -0
  209. package/src/abi/connectors/v2/INST-A.ts +3 -0
  210. package/src/abi/connectors/v2/INST-LM-A.ts +3 -0
  211. package/src/abi/connectors/v2/INST-STAKING-A.ts +3 -0
  212. package/src/abi/connectors/v2/INST-STAKING-B.ts +3 -0
  213. package/src/abi/connectors/v2/INSTAPOOL-A.ts +3 -0
  214. package/src/abi/connectors/v2/LIQUITY-A.ts +3 -0
  215. package/src/abi/connectors/v2/MAKERDAO-A.ts +3 -0
  216. package/src/abi/connectors/v2/MAKERDAO-CLAIM-A.ts +135 -0
  217. package/src/abi/connectors/v2/PARASWAP-A.ts +3 -0
  218. package/src/abi/connectors/v2/POLYGON-BRIDGE-A.ts +3 -0
  219. package/src/abi/connectors/v2/REFINANCE-A.ts +3 -0
  220. package/src/abi/connectors/v2/REFLEXER-A.ts +3 -0
  221. package/src/abi/connectors/v2/STAKE-ERC20-A.ts +3 -0
  222. package/src/abi/connectors/v2/UNISWAP-A.ts +297 -0
  223. package/src/abi/connectors/v2/UNISWAP-V2-A.ts +3 -0
  224. package/src/abi/connectors/v2/UNISWAP-V3-A.ts +3 -0
  225. package/src/abi/connectors/v2/UNISWAP-V3-STAKE-A.ts +3 -0
  226. package/src/abi/connectors/v2/WETH-A.ts +3 -0
  227. package/src/abi/connectors/v2/YEARN-VAULT-A.ts +3 -0
  228. package/src/abi/connectors/v2/index.ts +87 -0
  229. package/src/abi/erc20.json +350 -0
  230. package/src/abi/gnosisSafe.json +747 -0
  231. package/src/abi/index.ts +11 -0
  232. package/src/abi/interopX.json +1436 -0
  233. package/src/alias.ts +6 -0
  234. package/src/api/index.ts +36 -0
  235. package/src/config/index.ts +40 -0
  236. package/src/constants/addresses.ts +14 -0
  237. package/src/constants/blockConfirmations.ts +5 -0
  238. package/src/constants/index.ts +3 -0
  239. package/src/constants/tokens.ts +127 -0
  240. package/src/db/index.ts +1 -0
  241. package/src/db/models/index.ts +1 -0
  242. package/src/db/models/transaction.ts +136 -0
  243. package/src/db/sequelize.ts +22 -0
  244. package/src/gnosis/actions/aaveV2/index.ts +9 -0
  245. package/src/gnosis/actions/aaveV2/source.ts +118 -0
  246. package/src/gnosis/actions/aaveV2/target.ts +13 -0
  247. package/src/gnosis/actions/index.ts +5 -0
  248. package/src/gnosis/index.ts +19 -0
  249. package/src/index.ts +150 -0
  250. package/src/logger/index.ts +157 -0
  251. package/src/net/index.ts +3 -0
  252. package/src/net/peer/index.ts +121 -0
  253. package/src/net/pool/index.ts +158 -0
  254. package/src/net/protocol/dial/BaseDialProtocol.ts +104 -0
  255. package/src/net/protocol/dial/SignatureDialProtocol.ts +70 -0
  256. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  257. package/src/net/protocol/index.ts +204 -0
  258. package/src/tasks/AutoUpdateTask.ts +82 -0
  259. package/src/tasks/BaseTask.ts +85 -0
  260. package/src/tasks/InteropX/ProcessSubmitSubmitEvents.ts +270 -0
  261. package/src/tasks/InteropX/SyncLogSubmitEvents.ts +138 -0
  262. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +70 -0
  263. package/src/tasks/index.ts +37 -0
  264. package/src/typechain/Erc20.ts +491 -0
  265. package/src/typechain/GnosisSafe.ts +1728 -0
  266. package/src/typechain/InteropX.ts +1216 -0
  267. package/src/typechain/common.ts +44 -0
  268. package/src/typechain/factories/Erc20__factory.ts +368 -0
  269. package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
  270. package/src/typechain/factories/InteropX__factory.ts +1932 -0
  271. package/src/typechain/factories/index.ts +6 -0
  272. package/src/typechain/index.ts +10 -0
  273. package/src/types.ts +39 -0
  274. package/src/utils/index.ts +365 -0
  275. package/tsconfig.json +35 -0
@@ -0,0 +1,70 @@
1
+ import { BaseDialProtocol } from "./BaseDialProtocol";
2
+ import wait from "waait";
3
+ import config from "@/config";
4
+ import { Transaction } from "@/db";
5
+ import { signGnosisSafeTx } from "@/utils";
6
+ import { addresses } from "@/constants";
7
+ import { ChainId } from "@/types";
8
+ import { buildGnosisAction } from "@/gnosis";
9
+
10
+ export interface ISignatureRequest {
11
+ type: 'source' | 'target',
12
+ transactionHash: string
13
+ safeTxGas: string
14
+ safeNonce: string
15
+ chainId: number
16
+ }
17
+ export interface ISignatureResponse {
18
+ signer: string,
19
+ data?: string | null,
20
+ error?: string | null,
21
+ }
22
+ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, ISignatureResponse> {
23
+ protected timeout = 30000;
24
+
25
+ constructor(libp2p) {
26
+ super(libp2p, '/interop-x/signatures')
27
+ }
28
+
29
+ async response(data: ISignatureRequest): Promise<ISignatureResponse> {
30
+ console.log({
31
+ tag: 'SignatureDialProtocol',
32
+ data
33
+ })
34
+ const signer = config.wallet;
35
+
36
+ let transaction: Transaction | null;
37
+ let maxTimeout = 20000;
38
+
39
+ do {
40
+ transaction = await Transaction.findOne({ where: { transactionHash: data.transactionHash } })
41
+
42
+ if (!transaction) {
43
+ await wait(1000);
44
+ maxTimeout -= 1000;
45
+ }
46
+ } while (!transaction && maxTimeout > 0)
47
+
48
+ if (!transaction) {
49
+ return {
50
+ signer: signer.address,
51
+ data: null,
52
+ error: 'Event not found'
53
+ };
54
+ }
55
+ const { data: gnosisData } = await buildGnosisAction(transaction, data.type);
56
+
57
+ const signedData = await signGnosisSafeTx({
58
+ to: addresses[data.chainId].multisend,
59
+ data: gnosisData,
60
+ chainId: data.chainId as ChainId,
61
+ safeTxGas: data.safeTxGas,
62
+ nonce: data.safeNonce,
63
+ }, { signer });
64
+
65
+ return {
66
+ signer: signer.address,
67
+ data: signedData
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,33 @@
1
+ import { BaseDialProtocol } from "./BaseDialProtocol";
2
+ import { Transaction } from "@/db";
3
+
4
+ export class TransactionStatusDialProtocol extends BaseDialProtocol<string, Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'sourceLogs' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'targetLogs' | 'status'> | null> {
5
+ protected timeout = 30000;
6
+
7
+ constructor(libp2p) {
8
+ super(libp2p, '/interop-x/transaction-status')
9
+ }
10
+
11
+ async response(transactionHash: string){
12
+ const transaction = await Transaction.findOne({ where: { transactionHash } })
13
+
14
+ if(! transaction){
15
+ return null
16
+ }
17
+ return {
18
+ transactionHash: transaction.transactionHash,
19
+
20
+ sourceStatus: transaction.sourceStatus,
21
+ sourceTransactionHash: transaction.sourceTransactionHash,
22
+ sourceErrors: transaction.sourceErrors,
23
+ sourceLogs: transaction.sourceLogs,
24
+
25
+ targetStatus: transaction.targetStatus,
26
+ targetTransactionHash: transaction.targetTransactionHash,
27
+ targetErrors: transaction.targetErrors,
28
+ targetLogs: transaction.targetLogs,
29
+
30
+ status: transaction.status,
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,204 @@
1
+ import { EventEmitter } from "stream";
2
+ import Libp2p from 'libp2p';
3
+ import { bufferToInt, rlp } from 'ethereumjs-util'
4
+ import { SignatureDialProtocol, ISignatureRequest, ISignatureResponse } from "./dial/SignatureDialProtocol";
5
+ import { IPeerInfo, peerPool } from "..";
6
+ import config from "@/config";
7
+ import { Event } from "@/types";
8
+ import { Transaction } from "@/db";
9
+ import { TransactionStatusDialProtocol } from "./dial/TransactionStatusDialProtocol";
10
+
11
+ export interface ProtocolOptions {
12
+ /* Handshake timeout in ms (default: 8000) */
13
+ timeout?: number
14
+ }
15
+
16
+
17
+ export type Message = {
18
+ name: string
19
+ code: number
20
+ payload?: any
21
+ response?: number
22
+ encode: Function
23
+ decode: Function
24
+ }
25
+
26
+ interface BaseMessageEvent {
27
+ peerId: string
28
+ name: string
29
+ code: number
30
+ data: any
31
+ }
32
+
33
+
34
+ interface PeerInfoEvent extends BaseMessageEvent {
35
+ data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
36
+ }
37
+
38
+ interface TransactionStatusEvent extends BaseMessageEvent {
39
+ data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'sourceLogs' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'targetLogs' | 'status'>
40
+ }
41
+
42
+ declare interface Protocol {
43
+ on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
44
+ on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
45
+ on(event: string, listener: (payload: BaseMessageEvent) => void): this;
46
+ }
47
+
48
+ class Protocol extends EventEmitter {
49
+ private libp2p: Libp2p;
50
+ private topic: string;
51
+ private protocolMessages: Message[] = [
52
+ {
53
+ name: 'PeerInfo',
54
+ code: 0x01,
55
+ encode: (info: Pick<IPeerInfo, 'publicAddress'>) => [
56
+ Buffer.from(info.publicAddress),
57
+ ],
58
+ decode: ([publicAddress]: [Buffer]) => ({
59
+ publicAddress: publicAddress.toString(),
60
+ }),
61
+ },
62
+ {
63
+ name: 'TransactionStatus',
64
+ code: 0x02,
65
+ encode: (transaction: Transaction) => [
66
+ Buffer.from(transaction.transactionHash),
67
+
68
+ Buffer.from(transaction.sourceStatus),
69
+ Buffer.from(transaction.sourceTransactionHash || ''),
70
+ transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
71
+ transaction.sourceLogs ? transaction.sourceLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
72
+
73
+ Buffer.from(transaction.targetStatus),
74
+ Buffer.from(transaction.targetTransactionHash || ''),
75
+ transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
76
+ transaction.targetLogs ? transaction.targetLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
77
+
78
+ Buffer.from(transaction.status),
79
+ ],
80
+ decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, sourceLogs, targetStatus, targetTransactionHash, targetErrors, targetLogs, status]: [Buffer, Buffer, Buffer, Buffer[],[Buffer,Buffer][], Buffer, Buffer, Buffer[],[Buffer,Buffer][], Buffer]) => ({
81
+ transactionHash: transactionHash.toString(),
82
+
83
+ sourceStatus: sourceStatus.toString(),
84
+ sourceTransactionHash: sourceTransactionHash.toString() || null,
85
+ sourceErrors: sourceErrors.map((e) => e.toString()),
86
+ sourceLogs: sourceLogs.map(e => ({
87
+ type: e[0].toString(),
88
+ message: e[1].toString(),
89
+ })),
90
+
91
+ targetStatus: targetStatus.toString(),
92
+ targetTransactionHash: targetTransactionHash.toString() || null,
93
+ targetErrors: targetErrors.map((e) => e.toString()),
94
+ targetLogs: targetLogs.map(e => ({
95
+ type: e[0].toString(),
96
+ message: e[1].toString(),
97
+ })),
98
+
99
+ status: status.toString(),
100
+ }),
101
+ },
102
+ ];
103
+ private signature: SignatureDialProtocol;
104
+ private transactionStatus: TransactionStatusDialProtocol;
105
+
106
+
107
+ start({ libp2p, topic = null, }) {
108
+ this.libp2p = libp2p
109
+ this.topic = topic || 'itnerop-x-protocol'
110
+
111
+ if (this.libp2p.isStarted()) this.init()
112
+
113
+ this.on('PeerInfo', (payload) => {
114
+
115
+ config.events.emit(Event.PEER_CONNECTED, {
116
+ id: payload.peerId,
117
+ publicAddress: payload.data.publicAddress,
118
+ pooled: false,
119
+ updated: new Date(),
120
+ })
121
+ })
122
+
123
+ this.signature = new SignatureDialProtocol(this.libp2p);
124
+ this.transactionStatus = new TransactionStatusDialProtocol(this.libp2p);
125
+ }
126
+
127
+
128
+ init() {
129
+ this.libp2p.pubsub.subscribe(this.topic)
130
+
131
+ this.libp2p.pubsub.on(this.topic, (message) => {
132
+ try {
133
+ const [codeBuf, payload]: any = rlp.decode(message.data);
134
+
135
+ const code = bufferToInt(codeBuf);
136
+
137
+ const protocolMessage = this.protocolMessages.find((m) => m.code === code);
138
+ if (!protocolMessage) {
139
+ return;
140
+ }
141
+ const decoded = protocolMessage.decode(payload);
142
+ this.emit(
143
+ protocolMessage.name,
144
+ {
145
+ peerId: message.from,
146
+ name: protocolMessage.name,
147
+ code: protocolMessage.code,
148
+ data: decoded
149
+ }
150
+ )
151
+
152
+ this.emit('all', {
153
+ peerId: message.from,
154
+ name: protocolMessage.name,
155
+ code: protocolMessage.code,
156
+ data: decoded,
157
+ })
158
+ } catch (err) {
159
+ console.error(err)
160
+ }
161
+ })
162
+ }
163
+
164
+
165
+ public sendPeerInfo(data: Pick<IPeerInfo, 'publicAddress'>) {
166
+ const message = this.protocolMessages.find((m) => m.name === 'PeerInfo')!
167
+
168
+ const encoded = rlp.encode([message.code, message.encode(data)]);
169
+
170
+ this.libp2p.pubsub.publish(this.topic, encoded)
171
+ }
172
+
173
+ public sendTransaction(transaction: Transaction) {
174
+ const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
175
+
176
+ const encoded = rlp.encode([message.code, message.encode(transaction)]);
177
+
178
+ this.libp2p.pubsub.publish(this.topic, encoded)
179
+ }
180
+
181
+ async requestSignatures(data: ISignatureRequest, peerIds?: string[]) {
182
+ try {
183
+ peerIds = peerIds || peerPool.activePeerIds;
184
+ const promises = peerIds.map((peerId) => this.signature.send(data, peerId))
185
+ return (await Promise.allSettled(promises))
186
+ .map((p) => p.status === 'fulfilled' ? p.value : null)
187
+ .filter(Boolean) as ISignatureResponse[]
188
+ } catch (error) {
189
+ console.log(error);
190
+ return []
191
+ }
192
+ }
193
+
194
+ async requestTransactionStatus(transactionHash: string, peerId: string) {
195
+ try {
196
+ return await this.transactionStatus.send(transactionHash, peerId);
197
+ } catch (error) {
198
+ console.log(error);
199
+ return null
200
+ }
201
+ }
202
+ }
203
+
204
+ export const protocol = new Protocol();
@@ -0,0 +1,82 @@
1
+ import { BaseTask } from "./BaseTask";
2
+ import Logger from '@/logger';
3
+ import spawnAsync from 'await-spawn';
4
+ import { spawn } from 'child_process'
5
+ import config from "@/config";
6
+ import wait from "waait";
7
+ import packageJson from "../../package.json";
8
+
9
+ const currentVersion = packageJson.version;
10
+ const tag = config.staging ? 'dev' : 'latest';
11
+
12
+ class AutoUpdateTask extends BaseTask {
13
+ pollIntervalMs: number = 60 * 10 * 1000
14
+
15
+ constructor() {
16
+ super({
17
+ logger: new Logger("AutoUpdateTask"),
18
+ })
19
+ }
20
+
21
+ prePollHandler(): boolean {
22
+ return config.autoUpdate && !config.isLeadNode();
23
+ }
24
+
25
+ async getInstalledVersion() {
26
+ try {
27
+ const stdout = await spawnAsync('npm', ['-g', 'ls', '--depth=0', '--json'])
28
+ return JSON.parse(stdout.toString()).dependencies[packageJson.name].version
29
+ } catch (error) {
30
+ this.logger.error(error)
31
+ return currentVersion
32
+ }
33
+ }
34
+
35
+ async getLatestVersion() {
36
+ try {
37
+ const stdout = await spawnAsync('npm', ['view', `${packageJson.name}@${tag}`, 'version'])
38
+ return stdout.toString().trim()
39
+ } catch (error) {
40
+ this.logger.error(error)
41
+ return currentVersion
42
+ }
43
+ }
44
+
45
+ async pollHandler() {
46
+ const version = await this.getLatestVersion()
47
+
48
+ if (version === currentVersion) {
49
+ return;
50
+ }
51
+
52
+ this.logger.warn(`New version ${version} available.`)
53
+
54
+ this.logger.info('Updating...')
55
+
56
+ await spawnAsync('npm', ['-g', 'install', `@instadapp/interop-x@${tag}`, '-f']);
57
+
58
+ await wait(5000)
59
+
60
+ if (version !== await this.getInstalledVersion()) {
61
+ this.logger.warn(`failed to install ${version}, retrying in 5 minutes`)
62
+ return;
63
+ }
64
+
65
+ this.logger.warn(`Installed version ${version}`)
66
+ this.logger.warn(`Restarting...`)
67
+
68
+
69
+ // TODO: its restarting in the bg, but it should be in the fg
70
+ const subprocess = spawn(process.argv[0], process.argv.slice(1), {
71
+ cwd: process.cwd(),
72
+ stdio: "inherit",
73
+ // shell: process.env.SHELL,
74
+ });
75
+
76
+ subprocess.unref();
77
+
78
+ process.exit()
79
+ }
80
+ }
81
+
82
+ export default AutoUpdateTask;
@@ -0,0 +1,85 @@
1
+ import config from "@/config";
2
+ import EventEmitter from "events";
3
+ import wait from "waait";
4
+ import Logger from "@/logger";
5
+
6
+ export interface IBaseTask {
7
+ pollCheck(): Promise<void>
8
+ prePollHandler(): boolean
9
+ pollHandler(): Promise<void>
10
+ postPollHandler(): Promise<void>
11
+
12
+ start(): Promise<void>
13
+ stop(): Promise<void>
14
+ }
15
+
16
+
17
+ export class BaseTask extends EventEmitter implements IBaseTask {
18
+ logger: Logger
19
+ started: boolean = false
20
+ pollIntervalMs: number = 10 * 1000
21
+ leadNodeOnly: boolean = false
22
+ exceptLeadNode: boolean = false
23
+
24
+ public constructor({ logger }: { logger?: Logger }) {
25
+ super()
26
+
27
+ this.logger = logger ?? new Logger('BaseTask')
28
+ }
29
+
30
+ async pollCheck() {
31
+ while (true) {
32
+ if (!this.started) {
33
+ return
34
+ }
35
+ try {
36
+ const shouldPoll = this.prePollHandler()
37
+ if (shouldPoll) {
38
+ await this.pollHandler()
39
+ }
40
+ } catch (err) {
41
+ this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`)
42
+ }
43
+
44
+ await this.postPollHandler()
45
+ }
46
+ }
47
+
48
+ prePollHandler(): boolean {
49
+ if(config.isMaintenanceMode()){
50
+ this.logger.warn('Maintenance mode is enabled. Skipping task.')
51
+ return false
52
+ }
53
+
54
+ if (this.exceptLeadNode) {
55
+ return !config.isLeadNode();
56
+ }
57
+
58
+ if (this.leadNodeOnly) {
59
+ return config.isLeadNode()
60
+ }
61
+
62
+ return true
63
+ }
64
+
65
+ async pollHandler() {
66
+ this.logger.warn('pollHandler not implemented')
67
+ }
68
+
69
+ async postPollHandler() {
70
+ await wait(this.pollIntervalMs)
71
+ }
72
+
73
+ async start() {
74
+ this.started = true
75
+ try {
76
+ await this.pollCheck()
77
+ } catch (err) {
78
+ this.logger.error(`base task error: ${err.message}\ntrace: ${err.stack}`)
79
+ }
80
+ }
81
+
82
+ async stop() {
83
+ this.started = false
84
+ }
85
+ }