@instadapp/interop-x 0.0.0-dev.eb2b141 → 0.0.0-dev.edef11e

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. package/dist/package.json +4 -3
  2. package/dist/src/abi/interopBridgeToken.json +21 -9
  3. package/dist/src/abi/interopXGateway.json +11 -11
  4. package/dist/src/api/index.js +3 -3
  5. package/dist/src/config/index.js +11 -1
  6. package/dist/src/constants/addresses.js +1 -1
  7. package/dist/src/constants/itokens.js +1 -1
  8. package/dist/src/index.js +39 -6
  9. package/dist/src/net/peer/index.js +2 -1
  10. package/dist/src/net/pool/index.js +7 -2
  11. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +28 -0
  12. package/dist/src/net/protocol/index.js +11 -0
  13. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  14. package/dist/src/tasks/BaseTask.js +11 -3
  15. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +0 -1
  16. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +7 -6
  17. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +0 -1
  18. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +2 -3
  19. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
  20. package/dist/src/tasks/index.js +4 -0
  21. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +23 -11
  22. package/dist/src/typechain/factories/InteropXGateway__factory.js +14 -14
  23. package/dist/src/utils/index.js +20 -10
  24. package/package.json +4 -3
  25. package/src/abi/interopBridgeToken.json +21 -9
  26. package/src/abi/interopXGateway.json +11 -11
  27. package/src/api/index.ts +2 -2
  28. package/src/config/index.ts +11 -1
  29. package/src/constants/addresses.ts +1 -1
  30. package/src/constants/itokens.ts +1 -1
  31. package/src/index.ts +53 -8
  32. package/src/net/peer/index.ts +2 -1
  33. package/src/net/pool/index.ts +7 -3
  34. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +31 -0
  35. package/src/net/protocol/index.ts +12 -0
  36. package/src/tasks/AutoUpdateTask.ts +82 -0
  37. package/src/tasks/BaseTask.ts +13 -3
  38. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +0 -2
  39. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +7 -7
  40. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +0 -2
  41. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +2 -4
  42. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
  43. package/src/tasks/index.ts +5 -0
  44. package/src/typechain/InteropBridgeToken.ts +23 -17
  45. package/src/typechain/InteropXGateway.ts +13 -13
  46. package/src/typechain/factories/InteropBridgeToken__factory.ts +23 -11
  47. package/src/typechain/factories/InteropXGateway__factory.ts +14 -14
  48. package/src/utils/index.ts +21 -9
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.eb2b141",
3
+ "version": "0.0.0-dev.edef11e",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -24,9 +24,10 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@achingbrain/libp2p-gossipsub": "^0.12.2",
27
+ "@fastify/cors": "^7.0.0",
28
+ "await-spawn": "^4.0.2",
27
29
  "axios": "^0.27.1",
28
30
  "axios-retry": "^3.2.4",
29
- "bignumber.js": "^9.0.2",
30
31
  "chalk": "4.1.2",
31
32
  "dotenv": "^16.0.0",
32
33
  "ethereumjs-util": "^7.1.4",
@@ -34,7 +35,7 @@
34
35
  "ethers-multisend": "^2.1.1",
35
36
  "expand-home-dir": "^0.0.3",
36
37
  "fastify": "^3.28.0",
37
- "fastify-cors": "^6.0.3",
38
+ "fs-extra": "^10.1.0",
38
39
  "libp2p": "^0.36.2",
39
40
  "libp2p-bootstrap": "^0.14.0",
40
41
  "libp2p-kad-dht": "^0.28.6",
@@ -46,11 +46,17 @@
46
46
  "name": "amount",
47
47
  "type": "uint256"
48
48
  },
49
+ {
50
+ "indexed": false,
51
+ "internalType": "uint32",
52
+ "name": "sourceChainId",
53
+ "type": "uint32"
54
+ },
49
55
  {
50
56
  "indexed": true,
51
- "internalType": "uint256",
52
- "name": "chainId",
53
- "type": "uint256"
57
+ "internalType": "uint32",
58
+ "name": "targetChainId",
59
+ "type": "uint32"
54
60
  }
