@instadapp/interop-x 0.0.0-dev.1abc1ca → 0.0.0-dev.285d847

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/bin/interop-x +1 -1
  2. package/dist/package.json +6 -3
  3. package/dist/src/config/index.js +1 -0
  4. package/dist/src/constants/itokens.js +1 -1
  5. package/dist/src/index.js +39 -5
  6. package/dist/src/net/peer/index.js +6 -2
  7. package/dist/src/net/pool/index.js +27 -9
  8. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +11 -4
  9. package/dist/src/net/protocol/index.js +30 -1
  10. package/dist/src/tasks/AutoUpdateTask.js +44 -0
  11. package/dist/src/tasks/BaseTask.js +1 -1
  12. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +147 -0
  13. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +70 -0
  14. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +32 -22
  15. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +4 -4
  16. package/dist/src/tasks/index.js +15 -0
  17. package/dist/src/utils/index.js +98 -7
  18. package/package.json +6 -3
  19. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  20. package/src/config/index.ts +2 -0
  21. package/src/constants/itokens.ts +1 -1
  22. package/src/index.ts +47 -6
  23. package/src/net/peer/index.ts +7 -6
  24. package/src/net/pool/index.ts +37 -11
  25. package/src/net/protocol/dial/SignatureDialProtocol.ts +12 -4
  26. package/src/net/protocol/index.ts +45 -1
  27. package/src/tasks/AutoUpdateTask.ts +54 -0
  28. package/src/tasks/BaseTask.ts +1 -1
  29. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +233 -0
  30. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +121 -0
  31. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +44 -31
  32. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +6 -6
  33. package/src/tasks/index.ts +22 -2
  34. package/src/utils/index.ts +131 -9
@@ -15,6 +15,7 @@ const sequelize_1 = require("sequelize");
15
15
  const waait_1 = __importDefault(require("waait"));
16
16
  const net_1 = require("@/net");
17
17
  const generateGnosisTransaction = async (transactionData, safeContract) => {
18
+ console.log(transactionData);
18
19
  let isExecuted = await safeContract.dataHashes(await safeContract.getTransactionHash(transactionData.to, transactionData.value, transactionData.data, transactionData.operation, transactionData.safeTxGas, transactionData.baseGas, transactionData.gasPrice, transactionData.gasToken, transactionData.refundReceiver, transactionData.nonce));
19
20
  while (isExecuted == 1) {
20
21
  transactionData.safeTxGas = ethers_1.BigNumber.from(String(transactionData.safeTxGas)).add(1).toString();
@@ -37,30 +38,39 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
37
38
  where: {
38
39
  status: 'pending',
39
40
  sourceStatus: 'success',
41
+ targetStatus: 'uninitialised',
40
42
  action: 'deposit',
41
43
  sourceCreatedAt: {
42
44
  [sequelize_1.Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
43
45
  },
46
+ targetDelayUntil: {
47
+ [sequelize_1.Op.or]: {
48
+ [sequelize_1.Op.is]: null,
49
+ [sequelize_1.Op.lt]: new Date(),
50
+ }
51
+ },
44
52
  sourceBlockNumber: {
45
53
  [sequelize_1.Op.lt]: blockNumber - 12,
46
- }
54
+ },
55
+ sourceChainId: this.chainId,
47
56
  }
48
57
  });
49
58
  if (!transaction) {
50
59
  return;
51
60
  }
52
- transaction.sourceStatus = 'pending';
61
+ console.log(`Processing transaction ${transaction.transactionHash}`);
62
+ transaction.targetStatus = 'pending';
53
63
  await transaction.save();
54
64
  // refresh event data?
55
65
  const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(transaction.targetChainId));
56
66
  const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
57
67
  const safeAddress = constants_1.addresses[transaction.targetChainId].gnosisSafe;
58
- const safeContract = new ethers_1.ethers.Contract(safeAddress, abi_1.default.gnosisSafe, targetWallet);
68
+ const safeContract = (0, utils_1.getContract)(safeAddress, abi_1.default.gnosisSafe, targetWallet);
59
69
  const ownersThreshold = await safeContract.getThreshold();
60
70
  await (0, waait_1.default)(10000);
