@instadapp/interop-x 0.0.0-dev.b3789e6 → 0.0.0-dev.b8c571d

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 (33) hide show
  1. package/bin/interop-x +1 -1
  2. package/dist/package.json +6 -3
  3. package/dist/src/constants/addresses.js +0 -8
  4. package/dist/src/constants/index.js +1 -0
  5. package/dist/src/constants/itokens.js +13 -0
  6. package/dist/src/db/models/transaction.js +2 -0
  7. package/dist/src/index.js +1 -1
  8. package/dist/src/net/peer/index.js +6 -2
  9. package/dist/src/net/pool/index.js +16 -9
  10. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +22 -12
  11. package/dist/src/tasks/BaseTask.js +1 -1
  12. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +145 -0
  13. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +70 -0
  14. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +147 -0
  15. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +14 -5
  16. package/dist/src/tasks/index.js +13 -0
  17. package/dist/src/utils/index.js +124 -2
  18. package/package.json +6 -3
  19. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  20. package/src/constants/addresses.ts +0 -8
  21. package/src/constants/index.ts +1 -0
  22. package/src/constants/itokens.ts +10 -0
  23. package/src/db/models/transaction.ts +6 -2
  24. package/src/net/peer/index.ts +7 -6
  25. package/src/net/pool/index.ts +20 -10
  26. package/src/net/protocol/dial/SignatureDialProtocol.ts +25 -13
  27. package/src/tasks/BaseTask.ts +1 -1
  28. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +231 -0
  29. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +121 -0
  30. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +241 -0
  31. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +22 -7
  32. package/src/tasks/index.ts +19 -2
  33. package/src/utils/index.ts +175 -3
@@ -3,9 +3,14 @@
3
3
  */
4
4
  import axios from 'axios'
5
5
  import axiosRetry from "axios-retry";
6
- import { addresses } from '@/constants';
6
+ import { addresses, itokens, tokens } from '@/constants';
7
7
  import { ChainId } from '@/types'
8
8
  import { ethers } from 'ethers';
9
+ import { encodeMulti, MetaTransaction, OperationType } from 'ethers-multisend';
10
+ import { Transaction } from '@/db';
11
+ import config from '@/config';
12
+ import abi from '@/abi';
13
+ import { InteropBridgeToken, InteropXGateway } from '@/typechain';
9
14
 
10
15
  export const http = axios.create();
11
16
 
@@ -119,11 +124,178 @@ export const asyncCallWithTimeout = async <T>(asyncPromise: Promise<T>, timeout:
119
124
  }
120
125
 
121
126
 
122
- export const generateInteropTransactionHash = (data: { action: string, sourceTransactionHash: string, sourceChainId: string | number, targetChainId: string | number }) => {
127
+ export const generateInteropTransactionHash = (data: { action: string, submitTransactionHash: string, sourceChainId: string | number, targetChainId: string | number }) => {
123
128
  return ethers.utils.solidityKeccak256(['string', 'string', 'string', 'string'], [
124
129
  String(data.action),
125
- String(data.sourceTransactionHash),
130
+ String(data.submitTransactionHash),
126
131
  String(data.sourceChainId),
127
132
  String(data.targetChainId),
128
133
  ]);
134
+ }
135
+
136
+ export const buildDataForTransaction = async (transaction: Transaction, type?: 'source' | 'target') => {
137
+ type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
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') => {
150
+ const transactions: MetaTransaction[] = [];
151
+
152
+ if (transaction.action !== 'deposit') {
153
+ throw new Error(`Invalid action: ${transaction.action}`)
154
+ }
155
+
156
+ if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
157
+ throw Error('Cannot build data for pending deposit transaction');
158
+ }
159
+
160
+ if (!transaction.submitEvent) {
161
+ throw Error('Cannot build data for transaction without submitEvent');
162
+ }
163
+
164
+
165
+ const token = tokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === transaction.submitEvent.token.toLowerCase());
166
+
167
+ if (!token) {
168
+ throw Error('Cannot build data for transaction without token');
169
+ }
170
+
171
+ const itoken = itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
172
+
173
+ if (!itoken) {
174
+ throw Error('Cannot build data for transaction without itoken');
175
+ }
176
+
177
+ const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
178
+ const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
179
+ const interopBridgeContract = getContract<InteropBridgeToken>(itoken.address, abi.interopBridgeToken, targetWallet);
180
+
181
+ const { data } = await interopBridgeContract.populateTransaction.mint(
182
+ transaction.submitEvent.user,
183
+ ethers.BigNumber.from(transaction.submitEvent.amount.toString()),
184
+ ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()),
185
+ transaction.submitTransactionHash,
186
+ );
187
+
188
+ transactions.push({
189
+ to: itoken.address,
190
+ data: data!,
191
+ value: '0',
192
+ operation: OperationType.Call,
193
+ });
194
+
195
+ return encodeMulti(transactions).data
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.submitEvent.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
+
263
+ return new Proxy(contract, {
264
+ get(target, prop, receiver) {
265
+ const value = Reflect.get(target, prop, receiver);
266
+
267
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
268
+ return async (...args: any[]) => {
269
+ try {
270
+ return await value.bind(contract)(...args);
271
+ } catch (error) {
272
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
273
+ }
274
+ }
275
+ }
276
+
277
+
278
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
279
+ const parentProp = String(prop);
280
+
281
+ return new Proxy(value, {
282
+ get(target, prop, receiver) {
283
+ const value = Reflect.get(target, prop, receiver);
284
+
285
+ if (typeof value === 'function') {
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)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
291
+ }
292
+ }
293
+ }
294
+ }
295
+ })
296
+ }
297
+
298
+ return value;
299
+ },
300
+ });
129
301
  }