@instadapp/interop-x 0.0.0-dev.a168c79 → 0.0.0-dev.a775e30

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 (40) hide show
  1. package/bin/interop-x +1 -1
  2. package/dist/package.json +9 -5
  3. package/dist/src/api/index.js +3 -3
  4. package/dist/src/config/index.js +11 -1
  5. package/dist/src/constants/itokens.js +1 -1
  6. package/dist/src/index.js +69 -7
  7. package/dist/src/net/peer/index.js +8 -3
  8. package/dist/src/net/pool/index.js +32 -9
  9. package/dist/src/net/protocol/dial/SignatureDialProtocol.1.js +28 -0
  10. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +11 -4
  11. package/dist/src/net/protocol/index.js +41 -1
  12. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  13. package/dist/src/tasks/BaseTask.js +12 -4
  14. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +146 -0
  15. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +69 -0
  16. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +32 -23
  17. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +4 -5
  18. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
  19. package/dist/src/tasks/index.js +17 -0
  20. package/dist/src/utils/index.js +110 -9
  21. package/package.json +9 -5
  22. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  23. package/src/api/index.ts +2 -2
  24. package/src/config/index.ts +11 -1
  25. package/src/constants/itokens.ts +1 -1
  26. package/src/index.ts +90 -9
  27. package/src/net/peer/index.ts +9 -7
  28. package/src/net/pool/index.ts +41 -11
  29. package/src/net/protocol/dial/SignatureDialProtocol.1.ts +31 -0
  30. package/src/net/protocol/dial/SignatureDialProtocol.ts +12 -4
  31. package/src/net/protocol/index.ts +57 -1
  32. package/src/tasks/AutoUpdateTask.ts +82 -0
  33. package/src/tasks/BaseTask.ts +14 -4
  34. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +231 -0
  35. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +119 -0
  36. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +44 -33
  37. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +6 -8
  38. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
  39. package/src/tasks/index.ts +24 -2
  40. package/src/utils/index.ts +145 -11
@@ -10,13 +10,25 @@ 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
 