61
71
  let gnosisTx = await generateGnosisTransaction({
62
72
  baseGas: "0",
63
- data: (0, utils_1.buildDataForTransaction)(transaction),
73
+ data: await (0, utils_1.buildDataForTransaction)(transaction),
64
74
  gasPrice: "0",
65
75
  gasToken: "0x0000000000000000000000000000000000000000",
66
76
  nonce: '0',
@@ -74,6 +84,7 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
74
84
  const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
75
85
  const ownerPeerIds = net_1.peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id);
76
86
  console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
87
+ console.log(ownerPeerIds);
77
88
  const signatures = await net_1.protocol.requestSignatures({
78
89
  type: 'source',
79
90
  transactionHash: transaction.transactionHash,
@@ -84,33 +95,23 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
84
95
  console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
85
96
  if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
86
97
  await transaction.save();
87
- transaction.sourceDelayUntil = new Date(Date.now() + 30 * 1000);
88
- transaction.sourceStatus = 'pending';
98
+ transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
99
+ transaction.targetStatus = 'uninitialised';
89
100
  await transaction.save();
90
101
  const errorMessage = (_a = signatures.find(s => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
91
102
  throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
92
103
  }
93
- const execTransactionParams = [
94
- gnosisTx.to,
95
- gnosisTx.value,
96
- gnosisTx.data,
97
- gnosisTx.operation,
98
- gnosisTx.safeTxGas,
99
- gnosisTx.baseGas,
100
- gnosisTx.gasPrice,
101
- gnosisTx.gasToken,
102
- gnosisTx.refundReceiver,
103
- (0, utils_1.buildSignatureBytes)(validSignatures),
104
- ];
105
104
  console.log(`Executing transaction for execution ${transaction.transactionHash}`);
105
+ const { data: txData } = await safeContract.populateTransaction.execTransaction(gnosisTx.to, gnosisTx.value, gnosisTx.data, gnosisTx.operation, gnosisTx.safeTxGas, gnosisTx.baseGas, gnosisTx.gasPrice, gnosisTx.gasToken, gnosisTx.refundReceiver, (0, utils_1.buildSignatureBytes)(validSignatures));
106
106
  console.log({
107
- execTransactionParams
107
+ from: targetWallet.address,
108
+ gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9).toString(),
109
+ to: safeAddress,
110
+ data: txData,
108
111
  });
109
- const { data: txData } = await safeContract.populateTransaction.execTransaction(gnosisTx.to, gnosisTx.value, gnosisTx.data, gnosisTx.operation, gnosisTx.safeTxGas, gnosisTx.baseGas, gnosisTx.gasPrice, gnosisTx.gasToken, gnosisTx.refundReceiver, (0, utils_1.buildSignatureBytes)(validSignatures));
110
112
  const txSent = await targetWallet.sendTransaction({
111
113
  from: targetWallet.address,
112
114
  gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9),
113
- gasLimit: ethers_1.BigNumber.from(6000000),
114
115
  to: safeAddress,
115
116
  data: txData,
116
117
  });
@@ -124,16 +125,25 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
124
125
  });
125
126
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
126
127
  console.log('ExecutionSuccess');
128
+ transaction.targetStatus = 'success';
129
+ transaction.targetTransactionHash = txSent.hash;
130
+ transaction.status = 'success';
131
+ await transaction.save();
127
132
  }
128
133
  else {
129
134
  console.log('ExecutionFailure');
135
+ transaction.targetStatus = 'failed';
136
+ transaction.targetTransactionHash = txSent.hash;
137
+ transaction.status = 'failed';
138
+ await transaction.save();
130
139
  }
140
+ net_1.protocol.sendTransaction(transaction);
131
141
  }
132
142
  async start() {
133
143
  this.logger.info(`Starting execution watcher on interop chain`);
134
144
  this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
135
145
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
136
- this.contract = new ethers_1.ethers.Contract(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
146
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
137
147
  await super.start();
138
148
  }
139
149
  }
@@ -45,17 +45,17 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
45
45
  sourceChainId: sourceChainId.toString(),
46
46
  targetChainId: targetChainId.toString(),
47
47
  token: token,
48
- ammout: amount.toString(),
48
+ amount: amount.toString(),
49
49
  vnonce: vnonce.toString(),
50
50
  }, sourceEvent: {
51
51
  user,
52
52
  sourceChainId: sourceChainId.toString(),
53
53
  targetChainId: targetChainId.toString(),
54
54
  token: token,
55
- ammout: amount.toString(),
55
+ amount: amount.toString(),
56
56
  vnonce: vnonce.toString(),
57
57
  }, status: "pending" }));
