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

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.
@@ -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') {
@@ -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
+ }