@instadapp/interop-x 0.0.0-dev.adea608 → 0.0.0-dev.b031098

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. package/dist/package.json +6 -5
  2. package/dist/src/api/index.js +3 -3
  3. package/dist/src/config/index.js +11 -1
  4. package/dist/src/index.js +40 -6
  5. package/dist/src/net/peer/index.js +2 -1
  6. package/dist/src/net/pool/index.js +10 -2
  7. package/dist/src/net/protocol/dial/SignatureDialProtocol.1.js +28 -0
  8. package/dist/src/net/protocol/index.js +15 -4
  9. package/dist/src/tasks/AutoUpdateTask.js +69 -0
  10. package/dist/src/tasks/BaseTask.js +11 -3
  11. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +0 -1
  12. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +0 -1
  13. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +0 -1
  14. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +0 -1
  15. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
  16. package/dist/src/tasks/index.js +4 -0
  17. package/dist/src/utils/index.js +19 -4
  18. package/package.json +6 -5
  19. package/src/api/index.ts +2 -2
  20. package/src/config/index.ts +11 -1
  21. package/src/index.ts +48 -8
  22. package/src/net/peer/index.ts +2 -1
  23. package/src/net/pool/index.ts +10 -2
  24. package/src/net/protocol/dial/SignatureDialProtocol.1.ts +31 -0
  25. package/src/net/protocol/index.ts +18 -6
  26. package/src/tasks/AutoUpdateTask.ts +80 -0
  27. package/src/tasks/BaseTask.ts +13 -3
  28. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +0 -2
  29. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +0 -2
  30. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +0 -2
  31. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +0 -2
  32. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
  33. package/src/tasks/index.ts +5 -0
  34. package/src/utils/index.ts +21 -3
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.adea608",
3
+ "version": "0.0.0-dev.b031098",
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,6 +47,8 @@
46
47
  "libp2p-websockets": "^0.16.2",
47
48
  "luxon": "^2.3.2",
48
49
  "module-alias": "^2.2.2",
50
+ "patch-package": "^6.4.7",
51
+ "postinstall-postinstall": "^2.1.0",
49
52
  "sequelize": "6.18.0",
50
53
  "sqlite3": "^5.0.5",
51
54
  "waait": "^1.0.5"
@@ -60,8 +63,6 @@
60
63
  "@types/fs-extra": "^9.0.13",
61
64
  "@types/node": "^17.0.17",
62
65
  "nodemon": "^2.0.15",
63
- "patch-package": "^6.4.7",
64
- "postinstall-postinstall": "^2.1.0",
65
66
  "replace-in-file": "^6.3.2",
66
67
  "rimraf": "^3.0.2",
67
68
  "ts-node": "^10.5.0",
@@ -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();
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",
@@ -19,39 +21,71 @@ module_alias_1.default.addAliases({
19
21
  "@/typechain": __dirname + "/typechain"
20
22
  });
21
23
  (0, module_alias_1.default)();
22
- const assert_1 = __importDefault(require("assert"));
23
24
  const dotenv_1 = __importDefault(require("dotenv"));
25
+ const chalk_1 = __importDefault(require("chalk"));
24
26
  const ethers_1 = require("ethers");
25
27
  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');
