@instadapp/interop-x 0.0.0-dev.c3250d9 → 0.0.0-dev.d05464a

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.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.c3250d9",
3
+ "version": "0.0.0-dev.d05464a",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -24,6 +24,8 @@
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
31
  "bignumber.js": "^9.0.2",
@@ -34,7 +36,6 @@
34
36
  "ethers-multisend": "^2.1.1",
35
37
  "expand-home-dir": "^0.0.3",
36
38
  "fastify": "^3.28.0",
37
- "fastify-cors": "^6.0.3",
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';
@@ -8,6 +8,7 @@ class Config {
8
8
  this.maxPeers = 10;
9
9
  this.privateKey = process.env.PRIVATE_KEY;
10
10
  this.staging = !!process.env.STAGING && process.env.STAGING === 'true';
11
+ this.autoUpdate = !!process.env.AUTO_UPDATE && process.env.AUTO_UPDATE === 'true';
11
12
  this.wallet = new ethers_1.Wallet(this.privateKey);
12
13
  this.leadNodeAddress = '0x910E413DBF3F6276Fe8213fF656726bDc142E08E';
13
14
  }
package/dist/src/index.js CHANGED
@@ -19,28 +19,43 @@ module_alias_1.default.addAliases({
19
19
  "@/typechain": __dirname + "/typechain"
20
20
  });
21
21
  (0, module_alias_1.default)();
22
- const assert_1 = __importDefault(require("assert"));
23
22
  const dotenv_1 = __importDefault(require("dotenv"));
23
+ const chalk_1 = __importDefault(require("chalk"));
24
24
  const ethers_1 = require("ethers");
25
25
  const package_json_1 = __importDefault(require("../package.json"));
26
26
  dotenv_1.default.config();
27
27
  const logger_1 = __importDefault(require("@/logger"));
28
28
  const logger = new logger_1.default('Process');
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
+ if (process.argv.at(-1) === 'help') {
37
+ printUsage();
33
38
  process.exit(0);
34
39
  }
