@instadapp/interop-x 0.0.0-dev.8a0297a → 0.0.0-dev.8a917f1

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 (54) hide show
  1. package/dist/package.json +7 -6
  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 +3 -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/index.js +69 -7
  9. package/dist/src/net/peer/index.js +2 -1
  10. package/dist/src/net/pool/index.js +25 -9
  11. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +11 -4
  12. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +28 -0
  13. package/dist/src/net/protocol/index.js +41 -1
  14. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  15. package/dist/src/tasks/BaseTask.js +11 -3
  16. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +146 -0
  17. package/dist/src/tasks/InteropBridge/SyncBurnEvents.js +71 -0
  18. package/dist/src/tasks/InteropBridge/SyncMintEvents.js +67 -0
  19. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +32 -21
  20. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +5 -6
  21. package/dist/src/tasks/InteropXGateway/SyncWithdrawtEvents.js +72 -0
  22. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
  23. package/dist/src/tasks/index.js +25 -0
  24. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +23 -11
  25. package/dist/src/typechain/factories/InteropXGateway__factory.js +14 -14
  26. package/dist/src/utils/index.js +71 -11
  27. package/package.json +7 -6
  28. package/src/abi/interopBridgeToken.json +21 -9
  29. package/src/abi/interopXGateway.json +11 -11
  30. package/src/api/index.ts +2 -2
  31. package/src/config/index.ts +11 -1
  32. package/src/constants/addresses.ts +1 -1
  33. package/src/constants/itokens.ts +1 -1
  34. package/src/index.ts +90 -9
  35. package/src/net/peer/index.ts +2 -1
  36. package/src/net/pool/index.ts +33 -13
  37. package/src/net/protocol/dial/SignatureDialProtocol.ts +12 -4
  38. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +31 -0
  39. package/src/net/protocol/index.ts +57 -1
  40. package/src/tasks/AutoUpdateTask.ts +82 -0
  41. package/src/tasks/BaseTask.ts +13 -3
  42. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +231 -0
  43. package/src/tasks/InteropBridge/SyncBurnEvents.ts +121 -0
  44. package/src/tasks/InteropBridge/SyncMintEvents.ts +99 -0
  45. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +37 -25
  46. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +5 -7
  47. package/src/tasks/InteropXGateway/SyncWithdrawtEvents.ts +105 -0
  48. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
  49. package/src/tasks/index.ts +37 -0
  50. package/src/typechain/InteropBridgeToken.ts +23 -17
  51. package/src/typechain/InteropXGateway.ts +13 -13
  52. package/src/typechain/factories/InteropBridgeToken__factory.ts +23 -11
  53. package/src/typechain/factories/InteropXGateway__factory.ts +14 -14
  54. package/src/utils/index.ts +93 -12
@@ -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') {
@@ -169,9 +192,9 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
169
192
 
170
193
  const { data } = await interopBridgeContract.populateTransaction.mint(
171
194
  transaction.submitEvent.user,
172
- ethers.BigNumber.from(transaction.submitEvent.amount),
173
- transaction.sourceChainId,
174
- transaction.sourceTransactionHash,
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({
@@ -184,6 +207,58 @@ export const buildDataForTransaction = async (transaction: Transaction, type?: '
184
207
  return encodeMulti(transactions).data
185
208
  }
186
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, sourceChainId, targetChainId, itoken: itokenAddress } = transaction.submitEvent;
226
+
227
+ const itoken = itokens[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[targetChainId].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(targetChainId as ChainId));
240
+ const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
241
+ const gatewayAddress = addresses[targetChainId].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(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
+
187
262
 
188
263
  export function getContract<TContract extends ethers.Contract>(address: string, contractInterface: ethers.ContractInterface | any, signerOrProvider?: ethers.Signer | ethers.providers.Provider) {
189
264
  if (!ethers.utils.getAddress(address) || address === ethers.constants.AddressZero) {
@@ -196,15 +271,21 @@ export function getContract<TContract extends ethers.Contract>(address: string,
196
271
  signerOrProvider
197
272
  ) as TContract
198
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
+ }
199
280
 
200
281
  return new Proxy(contract, {
201
282
  get(target, prop, receiver) {
202
283
  const value = Reflect.get(target, prop, receiver);
203
284
 
204
- if (typeof value === 'function' && contract.functions.hasOwnProperty(prop)) {
285
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
205
286
  return async (...args: any[]) => {
206
287
  try {
207
- return await value(...args);
288
+ return await value.bind(contract)(...args);
208
289
  } catch (error) {
209
290
  throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
210
291
  }
@@ -222,7 +303,7 @@ export function getContract<TContract extends ethers.Contract>(address: string,
222
303
  if (typeof value === 'function') {
223
304
  return async (...args: any[]) => {
224
305
  try {
225
- return await value(...args);
306
+ return await value.bind(contract)(...args);
226
307
  } catch (error) {
227
308
  throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
228
309
  }