@instadapp/interop-x 0.0.0-dev.7a02577 → 0.0.0-dev.7adf1b5

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 (65) hide show
  1. package/dist/package.json +9 -5
  2. package/dist/src/abi/interopBridgeToken.json +21 -9
  3. package/dist/src/abi/interopXGateway.json +11 -11
  4. package/dist/src/api/index.js +6 -3
  5. package/dist/src/config/index.js +11 -1
  6. package/dist/src/constants/addresses.js +1 -1
  7. package/dist/src/constants/itokens.js +1 -1
  8. package/dist/src/db/models/transaction.js +8 -0
  9. package/dist/src/gnosis/actions/deposit.js +48 -0
  10. package/dist/src/gnosis/actions/index.js +11 -0
  11. package/dist/src/gnosis/actions/withdraw.js +50 -0
  12. package/dist/src/gnosis/index.js +20 -0
  13. package/dist/src/index.js +69 -7
  14. package/dist/src/net/peer/index.js +8 -3
  15. package/dist/src/net/pool/index.js +32 -9
  16. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +6 -4
  17. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +28 -0
  18. package/dist/src/net/protocol/index.js +41 -1
  19. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  20. package/dist/src/tasks/BaseTask.js +12 -4
  21. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +162 -0
  22. package/dist/src/tasks/InteropBridge/SyncBurnEvents.js +71 -0
  23. package/dist/src/tasks/InteropBridge/SyncMintEvents.js +67 -0
  24. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +47 -23
  25. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +6 -7
  26. package/dist/src/tasks/InteropXGateway/SyncWithdrawtEvents.js +72 -0
  27. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
  28. package/dist/src/tasks/index.js +28 -0
  29. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +23 -11
  30. package/dist/src/typechain/factories/InteropXGateway__factory.js +14 -14
  31. package/dist/src/utils/index.js +57 -37
  32. package/package.json +9 -5
  33. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  34. package/src/abi/interopBridgeToken.json +21 -9
  35. package/src/abi/interopXGateway.json +11 -11
  36. package/src/api/index.ts +5 -2
  37. package/src/config/index.ts +11 -1
  38. package/src/constants/addresses.ts +1 -1
  39. package/src/constants/itokens.ts +1 -1
  40. package/src/db/models/transaction.ts +10 -0
  41. package/src/gnosis/actions/deposit.ts +63 -0
  42. package/src/gnosis/actions/index.ts +7 -0
  43. package/src/gnosis/actions/withdraw.ts +67 -0
  44. package/src/gnosis/index.ts +19 -0
  45. package/src/index.ts +90 -9
  46. package/src/net/peer/index.ts +9 -7
  47. package/src/net/pool/index.ts +41 -11
  48. package/src/net/protocol/dial/SignatureDialProtocol.ts +8 -6
  49. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +31 -0
  50. package/src/net/protocol/index.ts +57 -1
  51. package/src/tasks/AutoUpdateTask.ts +82 -0
  52. package/src/tasks/BaseTask.ts +14 -4
  53. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +249 -0
  54. package/src/tasks/InteropBridge/SyncBurnEvents.ts +119 -0
  55. package/src/tasks/InteropBridge/SyncMintEvents.ts +99 -0
  56. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +60 -32
  57. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +8 -10
  58. package/src/tasks/InteropXGateway/SyncWithdrawtEvents.ts +105 -0
  59. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
  60. package/src/tasks/index.ts +43 -2
  61. package/src/typechain/InteropBridgeToken.ts +23 -17
  62. package/src/typechain/InteropXGateway.ts +13 -13
  63. package/src/typechain/factories/InteropBridgeToken__factory.ts +23 -11
  64. package/src/typechain/factories/InteropXGateway__factory.ts +14 -14
  65. package/src/utils/index.ts +66 -53
@@ -14,7 +14,9 @@ const config_1 = __importDefault(require("@/config"));
14
14
  const sequelize_1 = require("sequelize");
15
15
  const waait_1 = __importDefault(require("waait"));
16
16
  const net_1 = require("@/net");