58
- this.logger.info(`Execution queued: ${event.transactionHash} ${event.blockNumber}`);
58
+ this.logger.info(`Deposit queued: ${event.transactionHash} ${event.blockNumber}`);
59
59
  }
60
60
  catch (error) {
61
61
  this.logger.error(error);
@@ -68,7 +68,7 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
68
68
  this.logger.info(`Starting execution watcher on interop chain`);
69
69
  this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
70
70
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
71
- this.contract = new ethers_1.ethers.Contract(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
71
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
72
72
  await super.start();
73
73
  }
74
74
  }
@@ -4,12 +4,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Tasks = void 0;
7
+ const ProcessDepositEvents_1 = __importDefault(require("./InteropXGateway/ProcessDepositEvents"));
7
8
  const SyncDepositEvents_1 = __importDefault(require("./InteropXGateway/SyncDepositEvents"));
9
+ const SyncWithdrawEvents_1 = __importDefault(require("./InteropBridge/SyncWithdrawEvents"));
10
+ const ProcessWithdrawEvents_1 = __importDefault(require("./InteropBridge/ProcessWithdrawEvents"));
11
+ const AutoUpdateTask_1 = __importDefault(require("./AutoUpdateTask"));
8
12
  class Tasks {
9
13
  constructor() {
10
14
  this.tasks = [
15
+ new AutoUpdateTask_1.default(),
11
16
  new SyncDepositEvents_1.default({
12
17
  chainId: 43114
18
+ }),
19
+ new ProcessDepositEvents_1.default({
20
+ chainId: 43114
21
+ }),
22
+ new SyncWithdrawEvents_1.default({
23
+ chainId: 137,
24
+ itokenAddress: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
25
+ }),
26
+ new ProcessWithdrawEvents_1.default({
27
+ chainId: 137,
13
28
  })
14
29
  ];
15
30
  }
@@ -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.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.http = void 0;
7
7
  /**
8
8
  * @module util
9
9
  */
@@ -104,9 +104,20 @@ const generateInteropTransactionHash = (data) => {
104
104
  exports.generateInteropTransactionHash = generateInteropTransactionHash;
105
105
  const buildDataForTransaction = async (transaction, type) => {
106
106
  type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
107
+ switch (transaction.action) {
108
+ case "deposit":
109
+ return await (0, exports.buildDepositDataForTransaction)(transaction, type);
110
+ case "withdraw":
111
+ return await (0, exports.buildWithdrawDataForTransaction)(transaction, type);
112
+ default:
113
+ throw new Error(`Unknown action: ${transaction.action}`);
114
+ }
115
+ };
116
+ exports.buildDataForTransaction = buildDataForTransaction;
117
+ const buildDepositDataForTransaction = async (transaction, type) => {
107
118
  const transactions = [];
108
- if (transaction.action != 'deposit') {
109
- throw new Error('Invalid action');
119
+ if (transaction.action !== 'deposit') {
120
+ throw new Error(`Invalid action: ${transaction.action}`);
110
121
  }
111
122
  if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
112
123
  throw Error('Cannot build data for pending deposit transaction');
@@ -118,14 +129,14 @@ const buildDataForTransaction = async (transaction, type) => {
118
129
  if (!token) {
119
130
  throw Error('Cannot build data for transaction without token');
120
131
  }
121
- const itoken = constants_1.itokens[transaction.targetChainId].find(itoken => token.symbol.toLowerCase() === token.symbol.toLowerCase());
132
+ const itoken = constants_1.itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
122
133
  if (!itoken) {
123
134
  throw Error('Cannot build data for transaction without itoken');
124
135
  }
125
136
  const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, exports.getRpcProviderUrl)(transaction.targetChainId));
126
137
  const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
127
- const interopBridgeContract = new ethers_1.ethers.Contract(itoken.address, abi_1.default.interopBridgeToken, targetWallet);
128
- const { data } = await interopBridgeContract.populateTransaction.mint(transaction.submitEvent.to, transaction.submitEvent.amount, transaction.sourceChainId, transaction.sourceTransactionHash);
138
+ const interopBridgeContract = getContract(itoken.address, abi_1.default.interopBridgeToken, targetWallet);
139
+ const { data } = await interopBridgeContract.populateTransaction.mint(transaction.submitEvent.user, ethers_1.ethers.BigNumber.from(transaction.submitEvent.amount.toString()), ethers_1.ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()), transaction.submitTransactionHash);
129
140
  transactions.push({
130
141
  to: itoken.address,
131
142
  data: data,
@@ -134,4 +145,84 @@ const buildDataForTransaction = async (transaction, type) => {
134
145
  });
135
146
  return (0, ethers_multisend_1.encodeMulti)(transactions).data;
136
147
  };
137
- exports.buildDataForTransaction = buildDataForTransaction;
148
+ exports.buildDepositDataForTransaction = buildDepositDataForTransaction;
149
+ const buildWithdrawDataForTransaction = async (transaction, type) => {
150
+ const transactions = [];
151
+ if (transaction.action !== 'withdraw') {
152
+ throw new Error(`Invalid action: ${transaction.action}`);
153
+ }
154
+ if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
155
+ throw Error('Cannot build data for pending withdraw transaction');
156
+ }
157
+ if (!transaction.submitEvent) {
158
+ throw Error('Cannot build data for transaction without submitEvent');
159
+ }
160
+ const { to, amount, chainId, itoken: itokenAddress } = transaction.submitEvent;
161
+ const itoken = constants_1.itokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === itokenAddress.toLowerCase());
162
+ if (!itoken) {
163
+ throw Error('Cannot build data for transaction without itoken');
164
+ }
165
+ const token = constants_1.tokens[chainId].find(t => t.symbol.toLowerCase() === itoken.symbol.toLowerCase());
166
+ if (!token) {
167
+ throw Error('Cannot build data for transaction without token');
168
+ }
169
+ const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, exports.getRpcProviderUrl)(transaction.targetChainId));
170
+ const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
171
+ const gatewayAddress = constants_1.addresses[chainId].interopXGateway;
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.sourceChainId.toString()), transaction.submitTransactionHash);
174
+ transactions.push({
175
+ to: gatewayAddress,
176
+ data: data,
177
+ value: '0',
178
+ operation: ethers_multisend_1.OperationType.Call,
179
+ });
180
+ return (0, ethers_multisend_1.encodeMulti)(transactions).data;
181
+ };
182
+ exports.buildWithdrawDataForTransaction = buildWithdrawDataForTransaction;
183
+ function getContract(address, contractInterface, signerOrProvider) {
184
+ if (!ethers_1.ethers.utils.getAddress(address) || address === ethers_1.ethers.constants.AddressZero) {
185
+ throw Error(`Invalid 'address' parameter '${address}'.`);
186
+ }
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
+ }
193
+ return new Proxy(contract, {
194
+ get(target, prop, receiver) {
195
+ const value = Reflect.get(target, prop, receiver);
196
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
197
+ return async (...args) => {
198
+ try {
199
+ return await value.bind(contract)(...args);
200
+ }
201
+ catch (error) {
202
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`);
203
+ }
204
+ };
205
+ }
206
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
207
+ const parentProp = String(prop);
208
+ return new Proxy(value, {
209
+ get(target, prop, receiver) {
210
+ const value = Reflect.get(target, prop, receiver);
211
+ if (typeof value === 'function') {
212
+ return async (...args) => {
213
+ try {
214
+ return await value.bind(contract)(...args);
215
+ }
216
+ catch (error) {
217
+ throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`);
218
+ }
219
+ };
220
+ }
221
+ }
222
+ });
223
+ }
224
+ return value;
225
+ },
226
+ });
227
+ }
228
+ exports.getContract = getContract;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.1abc1ca",
3
+ "version": "0.0.0-dev.285d847",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -12,7 +12,8 @@
12
12
  "build": "yarn generate-abi-types && export GIT_REF=$(git rev-parse --short HEAD) && rimraf ./dist && tsc -p tsconfig.json && replace-in-file '@GIT_SHORT_HASH@' $GIT_REF ./dist/**/*.js",
