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

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.b8c571d",
3
+ "version": "0.0.0-dev.c3250d9",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
package/dist/src/index.js CHANGED
@@ -40,15 +40,33 @@ catch (e) {
40
40
  logger.error('Invalid private key');
41
41
  process.exit(1);
42
42
  }
43
- logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.b8c571d)`);
43
+ logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.c3250d9)`);
44
44
  const tasks_1 = require("@/tasks");
45
45
  const net_1 = require("@/net");
46
46
  const api_1 = require("@/api");
47
+ const db_1 = require("./db");
47
48
  async function main() {
48
49
  (0, net_1.startPeer)({});
49
50
  const tasks = new tasks_1.Tasks();
50
51
  tasks.start();
51
52
  (0, api_1.startApiServer)();
53
+ net_1.protocol.on('Transaction', async (payload) => {
54
+ if (!net_1.peerPool.isLeadNode(payload.peerId)) {
55
+ return;
56
+ }
57
+ const transaction = await db_1.Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } });
58
+ if (!transaction) {
59
+ return;
60
+ }
61
+ transaction.sourceStatus = payload.data.sourceStatus;
62
+ transaction.sourceTransactionHash = payload.data.sourceTransactionHash;
63
+ transaction.sourceErrors = payload.data.sourceErrors;
64
+ transaction.targetStatus = payload.data.targetStatus;
65
+ transaction.targetTransactionHash = payload.data.targetTransactionHash;
66
+ transaction.targetErrors = payload.data.targetErrors;
67
+ transaction.status = payload.data.status;
68
+ await transaction.save();
69
+ });
52
70
  }
53
71
  main()