17
+ const gnosis_1 = require("@/gnosis");
17
18
  const generateGnosisTransaction = async (transactionData, safeContract) => {
19
+ console.log(transactionData);
18
20
  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
21
  while (isExecuted == 1) {
20
22
  transactionData.safeTxGas = ethers_1.BigNumber.from(String(transactionData.safeTxGas)).add(1).toString();
@@ -37,30 +39,52 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
37
39
  where: {
38
40
  status: 'pending',
39
41
  sourceStatus: 'success',
42
+ targetStatus: 'uninitialised',
40
43
  action: 'deposit',
41
44
  sourceCreatedAt: {
42
45
  [sequelize_1.Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
43
46
  },
47
+ targetDelayUntil: {
48
+ [sequelize_1.Op.or]: {
49
+ [sequelize_1.Op.is]: null,
50
+ [sequelize_1.Op.lt]: new Date(),
51
+ }
52
+ },
44
53
  sourceBlockNumber: {
45
54
  [sequelize_1.Op.lt]: blockNumber - 12,
46
- }
55
+ },
56
+ sourceChainId: this.chainId,
47
57
  }
48
58
  });
49
59
  if (!transaction) {
50
60
  return;
51
61
  }
52
- transaction.sourceStatus = 'pending';
62
+ console.log(`Processing transaction ${transaction.transactionHash}`);
63
+ transaction.targetStatus = 'pending';
53
64
  await transaction.save();
54
65
  // refresh event data?
55
66
  const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(transaction.targetChainId));
56
67
  const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
57
68
  const safeAddress = constants_1.addresses[transaction.targetChainId].gnosisSafe;
58
- const safeContract = new ethers_1.ethers.Contract(safeAddress, abi_1.default.gnosisSafe, targetWallet);
69
+ const safeContract = (0, utils_1.getContract)(safeAddress, abi_1.default.gnosisSafe, targetWallet);
59
70
  const ownersThreshold = await safeContract.getThreshold();
60
71
  await (0, waait_1.default)(10000);
72
+ let data, logs = [];
73
+ try {
74
+ ({ data, logs } = await (0, gnosis_1.buildGnosisAction)(transaction));
75
+ }
76
+ catch (error) {
77
+ console.log(error);
78
+ transaction.targetStatus = 'failed';
79
+ transaction.targetErrors = [error.message];
80
+ transaction.status = 'failed';
81
+ await transaction.save();
82
+ net_1.protocol.sendTransaction(transaction);
83
+ return;
84
+ }
61
85
  let gnosisTx = await generateGnosisTransaction({
62
86
  baseGas: "0",
63
- data: (0, utils_1.buildDataForTransaction)(transaction),
87
+ data,
64
88
  gasPrice: "0",
65
89
  gasToken: "0x0000000000000000000000000000000000000000",
66
90
  nonce: '0',
@@ -74,6 +98,7 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
74
98
  const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
75
99
  const ownerPeerIds = net_1.peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id);
76
100
  console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
101
+ console.log(ownerPeerIds);
77
102
  const signatures = await net_1.protocol.requestSignatures({
78
103
  type: 'source',
79
104
  transactionHash: transaction.transactionHash,
@@ -84,33 +109,23 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
84
109
  console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
85
110
  if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
86
111
  await transaction.save();
87
- transaction.sourceDelayUntil = new Date(Date.now() + 30 * 1000);
88
- transaction.sourceStatus = 'pending';
112
+ transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
113
+ transaction.targetStatus = 'uninitialised';
89
114
  await transaction.save();
90
115
  const errorMessage = (_a = signatures.find(s => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
91
116
  throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
92
117
  }
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
118
  console.log(`Executing transaction for execution ${transaction.transactionHash}`);
119
+ 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
120
  console.log({
107
- execTransactionParams
121
+ from: targetWallet.address,
122
+ gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9).toString(),
123
+ to: safeAddress,
124
+ data: txData,
108
125
  });
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
126
  const txSent = await targetWallet.sendTransaction({
111
127
  from: targetWallet.address,
112
128
  gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9),
113
- gasLimit: ethers_1.BigNumber.from(6000000),
114
129
  to: safeAddress,
115
130
  data: txData,
116
131
  });
@@ -124,16 +139,25 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
124
139
  });
125
140
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
126
141
  console.log('ExecutionSuccess');
142
+ transaction.targetStatus = 'success';
143
+ transaction.targetTransactionHash = txSent.hash;
144
+ transaction.targetLogs = logs;
145
+ transaction.status = 'success';
146
+ await transaction.save();
127
147
  }