13
13
  "dev": "yarn generate-abi-types && NODE_ENV=development nodemon",
14
14
  "generate-abi-types": "typechain --target=ethers-v5 'src/abi/*.json' --out-dir 'src/typechain'",
15
- "prepublishOnly": "yarn build"
15
+ "prepublishOnly": "yarn build",
16
+ "postinstall": "patch-package"
16
17
  },
17
18
  "nodemonConfig": {
18
19
  "watch": [
@@ -45,7 +46,9 @@
45
46
  "libp2p-websockets": "^0.16.2",
46
47
  "luxon": "^2.3.2",
47
48
  "module-alias": "^2.2.2",
48
- "sequelize": "^6.19.0",
49
+ "patch-package": "^6.4.7",
50
+ "postinstall-postinstall": "^2.1.0",
51
+ "sequelize": "6.18.0",
49
52
  "sqlite3": "^5.0.5",
50
53
  "waait": "^1.0.5"
51
54
  },
@@ -0,0 +1,13 @@
1
+ diff --git a/node_modules/@ethersproject/properties/lib/index.js b/node_modules/@ethersproject/properties/lib/index.js
2
+ index 41e0b52..4c7a9e3 100644
3
+ --- a/node_modules/@ethersproject/properties/lib/index.js
4
+ +++ b/node_modules/@ethersproject/properties/lib/index.js
5
+ @@ -44,7 +44,7 @@ function defineReadOnly(object, name, value) {
6
+ Object.defineProperty(object, name, {
7
+ enumerable: true,
8
+ value: value,
9
+ - writable: false,
10
+ + writable: true,
11
+ });
12
+ }
13
+ exports.defineReadOnly = defineReadOnly;
@@ -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
  }