54
72
  .then(() => {
@@ -7,6 +7,7 @@ exports.peerPool = exports.PeerPool = void 0;
7
7
  const types_1 = require("@/types");
8
8
  const config_1 = __importDefault(require("@/config"));
9
9
  const logger_1 = __importDefault(require("@/logger"));
10
+ const utils_1 = require("ethers/lib/utils");
10
11
  const logger = new logger_1.default('PeerPool');
11
12
  class PeerPool {
12
13
  constructor() {
@@ -100,6 +101,13 @@ class PeerPool {
100
101
  get activePeerIds() {
101
102
  return this.activePeers.map((p) => p.id);
102
103
  }
104
+ isLeadNode(id) {
105
+ const peer = this.pool.get(id);
106
+ if (!peer) {
107
+ return false;
108
+ }
109
+ return (0, utils_1.getAddress)(peer.publicAddress) === (0, utils_1.getAddress)(config_1.default.leadNodeAddress);
110
+ }
103
111
  cleanup() {
104
112
  // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
105
113
  // this.peers.forEach((peerInfo) => {
@@ -16,7 +16,7 @@ class Protocol extends stream_1.EventEmitter {
16
16
  this.protocolMessages = [
17
17
  {
18
18
  name: 'PeerInfo',
19
- code: 0x09,
19
+ code: 0x01,
20
20
  encode: (info) => [
21
21
  Buffer.from(info.publicAddress),
22
22
  ],
@@ -24,6 +24,30 @@ class Protocol extends stream_1.EventEmitter {
24
24
  publicAddress: publicAddress.toString(),
25
25
  }),
26
26
  },
27
+ {
28
+ name: 'Transaction',
29
+ code: 0x02,
30
+ encode: (transaction) => [
31
+ Buffer.from(transaction.transactionHash),
32
+ Buffer.from(transaction.sourceStatus),
33
+ Buffer.from(transaction.sourceTransactionHash),
34
+ transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
35
+ Buffer.from(transaction.targetStatus),
36
+ Buffer.from(transaction.targetTransactionHash),
37
+ transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
38
+ Buffer.from(transaction.status),
39
+ ],
40
+ decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]) => ({
41
+ transactionHash: transactionHash.toString(),
42
+ sourceStatus: sourceStatus.toString(),
43
+ sourceTransactionHash: sourceTransactionHash.toString(),
44
+ sourceErrors: sourceErrors.map((e) => e.toString()),
45
+ targetStatus: targetStatus.toString(),
46
+ targetTransactionHash: targetTransactionHash.toString(),
47
+ targetErrors: targetErrors.map((e) => e.toString()),
48
+ status: status.toString(),
49
+ }),
50
+ },
27
51
  ];
28
52
  }
29
53
  start({ libp2p, topic = null, }) {
@@ -75,6 +99,11 @@ class Protocol extends stream_1.EventEmitter {
75
99
  const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(data)]);
76
100
  this.libp2p.pubsub.publish(this.topic, encoded);
77
101
  }
102
+ sendTransaction(transaction) {
103
+ const message = this.protocolMessages.find((m) => m.name === 'Transaction');
104
+ const encoded = ethereumjs_util_1.rlp.encode([message.code, message.encode(transaction)]);
105
+ this.libp2p.pubsub.publish(this.topic, encoded);
106
+ }
78
107
  async requestSignatures(data, peerIds) {
79
108
  try {
80
109
  peerIds = peerIds || __1.peerPool.activePeerIds;
@@ -126,12 +126,14 @@ class ProcessWithdrawEvents extends BaseTask_1.BaseTask {
126
126
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
127
127
  console.log('ExecutionSuccess');
128
128
  transaction.targetStatus = 'success';
129
+ transaction.targetTransactionHash = txSent.hash;
129
130
  transaction.status = 'success';
130
131
  await transaction.save();
131
132
  }
132
133
  else {
133
134
  console.log('ExecutionFailure');
134
135
  transaction.targetStatus = 'failed';
136
+ transaction.targetTransactionHash = txSent.hash;
135
137
  transaction.status = 'failed';
136
138
  await transaction.save();
137
139
  }
@@ -126,15 +126,18 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
126
126
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
127
127
  console.log('ExecutionSuccess');
128
128
  transaction.targetStatus = 'success';
129
+ transaction.targetTransactionHash = txSent.hash;
129
130
  transaction.status = 'success';
130
131
  await transaction.save();
131
132
  }
132
133
  else {
133
134
  console.log('ExecutionFailure');
134
135
  transaction.targetStatus = 'failed';
136
+ transaction.targetTransactionHash = txSent.hash;
135
137
  transaction.status = 'failed';
136
138
  await transaction.save();
137
139
  }
140
+ net_1.protocol.sendTransaction(transaction);
138
141
  }
139
142
  async start() {
140
143
  this.logger.info(`Starting execution watcher on interop chain`);
@@ -170,7 +170,7 @@ const buildWithdrawDataForTransaction = async (transaction, type) => {
170
170
  const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
171
171
  const gatewayAddress = constants_1.addresses[chainId].interopXGateway;
172
172
  const interopBridgeContract = getContract(gatewayAddress, abi_1.default.interopXGateway, targetWallet);
173
- const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(ethers_1.ethers.BigNumber.from(amount.toString()), to, token.address, ethers_1.ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()), transaction.submitTransactionHash);
173
+ const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(ethers_1.ethers.BigNumber.from(amount.toString()), to, token.address, ethers_1.ethers.BigNumber.from(transaction.sourceChainId.toString()), transaction.submitTransactionHash);
174
174
  transactions.push({
175
175
  to: gatewayAddress,
176
176
  data: data,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.b8c571d",
3
+ "version": "0.0.0-dev.c3250d9",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
package/src/index.ts CHANGED
@@ -45,8 +45,9 @@ try {
45
45
  logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
46
46
 
47
47
  import { Tasks } from "@/tasks";
48
- import { startPeer } from "@/net";
48
+ import { startPeer, protocol, peerPool } from "@/net";
49
49
  import { startApiServer } from '@/api';
50
+ import { Transaction } from './db';
50
51
 
51
52
  async function main() {
52
53
 
@@ -57,6 +58,30 @@ async function main() {
57
58
  tasks.start();
58
59
 
59
60
  startApiServer()
61
+
62
+ protocol.on('Transaction', async (payload) => {
63
+ if (!peerPool.isLeadNode(payload.peerId)) {
64
+ return;
65
+ }
66
+
67
+ const transaction = await Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } })
68
+
69
+ if (!transaction) {
70
+ return;
71
+ }
72
+
73
+ transaction.sourceStatus = payload.data.sourceStatus
74
+ transaction.sourceTransactionHash = payload.data.sourceTransactionHash
75
+ transaction.sourceErrors = payload.data.sourceErrors
76
+
77
+ transaction.targetStatus = payload.data.targetStatus
78
+ transaction.targetTransactionHash = payload.data.targetTransactionHash
79
+ transaction.targetErrors = payload.data.targetErrors
80
+
81
+ transaction.status = payload.data.status
82
+
83
+ await transaction.save()
84
+ })
60
85
  }
61
86
 
62
87
  main()
@@ -1,6 +1,7 @@
1
1
  import { Event } from "@/types";
2
2
  import config from "@/config";
3
3
  import Logger from "@/logger";
4
+ import { getAddress } from "ethers/lib/utils";
4
5
 
5
6
 
6
7
  const logger = new Logger('PeerPool')
@@ -83,8 +84,8 @@ export class PeerPool {
83
84
  const newPeer = !this.pool.get(peer.id);
84
85
  this.pool.set(peer.id, peer)
85
86
  peer.pooled = true
86
-
87
- if(newPeer) {
87
+
88
+ if (newPeer) {
88
89
  config.events.emit(Event.POOL_PEER_ADDED, peer)
89
90
  logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`)
90
91
  }
@@ -110,7 +111,7 @@ export class PeerPool {
110
111
  this.cleanup()
111
112
 
112
113
  return this.peers.filter((p) => {
113
- if(!p.pooled) return false;
114
+ if (!p.pooled) return false;
114
115
 
115
116
  const now = new Date()
116
117
 
@@ -123,6 +124,17 @@ export class PeerPool {
123
124
  }
124
125
 
125
126
 
127
+ isLeadNode(id: string) {
128
+ const peer = this.pool.get(id);
129
+
130
+ if (!peer) {
131
+ return false;
132
+ }
133
+
134
+ return getAddress(peer.publicAddress) === getAddress(config.leadNodeAddress)
135
+ }
136
+
137
+
126
138
  cleanup() {
127
139
  // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
128
140
 
@@ -5,6 +5,7 @@ import { SignatureDialProtocol, ISignatureRequest, ISignatureResponse } from "./
5
5
  import { IPeerInfo, peerPool } from "..";
6
6
  import config from "@/config";
7
7
  import { Event } from "@/types";
8
+ import { Transaction } from "@/db";
8
9
 
9
10
  export interface ProtocolOptions {
10
11
  /* Handshake timeout in ms (default: 8000) */
@@ -33,7 +34,12 @@ interface PeerInfoEvent extends BaseMessageEvent {
33
34
  data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
34
35
  }
35
36
 
37
+ interface TransactionEvent extends BaseMessageEvent {
38
+ data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
39
+ }
40
+
36
41
  declare interface Protocol {
42
+ on(event: 'Transaction', listener: (payload: TransactionEvent) => void): this;
37
43
  on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
38
44
  on(event: string, listener: (payload: BaseMessageEvent) => void): this;
39
45
  }
@@ -44,7 +50,7 @@ class Protocol extends EventEmitter {
44
50
  private protocolMessages: Message[] = [
45
51
  {
46
52
  name: 'PeerInfo',
47
- code: 0x09,
53
+ code: 0x01,
48
54
  encode: (info: Pick<IPeerInfo, 'publicAddress'>) => [
49
55
  Buffer.from(info.publicAddress),
50
56
  ],
@@ -52,6 +58,36 @@ class Protocol extends EventEmitter {
52
58
  publicAddress: publicAddress.toString(),
53
59
  }),
54
60
  },
61
+ {
62
+ name: 'Transaction',
63
+ code: 0x02,
64
+ encode: (transaction: Transaction) => [
65
+ Buffer.from(transaction.transactionHash),
66
+
67
+ Buffer.from(transaction.sourceStatus),
68
+ Buffer.from(transaction.sourceTransactionHash),
69
+ transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
70
+
71
+ Buffer.from(transaction.targetStatus),
72
+ Buffer.from(transaction.targetTransactionHash),
73
+ transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
74
+
75
+ Buffer.from(transaction.status),
76
+ ],
77
+ decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]: [Buffer, Buffer, Buffer, Buffer[], Buffer, Buffer, Buffer[], Buffer]) => ({
78
+ transactionHash: transactionHash.toString(),
79
+
80
+ sourceStatus: sourceStatus.toString(),
81
+ sourceTransactionHash: sourceTransactionHash.toString(),
82
+ sourceErrors: sourceErrors.map((e) => e.toString()),
83
+
84
+ targetStatus: targetStatus.toString(),
85
+ targetTransactionHash: targetTransactionHash.toString(),
86
+ targetErrors: targetErrors.map((e) => e.toString()),
87
+
88
+ status: status.toString(),
89
+ }),
90
+ },
55
91
  ];
56
92
  private signature: SignatureDialProtocol;
57
93
 
@@ -121,6 +157,14 @@ class Protocol extends EventEmitter {
121
157
  this.libp2p.pubsub.publish(this.topic, encoded)
122
158
  }
123
159
 
160
+ public sendTransaction(transaction: Transaction) {
161
+ const message = this.protocolMessages.find((m) => m.name === 'Transaction')!
162
+
163
+ const encoded = rlp.encode([message.code, message.encode(transaction)]);
164
+
165
+ this.libp2p.pubsub.publish(this.topic, encoded)
166
+ }
167
+
124
168
  async requestSignatures(data: ISignatureRequest, peerIds?: string[]) {
125
169
  try {
126
170
  peerIds = peerIds || peerPool.activePeerIds;
@@ -207,11 +207,13 @@ class ProcessWithdrawEvents extends BaseTask {
207
207
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
208
208
  console.log('ExecutionSuccess')
209
209
  transaction.targetStatus = 'success'
210
+ transaction.targetTransactionHash = txSent.hash
210
211
  transaction.status = 'success'
211
212
  await transaction.save();
212
213
  } else {
213
214
  console.log('ExecutionFailure')
214
215
  transaction.targetStatus = 'failed'
216
+ transaction.targetTransactionHash = txSent.hash
215
217
  transaction.status = 'failed'
216
218
  await transaction.save();
217
219
  }
@@ -209,14 +209,18 @@ class ProcessDepositEvents extends BaseTask {
209
209
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
210
210
  console.log('ExecutionSuccess')
211
211
  transaction.targetStatus = 'success'
212
+ transaction.targetTransactionHash = txSent.hash
212
213
  transaction.status = 'success'
213
214
  await transaction.save();
214
215
  } else {
215
216
  console.log('ExecutionFailure')
216
217
  transaction.targetStatus = 'failed'
218
+ transaction.targetTransactionHash = txSent.hash
217
219
  transaction.status = 'failed'
218
220
  await transaction.save();
219
221
  }
222
+
223
+ protocol.sendTransaction(transaction)
220
224
  }
221
225
 
222
226
  async start(): Promise<void> {
@@ -233,7 +233,7 @@ export const buildWithdrawDataForTransaction = async (transaction: Transaction,
233
233
  ethers.BigNumber.from(amount.toString()),
234
234
  to,
235
235
  token.address,
236
- ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()),
236
+ ethers.BigNumber.from(transaction.sourceChainId.toString()),
237
237
  transaction.submitTransactionHash,
238
238
  );
239
239