128
148
  else {
129
149
  console.log('ExecutionFailure');
150
+ transaction.targetStatus = 'failed';
151
+ transaction.targetTransactionHash = txSent.hash;
152
+ transaction.status = 'failed';
153
+ await transaction.save();
130
154
  }
155
+ net_1.protocol.sendTransaction(transaction);
131
156
  }
132
157
  async start() {
133
- this.logger.info(`Starting execution watcher on interop chain`);
134
158
  this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
135
159
  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));
160
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
137
161
  await super.start();
138
162
  }
139
163
  }
@@ -31,8 +31,8 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
31
31
  const uniqueIdentifier = {
32
32
  action: 'deposit',
33
33
  submitTransactionHash: event.transactionHash,
34
- sourceChainId: sourceChainId.toNumber(),
35
- targetChainId: targetChainId.toNumber(),
34
+ sourceChainId: sourceChainId,
35
+ targetChainId: targetChainId,
36
36
  };
37
37
  if (await db_1.Transaction.findOne({ where: uniqueIdentifier })) {
38
38
  continue;
@@ -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);
@@ -65,10 +65,9 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
65
65
  this.logger.info(`${processedEvents} events processed`);
66
66
  }
67
67
  async start() {
68
- this.logger.info(`Starting execution watcher on interop chain`);
69
68
  this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
70
69
  this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
71
- this.contract = new ethers_1.ethers.Contract(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
70
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
72
71
  await super.start();
73
72
  }
74
73
  }