@@ -2,7 +2,7 @@ export const itokens = {
2
2
  1: [],
3
3
  137: [
4
4
  {
5
- address: '0x6c20F03598d5ABF729348E2868b0ff5e8A48aB1F',
5
+ address: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
6
6
  symbol: 'USDC',
7
7
  }
8
8
  ],
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,28 +25,43 @@ 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
+ if (process.argv.at(-1) === 'version') {
42
+ console.log(`Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
43
+ process.exit(0)
44
+ }
37
45
 
46
+ if(! process.env.PRIVATE_KEY) {
47
+ console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
48
+ printUsage()
49
+ process.exit(1)
50
+ }
38
51
  try {
39
52
  new ethers.Wallet(process.env.PRIVATE_KEY!)
40
53
  } catch (e) {
41
- logger.error('Invalid private key')
54
+ console.error(chalk.bgRed.white('Invalid private key\n'))
55
+ printUsage()
42
56
  process.exit(1)
43
57
  }
44
58
 
45
59
  logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
46
60
 
47
61
  import { Tasks } from "@/tasks";
48
- import { startPeer } from "@/net";
62
+ import { startPeer, protocol, peerPool } from "@/net";
49
63
  import { startApiServer } from '@/api';
64
+ import { Transaction } from './db';
50
65
 
51
66
  async function main() {
52
67
 
@@ -57,6 +72,32 @@ async function main() {
57
72
  tasks.start();
58
73
 
59
74
  startApiServer()
75
+
76
+ protocol.on('TransactionStatus', async (payload) => {
77
+ if (!peerPool.isLeadNode(payload.peerId)) {
78
+ const peer = peerPool.getPeer(payload.peerId)
79
+ logger.info(`ignored transaction status from ${payload.peerId} ${peer?.publicAddress} `)
80
+ return;
81
+ }
82
+
83
+ const transaction = await Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } })
84
+
85
+ if (!transaction) {
86
+ return;
87
+ }
88
+
89
+ transaction.sourceStatus = payload.data.sourceStatus
90
+ transaction.sourceTransactionHash = payload.data.sourceTransactionHash
91
+ transaction.sourceErrors = payload.data.sourceErrors
92
+
93
+ transaction.targetStatus = payload.data.targetStatus
94
+ transaction.targetTransactionHash = payload.data.targetTransactionHash
95
+ transaction.targetErrors = payload.data.targetErrors
96
+
97
+ transaction.status = payload.data.status
98
+
99
+ await transaction.save()
100
+ })
60
101
  }
61
102
 
62
103
  main()
@@ -88,12 +88,13 @@ export const startPeer = async ({ }: IPeerOptions) => {
88
88
  libp2p: node
89
89
  })
90
90
 
91
- node.on("peer:discovery", (peer) =>
92
- logger.log(`Discovered peer ${peer}`)
93
- ); // peer disc.
94
- node.connectionManager.on("peer:connect", (connection) =>
95
- logger.log(`Connected to ${connection.remotePeer.toB58String()}`)
96
- );
91
+ node.on("peer:discovery", (peer) => {
92
+ // logger.log(`Discovered peer ${peer}`)
93
+ }); // peer disc.
94
+
95
+ node.connectionManager.on("peer:connect", (connection) => {
96
+ // logger.log(`Connected to ${connection.remotePeer.toB58String()}`)
97
+ });
97
98
 
98
99
  logger.log("Peer discovery started");
99
100
 
@@ -1,5 +1,10 @@
1
1
  import { Event } from "@/types";
2
2
  import config from "@/config";
3
+ import Logger from "@/logger";
4
+ import { getAddress } from "ethers/lib/utils";
5
+
6
+
7
+ const logger = new Logger('PeerPool')
3
8
 
4
9
  export interface IPeerInfo {
5
10
  id: string;
@@ -75,10 +80,15 @@ export class PeerPool {
75
80
  * @emits {@link Event.POOL_PEER_ADDED}
76
81
  */
77
82
  add(peer?: IPeerInfo) {
78
- if (peer && peer.id && !this.pool.get(peer.id)) {
83
+ if (peer && peer.id) {
84
+ const newPeer = !this.pool.get(peer.id);
79
85
  this.pool.set(peer.id, peer)
80
86
  peer.pooled = true
81
- config.events.emit(Event.POOL_PEER_ADDED, peer)
87
+
88
+ if (newPeer) {
89
+ config.events.emit(Event.POOL_PEER_ADDED, peer)
90
+ logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`)
91
+ }
82
92
  }
