@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
@@ -3,7 +3,7 @@ import Logger from '@/logger';
3
3
  import { ethers } from "ethers";
4
4
  import abi from "@/abi";
5
5
  import { Transaction } from "@/db";
6
- import { generateInteropTransactionHash, getRpcProviderUrl } from "@/utils";
6
+ import { generateInteropTransactionHash, getContract, getRpcProviderUrl } from "@/utils";
7
7
  import { addresses } from "@/constants";
8
8
  import { ChainId } from "@/types";
9
9
  import config from "@/config";
@@ -77,7 +77,7 @@ class SyncDepositEvents extends BaseTask {
77
77
  sourceChainId: sourceChainId.toString(),
78
78
  targetChainId: targetChainId.toString(),
79
79
  token: token,
80
- ammout: amount.toString(),
80
+ amount: amount.toString(),
81
81
  vnonce: vnonce.toString(),
82
82
  },
83
83
 
@@ -86,14 +86,14 @@ class SyncDepositEvents extends BaseTask {
86
86
  sourceChainId: sourceChainId.toString(),
87
87
  targetChainId: targetChainId.toString(),
88
88
  token: token,
89
- ammout: amount.toString(),
89
+ amount: amount.toString(),
90
90
  vnonce: vnonce.toString(),
91
91
  },
92
92
  status: "pending",
93
93
  })
94
94
 
95
95
  this.logger.info(
96
- `Execution queued: ${event.transactionHash} ${event.blockNumber}`
96
+ `Deposit queued: ${event.transactionHash} ${event.blockNumber}`
97
97
  );
98
98
  } catch (error) {
99
99
  this.logger.error(error);
@@ -113,11 +113,11 @@ class SyncDepositEvents extends BaseTask {
113
113
  getRpcProviderUrl(this.chainId)
114
114
  );
115
115
 
116
- this.contract = new ethers.Contract(
116
+ this.contract = getContract<InteropXGateway>(
117
117
  this.contractAddress,
118
118
  abi.interopXGateway,
119
119
  new ethers.Wallet(config.privateKey!, this.provider)
120
- ) as InteropXGateway;
120
+ );
121
121
 
122
122
  await super.start()
123
123
  }
@@ -1,11 +1,31 @@
1
1
  import { BaseTask } from "./BaseTask";
2
- import SyncInteropXGatewayDepositEvents from "./InteropXGateway/SyncDepositEvents";
2
+ import InteropXGatewayProcessDepositEvents from "./InteropXGateway/ProcessDepositEvents";
3
+ import InteropXGatewaySyncDepositEvents from "./InteropXGateway/SyncDepositEvents";
4
+
5
+ import InteropBridgeSyncWithdrawEvents from "./InteropBridge/SyncWithdrawEvents";
6
+ import InteropBridgeProcessWithdrawEvents from "./InteropBridge/ProcessWithdrawEvents";
7
+ import AutoUpdateTask from "./AutoUpdateTask";
3
8
 