@@ -0,0 +1,72 @@
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
+ class SyncWithdrawEvents extends BaseTask_1.BaseTask {
15
+ constructor({ chainId }) {
16
+ super({
17
+ logger: new logger_1.default("InteropXGateway::SyncWithdrawEvents"),
18
+ });
19
+ this.chainId = chainId;
20
+ }
21
+ async pollHandler() {
22
+ const currentBlock = await this.provider.getBlockNumber();
23
+ const events = await this.contract.queryFilter(this.contract.filters.LogGatewayWithdraw(), currentBlock - 500, currentBlock);
24
+ let processedEvents = 0;
25
+ for (const event of events) {
26
+ try {
27
+ if (!event.args) {
28
+ continue;
29
+ }
30
+ const { user, token, amount, sourceChainId, targetChainId, transactionHash } = event.args;
31
+ const uniqueIdentifier = {
32
+ action: 'withdraw',
33
+ submitTransactionHash: transactionHash,
34
+ sourceChainId: sourceChainId,
35
+ targetChainId: targetChainId,
36
+ targetEvent: null
37
+ };
38
+ const transaction = await db_1.Transaction.findOne({ where: uniqueIdentifier });
39
+ if (!transaction) {
40
+ return;
41
+ }
42
+ const tx = await event.getTransaction();
43
+ transaction.targetStatus = 'success';
44
+ transaction.targetErrors = [];
45
+ transaction.targetTransactionHash = tx.hash;
46
+ transaction.targetEvent = {
47
+ user,
48
+ token,
49
+ amount: amount.toString(),
50
+ sourceChainId,
51
+ targetChainId,
52
+ transactionHash,
53
+ };
54
+ transaction.status = 'success';
55
+ await transaction.save();
56
+ this.logger.info(`Witdraw confirmation received: ${transaction.transactionHash} `);
57
+ }
58
+ catch (error) {
59
+ this.logger.error(error);
60
+ }
61
+ }
62
+ if (processedEvents > 0)
63
+ this.logger.info(`${processedEvents} events processed`);
64
+ }
65
+ async start() {
66
+ this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
67
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
68
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
69
+ await super.start();
70
+ }
71
+ }
72
+ exports.default = SyncWithdrawEvents;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("../BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const net_1 = require("@/net");
9
+ const db_1 = require("@/db");
10
+ const sequelize_1 = require("sequelize");
11
+ class SyncTransactionStatusTask extends BaseTask_1.BaseTask {
12
+ constructor() {
13
+ super({
14
+ logger: new logger_1.default("SyncTransactionStatusTask"),
15
+ });
16
+ this.pollIntervalMs = 60 * 1000;
17
+ this.exceptLeadNode = true;
18
+ }
19
+ async pollHandler() {
20
+ // if transaction is pending for more than 1 hour, check lead node for status
21
+ const leadNode = net_1.peerPool.getLeadPeer();
22
+ if (!leadNode) {
23
+ return;
24
+ }
25
+ const transaction = await db_1.Transaction.findOne({
26
+ where: {
27
+ status: 'pending',
28
+ sourceCreatedAt: {
29
+ [sequelize_1.Op.gte]: new Date(Date.now() - 60 * 60 * 1000),
30
+ },
31
+ }
32
+ });
33
+ if (!transaction) {
34
+ return;
35
+ }
36
+ this.logger.info(`Requesting transaction status for ${transaction.transactionHash}`);
37
+ const transactionStatus = await net_1.protocol.requestTransactionStatus(transaction.transactionHash, leadNode.id);
38
+ if (!transactionStatus) {
39
+ return;
40
+ }
41
+ this.logger.info(`Received transaction status for ${transaction.transactionHash}`);
42
+ transaction.sourceStatus = transactionStatus.sourceStatus;
43
+ transaction.sourceTransactionHash = transactionStatus.sourceTransactionHash;
44
+ transaction.sourceErrors = transactionStatus.sourceErrors;
45
+ transaction.targetStatus = transactionStatus.targetStatus;
46
+ transaction.targetTransactionHash = transactionStatus.targetTransactionHash;
47
+ transaction.targetErrors = transactionStatus.targetErrors;
48
+ transaction.status = transactionStatus.status;
49
+ await transaction.save();
50
+ this.logger.info(`Updated transaction status for ${transaction.transactionHash}`);
51
+ }
52
+ }
53
+ exports.default = SyncTransactionStatusTask;
@@ -4,12 +4,40 @@ 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 SyncWithdrawtEvents_1 = __importDefault(require("./InteropXGateway/SyncWithdrawtEvents"));
10
+ const ProcessWithdrawEvents_1 = __importDefault(require("./InteropBridge/ProcessWithdrawEvents"));
11
+ const SyncBurnEvents_1 = __importDefault(require("./InteropBridge/SyncBurnEvents"));
12
+ const SyncMintEvents_1 = __importDefault(require("./InteropBridge/SyncMintEvents"));
13
+ const SyncTransactionStatusTask_1 = __importDefault(require("./Transactions/SyncTransactionStatusTask"));
14
+ const AutoUpdateTask_1 = __importDefault(require("./AutoUpdateTask"));
8
15
  class Tasks {
9
16
  constructor() {
10
17
  this.tasks = [
18
+ new SyncTransactionStatusTask_1.default(),
19
+ new AutoUpdateTask_1.default(),
20
+ // InteropXGateway
11
21
  new SyncDepositEvents_1.default({
12
22
  chainId: 43114
23
+ }),
24
+ new ProcessDepositEvents_1.default({
25
+ chainId: 43114
26
+ }),
27
+ new SyncWithdrawtEvents_1.default({
28
+ chainId: 43114
29
+ }),
30
+ // InteropBridge
31
+ new ProcessWithdrawEvents_1.default({
32
+ chainId: 137,
33
+ }),
34
+ new SyncMintEvents_1.default({
35
+ chainId: 137,
36
+ itokenAddress: '0x62c0045f3277e7067cacad3c8038eeabb1bd92d1',
37
+ }),
38
+ new SyncBurnEvents_1.default({
39
+ chainId: 137,
40
+ itokenAddress: '0x62c0045f3277e7067cacad3c8038eeabb1bd92d1',
13
41
  })
14
42
  ];
15
43
  }
@@ -57,11 +57,17 @@ const _abi = [
57
57
  name: "amount",
58
58
  type: "uint256",
59
59
  },
60
+ {
61
+ indexed: false,
62
+ internalType: "uint32",
63
+ name: "sourceChainId",
64
+ type: "uint32",
65
+ },
60
66
  {
61
67
  indexed: true,
62
- internalType: "uint256",
63
- name: "chainId",
64
- type: "uint256",
68
+ internalType: "uint32",
69
+ name: "targetChainId",
70
+ type: "uint32",
65
71
  },
66
72
  ],
67
73
  name: "Burn",
@@ -84,14 +90,20 @@ const _abi = [
84
90
  },
85
91
  {
86
92
  indexed: true,
87
- internalType: "uint256",
88
- name: "chainId",
89
- type: "uint256",
93
+ internalType: "uint32",
94
+ name: "sourceChainId",
95
+ type: "uint32",
96
+ },
97
+ {
98
+ indexed: false,
99
+ internalType: "uint32",
100
+ name: "targetChainId",
101
+ type: "uint32",
90
102
  },
91
103
  {
92
104
  indexed: true,
93
105
  internalType: "bytes32",
94
- name: "transactionHash",
106
+ name: "submitTransactionHash",
95
107
  type: "bytes32",
96
108
  },
97
109
  ],