83
93
  }
84
94
 
@@ -92,6 +102,7 @@ export class PeerPool {
92
102
  if (this.pool.delete(peer.id)) {
93
103
  peer.pooled = false
94
104
  config.events.emit(Event.POOL_PEER_REMOVED, peer)
105
+ logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`)
95
106
  }
96
107
  }
97
108
  }
@@ -100,7 +111,7 @@ export class PeerPool {
100
111
  this.cleanup()
101
112
 
102
113
  return this.peers.filter((p) => {
103
- if(!p.pooled) return false;
114
+ if (!p.pooled) return false;
104
115
 
105
116
  const now = new Date()
106
117
 
@@ -113,15 +124,30 @@ export class PeerPool {
113
124
  }
114
125
 
115
126
 
116
- cleanup() {
117
- let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
127
+ getPeer(id: string){
128
+ return this.pool.get(id);
129
+ }
118
130
 
119
- this.peers.forEach((peerInfo) => {
120
- if (peerInfo.updated.getTime() < compDate) {
121
- console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
122
- this.remove(peerInfo)
123
- }
124
- })
131
+ isLeadNode(id: string) {
132
+ const peer = this.pool.get(id);
133
+
134
+ if (!peer) {
135
+ return false;
136
+ }
137
+
138
+ return getAddress(peer.publicAddress) === getAddress(config.leadNodeAddress)
139
+ }
140
+
141
+
142
+ cleanup() {
143
+ // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
144
+
145
+ // this.peers.forEach((peerInfo) => {
146
+ // if (peerInfo.updated.getTime() < compDate) {
147
+ // console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
148
+ // this.remove(peerInfo)
149
+ // }
150
+ // })
125
151
  }
126
152
  }
127
153
 
@@ -47,12 +47,20 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
47
47
  };
48
48
  }
49
49
 
50
+ console.log("signing:", {
51
+ to: addresses[transaction.targetChainId].multisend,
52
+ data: await buildDataForTransaction(transaction, data.type),
53
+ chainId: transaction.targetChainId as ChainId,
54
+ safeTxGas: data.safeTxGas,
55
+ nonce: data.safeNonce,
56
+ });
57
+
50
58
  const signedData = await signGnosisSafeTx({
51
- //TODO: chain id depends on event type
52
- to: addresses[transaction.sourceChainId].multisend,
53
- data: buildDataForTransaction(transaction, data.type),
54
- chainId: transaction.sourceChainId as ChainId,
59
+ to: addresses[transaction.targetChainId].multisend,
60
+ data: await buildDataForTransaction(transaction, data.type),
61
+ chainId: transaction.targetChainId as ChainId,
55
62
  safeTxGas: data.safeTxGas,
63
+ nonce: data.safeNonce,
56
64
  }, { signer });
57
65
 
58
66
  return {