4
9
  export class Tasks {
5
10
 
6
11
  tasks: BaseTask[] = [
7
- new SyncInteropXGatewayDepositEvents({
12
+ new AutoUpdateTask(),
13
+
14
+ new InteropXGatewaySyncDepositEvents({
15
+ chainId: 43114
16
+ }),
17
+
18
+ new InteropXGatewayProcessDepositEvents({
8
19
  chainId: 43114
20
+ }),
21
+
22
+ new InteropBridgeSyncWithdrawEvents({
23
+ chainId: 137,
24
+ itokenAddress: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
25
+ }),
26
+
27
+ new InteropBridgeProcessWithdrawEvents({
28
+ chainId: 137,
9
29
  })
10
30
  ];
11
31
 
@@ -10,7 +10,7 @@ import { encodeMulti, MetaTransaction, OperationType } from 'ethers-multisend';
10
10
  import { Transaction } from '@/db';
11
11
  import config from '@/config';
12
12
  import abi from '@/abi';
13
- import { InteropBridgeToken } from '@/typechain';
13
+ import { InteropBridgeToken, InteropXGateway } from '@/typechain';
14
14
 
15
15
  export const http = axios.create();
16
16
 
@@ -136,10 +136,21 @@ export const generateInteropTransactionHash = (data: { action: string, submitTra
136
136
  export const buildDataForTransaction = async (transaction: Transaction, type?: 'source' | 'target') => {
137
137
  type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
138
138
 
139
+ switch (transaction.action) {
140
+ case "deposit":
141
+ return await buildDepositDataForTransaction(transaction, type);
142
+ case "withdraw":
143
+ return await buildWithdrawDataForTransaction(transaction, type);
144
+ default:
145
+ throw new Error(`Unknown action: ${transaction.action}`);
146
+ }
147
+ }
148
+
149
+ export const buildDepositDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
139
150
  const transactions: MetaTransaction[] = [];
140
151
 
141
- if(transaction.action != 'deposit') {
142
- throw new Error('Invalid action');
152
+ if (transaction.action !== 'deposit') {
153
+ throw new Error(`Invalid action: ${transaction.action}`)
143
154
  }
144
155
 
145
156
  if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
@@ -157,7 +168,7 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
157
168
  throw Error('Cannot build data for transaction without token');
158
169
  }
159
170
 
160
- const itoken = itokens[transaction.targetChainId].find(itoken => token.symbol.toLowerCase() === token.symbol.toLowerCase());
171
+ const itoken = itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
161
172
 
162
173
  if (!itoken) {
163
174
  throw Error('Cannot build data for transaction without itoken');
@@ -165,13 +176,13 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
165
176
 
166
177
  const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
167
178
  const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
168
- const interopBridgeContract = new ethers.Contract(itoken.address, abi.interopBridgeToken, targetWallet) as InteropBridgeToken;
179
+ const interopBridgeContract = getContract<InteropBridgeToken>(itoken.address, abi.interopBridgeToken, targetWallet);
169
180
 
170
181
  const { data } = await interopBridgeContract.populateTransaction.mint(
171
- transaction.submitEvent.to,
172
- transaction.submitEvent.amount,
173
- transaction.sourceChainId,
174
- transaction.sourceTransactionHash,
182
+ transaction.submitEvent.user,
183
+ ethers.BigNumber.from(transaction.submitEvent.amount.toString()),
184
+ ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()),
185
+ transaction.submitTransactionHash,
175
186
  );
176
187
 
177
188
  transactions.push({
@@ -183,3 +194,114 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
183
194
 
184
195
  return encodeMulti(transactions).data
185
196
  }
197
+
198
+ export const buildWithdrawDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
199
+ const transactions: MetaTransaction[] = [];
200
+
201
+ if (transaction.action !== 'withdraw') {
202
+ throw new Error(`Invalid action: ${transaction.action}`)
203
+ }
204
+
205
+ if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
206
+ throw Error('Cannot build data for pending withdraw transaction');
207
+ }
208
+
209
+ if (!transaction.submitEvent) {
210
+ throw Error('Cannot build data for transaction without submitEvent');
211
+ }
212
+
213
+ const { to, amount, chainId, itoken: itokenAddress } = transaction.submitEvent;
214
+
215
+ const itoken = itokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === itokenAddress.toLowerCase());
216
+
217
+ if (!itoken) {
218
+ throw Error('Cannot build data for transaction without itoken');
219
+ }
220
+
221
+ const token = tokens[chainId].find(t => t.symbol.toLowerCase() === itoken.symbol.toLowerCase());
222
+
223
+ if (!token) {
224
+ throw Error('Cannot build data for transaction without token');
225
+ }
226
+
227
+ const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
228
+ const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
229
+ const gatewayAddress = addresses[chainId].interopXGateway;
230
+ const interopBridgeContract = getContract<InteropXGateway>(gatewayAddress, abi.interopXGateway, targetWallet);
231
+
232
+ const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(
233
+ ethers.BigNumber.from(amount.toString()),
234
+ to,
235
+ token.address,
236
+ ethers.BigNumber.from(transaction.sourceChainId.toString()),
237
+ transaction.submitTransactionHash,
238
+ );
239
+
240
+ transactions.push({
241
+ to: gatewayAddress,
242
+ data: data!,
243
+ value: '0',
244
+ operation: OperationType.Call,
245
+ });
246
+
247
+ return encodeMulti(transactions).data
248
+ }
249
+
250
+
251
+ export function getContract<TContract extends ethers.Contract>(address: string, contractInterface: ethers.ContractInterface | any, signerOrProvider?: ethers.Signer | ethers.providers.Provider) {
252
+ if (!ethers.utils.getAddress(address) || address === ethers.constants.AddressZero) {
253
+ throw Error(`Invalid 'address' parameter '${address}'.`)
254
+ }
255
+
256
+ const contract = new ethers.Contract(
257
+ address,
258
+ contractInterface,
259
+ signerOrProvider
260
+ ) as TContract
261
+
262
+ // Make sure the contract properties is writable
263
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
264
+
265
+ if (!desc || desc.writable !== true) {
266
+ return contract
267
+ }
268
+
269
+ return new Proxy(contract, {
270
+ get(target, prop, receiver) {
271
+ const value = Reflect.get(target, prop, receiver);
272
+
273
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
274
+ return async (...args: any[]) => {
275
+ try {
276
+ return await value.bind(contract)(...args);
277
+ } catch (error) {
278
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
279
+ }
280
+ }
281
+ }
282
+
283
+
284
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
285
+ const parentProp = String(prop);
286
+
287
+ return new Proxy(value, {
288
+ get(target, prop, receiver) {
289
+ const value = Reflect.get(target, prop, receiver);
290
+
291
+ if (typeof value === 'function') {
292
+ return async (...args: any[]) => {
293
+ try {
294
+ return await value.bind(contract)(...args);
295
+ } catch (error) {
296
+ throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
297
+ }
298
+ }
299
+ }
300
+ }
301
+ })
302
+ }
303
+
304
+ return value;
305
+ },
306
+ });
307
+ }