@@ -222,9 +234,9 @@ const _abi = [
222
234
  type: "uint256",
223
235
  },
224
236
  {
225
- internalType: "uint256",
237
+ internalType: "uint32",
226
238
  name: "chainId",
227
- type: "uint256",
239
+ type: "uint32",
228
240
  },
229
241
  ],
230
242
  name: "burn",
@@ -306,9 +318,9 @@ const _abi = [
306
318
  type: "uint256",
307
319
  },
308
320
  {
309
- internalType: "uint256",
321
+ internalType: "uint32",
310
322
  name: "chainId",
311
- type: "uint256",
323
+ type: "uint32",
312
324
  },
313
325
  {
314
326
  internalType: "bytes32",
@@ -46,15 +46,15 @@ const _abi = [
46
46
  },
47
47
  {
48
48
  indexed: false,
49
- internalType: "uint256",
49
+ internalType: "uint32",
50
50
  name: "sourceChainId",
51
- type: "uint256",
51
+ type: "uint32",
52
52
  },
53
53
  {
54
54
  indexed: true,
55
- internalType: "uint256",
55
+ internalType: "uint32",
56
56
  name: "targetChainId",
57
- type: "uint256",
57
+ type: "uint32",
58
58
  },
59
59
  ],
60
60
  name: "LogGatewayDeposit",
@@ -83,15 +83,15 @@ const _abi = [
83
83
  },
84
84
  {
85
85
  indexed: true,
86
- internalType: "uint256",
86
+ internalType: "uint32",
87
87
  name: "sourceChainId",
88
- type: "uint256",
88
+ type: "uint32",
89
89
  },
90
90
  {
91
91
  indexed: false,
92
- internalType: "uint256",
92
+ internalType: "uint32",
93
93
  name: "targetChainId",
94
- type: "uint256",
94
+ type: "uint32",
95
95
  },
96
96
  {
97
97
  indexed: true,
@@ -148,9 +148,9 @@ const _abi = [
148
148
  type: "uint256",
149
149
  },
150
150
  {
151
- internalType: "uint256",
151
+ internalType: "uint32",
152
152
  name: "chainId_",
153
- type: "uint256",
153
+ type: "uint32",
154
154
  },
155
155
  ],
156
156
  name: "deposit",
@@ -176,9 +176,9 @@ const _abi = [
176
176
  type: "uint256",
177
177
  },
178
178
  {
179
- internalType: "uint256",
179
+ internalType: "uint32",
180
180
  name: "chainId_",
181
- type: "uint256",
181
+ type: "uint32",
182
182
  },
183
183
  ],
184
184
  name: "depositFor",
@@ -224,9 +224,9 @@ const _abi = [
224
224
  type: "address",
225
225
  },
226
226
  {
227
- internalType: "uint256",
227
+ internalType: "uint32",
228
228
  name: "chainId_",
229
- type: "uint256",
229
+ type: "uint32",
230
230
  },