55
61
  ],
56
62
  "name": "Burn",
@@ -73,14 +79,20 @@
73
79
  },
74
80
  {
75
81
  "indexed": true,
76
- "internalType": "uint256",
77
- "name": "chainId",
78
- "type": "uint256"
82
+ "internalType": "uint32",
83
+ "name": "sourceChainId",
84
+ "type": "uint32"
85
+ },
86
+ {
87
+ "indexed": false,
88
+ "internalType": "uint32",
89
+ "name": "targetChainId",
90
+ "type": "uint32"
79
91
  },
80
92
  {
81
93
  "indexed": true,
82
94
  "internalType": "bytes32",
83
- "name": "transactionHash",
95
+ "name": "submitTransactionHash",
84
96
  "type": "bytes32"
85
97
  }
86
98
  ],
@@ -164,7 +176,7 @@
164
176
  "inputs": [
165
177
  { "internalType": "address", "name": "to", "type": "address" },
166
178
  { "internalType": "uint256", "name": "amount", "type": "uint256" },
167
- { "internalType": "uint256", "name": "chainId", "type": "uint256" }
179
+ { "internalType": "uint32", "name": "chainId", "type": "uint32" }
168
180
  ],
169
181
  "name": "burn",
170
182
  "outputs": [],
@@ -206,7 +218,7 @@
206
218
  "inputs": [
207
219
  { "internalType": "address", "name": "to", "type": "address" },
208
220
  { "internalType": "uint256", "name": "amount", "type": "uint256" },
209
- { "internalType": "uint256", "name": "chainId", "type": "uint256" },
221
+ { "internalType": "uint32", "name": "chainId", "type": "uint32" },
210
222
  {
211
223
  "internalType": "bytes32",
212
224
  "name": "transactionHash",
@@ -35,15 +35,15 @@
35
35
  },
36
36
  {
37
37
  "indexed": false,
38
- "internalType": "uint256",
38
+ "internalType": "uint32",
39
39
  "name": "sourceChainId",
40
- "type": "uint256"
40
+ "type": "uint32"
41
41
  },
42
42
  {
43
43
  "indexed": true,
44
- "internalType": "uint256",
44
+ "internalType": "uint32",
45
45
  "name": "targetChainId",
46
- "type": "uint256"
46
+ "type": "uint32"
47
47
  }
48
48
  ],
49
49
  "name": "LogGatewayDeposit",
@@ -72,15 +72,15 @@
72
72
  },
73
73
  {
74
74
  "indexed": true,
75
- "internalType": "uint256",
75
+ "internalType": "uint32",
76
76
  "name": "sourceChainId",
77
- "type": "uint256"
77
+ "type": "uint32"
78
78
  },
79
79
  {
80
80
  "indexed": false,
81
- "internalType": "uint256",
81
+ "internalType": "uint32",
82
82
  "name": "targetChainId",
83
- "type": "uint256"
83
+ "type": "uint32"
84
84
  },
85
85
  {
86
86
  "indexed": true,
@@ -122,7 +122,7 @@
122
122
  "inputs": [
123
123
  { "internalType": "address", "name": "token_", "type": "address" },
124
124
  { "internalType": "uint256", "name": "amount_", "type": "uint256" },
125
- { "internalType": "uint256", "name": "chainId_", "type": "uint256" }
125
+ { "internalType": "uint32", "name": "chainId_", "type": "uint32" }
126
126
  ],
127
127
  "name": "deposit",
128
128
  "outputs": [],
@@ -134,7 +134,7 @@
134
134
  { "internalType": "address", "name": "to_", "type": "address" },
135
135
  { "internalType": "address", "name": "token_", "type": "address" },
136
136
  { "internalType": "uint256", "name": "amount_", "type": "uint256" },
137
- { "internalType": "uint256", "name": "chainId_", "type": "uint256" }
137
+ { "internalType": "uint32", "name": "chainId_", "type": "uint32" }
138
138
  ],