17
17
  axiosRetry(http, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
18
18
 
19
19
 
20
+ export function shortenHash(hash: string, length: number = 4) {
21
+ if (!hash) return;
22
+
23
+ if (hash.length < 12) return hash;
24
+
25
+ const beginningChars = hash.startsWith("0x") ? length + 2 : length;
26
+
27
+ const shortened = hash.substr(0, beginningChars) + "…" + hash.substr(-length);
28
+
29
+ return shortened;
30
+ }
31
+
20
32
  export function short(buffer: Buffer): string {
21
33
  return buffer.toString('hex').slice(0, 8) + '...'
22
34
  }
@@ -76,11 +88,11 @@ export const signGnosisSafeTx = async ({
76
88
  export const getRpcProviderUrl = (chainId: ChainId) => {
77
89
  switch (chainId) {
78
90
  case 1:
79
- return 'https://rpc.instadapp.io/mainnet';
91
+ return 'https://rpc.ankr.com/eth';
80
92
  case 137:
81
- return 'https://rpc.instadapp.io/polygon';
93
+ return 'https://rpc.ankr.com/polygon';
82
94
  case 43114:
83
- return 'https://rpc.instadapp.io/avalanche';
95
+ return 'https://rpc.ankr.com/avalanche';
84
96
  default:
85
97
  throw new Error(`Unknown chainId: ${chainId}`);
86
98
  }
@@ -136,10 +148,21 @@ export const generateInteropTransactionHash = (data: { action: string, submitTra
136
148
  export const buildDataForTransaction = async (transaction: Transaction, type?: 'source' | 'target') => {
137
149
  type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
138
150
 
151
+ switch (transaction.action) {
152
+ case "deposit":
153
+ return await buildDepositDataForTransaction(transaction, type);
154
+ case "withdraw":
155
+ return await buildWithdrawDataForTransaction(transaction, type);
156
+ default:
157
+ throw new Error(`Unknown action: ${transaction.action}`);
158
+ }
159
+ }
160
+
161
+ export const buildDepositDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
139
162
  const transactions: MetaTransaction[] = [];
140
163
 
141
- if(transaction.action != 'deposit') {
142
- throw new Error('Invalid action');
164
+ if (transaction.action !== 'deposit') {
165
+ throw new Error(`Invalid action: ${transaction.action}`)
143
166
  }
144
167
 
145
168
  if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
@@ -165,13 +188,13 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
165
188
 
166
189
  const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
167
190
  const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
168
- const interopBridgeContract = new ethers.Contract(itoken.address, abi.interopBridgeToken, targetWallet) as InteropBridgeToken;
191
+ const interopBridgeContract = getContract<InteropBridgeToken>(itoken.address, abi.interopBridgeToken, targetWallet);
169
192
 
170
193
  const { data } = await interopBridgeContract.populateTransaction.mint(
171
- transaction.submitEvent.to,
172
- transaction.submitEvent.amount,
173
- transaction.sourceChainId,
174
- transaction.sourceTransactionHash,
194
+ transaction.submitEvent.user,
195
+ ethers.BigNumber.from(transaction.submitEvent.amount.toString()),
196
+ ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()),
197
+ transaction.submitTransactionHash,
175
198
  );
176
199
 
177
200
  transactions.push({
@@ -183,3 +206,114 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
183
206
 
184
207
  return encodeMulti(transactions).data
185
208
  }
209
+
210
+ export const buildWithdrawDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
211
+ const transactions: MetaTransaction[] = [];
212
+
213
+ if (transaction.action !== 'withdraw') {
214
+ throw new Error(`Invalid action: ${transaction.action}`)
215
+ }
216
+
217
+ if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
218
+ throw Error('Cannot build data for pending withdraw transaction');
219
+ }
220
+
221
+ if (!transaction.submitEvent) {
222
+ throw Error('Cannot build data for transaction without submitEvent');
223
+ }
224
+
225
+ const { to, amount, chainId, itoken: itokenAddress } = transaction.submitEvent;
226
+
227
+ const itoken = itokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === itokenAddress.toLowerCase());
228
+
229
+ if (!itoken) {
230
+ throw Error('Cannot build data for transaction without itoken');
231
+ }
232
+
233
+ const token = tokens[chainId].find(t => t.symbol.toLowerCase() === itoken.symbol.toLowerCase());
234
+
235
+ if (!token) {
236
+ throw Error('Cannot build data for transaction without token');
237
+ }
238
+
239
+ const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
240
+ const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
241
+ const gatewayAddress = addresses[chainId].interopXGateway;
242
+ const interopBridgeContract = getContract<InteropXGateway>(gatewayAddress, abi.interopXGateway, targetWallet);
243
+
244
+ const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(
245
+ ethers.BigNumber.from(amount.toString()),
246
+ to,
247
+ token.address,
248
+ ethers.BigNumber.from(transaction.sourceChainId.toString()),
249
+ transaction.submitTransactionHash,
250
+ );
251
+
252
+ transactions.push({
253
+ to: gatewayAddress,
254
+ data: data!,
255
+ value: '0',
256
+ operation: OperationType.Call,
257
+ });
258
+
259
+ return encodeMulti(transactions).data
260
+ }
261
+
262
+
263
+ export function getContract<TContract extends ethers.Contract>(address: string, contractInterface: ethers.ContractInterface | any, signerOrProvider?: ethers.Signer | ethers.providers.Provider) {
264
+ if (!ethers.utils.getAddress(address) || address === ethers.constants.AddressZero) {
265
+ throw Error(`Invalid 'address' parameter '${address}'.`)
266
+ }
267
+
268
+ const contract = new ethers.Contract(
269
+ address,
270
+ contractInterface,
271
+ signerOrProvider
272
+ ) as TContract
273
+
274
+ // Make sure the contract properties is writable
275
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
276
+
277
+ if (!desc || desc.writable !== true) {
278
+ return contract
279
+ }
280
+
281
+ return new Proxy(contract, {
282
+ get(target, prop, receiver) {
283
+ const value = Reflect.get(target, prop, receiver);
284
+
285
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
286
+ return async (...args: any[]) => {
287
+ try {
288
+ return await value.bind(contract)(...args);
289
+ } catch (error) {
290
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
291
+ }
292
+ }
293
+ }
294
+
295
+
296
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
297
+ const parentProp = String(prop);
298
+
299
+ return new Proxy(value, {
300
+ get(target, prop, receiver) {
301
+ const value = Reflect.get(target, prop, receiver);
302
+
303
+ if (typeof value === 'function') {
304
+ return async (...args: any[]) => {
305
+ try {
306
+ return await value.bind(contract)(...args);
307
+ } catch (error) {
308
+ throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
309
+ }
310
+ }
311
+ }
312
+ }
313
+ })
314
+ }
315
+
316
+ return value;
317
+ },
318
+ });
319
+ }