@instadapp/interop-x 0.0.0-dev.9387bd9 → 0.0.0-dev.a8594f9

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 (93) hide show
  1. package/.env.example +2 -1
  2. package/bin/interop-x +1 -1
  3. package/dist/package.json +72 -0
  4. package/dist/src/abi/erc20.json +350 -0
  5. package/dist/src/abi/gnosisSafe.json +747 -0
  6. package/dist/src/abi/index.js +15 -0
  7. package/dist/src/abi/interopBridgeToken.json +286 -0
  8. package/dist/src/abi/interopXGateway.json +184 -0
  9. package/dist/src/api/index.js +33 -0
  10. package/dist/{config → src/config}/index.js +5 -1
  11. package/dist/src/constants/addresses.js +20 -0
  12. package/dist/{constants → src/constants}/index.js +2 -0
  13. package/dist/src/constants/itokens.js +13 -0
  14. package/dist/src/constants/tokens.js +107 -0
  15. package/dist/{db → src/db}/index.js +0 -0
  16. package/dist/{db → src/db}/models/index.js +1 -1
  17. package/dist/src/db/models/transaction.js +54 -0
  18. package/dist/{db → src/db}/sequelize.js +2 -1
  19. package/dist/{index.js → src/index.js} +29 -3
  20. package/dist/{logger → src/logger}/index.js +0 -0
  21. package/dist/{net → src/net}/index.js +0 -0
  22. package/dist/{net → src/net}/peer/index.js +11 -7
  23. package/dist/{net → src/net}/pool/index.js +18 -11
  24. package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +1 -1
  25. package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +22 -14
  26. package/dist/{net → src/net}/protocol/index.js +3 -3
  27. package/dist/{tasks → src/tasks}/BaseTask.js +3 -3
  28. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +145 -0
  29. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +70 -0
  30. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +147 -0
  31. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +75 -0
  32. package/dist/src/tasks/index.js +40 -0
  33. package/dist/src/typechain/Erc20.js +2 -0
  34. package/dist/src/typechain/GnosisSafe.js +2 -0
  35. package/dist/src/typechain/InteropBridgeToken.js +2 -0
  36. package/dist/src/typechain/InteropXGateway.js +2 -0
  37. package/dist/src/typechain/common.js +2 -0
  38. package/dist/src/typechain/factories/Erc20__factory.js +367 -0
  39. package/dist/src/typechain/factories/GnosisSafe__factory.js +1174 -0
  40. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +459 -0
  41. package/dist/src/typechain/factories/InteropXGateway__factory.js +265 -0
  42. package/dist/src/typechain/factories/index.js +14 -0
  43. package/dist/src/typechain/index.js +35 -0
  44. package/dist/{types.js → src/types.js} +0 -0
  45. package/dist/src/utils/index.js +223 -0
  46. package/package.json +19 -6
  47. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  48. package/src/abi/erc20.json +350 -0
  49. package/src/abi/gnosisSafe.json +747 -0
  50. package/src/abi/index.ts +11 -0
  51. package/src/abi/interopBridgeToken.json +286 -0
  52. package/src/abi/interopXGateway.json +184 -0
  53. package/src/api/index.ts +33 -0
  54. package/src/config/index.ts +7 -1
  55. package/src/constants/addresses.ts +9 -2
  56. package/src/constants/index.ts +2 -0
  57. package/src/constants/itokens.ts +10 -0
  58. package/src/constants/tokens.ts +104 -0
  59. package/src/db/index.ts +1 -1
  60. package/src/db/models/index.ts +1 -1
  61. package/src/db/models/transaction.ts +96 -0
  62. package/src/db/sequelize.ts +2 -1
  63. package/src/index.ts +35 -3
  64. package/src/net/peer/index.ts +10 -9
  65. package/src/net/pool/index.ts +22 -12
  66. package/src/net/protocol/dial/BaseDialProtocol.ts +1 -1
  67. package/src/net/protocol/dial/SignatureDialProtocol.ts +26 -17
  68. package/src/net/protocol/index.ts +3 -3
  69. package/src/tasks/BaseTask.ts +3 -4
  70. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +231 -0
  71. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +121 -0
  72. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +241 -0
  73. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +126 -0
  74. package/src/tasks/index.ts +21 -1
  75. package/src/typechain/Erc20.ts +491 -0
  76. package/src/typechain/GnosisSafe.ts +1728 -0
  77. package/src/typechain/InteropBridgeToken.ts +686 -0
  78. package/src/typechain/InteropXGateway.ts +407 -0
  79. package/src/typechain/common.ts +44 -0
  80. package/src/typechain/factories/Erc20__factory.ts +368 -0
  81. package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
  82. package/src/typechain/factories/InteropBridgeToken__factory.ts +466 -0
  83. package/src/typechain/factories/InteropXGateway__factory.ts +272 -0
  84. package/src/typechain/factories/index.ts +7 -0
  85. package/src/typechain/index.ts +12 -0
  86. package/src/types.ts +2 -2
  87. package/src/utils/index.ts +187 -2
  88. package/tsconfig.json +3 -0
  89. package/dist/constants/addresses.js +0 -13
  90. package/dist/db/models/execution.js +0 -38
  91. package/dist/tasks/index.js +0 -19
  92. package/dist/utils/index.js +0 -89
  93. package/src/db/models/execution.ts +0 -57
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tokens = void 0;
4
+ exports.tokens = {
5
+ 1: [
6
+ {
7
+ symbol: "ETH",
8
+ name: "Ethereum",
9
+ address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
10
+ decimals: 18,
11
+ },
12
+ {
13
+ symbol: "DAI",
14
+ name: "DAI Stable",
15
+ address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
16
+ decimals: 18,
17
+ },
18
+ {
19
+ symbol: "USDC",
20
+ name: "USD Coin",
21
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
22
+ decimals: 6,
23
+ },
24
+ {
25
+ symbol: "USDT",
26
+ name: "Tether USD Coin",
27
+ address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
28
+ decimals: 6,
29
+ },
30
+ {
31
+ symbol: "WBTC",
32
+ name: "Wrapped BTC",
33
+ address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
34
+ decimals: 8,
35
+ },
36
+ ],
37
+ 137: [
38
+ {
39
+ symbol: "ETH",
40
+ name: "Ethereum",
41
+ address: "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619",
42
+ decimals: 18,
43
+ },
44
+ {
45
+ symbol: "DAI",
46
+ name: "DAI Stable",
47
+ address: "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063",
48
+ decimals: 18,
49
+ },
50
+ {
51
+ symbol: "USDC",
52
+ name: "USD Coin",
53
+ address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
54
+ decimals: 6,
55
+ },
56
+ {
57
+ symbol: "USDT",
58
+ name: "Tether USD Coin",
59
+ address: "0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
60
+ decimals: 6,
61
+ },
62
+ {
63
+ symbol: "WBTC",
64
+ name: "Wrapped BTC",
65
+ address: "0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6",
66
+ decimals: 8,
67
+ },
68
+ {
69
+ symbol: "AVAX",
70
+ name: "Avalanche Token",
71
+ address: "0x2C89bbc92BD86F8075d1DEcc58C7F4E0107f286b",
72
+ decimals: 18,
73
+ },
74
+ ],
75
+ 43114: [
76
+ {
77
+ symbol: "ETH",
78
+ name: "Ethereum",
79
+ address: "0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB",
80
+ decimals: 18,
81
+ },
82
+ {
83
+ symbol: "DAI",
84
+ name: "DAI Stable",
85
+ address: "0xd586E7F844cEa2F87f50152665BCbc2C279D8d70",
86
+ decimals: 18,
87
+ },
88
+ {
89
+ symbol: "USDC",
90
+ name: "USD Coin",
91
+ address: "0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664",
92
+ decimals: 6,
93
+ },
94
+ {
95
+ symbol: "USDT",
96
+ name: "Tether USD Coin",
97
+ address: "0xc7198437980c041c805A1EDcbA50c1Ce5db95118",
98
+ decimals: 6,
99
+ },
100
+ {
101
+ symbol: "WBTC",
102
+ name: "Wrapped BTC",
103
+ address: "0x50b7545627a5162F82A992c33b87aDc75187B218",
104
+ decimals: 8,
105
+ },
106
+ ],
107
+ };
File without changes
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./execution"), exports);
17
+ __exportStar(require("./transaction"), exports);
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Transaction = void 0;
4
+ const sequelize_1 = require("@/db/sequelize");
5
+ const sequelize_2 = require("sequelize");
6
+ class Transaction extends sequelize_2.Model {
7
+ }
8
+ exports.Transaction = Transaction;
9
+ Transaction.init({
10
+ id: {
11
+ type: sequelize_2.DataTypes.INTEGER,
12
+ autoIncrement: true,
13
+ primaryKey: true
14
+ },
15
+ submitTransactionHash: sequelize_2.DataTypes.NUMBER,
16
+ submitBlockNumber: sequelize_2.DataTypes.NUMBER,
17
+ transactionHash: sequelize_2.DataTypes.STRING,
18
+ action: sequelize_2.DataTypes.STRING,
19
+ from: sequelize_2.DataTypes.STRING,
20
+ to: sequelize_2.DataTypes.STRING,
21
+ sourceChainId: sequelize_2.DataTypes.NUMBER,
22
+ sourceTransactionHash: sequelize_2.DataTypes.STRING,
23
+ sourceBlockNumber: sequelize_2.DataTypes.NUMBER,
24
+ sourceStatus: sequelize_2.DataTypes.STRING,
25
+ sourceErrors: {
26
+ type: sequelize_2.DataTypes.JSON,
27
+ // defaultValue: [],
28
+ },
29
+ sourceCreatedAt: {
30
+ type: sequelize_2.DataTypes.DATE,
31
+ defaultValue: Date.now()
32
+ },
33
+ sourceDelayUntil: sequelize_2.DataTypes.STRING,
34
+ targetChainId: sequelize_2.DataTypes.NUMBER,
35
+ targetTransactionHash: sequelize_2.DataTypes.STRING,
36
+ targetBlockNumber: sequelize_2.DataTypes.NUMBER,
37
+ targetStatus: sequelize_2.DataTypes.STRING,
38
+ targetErrors: {
39
+ type: sequelize_2.DataTypes.JSON,
40
+ // defaultValue: [],
41
+ },
42
+ targetCreatedAt: sequelize_2.DataTypes.DATE,
43
+ targetDelayUntil: sequelize_2.DataTypes.DATE,
44
+ submitEvent: sequelize_2.DataTypes.JSON,
45
+ sourceEvent: sequelize_2.DataTypes.JSON,
46
+ targetEvent: sequelize_2.DataTypes.JSON,
47
+ metadata: sequelize_2.DataTypes.JSON,
48
+ status: {
49
+ type: sequelize_2.DataTypes.STRING,
50
+ defaultValue: 'pending'
51
+ },
52
+ createdAt: sequelize_2.DataTypes.DATE,
53
+ updatedAt: sequelize_2.DataTypes.DATE,
54
+ }, { sequelize: sequelize_1.sequelize, tableName: 'transactions' });
@@ -5,9 +5,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.sequelize = void 0;
7
7
  //@ts-ignore