29
- if (process.argv.at(-1) === 'help') {
31
+ const printUsage = () => {
30
32
  console.log('Usage:');
31
33
  console.log(' PRIVATE_KEY=abcd1234 interop-x');
32
34
  console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x');
35
+ console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x');
36
+ console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x');
37
+ };
38
+ if (process.argv.at(-1) === 'help') {
39
+ printUsage();
33
40
  process.exit(0);
34
41
  }
35
- (0, assert_1.default)(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
42
+ const basePath = (0, expand_home_dir_1.default)(`~/.interop-x`);
43
+ if (process.argv.at(-1) === 'down') {
44
+ fs_extra_1.default.outputFileSync(basePath + '/maintenance', Date.now().toString());
45
+ console.log(chalk_1.default.red('Maintenance mode enabled'));
46
+ process.exit(0);
47
+ }
48
+ if (process.argv.at(-1) === 'up') {
49
+ fs_extra_1.default.removeSync(basePath + '/maintenance');
50
+ console.log(chalk_1.default.green('Maintenance mode disabled'));
51
+ process.exit(0);
52
+ }
53
+ const GIT_SHORT_HASH = 'b031098';
54
+ if (process.argv.at(-1) === 'version') {
55
+ console.log(`Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
56
+ process.exit(0);
57
+ }
58
+ if (!process.env.PRIVATE_KEY) {
59
+ console.error(chalk_1.default.bgRed.white.bold('Please provide a private key\n'));
60
+ printUsage();
61
+ process.exit(1);
62
+ }
36
63
  try {
37
64
  new ethers_1.ethers.Wallet(process.env.PRIVATE_KEY);
38
65
  }
39
66
  catch (e) {
40
- logger.error('Invalid private key');
67
+ console.error(chalk_1.default.bgRed.white('Invalid private key\n'));
68
+ printUsage();
41
69
  process.exit(1);
42
70
  }
43
- logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.adea608)`);
71
+ logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
44
72
  const tasks_1 = require("@/tasks");
45
73
  const net_1 = require("@/net");
46
74
  const api_1 = require("@/api");
47
75
  const db_1 = require("./db");
76
+ const utils_1 = require("./utils");
48
77
  async function main() {
49
78
  (0, net_1.startPeer)({});
50
79
  const tasks = new tasks_1.Tasks();
51
80
  tasks.start();
52
81
  (0, api_1.startApiServer)();
53
- net_1.protocol.on('Transaction', async (payload) => {
82
+ net_1.protocol.on('TransactionStatus', async (payload) => {
54
83
  if (!net_1.peerPool.isLeadNode(payload.peerId)) {
84
+ const peer = net_1.peerPool.getPeer(payload.peerId);
85
+ if (!peer) {
86
+ return;
87
+ }
88
+ logger.info(`ignored transaction status from ${payload.peerId} ${(0, utils_1.shortenHash)(peer.publicAddress)} `);
55
89
  return;
56
90
  }
57
91
  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
  }
@@ -101,6 +103,9 @@ class PeerPool {
101
103
  get activePeerIds() {
102
104
  return this.activePeers.map((p) => p.id);
103
105
  }
106
+ getPeer(id) {
107
+ return this.pool.get(id);
108
+ }
104
109
  isLeadNode(id) {
105
110
  const peer = this.pool.get(id);
106
111
  if (!peer) {
@@ -108,6 +113,9 @@ class PeerPool {
108
113
  }
109
114
  return (0, utils_1.getAddress)(peer.publicAddress) === (0, utils_1.getAddress)(config_1.default.leadNodeAddress);
110
115
  }
116
+ getLeadPeer() {
117
+ return this.peers.find((p) => this.isLeadNode(p.id));
118
+ }
111
119
  cleanup() {
112
120
  // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
113
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 SignatureDialProtocol_1_1 = require("./dial/SignatureDialProtocol.1");
13
14
  class Protocol extends stream_1.EventEmitter {
14
15
  constructor() {
15
16
  super(...arguments);
@@ -25,16 +26,16 @@ class Protocol extends stream_1.EventEmitter {
25
26
  }),
26
27
  },
27
28
  {
28
- name: 'Transaction',
29
+ name: 'TransactionStatus',
29
30
  code: 0x02,
30
31
  encode: (transaction) => [
31
32
  Buffer.from(transaction.transactionHash),
32
33
  Buffer.from(transaction.sourceStatus),
33
34
  Buffer.from(transaction.sourceTransactionHash),
34
- transaction.sourceErrors.map((e) => Buffer.from(e)),
35
+ transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
35
36
  Buffer.from(transaction.targetStatus),
36
37
  Buffer.from(transaction.targetTransactionHash),
37
- transaction.targetErrors.map((e) => Buffer.from(e)),
38
+ transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
38
39
  Buffer.from(transaction.status),
39
40
  ],
40
41
  decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]) => ({
@@ -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 SignatureDialProtocol_1_1.TransactionStatusDialProtocol(this.libp2p);
67
69
  }
68
70
  init() {
69
71
  this.libp2p.pubsub.subscribe(this.topic);
@@ -100,7 +102,7 @@ class Protocol extends stream_1.EventEmitter {
100
102
  this.libp2p.pubsub.publish(this.topic, encoded);
101
103
  }
102
104
  sendTransaction(transaction) {
103
- const message = this.protocolMessages.find((m) => m.name === 'Transaction');
105
+ const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus');
104
106
  const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(transaction)]);
105
107
  this.libp2p.pubsub.publish(this.topic, encoded);
106
108
  }
@@ -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,69 @@
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
+ const subprocess = (0, child_process_1.spawn)(process.argv[0], process.argv.slice(1), {
61
+ cwd: process.cwd(),
62
+ stdio: "inherit",
63
+ detached: true,
64
+ });
65
+ subprocess.unref();
66
+ process.exit();
67
+ }
68
+ }
69
+ 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
  }
@@ -61,7 +61,6 @@ class SyncWithdrawEvents extends BaseTask_1.BaseTask {
61
61
  this.logger.info(`${processedEvents} events processed`);
62
62
  }
63
63
  async start() {
64
- this.logger.info(`Starting execution watcher on interop chain`);
65
64
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
66
65
  this.contract = (0, utils_1.getContract)(this.itokenAddress, abi_1.default.interopBridgeToken, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
67
66
  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));
@@ -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));
@@ -0,0 +1,53 @@
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 net_1 = require("@/net");
9
+ const db_1 = require("@/db");
10
+ const sequelize_1 = require("sequelize");
11
+ class SyncTransactionStatusTask extends BaseTask_1.BaseTask {
12
+ constructor() {
13
+ super({
14
+ logger: new logger_1.default("SyncTransactionStatusTask"),
15
+ });
16
+ this.pollIntervalMs = 60 * 1000;
17
+ this.exceptLeadNode = true;
18
+ }
19
+ async pollHandler() {
20
+ // if transaction is pending for more than 1 hour, check lead node for status
21
+ const leadNode = net_1.peerPool.getLeadPeer();
22
+ if (!leadNode) {
23
+ return;
24
+ }
25
+ const transaction = await db_1.Transaction.findOne({
26
+ where: {
27
+ status: 'pending',
28
+ sourceCreatedAt: {
29
+ [sequelize_1.Op.gte]: new Date(Date.now() - 60 * 60 * 1000),
30
+ },
31
+ }
32
+ });
33
+ if (!transaction) {
34
+ return;
35
+ }
36
+ this.logger.info(`Requesting transaction status for ${transaction.transactionHash}`);
37
+ const transactionStatus = await net_1.protocol.requestTransactionStatus(transaction.transactionHash, leadNode.id);
38
+ if (!transactionStatus) {
39
+ return;
40
+ }
41
+ this.logger.info(`Received transaction status for ${transaction.transactionHash}`);
42
+ transaction.sourceStatus = transactionStatus.sourceStatus;
43
+ transaction.sourceTransactionHash = transactionStatus.sourceTransactionHash;
44
+ transaction.sourceErrors = transactionStatus.sourceErrors;
45
+ transaction.targetStatus = transactionStatus.targetStatus;
46
+ transaction.targetTransactionHash = transactionStatus.targetTransactionHash;
47
+ transaction.targetErrors = transactionStatus.targetErrors;
48
+ transaction.status = transactionStatus.status;
49
+ await transaction.save();
50
+ this.logger.info(`Updated transaction status for ${transaction.transactionHash}`);
51
+ }
52
+ }
53
+ exports.default = SyncTransactionStatusTask;
@@ -8,9 +8,13 @@ const ProcessDepositEvents_1 = __importDefault(require("./InteropXGateway/Proces
8
8
  const SyncDepositEvents_1 = __importDefault(require("./InteropXGateway/SyncDepositEvents"));
9
9
  const SyncWithdrawEvents_1 = __importDefault(require("./InteropBridge/SyncWithdrawEvents"));
10
10
  const ProcessWithdrawEvents_1 = __importDefault(require("./InteropBridge/ProcessWithdrawEvents"));
11
+ const AutoUpdateTask_1 = __importDefault(require("./AutoUpdateTask"));
12
+ const SyncTransactionStatusTask_1 = __importDefault(require("./Transactions/SyncTransactionStatusTask"));
11
13
  class Tasks {
12
14
  constructor() {
13
15
  this.tasks = [
16
+ new SyncTransactionStatusTask_1.default(),
17
+ new AutoUpdateTask_1.default(),
14
18
  new SyncDepositEvents_1.default({
15
19
  chainId: 43114
16
20
  }),
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getContract = exports.buildWithdrawDataForTransaction = exports.buildDepositDataForTransaction = exports.buildDataForTransaction = exports.generateInteropTransactionHash = exports.asyncCallWithTimeout = exports.buildSignatureBytes = exports.getRpcProviderUrl = exports.signGnosisSafeTx = exports.short = exports.http = void 0;
6
+ exports.getContract = exports.buildWithdrawDataForTransaction = exports.buildDepositDataForTransaction = exports.buildDataForTransaction = exports.generateInteropTransactionHash = exports.asyncCallWithTimeout = exports.buildSignatureBytes = exports.getRpcProviderUrl = exports.signGnosisSafeTx = exports.short = exports.shortenHash = exports.http = void 0;
7
7
  /**
8
8
  * @module util
9
9
  */
@@ -16,6 +16,16 @@ const config_1 = __importDefault(require("@/config"));
16
16
  const abi_1 = __importDefault(require("@/abi"));
17
17
  exports.http = axios_1.default.create();
18
18
  (0, axios_retry_1.default)(exports.http, { retries: 3, retryDelay: axios_retry_1.default.exponentialDelay });
19
+ function shortenHash(hash, length = 4) {
20
+ if (!hash)
21
+ return;
22
+ if (hash.length < 12)
23
+ return hash;
24
+ const beginningChars = hash.startsWith("0x") ? length + 2 : length;
25
+ const shortened = hash.substr(0, beginningChars) + "…" + hash.substr(-length);
26
+ return shortened;
27
+ }
28
+ exports.shortenHash = shortenHash;
19
29
  function short(buffer) {
20
30
  return buffer.toString('hex').slice(0, 8) + '...';
21
31
  }
@@ -59,11 +69,11 @@ exports.signGnosisSafeTx = signGnosisSafeTx;
59
69
  const getRpcProviderUrl = (chainId) => {
60
70
  switch (chainId) {
61
71
  case 1:
62
- return 'https://rpc.instadapp.io/mainnet';
72
+ return 'https://rpc.ankr.com/eth';
63
73
  case 137:
64
- return 'https://rpc.instadapp.io/polygon';
74
+ return 'https://rpc.ankr.com/polygon';
65
75
  case 43114:
66
- return 'https://rpc.instadapp.io/avalanche';
76
+ return 'https://rpc.ankr.com/avalanche';
67
77
  default:
68
78
  throw new Error(`Unknown chainId: ${chainId}`);
69
79
  }
@@ -185,6 +195,11 @@ function getContract(address, contractInterface, signerOrProvider) {
185
195
  throw Error(`Invalid 'address' parameter '${address}'.`);
186
196
  }
187
197
  const contract = new ethers_1.ethers.Contract(address, contractInterface, signerOrProvider);
198
+ // Make sure the contract properties is writable
199
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
200
+ if (!desc || desc.writable !== true) {
201
+ return contract;
202
+ }
188
203
  return new Proxy(contract, {
189
204
  get(target, prop, receiver) {
190
205
  const value = Reflect.get(target, prop, receiver);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.adea608",
3
+ "version": "0.0.0-dev.b031098",
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,6 +47,8 @@
46
47
  "libp2p-websockets": "^0.16.2",
47
48
  "luxon": "^2.3.2",
48
49
  "module-alias": "^2.2.2",
50
+ "patch-package": "^6.4.7",
51
+ "postinstall-postinstall": "^2.1.0",
49
52
  "sequelize": "6.18.0",
50
53
  "sqlite3": "^5.0.5",
51
54
  "waait": "^1.0.5"
@@ -60,8 +63,6 @@
60
63
  "@types/fs-extra": "^9.0.13",
61
64
  "@types/node": "^17.0.17",
62
65
  "nodemon": "^2.0.15",
63
- "patch-package": "^6.4.7",
64
- "postinstall-postinstall": "^2.1.0",
65
66
  "replace-in-file": "^6.3.2",
66
67
  "rimraf": "^3.0.2",
67
68
  "ts-node": "^10.5.0",
package/src/api/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import fastify from "fastify"
2
- import cors from 'fastify-cors'
2
+ import cors from '@fastify/cors'
3
3
  import Logger from "@/logger"
4
4
  import { Transaction } from "@/db";
5
5
 
@@ -27,7 +27,7 @@ export const startApiServer = async () => {
27
27
 
28
28
  logger.log(`RPC Server listening at http://${HOST}:${PORT}`)
29
29
  } catch (err) {
30
- logger.error(err)
30
+ logger.error(err.message)
31
31
  process.exit(1)
32
32
  }
33
33
  }
@@ -1,5 +1,7 @@
1
1
  import { ethers, Wallet } from "ethers"
2
2
  import { EventBus, EventBusType } from "@/types"
3
+ import fs from 'fs-extra'
4
+ import expandHomeDir from "expand-home-dir";
3
5
 
4
6
  class Config {
5
7
  public readonly events: EventBusType
@@ -8,14 +10,18 @@ class Config {
8
10
  public readonly privateKey: string
9
11
  public readonly wallet: Wallet
10
12
  public readonly staging: boolean
13
+ public readonly autoUpdate: boolean
14
+ public readonly baseConfigPath: string
11
15
 
12
16
  constructor() {
13
17
  this.events = new EventBus() as EventBusType
14
- this.maxPeers = 10
18
+ this.maxPeers = 20
15
19
  this.privateKey = process.env.PRIVATE_KEY as string;
16
20
  this.staging = !! process.env.STAGING && process.env.STAGING === 'true';
21
+ this.autoUpdate = !! process.env.AUTO_UPDATE && process.env.AUTO_UPDATE === 'true';
17
22
  this.wallet = new Wallet(this.privateKey);
18
23
  this.leadNodeAddress = '0x910E413DBF3F6276Fe8213fF656726bDc142E08E'
24
+ this.baseConfigPath = expandHomeDir(`~/.interop-x`);
19
25
  }
20
26
 
21
27
  get publicAddress(){
@@ -25,6 +31,10 @@ class Config {
25
31
  isLeadNode() {
26
32
  return ethers.utils.getAddress(this.leadNodeAddress) === ethers.utils.getAddress(this.wallet.address)
27
33
  }
34
+
35
+ isMaintenanceMode(){
36
+ return fs.existsSync(this.baseConfigPath + '/maintenance')
37
+ }
28
38
  }
29
39
 
30
40
  export default new Config()
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import moduleAlias from 'module-alias';
2
-
2
+ import expandHomeDir from "expand-home-dir";
3
+ import fs from 'fs-extra'
3
4
  moduleAlias.addAliases({
4
5
  "@/": __dirname + "/",
5
6
  "@/logger": __dirname + "/logger",
@@ -16,8 +17,8 @@ moduleAlias.addAliases({
16
17
  })
17
18
 
18
19
  moduleAlias();
19
- import assert from "assert";
20
20
  import dotenv from "dotenv";
21
+ import chalk from 'chalk';
21
22
  import { ethers } from "ethers";
22
23
  import packageJson from '../package.json'
23
24
  dotenv.config();
@@ -25,29 +26,61 @@ dotenv.config();
25
26
  import Logger from "@/logger";
26
27
  const logger = new Logger('Process')
27
28
 
28
-
29
- if (process.argv.at(-1) === 'help') {
29
+ const printUsage = () => {
30
30
  console.log('Usage:')
31
31
  console.log(' PRIVATE_KEY=abcd1234 interop-x')
32
32
  console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x')
33
+ console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x')
34
+ console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x')
35
+ }
36
+
37
+ if (process.argv.at(-1) === 'help') {
38
+ printUsage()
33
39
  process.exit(0)
34
40
  }
35
41
 
36
- assert(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
42
+ const basePath = expandHomeDir(`~/.interop-x`);
37
43
 
44
+ if (process.argv.at(-1) === 'down') {
45
+ fs.outputFileSync(basePath + '/maintenance', Date.now().toString())
46
+ console.log(chalk.red('Maintenance mode enabled'))
47
+ process.exit(0)
48
+ }
49
+
50
+ if (process.argv.at(-1) === 'up') {
51
+ fs.removeSync(basePath + '/maintenance')
52
+ console.log(chalk.green('Maintenance mode disabled'))
53
+ process.exit(0)
54
+ }
55
+
56
+
57
+ const GIT_SHORT_HASH = '@GIT_SHORT_HASH@';
58
+
59
+ if (process.argv.at(-1) === 'version') {
60
+ console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
61
+ process.exit(0)
62
+ }
63
+
64
+ if (!process.env.PRIVATE_KEY) {
65
+ console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
66
+ printUsage()
67
+ process.exit(1)
68
+ }
38
69
  try {
39
70
  new ethers.Wallet(process.env.PRIVATE_KEY!)
40
71
  } catch (e) {
41
- logger.error('Invalid private key')
72
+ console.error(chalk.bgRed.white('Invalid private key\n'))
73
+ printUsage()
42
74
  process.exit(1)
43
75
  }
44
76
 
45
- logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
77
+ logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
46
78
 
47
79
  import { Tasks } from "@/tasks";
48
80
  import { startPeer, protocol, peerPool } from "@/net";
49
81
  import { startApiServer } from '@/api';
50
82
  import { Transaction } from './db';
83
+ import { shortenHash } from './utils';
51
84
 
52
85
  async function main() {
53
86
 
@@ -59,8 +92,15 @@ async function main() {
59
92
 
60
93
  startApiServer()
61
94
 
62
- protocol.on('Transaction', async (payload) => {
95
+ protocol.on('TransactionStatus', async (payload) => {
63
96
  if (!peerPool.isLeadNode(payload.peerId)) {
97
+ const peer = peerPool.getPeer(payload.peerId)
98
+
99
+ if (!peer) {
100
+ return;
101
+ }
102
+
103
+ logger.info(`ignored transaction status from ${payload.peerId} ${shortenHash(peer.publicAddress)} `)
64
104
  return;
65
105
  }
66
106
 
@@ -17,6 +17,7 @@ import KadDHT from "libp2p-kad-dht";
17
17
  import PubsubPeerDiscovery from "libp2p-pubsub-peer-discovery";
18
18
  import { protocol } from "@/net";
19
19
  import config from "@/config";
20
+ import chalk from "chalk";
20
21
 
21
22
  const logger = new Logger("Peer");
22
23
 
@@ -80,7 +81,7 @@ export const startPeer = async ({ }: IPeerOptions) => {
80
81
  },
81
82
  });
82
83
 
83
- logger.info("Peer ID:", node.peerId.toB58String());
84
+ logger.info("Peer ID:", chalk.bold(node.peerId.toB58String()));
84
85
 
85
86
  await node.start();
86
87
 
@@ -2,6 +2,8 @@ import { Event } from "@/types";
2
2
  import config from "@/config";
3
3
  import Logger from "@/logger";
4
4
  import { getAddress } from "ethers/lib/utils";
5
+ import { shortenHash } from "@/utils";
6
+ import chalk from "chalk";
5
7
 
6
8
 
7
9
  const logger = new Logger('PeerPool')
@@ -87,7 +89,7 @@ export class PeerPool {
87
89
 
88
90
  if (newPeer) {
89
91
  config.events.emit(Event.POOL_PEER_ADDED, peer)
90
- logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`)
92
+ logger.info(`Peer ${chalk.bold(shortenHash(peer.id, 16))} with address ${chalk.bold(shortenHash(peer.publicAddress))} added to pool`)
91
93
  }
92
94
  }
93
95
  }
@@ -102,7 +104,7 @@ export class PeerPool {
102
104
  if (this.pool.delete(peer.id)) {
103
105
  peer.pooled = false
104
106
  config.events.emit(Event.POOL_PEER_REMOVED, peer)
105
- logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`)
107
+ logger.info(`Peer ${chalk.bold(shortenHash(peer.id, 16))} with address ${chalk.bold(shortenHash(peer.publicAddress))} removed from pool`)
106
108
  }
107
109
  }
108
110
  }
@@ -123,6 +125,9 @@ export class PeerPool {
123
125
  return this.activePeers.map((p) => p.id)
124
126
  }
125
127
 
128
+ getPeer(id: string){
129
+ return this.pool.get(id);
130
+ }
126
131
 
127
132
  isLeadNode(id: string) {
128
133
  const peer = this.pool.get(id);
@@ -134,6 +139,9 @@ export class PeerPool {
134
139
  return getAddress(peer.publicAddress) === getAddress(config.leadNodeAddress)
135
140
  }
136
141
 
142
+ getLeadPeer() {
143
+ return this.peers.find((p) => this.isLeadNode(p.id))
144
+ }
137
145
 
138
146
  cleanup() {
139
147
  // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
@@ -0,0 +1,31 @@
1
+ import { BaseDialProtocol } from "./BaseDialProtocol";
2
+ import { Transaction } from "@/db";
3
+
4
+ export class TransactionStatusDialProtocol extends BaseDialProtocol<string, Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | '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
+
24
+ targetStatus: transaction.targetStatus,
25
+ targetTransactionHash: transaction.targetTransactionHash,
26
+ targetErrors: transaction.targetErrors,
27
+
28
+ status: transaction.status,
29
+ }
30
+ }
31
+ }
@@ -6,6 +6,7 @@ import { IPeerInfo, peerPool } from "..";
6
6
  import config from "@/config";
7
7
  import { Event } from "@/types";
8
8
  import { Transaction } from "@/db";
9
+ import { TransactionStatusDialProtocol } from "./dial/SignatureDialProtocol.1";
9
10
 
10
11
  export interface ProtocolOptions {
11
12
  /* Handshake timeout in ms (default: 8000) */
@@ -34,12 +35,12 @@ interface PeerInfoEvent extends BaseMessageEvent {
34
35
  data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
35
36
  }
36
37
 
37
- interface TransactionEvent extends BaseMessageEvent {
38
+ interface TransactionStatusEvent extends BaseMessageEvent {
38
39
  data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
39
40
  }
40
41
 
41
42
  declare interface Protocol {
42
- on(event: 'Transaction', listener: (payload: TransactionEvent) => void): this;
43
+ on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
43
44
  on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
44
45
  on(event: string, listener: (payload: BaseMessageEvent) => void): this;
45
46
  }
@@ -59,18 +60,18 @@ class Protocol extends EventEmitter {
59
60
  }),
60
61
  },
61
62
  {
62
- name: 'Transaction',
63
+ name: 'TransactionStatus',
63
64
  code: 0x02,
64
65
  encode: (transaction: Transaction) => [
65
66
  Buffer.from(transaction.transactionHash),
66
67
 
67
68
  Buffer.from(transaction.sourceStatus),
68
69
  Buffer.from(transaction.sourceTransactionHash),
69
- transaction.sourceErrors.map((e) => Buffer.from(e)),
70
+ transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
70
71
 
71
72
  Buffer.from(transaction.targetStatus),
72
73
  Buffer.from(transaction.targetTransactionHash),
73
- transaction.targetErrors.map((e) => Buffer.from(e)),
74
+ transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
74
75
 
75
76
  Buffer.from(transaction.status),
76
77
  ],
@@ -90,6 +91,7 @@ class Protocol extends EventEmitter {
90
91
  },
91
92
  ];
92
93
  private signature: SignatureDialProtocol;
94
+ private transactionStatus: TransactionStatusDialProtocol;
93
95
 
94
96
 
95
97
  start({ libp2p, topic = null, }) {
@@ -109,6 +111,7 @@ class Protocol extends EventEmitter {
109
111
  })
110
112
 
111
113
  this.signature = new SignatureDialProtocol(this.libp2p);
114
+ this.transactionStatus = new TransactionStatusDialProtocol(this.libp2p);
112
115
  }
113
116
 
114
117
 
@@ -158,7 +161,7 @@ class Protocol extends EventEmitter {
158
161
  }
159
162
 
160
163
  public sendTransaction(transaction: Transaction) {
161
- const message = this.protocolMessages.find((m) => m.name === 'Transaction')!
164
+ const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
162
165
 
163
166
  const encoded = rlp.encode([message.code, message.encode(transaction)]);
164
167
 
@@ -177,6 +180,15 @@ class Protocol extends EventEmitter {
177
180
  return []
178
181
  }
179
182
  }
183
+
184
+ async requestTransactionStatus(transactionHash: string, peerId: string) {
185
+ try {
186
+ return await this.transactionStatus.send(transactionHash, peerId);
187
+ } catch (error) {
188
+ console.log(error);
189
+ return null
190
+ }
191
+ }
180
192
  }
181
193
 
182
194
  export const protocol = new Protocol();
@@ -0,0 +1,80 @@
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
+ const subprocess = spawn(process.argv[0], process.argv.slice(1), {
69
+ cwd: process.cwd(),
70
+ stdio: "inherit",
71
+ detached: true,
72
+ });
73
+
74
+ subprocess.unref();
75
+
76
+ process.exit()
77
+ }
78
+ }
79
+
80
+ export default AutoUpdateTask;
@@ -19,6 +19,7 @@ export class BaseTask extends EventEmitter implements IBaseTask {
19
19
  started: boolean = false
20
20
  pollIntervalMs: number = 10 * 1000
21
21
  leadNodeOnly: boolean = false
22
+ exceptLeadNode: boolean = false
22
23
 
23
24
  public constructor({ logger }: { logger?: Logger }) {
24
25
  super()
@@ -45,11 +46,20 @@ export class BaseTask extends EventEmitter implements IBaseTask {
45
46
  }
46
47
 
47
48
  prePollHandler(): boolean {
48
- if (!this.leadNodeOnly) {
49
- return true
49
+ if(config.isMaintenanceMode()){
50
+ this.logger.warn('Maintenance mode is enabled. Skipping task.')
51
+ return false
50
52
  }
51
53
 
52
- return config.isLeadNode()
54
+ if (this.exceptLeadNode) {
55
+ return !config.isLeadNode();
56
+ }
57
+
58
+ if (this.leadNodeOnly) {
59
+ return config.isLeadNode()
60
+ }
61
+
62
+ return true
53
63
  }
54
64
 
55
65
  async pollHandler() {
@@ -220,8 +220,6 @@ class ProcessWithdrawEvents extends BaseTask {
220
220
  }
221
221
 
222
222
  async start(): Promise<void> {
223
- this.logger.info(`Starting execution watcher on interop chain`);
224
-
225
223
  this.provider = new ethers.providers.JsonRpcProvider(
226
224
  getRpcProviderUrl(this.chainId)
227
225
  );
@@ -102,8 +102,6 @@ class SyncWithdrawEvents extends BaseTask {
102
102
  }
103
103
 
104
104
  async start(): Promise<void> {
105
- this.logger.info(`Starting execution watcher on interop chain`);
106
-
107
105
  this.provider = new ethers.providers.JsonRpcProvider(
108
106
  getRpcProviderUrl(this.chainId)
109
107
  );
@@ -224,8 +224,6 @@ class ProcessDepositEvents extends BaseTask {
224
224
  }
225
225
 
226
226
  async start(): Promise<void> {
227
- this.logger.info(`Starting execution watcher on interop chain`);
228
-
229
227
  this.contractAddress = addresses[this.chainId].interopXGateway;
230
228
 
231
229
  this.provider = new ethers.providers.JsonRpcProvider(
@@ -105,8 +105,6 @@ class SyncDepositEvents extends BaseTask {
105
105
  }
106
106
 
107
107
  async start(): Promise<void> {
108
- this.logger.info(`Starting execution watcher on interop chain`);
109
-
110
108
  this.contractAddress = addresses[this.chainId].interopXGateway;
111
109
 
112
110
  this.provider = new ethers.providers.JsonRpcProvider(
@@ -0,0 +1,65 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import config from "@/config";
4
+ import { peerPool, protocol } from "@/net";
5
+ import { Transaction } from "@/db";
6
+ import { Op } from "sequelize";
7
+
8
+ class SyncTransactionStatusTask extends BaseTask {
9
+ pollIntervalMs: number = 60 * 1000
10
+ exceptLeadNode: boolean = true;
11
+
12
+ constructor() {
13
+ super({
14
+ logger: new Logger("SyncTransactionStatusTask"),
15
+ })
16
+ }
17
+
18
+ async pollHandler() {
19
+ // if transaction is pending for more than 1 hour, check lead node for status
20
+ const leadNode = peerPool.getLeadPeer();
21
+
22
+ if (!leadNode) {
23
+ return;
24
+ }
25
+
26
+ const transaction = await Transaction.findOne({
27
+ where: {
28
+ status: 'pending',
29
+ sourceCreatedAt: {
30
+ [Op.gte]: new Date(Date.now() - 60 * 60 * 1000),
31
+ },
32
+ }
33
+ })
34
+
35
+ if (!transaction) {
36
+ return;
37
+ }
38
+
39
+ this.logger.info(`Requesting transaction status for ${transaction.transactionHash}`)
40
+
41
+ const transactionStatus = await protocol.requestTransactionStatus(transaction.transactionHash, leadNode.id);
42
+
43
+ if (!transactionStatus) {
44
+ return;
45
+ }
46
+
47
+ this.logger.info(`Received transaction status for ${transaction.transactionHash}`)
48
+
49
+ transaction.sourceStatus = transactionStatus.sourceStatus
50
+ transaction.sourceTransactionHash = transactionStatus.sourceTransactionHash
51
+ transaction.sourceErrors = transactionStatus.sourceErrors
52
+
53
+ transaction.targetStatus = transactionStatus.targetStatus
54
+ transaction.targetTransactionHash = transactionStatus.targetTransactionHash
55
+ transaction.targetErrors = transactionStatus.targetErrors
56
+
57
+ transaction.status = transactionStatus.status
58
+
59
+ await transaction.save()
60
+
61
+ this.logger.info(`Updated transaction status for ${transaction.transactionHash}`)
62
+ }
63
+ }
64
+
65
+ export default SyncTransactionStatusTask;
@@ -4,10 +4,15 @@ import InteropXGatewaySyncDepositEvents from "./InteropXGateway/SyncDepositEvent
4
4
 
5
5
  import InteropBridgeSyncWithdrawEvents from "./InteropBridge/SyncWithdrawEvents";
6
6
  import InteropBridgeProcessWithdrawEvents from "./InteropBridge/ProcessWithdrawEvents";
7
+ import AutoUpdateTask from "./AutoUpdateTask";
8
+ import SyncTransactionStatusTask from "./Transactions/SyncTransactionStatusTask";
7
9
 
8
10
  export class Tasks {
9
11
 
10
12
  tasks: BaseTask[] = [
13
+ new SyncTransactionStatusTask(),
14
+ new AutoUpdateTask(),
15
+
11
16
  new InteropXGatewaySyncDepositEvents({
12
17
  chainId: 43114
13
18
  }),
@@ -17,6 +17,18 @@ export const http = axios.create();
17
17
  axiosRetry(http, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
18
18
 
19
19
 
20
+ export function shortenHash(hash: string, length: number = 4) {
21
+ if (!hash) return;
22
+
23
+ if (hash.length < 12) return hash;
24
+
25
+ const beginningChars = hash.startsWith("0x") ? length + 2 : length;
26
+
27
+ const shortened = hash.substr(0, beginningChars) + "…" + hash.substr(-length);
28
+
29
+ return shortened;
30
+ }
31
+
20
32
  export function short(buffer: Buffer): string {
21
33
  return buffer.toString('hex').slice(0, 8) + '...'
22
34
  }
@@ -76,11 +88,11 @@ export const signGnosisSafeTx = async ({
76
88
  export const getRpcProviderUrl = (chainId: ChainId) => {
77
89
  switch (chainId) {
78
90
  case 1:
79
- return 'https://rpc.instadapp.io/mainnet';
91
+ return 'https://rpc.ankr.com/eth';
80
92
  case 137:
81
- return 'https://rpc.instadapp.io/polygon';
93
+ return 'https://rpc.ankr.com/polygon';
82
94
  case 43114:
83
- return 'https://rpc.instadapp.io/avalanche';
95
+ return 'https://rpc.ankr.com/avalanche';
84
96
  default:
85
97
  throw new Error(`Unknown chainId: ${chainId}`);
86
98
  }
@@ -259,6 +271,12 @@ export function getContract<TContract extends ethers.Contract>(address: string,
259
271
  signerOrProvider
260
272
  ) as TContract
261
273
 
274
+ // Make sure the contract properties is writable
275
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
276
+
277
+ if (!desc || desc.writable !== true) {
278
+ return contract
279
+ }
262
280
 
263
281
  return new Proxy(contract, {
264
282
  get(target, prop, receiver) {