231
231
  {
232
232
  internalType: "bytes32",
@@ -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.generateInteropTransactionHash = exports.asyncCallWithTimeout = exports.buildSignatureBytes = exports.getRpcProviderUrl = exports.signGnosisSafeTx = exports.short = exports.shortenHash = exports.http = void 0;
7
7
  /**
8
8
  * @module util
9
9
  */
@@ -11,11 +11,18 @@ const axios_1 = __importDefault(require("axios"));
11
11
  const axios_retry_1 = __importDefault(require("axios-retry"));
12
12
  const constants_1 = require("@/constants");
13
13
  const ethers_1 = require("ethers");
14
- const ethers_multisend_1 = require("ethers-multisend");
15
- const config_1 = __importDefault(require("@/config"));
16
- const abi_1 = __importDefault(require("@/abi"));
17
14
  exports.http = axios_1.default.create();
18
15
  (0, axios_retry_1.default)(exports.http, { retries: 3, retryDelay: axios_retry_1.default.exponentialDelay });
16
+ function shortenHash(hash, length = 4) {
17
+ if (!hash)
18
+ return;
19
+ if (hash.length < 12)
20
+ return hash;
21
+ const beginningChars = hash.startsWith("0x") ? length + 2 : length;
22
+ const shortened = hash.substr(0, beginningChars) + "…" + hash.substr(-length);
23
+ return shortened;
24
+ }
25
+ exports.shortenHash = shortenHash;
19
26
  function short(buffer) {
20
27
  return buffer.toString('hex').slice(0, 8) + '...';
21
28
  }
@@ -59,11 +66,11 @@ exports.signGnosisSafeTx = signGnosisSafeTx;
59
66
  const getRpcProviderUrl = (chainId) => {
60
67
  switch (chainId) {
61
68
  case 1:
62
- return 'https://rpc.instadapp.io/mainnet';
69
+ return 'https://rpc.ankr.com/eth';
63
70
  case 137:
64
- return 'https://rpc.instadapp.io/polygon';
71
+ return 'https://rpc.ankr.com/polygon';
65
72
  case 43114:
66
- return 'https://rpc.instadapp.io/avalanche';
73
+ return 'https://rpc.ankr.com/avalanche';
67
74
  default:
68
75
  throw new Error(`Unknown chainId: ${chainId}`);
69
76
  }
@@ -102,36 +109,49 @@ const generateInteropTransactionHash = (data) => {
102
109
  ]);
103
110
  };
104
111
  exports.generateInteropTransactionHash = generateInteropTransactionHash;
105
- const buildDataForTransaction = async (transaction, type) => {
106
- type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
107
- const transactions = [];
108
- if (transaction.action != 'deposit') {
109
- throw new Error('Invalid action');
110
- }
111
- if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
112
- throw Error('Cannot build data for pending deposit transaction');
113
- }
114
- if (!transaction.submitEvent) {
115
- throw Error('Cannot build data for transaction without submitEvent');
112
+ function getContract(address, contractInterface, signerOrProvider) {
113
+ if (!ethers_1.ethers.utils.getAddress(address) || address === ethers_1.ethers.constants.AddressZero) {
114
+ throw Error(`Invalid 'address' parameter '${address}'.`);
116
115
  }
117
- const token = constants_1.tokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === transaction.submitEvent.token.toLowerCase());
118
- if (!token) {
119
- throw Error('Cannot build data for transaction without token');
116
+ const contract = new ethers_1.ethers.Contract(address, contractInterface, signerOrProvider);
117
+ // Make sure the contract properties is writable
118
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
119
+ if (!desc || desc.writable !== true) {
120
+ return contract;
120
121
  }
121
- const itoken = constants_1.itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
122
- if (!itoken) {
123
- throw Error('Cannot build data for transaction without itoken');
124
- }
125
- const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, exports.getRpcProviderUrl)(transaction.targetChainId));
126
- 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);
129
- transactions.push({
130
- to: itoken.address,
131
- data: data,
132
- value: '0',
133
- operation: ethers_multisend_1.OperationType.Call,
122
+ return new Proxy(contract, {
123
+ get(target, prop, receiver) {
124
+ const value = Reflect.get(target, prop, receiver);
125
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
126
+ return async (...args) => {
127
+ try {
128
+ return await value.bind(contract)(...args);
129
+ }
130
+ catch (error) {
131
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`);
132
+ }
133
+ };
134
+ }
135
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
136
+ const parentProp = String(prop);
137
+ return new Proxy(value, {
138
+ get(target, prop, receiver) {
139
+ const value = Reflect.get(target, prop, receiver);
140
+ if (typeof value === 'function') {
141
+ return async (...args) => {
142
+ try {
143
+ return await value.bind(contract)(...args);
144
+ }
145
+ catch (error) {
146
+ throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`);
147
+ }
148
+ };
149
+ }
150
+ }
151
+ });
152
+ }
153
+ return value;
154
+ },
134
155
  });
135
- return (0, ethers_multisend_1.encodeMulti)(transactions).data;
136
- };
137
- exports.buildDataForTransaction = buildDataForTransaction;
156
+ }
157
+ exports.getContract = getContract;