35
- (0, assert_1.default)(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
40
+ const GIT_SHORT_HASH = 'd05464a';
41
+ if (process.argv.at(-1) === 'version') {
42
+ console.log(`Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
43
+ process.exit(0);
44
+ }
45
+ if (!process.env.PRIVATE_KEY) {
46
+ console.error(chalk_1.default.bgRed.white.bold('Please provide a private key\n'));
47
+ printUsage();
48
+ process.exit(1);
49
+ }
36
50
  try {
37
51
  new ethers_1.ethers.Wallet(process.env.PRIVATE_KEY);
38
52
  }
39
53
  catch (e) {
40
- logger.error('Invalid private key');
54
+ console.error(chalk_1.default.bgRed.white('Invalid private key\n'));
55
+ printUsage();
41
56
  process.exit(1);
42
57
  }
43
- logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.c3250d9)`);
58
+ logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.${GIT_SHORT_HASH})`);
44
59
  const tasks_1 = require("@/tasks");
45
60
  const net_1 = require("@/net");
46
61
  const api_1 = require("@/api");
@@ -50,8 +65,10 @@ async function main() {
50
65
  const tasks = new tasks_1.Tasks();
51
66
  tasks.start();
52
67
  (0, api_1.startApiServer)();
53
- net_1.protocol.on('Transaction', async (payload) => {
68
+ net_1.protocol.on('TransactionStatus', async (payload) => {
54
69
  if (!net_1.peerPool.isLeadNode(payload.peerId)) {
70
+ const peer = net_1.peerPool.getPeer(payload.peerId);
71
+ logger.info(`ignored transaction status from ${payload.peerId} ${peer === null || peer === void 0 ? void 0 : peer.publicAddress} `);
55
72
  return;
56
73
  }
57
74
  const transaction = await db_1.Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } });
@@ -101,6 +101,9 @@ class PeerPool {
101
101
  get activePeerIds() {
102
102
  return this.activePeers.map((p) => p.id);
103
103
  }
104
+ getPeer(id) {
105
+ return this.pool.get(id);
106
+ }
104
107
  isLeadNode(id) {
105
108
  const peer = this.pool.get(id);
106
109
  if (!peer) {
@@ -25,7 +25,7 @@ class Protocol extends stream_1.EventEmitter {
25
25
  }),
26
26
  },
27
27
  {
28
- name: 'Transaction',
28
+ name: 'TransactionStatus',
29
29
  code: 0x02,
30
30
  encode: (transaction) => [
31
31
  Buffer.from(transaction.transactionHash),
@@ -100,7 +100,7 @@ class Protocol extends stream_1.EventEmitter {
100
100
  this.libp2p.pubsub.publish(this.topic, encoded);
101
101
  }
102
102
  sendTransaction(transaction) {
103
- const message = this.protocolMessages.find((m) => m.name === 'Transaction');
103
+ const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus');
104
104
  const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(transaction)]);
105
105
  this.libp2p.pubsub.publish(this.topic, encoded);
106
106
  }
@@ -0,0 +1,48 @@
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 utils_1 = require("@/utils");
9
+ const await_spawn_1 = __importDefault(require("await-spawn"));
10
+ const config_1 = __importDefault(require("@/config"));
11
+ const waait_1 = __importDefault(require("waait"));
12
+ class AutoUpdateTask extends BaseTask_1.BaseTask {
13
+ constructor() {
14
+ super({
15
+ logger: new logger_1.default("AutoUpdateTask"),
16
+ });
17
+ this.pollIntervalMs = 60 * 5 * 1000;
18
+ }
19
+ prePollHandler() {
20
+ return config_1.default.autoUpdate && !config_1.default.isLeadNode();
21
+ }
22
+ getCurrentVersion() {
23
+ return require('../../package.json').version;
24
+ }
25
+ async pollHandler() {
26
+ const { data } = await utils_1.http.get('https://registry.npmjs.org/@instadapp/interop-x');
27
+ const version = data['dist-tags'].latest;
28
+ const currentVersion = this.getCurrentVersion();
29
+ if (version === currentVersion) {
30
+ return;
31
+ }
32
+ this.logger.warn(`New version ${version} available.`);
33
+ await (0, await_spawn_1.default)('npm', ['-g', 'install', '@instadapp/interop-x', '-f']);
34
+ await (0, waait_1.default)(5000);
35
+ if (currentVersion === this.getCurrentVersion()) {
36
+ this.logger.warn(`failed to install ${version}, retrying in 5 minutes`);
37
+ return;
38
+ }
39
+ this.logger.warn(`Installed version ${version}`);
40
+ this.logger.warn(`Restarting...`);
41
+ (0, await_spawn_1.default)(process.argv[0], process.argv.slice(1), {
42
+ cwd: process.cwd(),
43
+ stdio: "inherit"
44
+ });
45
+ process.exit();
46
+ }
47
+ }
48
+ exports.default = AutoUpdateTask;
@@ -8,9 +8,11 @@ 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"));
11
12
  class Tasks {
12
13
  constructor() {
13
14
  this.tasks = [
15
+ new AutoUpdateTask_1.default(),
14
16
  new SyncDepositEvents_1.default({
15
17
  chainId: 43114
16
18
  }),
@@ -185,6 +185,11 @@ function getContract(address, contractInterface, signerOrProvider) {
185
185
  throw Error(`Invalid 'address' parameter '${address}'.`);
186
186
  }
187
187
  const contract = new ethers_1.ethers.Contract(address, contractInterface, signerOrProvider);
188
+ // Make sure the contract properties is writable
189
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
190
+ if (!desc || desc.writable !== true) {
191
+ return contract;
192
+ }
188
193
  return new Proxy(contract, {
189
194
  get(target, prop, receiver) {
190
195
  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.c3250d9",
3
+ "version": "0.0.0-dev.d05464a",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -24,6 +24,8 @@
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
31
  "bignumber.js": "^9.0.2",
@@ -34,7 +36,6 @@
34
36
  "ethers-multisend": "^2.1.1",
35
37
  "expand-home-dir": "^0.0.3",
36
38
  "fastify": "^3.28.0",
37
- "fastify-cors": "^6.0.3",
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
 
@@ -8,12 +8,14 @@ class Config {
8
8
  public readonly privateKey: string
9
9
  public readonly wallet: Wallet
10
10
  public readonly staging: boolean
11
+ public readonly autoUpdate: boolean
11
12
 
12
13
  constructor() {
13
14
  this.events = new EventBus() as EventBusType
14
15
  this.maxPeers = 10
15
16
  this.privateKey = process.env.PRIVATE_KEY as string;
16
17
  this.staging = !! process.env.STAGING && process.env.STAGING === 'true';
18
+ this.autoUpdate = !! process.env.AUTO_UPDATE && process.env.AUTO_UPDATE === 'true';
17
19
  this.wallet = new Wallet(this.privateKey);
18
20
  this.leadNodeAddress = '0x910E413DBF3F6276Fe8213fF656726bDc142E08E'
19
21
  }
package/src/index.ts CHANGED
@@ -16,8 +16,8 @@ moduleAlias.addAliases({
16
16
  })
17
17
 
18
18
  moduleAlias();
19
- import assert from "assert";
20
19
  import dotenv from "dotenv";
20
+ import chalk from 'chalk';
21
21
  import { ethers } from "ethers";
22
22
  import packageJson from '../package.json'
23
23
  dotenv.config();
@@ -25,24 +25,40 @@ dotenv.config();
25
25
  import Logger from "@/logger";
26
26
  const logger = new Logger('Process')
27
27
 
28
-
29
- if (process.argv.at(-1) === 'help') {
28
+ const printUsage = () => {
30
29
  console.log('Usage:')
31
30
  console.log(' PRIVATE_KEY=abcd1234 interop-x')
32
31
  console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x')
32
+ console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x')
33
+ console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x')
34
+ }
35
+
36
+ if (process.argv.at(-1) === 'help') {
37
+ printUsage()
33
38
  process.exit(0)
34
39
  }
35
40
 
36
- assert(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
41
+ const GIT_SHORT_HASH = '@GIT_SHORT_HASH@';
37
42
 
43
+ if (process.argv.at(-1) === 'version') {
44
+ console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
45
+ process.exit(0)
46
+ }
47
+
48
+ if(! process.env.PRIVATE_KEY) {
49
+ console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
50
+ printUsage()
51
+ process.exit(1)
52
+ }
38
53
  try {
39
54
  new ethers.Wallet(process.env.PRIVATE_KEY!)
40
55
  } catch (e) {
41
- logger.error('Invalid private key')
56
+ console.error(chalk.bgRed.white('Invalid private key\n'))
57
+ printUsage()
42
58
  process.exit(1)
43
59
  }
44
60
 
45
- logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
61
+ logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
46
62
 
47
63
  import { Tasks } from "@/tasks";
48
64
  import { startPeer, protocol, peerPool } from "@/net";
@@ -59,8 +75,10 @@ async function main() {
59
75
 
60
76
  startApiServer()
61
77
 
62
- protocol.on('Transaction', async (payload) => {
78
+ protocol.on('TransactionStatus', async (payload) => {
63
79
  if (!peerPool.isLeadNode(payload.peerId)) {
80
+ const peer = peerPool.getPeer(payload.peerId)
81
+ logger.info(`ignored transaction status from ${payload.peerId} ${peer?.publicAddress} `)
64
82
  return;
65
83
  }
66
84
 
@@ -124,6 +124,10 @@ export class PeerPool {
124
124
  }
125
125
 
126
126
 
127
+ getPeer(id: string){
128
+ return this.pool.get(id);
129
+ }
130
+
127
131
  isLeadNode(id: string) {
128
132
  const peer = this.pool.get(id);
129
133
 
@@ -34,12 +34,12 @@ interface PeerInfoEvent extends BaseMessageEvent {
34
34
  data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
35
35
  }
36
36
 
37
- interface TransactionEvent extends BaseMessageEvent {
37
+ interface TransactionStatusEvent extends BaseMessageEvent {
38
38
  data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
39
39
  }
40
40
 
41
41
  declare interface Protocol {
42
- on(event: 'Transaction', listener: (payload: TransactionEvent) => void): this;
42
+ on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
43
43
  on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
44
44
  on(event: string, listener: (payload: BaseMessageEvent) => void): this;
45
45
  }
@@ -59,7 +59,7 @@ class Protocol extends EventEmitter {
59
59
  }),
60
60
  },
61
61
  {
62
- name: 'Transaction',
62
+ name: 'TransactionStatus',
63
63
  code: 0x02,
64
64
  encode: (transaction: Transaction) => [
65
65
  Buffer.from(transaction.transactionHash),
@@ -158,7 +158,7 @@ class Protocol extends EventEmitter {
158
158
  }
159
159
 
160
160
  public sendTransaction(transaction: Transaction) {
161
- const message = this.protocolMessages.find((m) => m.name === 'Transaction')!
161
+ const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
162
162
 
163
163
  const encoded = rlp.encode([message.code, message.encode(transaction)]);
164
164
 
@@ -0,0 +1,60 @@
1
+ import { BaseTask } from "./BaseTask";
2
+ import Logger from '@/logger';
3
+ import { http } from "@/utils";
4
+ import spawn from 'await-spawn';
5
+ import config from "@/config";
6
+ import wait from "waait";
7
+
8
+ class AutoUpdateTask extends BaseTask {
9
+ pollIntervalMs: number = 60 * 5 * 1000
10
+
11
+ constructor() {
12
+ super({
13
+ logger: new Logger("AutoUpdateTask"),
14
+ })
15
+ }
16
+
17
+ prePollHandler(): boolean {
18
+ return config.autoUpdate && !config.isLeadNode();
19
+ }
20
+
21
+ getCurrentVersion() {
22
+ return require('../../package.json').version
23
+ }
24
+ async pollHandler() {
25
+
26
+ const { data } = await http.get('https://registry.npmjs.org/@instadapp/interop-x')
27
+
28
+ const version = data['dist-tags'].latest
29
+ const currentVersion = this.getCurrentVersion()
30
+
31
+ if (version === currentVersion) {
32
+ return;
33
+ }
34
+
35
+ this.logger.warn(`New version ${version} available.`)
36
+
37
+
38
+ await spawn('npm', ['-g', 'install', '@instadapp/interop-x', '-f']);
39
+
40
+
41
+ await wait(5000)
42
+
43
+ if (currentVersion === this.getCurrentVersion()) {
44
+ this.logger.warn(`failed to install ${version}, retrying in 5 minutes`)
45
+ return;
46
+ }
47
+
48
+ this.logger.warn(`Installed version ${version}`)
49
+ this.logger.warn(`Restarting...`)
50
+
51
+ spawn(process.argv[0], process.argv.slice(1), {
52
+ cwd: process.cwd(),
53
+ stdio: "inherit"
54
+ });
55
+
56
+ process.exit()
57
+ }
58
+ }
59
+
60
+ export default AutoUpdateTask;
@@ -4,10 +4,13 @@ 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";
7
8
 
8
9
  export class Tasks {
9
10
 
10
11
  tasks: BaseTask[] = [
12
+ new AutoUpdateTask(),
13
+
11
14
  new InteropXGatewaySyncDepositEvents({
12
15
  chainId: 43114
13
16
  }),
@@ -259,6 +259,12 @@ export function getContract<TContract extends ethers.Contract>(address: string,
259
259
  signerOrProvider
260
260
  ) as TContract
261
261
 
262
+ // Make sure the contract properties is writable
263
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
264
+
265
+ if (!desc || desc.writable !== true) {
266
+ return contract
267
+ }
262
268
 
263
269
  return new Proxy(contract, {
264
270
  get(target, prop, receiver) {