139
139
  "name": "depositFor",
140
140
  "outputs": [],
@@ -160,7 +160,7 @@
160
160
  { "internalType": "uint256", "name": "amount_", "type": "uint256" },
161
161
  { "internalType": "address", "name": "user_", "type": "address" },
162
162
  { "internalType": "address", "name": "token_", "type": "address" },
163
- { "internalType": "uint256", "name": "chainId_", "type": "uint256" },
163
+ { "internalType": "uint32", "name": "chainId_", "type": "uint32" },
164
164
  {
165
165
  "internalType": "bytes32",
166
166
  "name": "transactionHash_",
@@ -5,12 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.startApiServer = void 0;
7
7
  const fastify_1 = __importDefault(require("fastify"));
8
- const fastify_cors_1 = __importDefault(require("fastify-cors"));
8
+ const cors_1 = __importDefault(require("@fastify/cors"));
9
9
  const logger_1 = __importDefault(require("@/logger"));
10
10
  const db_1 = require("@/db");
11
11
  const logger = new logger_1.default("RPC");
12
12
  const server = (0, fastify_1.default)({ logger: false });
13
- server.register(fastify_cors_1.default, {});
13
+ server.register(cors_1.default, {});
14
14
  server.get('/', async () => 'Interop X API');
15
15
  const startApiServer = async () => {
16
16
  const HOST = process.env.API_HOST || '0.0.0.0';
@@ -26,7 +26,7 @@ const startApiServer = async () => {
26
26
  logger.log(`RPC Server listening at http://${HOST}:${PORT}`);
27
27
  }
28
28
  catch (err) {
29
- logger.error(err);
29
+ logger.error(err.message);
30
30
  process.exit(1);
31
31
  }
32
32
  };
@@ -1,15 +1,22 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  const ethers_1 = require("ethers");
4
7
  const types_1 = require("@/types");
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const expand_home_dir_1 = __importDefault(require("expand-home-dir"));
5
10
  class Config {
6
11
  constructor() {
7
12
  this.events = new types_1.EventBus();
8
- this.maxPeers = 10;
13
+ this.maxPeers = 20;
9
14
  this.privateKey = process.env.PRIVATE_KEY;
10
15
  this.staging = !!process.env.STAGING && process.env.STAGING === 'true';
16
+ this.autoUpdate = !!process.env.AUTO_UPDATE && process.env.AUTO_UPDATE === 'true';
11
17
  this.wallet = new ethers_1.Wallet(this.privateKey);
12
18
  this.leadNodeAddress = '0x910E413DBF3F6276Fe8213fF656726bDc142E08E';
19
+ this.baseConfigPath = (0, expand_home_dir_1.default)(`~/.interop-x`);
13
20
  }
14
21
  get publicAddress() {
15
22
  return this.wallet.address;
@@ -17,5 +24,8 @@ class Config {
17
24
  isLeadNode() {
18
25
  return ethers_1.ethers.utils.getAddress(this.leadNodeAddress) === ethers_1.ethers.utils.getAddress(this.wallet.address);
19
26
  }
27
+ isMaintenanceMode() {
28
+ return fs_extra_1.default.existsSync(this.baseConfigPath + '/maintenance');
29
+ }
20
30
  }
21
31
  exports.default = new Config();
@@ -15,6 +15,6 @@ exports.addresses = {
15
15
  43114: {
16
16
  gnosisSafe: '0x31d7a5194Fe60AC209Cf1Ce2d539C9A60662Ed6b',
17
17
  multisend: '0x998739BFdAAdde7C933B942a68053933098f9EDa',
18
- interopXGateway: '0x8D27758751BA488690974B6Ccfcda771D462945f',
18
+ interopXGateway: '0xF0317C5Bc206F2291dd2f3eE9C4cDB5Bbb25418d',
19
19
  }
20
20
  };
@@ -5,7 +5,7 @@ exports.itokens = {
5
5
  1: [],
6
6
  137: [
7
7
  {
8
- address: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
8
+ address: '0x62C0045f3277E7067cAcad3c8038eEaBB1Bd92D1',
9
9
  symbol: 'USDC',
10
10
  }
11
11
  ],
package/dist/src/index.js CHANGED
@@ -4,6 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const module_alias_1 = __importDefault(require("module-alias"));
7
+ const expand_home_dir_1 = __importDefault(require("expand-home-dir"));
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
7
9
  module_alias_1.default.addAliases({
8
10
  "@/": __dirname + "/",
9
11
  "@/logger": __dirname + "/logger",
@@ -26,18 +28,45 @@ const package_json_1 = __importDefault(require("../package.json"));
26
28
  dotenv_1.default.config();
27
29
  const logger_1 = __importDefault(require("@/logger"));
28
30
  const logger = new logger_1.default('Process');
31
+ const GIT_SHORT_HASH = 'edef11e';
29
32
  const printUsage = () => {
33
+ console.log();
34
+ console.log(`Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
35
+ console.log();
30
36
  console.log('Usage:');
31
- console.log(' PRIVATE_KEY=abcd1234 interop-x');
32
- console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x');
33
- console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x');
37
+ console.log(' interop-x help Show this message');
38
+ console.log(' interop-x version Print out the installed version of Interop X');
39
+ console.log();
40
+ console.log(' interop-x down Put the node into maintenance mode');
41
+ console.log(' interop-x up Take the node out of maintenance mode');
42
+ console.log();
43
+ console.log(' PRIVATE_KEY=abcd1234 interop-x Start the node with the given private key');
44
+ console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x Start the node in staging mode');
45
+ console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x Start the node in auto update mode');
46
+ console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x Start the node with custom API host and port');
47
+ console.log();
34
48
  };
35
49
  if (process.argv.at(-1) === 'help') {
36
50
  printUsage();
37
51
  process.exit(0);
38
52
  }
53
+ const basePath = (0, expand_home_dir_1.default)(`~/.interop-x`);
54
+ if (process.argv.at(-1) === 'down') {
55
+ fs_extra_1.default.outputFileSync(basePath + '/maintenance', Date.now().toString());
56
+ console.log(chalk_1.default.red('Maintenance mode enabled'));
57
+ process.exit(0);
58
+ }
59
+ if (process.argv.at(-1) === 'up') {
60
+ fs_extra_1.default.removeSync(basePath + '/maintenance');
61
+ console.log(chalk_1.default.green('Maintenance mode disabled'));
62
+ process.exit(0);
63
+ }
64
+ if (process.argv.at(-1) === 'version') {
65
+ console.log(`Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
66
+ process.exit(0);
67
+ }
39
68
  if (!process.env.PRIVATE_KEY) {
40
- console.error(chalk_1.default.bgRed.white('Please provide a private key\n'));
69
+ console.error(chalk_1.default.bgRed.white.bold('Please provide a private key\n'));
41
70
  printUsage();
42
71
  process.exit(1);
43
72
  }
@@ -49,11 +78,12 @@ catch (e) {
49
78
  printUsage();
50
79
  process.exit(1);
51
80
  }
52
- logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.eb2b141)`);
81
+ logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
53
82
  const tasks_1 = require("@/tasks");
54
83
  const net_1 = require("@/net");
55
84
  const api_1 = require("@/api");
56
85
  const db_1 = require("./db");
86
+ const utils_1 = require("./utils");
57
87
  async function main() {
58
88
  (0, net_1.startPeer)({});
59
89
  const tasks = new tasks_1.Tasks();
@@ -62,7 +92,10 @@ async function main() {
62
92
  net_1.protocol.on('TransactionStatus', async (payload) => {
63
93
  if (!net_1.peerPool.isLeadNode(payload.peerId)) {
64
94
  const peer = net_1.peerPool.getPeer(payload.peerId);
65
- logger.info(`ignored transaction status from ${payload.peerId} ${peer === null || peer === void 0 ? void 0 : peer.publicAddress} `);
95
+ if (!peer) {
96
+ return;
97
+ }
98
+ logger.info(`ignored transaction status from ${payload.peerId} ${(0, utils_1.shortenHash)(peer.publicAddress)} `);
66
99
  return;
67
100
  }
68
101
  const transaction = await db_1.Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } });
@@ -23,6 +23,7 @@ const libp2p_kad_dht_1 = __importDefault(require("libp2p-kad-dht"));
23
23
  const libp2p_pubsub_peer_discovery_1 = __importDefault(require("libp2p-pubsub-peer-discovery"));
24
24
  const net_1 = require("@/net");
25
25
  const config_1 = __importDefault(require("@/config"));
26
+ const chalk_1 = __importDefault(require("chalk"));
26
27
  const logger = new logger_1.default("Peer");
27
28
  let node;
28
29
  // Known peers addresses
@@ -77,7 +78,7 @@ const startPeer = async ({}) => {
77
78
  persistence: true,
78
79
  },
79
80
  });
80
- logger.info("Peer ID:", node.peerId.toB58String());
81
+ logger.info("Peer ID:", chalk_1.default.bold(node.peerId.toB58String()));
81
82
  await node.start();
82
83
  net_1.protocol.start({
83
84
  libp2p: node
@@ -8,6 +8,8 @@ const types_1 = require("@/types");
8
8
  const config_1 = __importDefault(require("@/config"));
9
9
  const logger_1 = __importDefault(require("@/logger"));
10
10
  const utils_1 = require("ethers/lib/utils");
11
+ const utils_2 = require("@/utils");
12
+ const chalk_1 = __importDefault(require("chalk"));
11
13
  const logger = new logger_1.default('PeerPool');
12
14
  class PeerPool {
13
15
  constructor() {
@@ -71,7 +73,7 @@ class PeerPool {
71
73
  peer.pooled = true;
72
74
  if (newPeer) {
73
75
  config_1.default.events.emit(types_1.Event.POOL_PEER_ADDED, peer);
74
- logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`);
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`);
75
77
  }
76
78
  }
77
79
  }
@@ -85,7 +87,7 @@ class PeerPool {
85
87
  if (this.pool.delete(peer.id)) {
86
88
  peer.pooled = false;
87
89
  config_1.default.events.emit(types_1.Event.POOL_PEER_REMOVED, peer);
88
- logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`);
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`);
89
91
  }
90
92
  }
91
93
  }
@@ -111,6 +113,9 @@ class PeerPool {
111
113
  }
112
114
  return (0, utils_1.getAddress)(peer.publicAddress) === (0, utils_1.getAddress)(config_1.default.leadNodeAddress);
113
115
  }
116
+ getLeadPeer() {
117
+ return this.peers.find((p) => this.isLeadNode(p.id));
118
+ }
114
119
  cleanup() {
115
120
  // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
116
121
  // this.peers.forEach((peerInfo) => {
@@ -0,0 +1,28 @@
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
+ targetStatus: transaction.targetStatus,
22
+ targetTransactionHash: transaction.targetTransactionHash,
23
+ targetErrors: transaction.targetErrors,
24
+ status: transaction.status,
25
+ };
26
+ }
27
+ }
28
+ exports.TransactionStatusDialProtocol = TransactionStatusDialProtocol;
@@ -10,6 +10,7 @@ const SignatureDialProtocol_1 = require("./dial/SignatureDialProtocol");
10
10
  const __1 = require("..");
11
11
  const config_1 = __importDefault(require("@/config"));
12
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);
@@ -64,6 +65,7 @@ class Protocol extends stream_1.EventEmitter {
64
65
  });
65
66
  });
66
67
  this.signature = new SignatureDialProtocol_1.SignatureDialProtocol(this.libp2p);
68
+ this.transactionStatus = new TransactionStatusDialProtocol_1.TransactionStatusDialProtocol(this.libp2p);
67
69
  }
68
70
  init() {
69
71
  this.libp2p.pubsub.subscribe(this.topic);
@@ -117,5 +119,14 @@ class Protocol extends stream_1.EventEmitter {
117
119
  return [];
118
120
  }
119
121
  }
122
+ async requestTransactionStatus(transactionHash, peerId) {
123
+ try {
124
+ return await this.transactionStatus.send(transactionHash, peerId);
125
+ }
126
+ catch (error) {
127
+ console.log(error);
128
+ return null;
129
+ }
130
+ }
120
131
  }
121
132
  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;
@@ -14,6 +14,7 @@ class BaseTask extends events_1.default {
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() {
@@ -34,10 +35,17 @@ class BaseTask extends events_1.default {
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');
@@ -139,7 +139,6 @@ class ProcessWithdrawEvents extends BaseTask_1.BaseTask {
139
139
  }
140
140
  }
141
141
  async start() {
142
- this.logger.info(`Starting execution watcher on interop chain`);
143
142
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
144
143
  await super.start();
145
144
  }
@@ -27,12 +27,12 @@ class SyncWithdrawEvents extends BaseTask_1.BaseTask {
27
27
  if (!event.args) {
28
28
  continue;
29
29
  }
30
- const { to, amount, chainId } = event.args;
30
+ const { to, amount, sourceChainId, targetChainId } = event.args;
31
31
  const uniqueIdentifier = {
32
32
  action: 'withdraw',
33
33
  submitTransactionHash: event.transactionHash,
34
- sourceChainId: this.chainId,
35
- targetChainId: chainId.toNumber(),
34
+ sourceChainId: sourceChainId,
35
+ targetChainId: targetChainId,
36
36
  };
37
37
  if (await db_1.Transaction.findOne({ where: uniqueIdentifier })) {
38
38
  continue;
@@ -44,12 +44,14 @@ class SyncWithdrawEvents extends BaseTask_1.BaseTask {
44
44
  to,
45
45
  amount: amount.toString(),
46
46
  itoken: this.itokenAddress,
47
- chainId: chainId.toString()
47
+ sourceChainId: sourceChainId,
48
+ targetChainId: targetChainId,
48
49
  }, sourceEvent: {
49
50
  to,
50
51
  amount: amount.toString(),
51
52
  itoken: this.itokenAddress,
52
- chainId: chainId.toString(),
53
+ sourceChainId: sourceChainId,
54
+ targetChainId: targetChainId,
53
55
  }, status: "pending" }));
54
56
  this.logger.info(`Withdraw queued: ${event.transactionHash} ${event.blockNumber}`);
55
57
  }
@@ -61,7 +63,6 @@ class SyncWithdrawEvents extends BaseTask_1.BaseTask {
61
63
  this.logger.info(`${processedEvents} events processed`);
62
64
  }
63
65
  async start() {
64
- this.logger.info(`Starting execution watcher on interop chain`);
65
66
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
66
67
  this.contract = (0, utils_1.getContract)(this.itokenAddress, abi_1.default.interopBridgeToken, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
67
68
  await super.start();
@@ -140,7 +140,6 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
140
140
  net_1.protocol.sendTransaction(transaction);
141
141
  }
142
142
  async start() {
143
- this.logger.info(`Starting execution watcher on interop chain`);
144
143
  this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
145
144
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
146
145
  this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
@@ -31,8 +31,8 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
31
31
  const uniqueIdentifier = {
32
32
  action: 'deposit',
33
33
  submitTransactionHash: event.transactionHash,
34
- sourceChainId: sourceChainId.toNumber(),
35
- targetChainId: targetChainId.toNumber(),
34
+ sourceChainId: sourceChainId,
35
+ targetChainId: targetChainId,
36
36
  };
37
37
  if (await db_1.Transaction.findOne({ where: uniqueIdentifier })) {
38
38
  continue;
@@ -65,7 +65,6 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
65
65
  this.logger.info(`${processedEvents} events processed`);
66
66
  }
67
67
  async start() {
68
- this.logger.info(`Starting execution watcher on interop chain`);
69
68
  this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
70
69
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
71
70
  this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));