8
+ const config_1 = __importDefault(require("@/config"));
8
9
  const expand_home_dir_1 = __importDefault(require("expand-home-dir"));
9
10
  const sequelize_1 = require("sequelize");
10
- const basePath = (0, expand_home_dir_1.default)('~/.interop-x/data');
11
+ const basePath = (0, expand_home_dir_1.default)(`~/.interop-x/data/${config_1.default.publicAddress}/${config_1.default.staging ? 'staging' : ''}`);
11
12
  exports.sequelize = new sequelize_1.Sequelize({
12
13
  dialect: 'sqlite',
13
14
  storage: `${basePath}/localDB.sqlite`,
@@ -3,12 +3,35 @@ 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
+ const module_alias_1 = __importDefault(require("module-alias"));
7
+ module_alias_1.default.addAliases({
8
+ "@/": __dirname + "/",
9
+ "@/logger": __dirname + "/logger",
10
+ "@/tasks": __dirname + "/tasks",
11
+ "@/utils": __dirname + "/utils",
12
+ "@/api": __dirname + "/api",
13
+ "@/net": __dirname + "/net",
14
+ "@/db": __dirname + "/db",
15
+ "@/config": __dirname + "/config",
16
+ "@/types": __dirname + "/types",
17
+ "@/abi": __dirname + "/abi",
18
+ "@/constants": __dirname + "/constants",
19
+ "@/typechain": __dirname + "/typechain"
20
+ });
21
+ (0, module_alias_1.default)();
6
22
  const assert_1 = __importDefault(require("assert"));
7
23
  const dotenv_1 = __importDefault(require("dotenv"));
8
- const logger_1 = __importDefault(require("./logger"));
9
24
  const ethers_1 = require("ethers");
25
+ const package_json_1 = __importDefault(require("../package.json"));
10
26
  dotenv_1.default.config();
27
+ const logger_1 = __importDefault(require("@/logger"));
11
28
  const logger = new logger_1.default('Process');
29
+ if (process.argv.at(-1) === 'help') {
30
+ console.log('Usage:');
31
+ console.log(' PRIVATE_KEY=abcd1234 interop-x');
32
+ console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x');
33
+ process.exit(0);
34
+ }
12
35
  (0, assert_1.default)(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
13
36
  try {
14
37
  new ethers_1.ethers.Wallet(process.env.PRIVATE_KEY);
@@ -17,12 +40,15 @@ catch (e) {
17
40
  logger.error('Invalid private key');
18
41
  process.exit(1);
19
42
  }
20
- const tasks_1 = require("./tasks");
21
- const net_1 = require("./net");
43
+ logger.debug(`Starting Interop X Node (v${package_json_1.default.version} - rev.a8594f9)`);
44
+ const tasks_1 = require("@/tasks");
45
+ const net_1 = require("@/net");
46
+ const api_1 = require("@/api");
22
47
  async function main() {
23
48
  (0, net_1.startPeer)({});
24
49
  const tasks = new tasks_1.Tasks();
25
50
  tasks.start();
51
+ (0, api_1.startApiServer)();
26
52
  }
27
53
  main()
28
54
  .then(() => {
File without changes
File without changes
@@ -11,7 +11,7 @@ const libp2p_websockets_1 = __importDefault(require("libp2p-websockets"));
11
11
  //@ts-ignore
12
12
  const libp2p_mplex_1 = __importDefault(require("libp2p-mplex"));
13
13
  const libp2p_noise_1 = require("libp2p-noise");
14
- const logger_1 = __importDefault(require("../../logger"));
14
+ const logger_1 = __importDefault(require("@/logger"));
15
15
  const libp2p_bootstrap_1 = __importDefault(require("libp2p-bootstrap"));
16
16
  const waait_1 = __importDefault(require("waait"));
17
17
  const libp2p_gossipsub_1 = __importDefault(require("@achingbrain/libp2p-gossipsub"));
@@ -21,8 +21,8 @@ const libp2p_mdns_1 = __importDefault(require("libp2p-mdns"));
21
21
  const libp2p_kad_dht_1 = __importDefault(require("libp2p-kad-dht"));
22
22
  //@ts-ignore
23
23
  const libp2p_pubsub_peer_discovery_1 = __importDefault(require("libp2p-pubsub-peer-discovery"));
24
- const protocol_1 = require("../protocol");
25
- const config_1 = __importDefault(require("../../config"));
24
+ const net_1 = require("@/net");
25
+ const config_1 = __importDefault(require("@/config"));
26
26
  const logger = new logger_1.default("Peer");
27
27
  let node;
28
28
  // Known peers addresses
@@ -79,14 +79,18 @@ const startPeer = async ({}) => {
79
79
  });
80
80
  logger.info("Peer ID:", node.peerId.toB58String());
81
81
  await node.start();
82
- protocol_1.protocol.start({
82
+ net_1.protocol.start({
83
83
  libp2p: node
84
84
  });
85
- node.on("peer:discovery", (peer) => logger.log(`Discovered peer ${peer}`)); // peer disc.
86
- node.connectionManager.on("peer:connect", (connection) => logger.log(`Connected to ${connection.remotePeer.toB58String()}`));
85
+ node.on("peer:discovery", (peer) => {
86
+ // logger.log(`Discovered peer ${peer}`)
87
+ }); // peer disc.
88
+ node.connectionManager.on("peer:connect", (connection) => {
89
+ // logger.log(`Connected to ${connection.remotePeer.toB58String()}`)
90
+ });
87
91
  logger.log("Peer discovery started");
88
92
  await (0, waait_1.default)(1000);
89
- setInterval(() => protocol_1.protocol.sendPeerInfo({
93
+ setInterval(() => net_1.protocol.sendPeerInfo({
90
94
  publicAddress: config_1.default.wallet.address,
91
95
  }), 5000);
92
96
  };
@@ -4,8 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.peerPool = exports.PeerPool = void 0;
7
- const types_1 = require("../../types");
8
- const config_1 = __importDefault(require("../../config"));
7
+ const types_1 = require("@/types");
8
+ const config_1 = __importDefault(require("@/config"));
9
+ const logger_1 = __importDefault(require("@/logger"));
10
+ const logger = new logger_1.default('PeerPool');
9
11
  class PeerPool {
10
12
  constructor() {
11
13
  this.PEERS_CLEANUP_TIME_LIMIT = 1;
@@ -62,10 +64,14 @@ class PeerPool {
62
64
  * @emits {@link Event.POOL_PEER_ADDED}
63
65
  */
64
66
  add(peer) {
65
- if (peer && peer.id && !this.pool.get(peer.id)) {
67
+ if (peer && peer.id) {
68
+ const newPeer = !this.pool.get(peer.id);
66
69
  this.pool.set(peer.id, peer);
67
70
  peer.pooled = true;
68
- config_1.default.events.emit(types_1.Event.POOL_PEER_ADDED, peer);
71
+ if (newPeer) {
72
+ config_1.default.events.emit(types_1.Event.POOL_PEER_ADDED, peer);
73
+ logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`);
74
+ }
69
75
  }
70
76
  }
71
77
  /**
@@ -78,6 +84,7 @@ class PeerPool {
78
84
  if (this.pool.delete(peer.id)) {
79
85
  peer.pooled = false;
80
86
  config_1.default.events.emit(types_1.Event.POOL_PEER_REMOVED, peer);
87
+ logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`);
81
88
  }
82
89
  }
83
90
  }
@@ -94,13 +101,13 @@ class PeerPool {
94
101
  return this.activePeers.map((p) => p.id);
95
102
  }
96
103
  cleanup() {
97
- let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60;
98
- this.peers.forEach((peerInfo) => {
99
- if (peerInfo.updated.getTime() < compDate) {
100
- console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`);
101
- this.remove(peerInfo);
102
- }
103
- });
104
+ // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
105
+ // this.peers.forEach((peerInfo) => {
106
+ // if (peerInfo.updated.getTime() < compDate) {
107
+ // console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
108
+ // this.remove(peerInfo)
109
+ // }
110
+ // })
104
111
  }
105
112
  }
106
113
  exports.PeerPool = PeerPool;
@@ -13,7 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  exports.BaseDialProtocol = void 0;
14
14
  const it_pipe_1 = __importDefault(require("it-pipe"));
15
15
  const peer_id_1 = __importDefault(require("peer-id"));
16
- const utils_1 = require("../../../utils");
16
+ const utils_1 = require("@/utils");
17
17
  const waait_1 = __importDefault(require("waait"));
18
18
  class BaseDialProtocol {
19
19
  constructor(libp2p, protocol) {
@@ -4,44 +4,52 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SignatureDialProtocol = void 0;
7
- const utils_1 = require("../../../utils");
8
7
  const BaseDialProtocol_1 = require("./BaseDialProtocol");
9
8
  const waait_1 = __importDefault(require("waait"));
10
- const config_1 = __importDefault(require("../../../config"));
11
- const constants_1 = require("../../../constants");
12
- const db_1 = require("db");
9
+ const config_1 = __importDefault(require("@/config"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
13
  class SignatureDialProtocol extends BaseDialProtocol_1.BaseDialProtocol {
14
14
  constructor(libp2p) {
15
- super(libp2p, '/signatures');
15
+ super(libp2p, '/interop-x/signatures');
16
16
  this.timeout = 30000;
17
17
  }
18
18
  async response(data) {
19
19
  const signer = config_1.default.wallet;
20
- let event;
20
+ let transaction;
21
21
  let maxTimeout = 20000;
22
22
  do {
23
- event = await db_1.Execution.findOne({ where: { vnonce: data.vnonce.toString() } });
24
- if (!event) {
23
+ transaction = await db_1.Transaction.findOne({ where: { transactionHash: data.transactionHash } });
24
+ if (!transaction) {
25
25
  await (0, waait_1.default)(1000);
26
26
  maxTimeout -= 1000;
27
27
  }
28
- } while (!event && maxTimeout > 0);
29
- if (!event) {
28
+ } while (!transaction && maxTimeout > 0);
29
+ if (!transaction) {
30
30
  return {
31
31
  signer: signer.address,
32
32
  data: null,
33
33
  error: 'Event not found'
34
34
  };
35
35
  }
36
+ console.log("signing:", {
37
+ to: constants_1.addresses[transaction.targetChainId].multisend,
38
+ data: await (0, utils_1.buildDataForTransaction)(transaction, data.type),
39
+ chainId: transaction.targetChainId,
40
+ safeTxGas: data.safeTxGas,
41
+ nonce: data.safeNonce,
42
+ });
36
43
  const signedData = await (0, utils_1.signGnosisSafeTx)({
37
- to: constants_1.addresses[event.chainId].multisend,
38
- data: 'TODO',
39
- chainId: event.chainId,
44
+ to: constants_1.addresses[transaction.targetChainId].multisend,
45
+ data: await (0, utils_1.buildDataForTransaction)(transaction, data.type),
46
+ chainId: transaction.targetChainId,
40
47
  safeTxGas: data.safeTxGas,
48
+ nonce: data.safeNonce,
41
49
  }, { signer });
42
50
  return {
43
51
  signer: signer.address,
44
- data: signedData,
52
+ data: signedData
45
53
  };
46
54
  }
47
55
  }
@@ -8,8 +8,8 @@ const stream_1 = require("stream");
8
8
  const ethereumjs_util_1 = require("ethereumjs-util");
9
9
  const SignatureDialProtocol_1 = require("./dial/SignatureDialProtocol");
10
10
  const __1 = require("..");
11
- const config_1 = __importDefault(require("config"));
12
- const types_1 = require("types");
11
+ const config_1 = __importDefault(require("@/config"));
12
+ const types_1 = require("@/types");
13
13
  class Protocol extends stream_1.EventEmitter {
14
14
  constructor() {
15
15
  super(...arguments);
@@ -28,7 +28,7 @@ class Protocol extends stream_1.EventEmitter {
28
28
  }
29
29
  start({ libp2p, topic = null, }) {
30
30
  this.libp2p = libp2p;
31
- this.topic = topic || 'protocol';
31
+ this.topic = topic || 'itnerop-x-protocol';
32
32
  if (this.libp2p.isStarted())
33
33
  this.init();
34
34
  this.on('PeerInfo', (payload) => {
@@ -4,10 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.BaseTask = void 0;
7
- const config_1 = __importDefault(require("config"));
7
+ const config_1 = __importDefault(require("@/config"));
8
8
  const events_1 = __importDefault(require("events"));
9
9
  const waait_1 = __importDefault(require("waait"));
10
- const logger_1 = __importDefault(require("../logger"));
10
+ const logger_1 = __importDefault(require("@/logger"));
11
11
  class BaseTask extends events_1.default {
12
12
  constructor({ logger }) {
13
13
  super();
@@ -28,7 +28,7 @@ class BaseTask extends events_1.default {
28
28
  }
29
29
  }
30
30
  catch (err) {
31
- this.logger.error(`poll check error: ${err.message}\ntrace: ${err.stack}`);
31
+ this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`);
32
32
  }
33
33
  await this.postPollHandler();
34
34
  }
@@ -0,0 +1,145 @@
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 ethers_1 = require("ethers");
9
+ const abi_1 = __importDefault(require("@/abi"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
+ const config_1 = __importDefault(require("@/config"));
14
+ const sequelize_1 = require("sequelize");
15
+ const waait_1 = __importDefault(require("waait"));
16
+ const net_1 = require("@/net");
17
+ const generateGnosisTransaction = async (transactionData, safeContract) => {
18
+ console.log(transactionData);
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));
20
+ while (isExecuted == 1) {
21
+ transactionData.safeTxGas = ethers_1.BigNumber.from(String(transactionData.safeTxGas)).add(1).toString();
22
+ 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));
23
+ }
24
+ return transactionData;
25
+ };
26
+ class ProcessWithdrawEvents extends BaseTask_1.BaseTask {
27
+ constructor({ chainId }) {
28
+ super({
29
+ logger: new logger_1.default("InteropXGateway::ProcessWithdrawEvents"),
30
+ });
31
+ this.leadNodeOnly = true;
32
+ this.chainId = chainId;
33
+ }
34
+ async pollHandler() {
35
+ var _a;
36
+ const blockNumber = await this.provider.getBlockNumber();
37
+ const transaction = await db_1.Transaction.findOne({
38
+ where: {
39
+ status: 'pending',
40
+ sourceStatus: 'success',
41
+ targetStatus: 'uninitialised',
42
+ action: 'withdraw',
43
+ sourceCreatedAt: {
44
+ [sequelize_1.Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
45
+ },
46
+ targetDelayUntil: {
47
+ [sequelize_1.Op.or]: {
48
+ [sequelize_1.Op.is]: null,
49
+ [sequelize_1.Op.lt]: new Date(),
50
+ }
51
+ },
52
+ sourceBlockNumber: {
53
+ [sequelize_1.Op.lt]: blockNumber - 12,
54
+ },
55
+ sourceChainId: this.chainId,
56
+ }
57
+ });
58
+ if (!transaction) {
59
+ return;
60
+ }
61
+ console.log(`Processing transaction ${transaction.transactionHash}`);
62
+ transaction.targetStatus = 'pending';
63
+ await transaction.save();
64
+ // refresh event data?
65
+ const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(transaction.targetChainId));
66
+ const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
67
+ const safeAddress = constants_1.addresses[transaction.targetChainId].gnosisSafe;
68
+ const safeContract = (0, utils_1.getContract)(safeAddress, abi_1.default.gnosisSafe, targetWallet);
69
+ const ownersThreshold = await safeContract.getThreshold();
70
+ await (0, waait_1.default)(10000);
71
+ let gnosisTx = await generateGnosisTransaction({
72
+ baseGas: "0",
73
+ data: await (0, utils_1.buildDataForTransaction)(transaction),
74
+ gasPrice: "0",
75
+ gasToken: "0x0000000000000000000000000000000000000000",
76
+ nonce: '0',
77
+ operation: "1",
78
+ refundReceiver: "0x0000000000000000000000000000000000000000",
79
+ safeAddress: safeAddress,
80
+ safeTxGas: "79668",
81
+ to: constants_1.addresses[transaction.targetChainId].multisend,
82
+ value: "0",
83
+ }, safeContract);
84
+ const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
85
+ const ownerPeerIds = net_1.peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id);
86
+ console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
87
+ console.log(ownerPeerIds);
88
+ const signatures = await net_1.protocol.requestSignatures({
89
+ type: 'source',
90
+ transactionHash: transaction.transactionHash,
91
+ safeTxGas: gnosisTx.safeTxGas,
92
+ safeNonce: gnosisTx.nonce
93
+ }, ownerPeerIds);
94
+ const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x');
95
+ console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
96
+ if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
97
+ await transaction.save();
98
+ transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
99
+ transaction.targetStatus = 'uninitialised';
100
+ await transaction.save();
101
+ const errorMessage = (_a = signatures.find(s => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
102
+ throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
103
+ }
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
+ console.log({
107
+ from: targetWallet.address,
108
+ gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9).toString(),
109
+ to: safeAddress,
110
+ data: txData,
111
+ });
112
+ const txSent = await targetWallet.sendTransaction({
113
+ from: targetWallet.address,
114
+ gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9),
115
+ to: safeAddress,
116
+ data: txData,
117
+ });
118
+ const receipt = await txSent.wait();
119
+ const parsedLogs = [];
120
+ receipt.logs.forEach((log) => {
121
+ try {
122
+ parsedLogs.push(safeContract.interface.parseLog(log));
123
+ }
124
+ catch (e) { }
125
+ });
126
+ if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
127
+ console.log('ExecutionSuccess');
128
+ transaction.targetStatus = 'success';
129
+ transaction.status = 'success';
130
+ await transaction.save();
131
+ }
132
+ else {
133
+ console.log('ExecutionFailure');
134
+ transaction.targetStatus = 'failed';
135
+ transaction.status = 'failed';
136
+ await transaction.save();
137
+ }
138
+ }
139
+ async start() {
140
+ this.logger.info(`Starting execution watcher on interop chain`);
141
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
142
+ await super.start();
143
+ }
144
+ }
145
+ exports.default = ProcessWithdrawEvents;