@instadapp/interop-x 0.0.0-dev.733ff78 → 0.0.0-dev.76d0265

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. package/dist/package.json +13 -12
  2. package/dist/src/abi/index.js +2 -4
  3. package/dist/src/abi/interopXContract.json +454 -0
  4. package/dist/src/alias.js +10 -0
  5. package/dist/src/constants/addresses.js +7 -7
  6. package/dist/src/constants/index.js +0 -1
  7. package/dist/src/constants/tokens.js +62 -39
  8. package/dist/src/db/models/transaction.js +19 -11
  9. package/dist/src/gnosis/actions/index.js +0 -2
  10. package/dist/src/gnosis/actions/withdraw/index.js +115 -0
  11. package/dist/src/gnosis/index.js +4 -4
  12. package/dist/src/index.js +5 -19
  13. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +6 -2
  14. package/dist/src/tasks/{InteropBridge/ProcessWithdrawEvents.js → InteropXContract/ProcessBridgeRequestEvents.js} +50 -53
  15. package/dist/src/tasks/InteropXContract/SyncBridgeCommittedEvents.js +93 -0
  16. package/dist/src/tasks/InteropXContract/SyncBridgeRequestEvents.js +78 -0
  17. package/dist/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.js +90 -0
  18. package/dist/src/tasks/index.js +16 -30
  19. package/dist/src/typechain/{InteropBridgeToken.js → InteropXContract.js} +0 -0
  20. package/dist/src/typechain/factories/InteropXContract__factory.js +635 -0
  21. package/dist/src/typechain/factories/index.js +3 -5
  22. package/dist/src/typechain/index.js +3 -5
  23. package/dist/src/utils/index.js +44 -8
  24. package/package.json +13 -12
  25. package/src/abi/index.ts +2 -4
  26. package/src/abi/interopXContract.json +454 -0
  27. package/src/alias.ts +6 -0
  28. package/src/constants/addresses.ts +8 -8
  29. package/src/constants/index.ts +0 -1
  30. package/src/constants/tokens.ts +63 -40
  31. package/src/db/models/transaction.ts +66 -27
  32. package/src/gnosis/actions/index.ts +0 -2
  33. package/src/gnosis/actions/withdraw/index.ts +155 -0
  34. package/src/gnosis/index.ts +5 -5
  35. package/src/index.ts +4 -18
  36. package/src/net/protocol/dial/SignatureDialProtocol.ts +6 -2
  37. package/src/tasks/{InteropBridge/ProcessWithdrawEvents.ts → InteropXContract/ProcessBridgeRequestEvents.ts} +78 -100
  38. package/src/tasks/InteropXContract/SyncBridgeCommittedEvents.ts +125 -0
  39. package/src/tasks/InteropXContract/SyncBridgeRequestEvents.ts +115 -0
  40. package/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.ts +121 -0
  41. package/src/tasks/index.ts +18 -36
  42. package/src/typechain/InteropXContract.ts +680 -0
  43. package/src/typechain/factories/InteropXContract__factory.ts +642 -0
  44. package/src/typechain/factories/index.ts +1 -2
  45. package/src/typechain/index.ts +2 -4
  46. package/src/utils/index.ts +85 -8
  47. package/tsconfig.json +7 -2
  48. package/dist/src/abi/interopBridgeToken.json +0 -298
  49. package/dist/src/abi/interopXGateway.json +0 -184
  50. package/dist/src/constants/itokens.js +0 -13
  51. package/dist/src/gnosis/actions/deposit.js +0 -48
  52. package/dist/src/gnosis/actions/withdraw.js +0 -50
  53. package/dist/src/tasks/InteropBridge/SyncBurnEvents.js +0 -71
  54. package/dist/src/tasks/InteropBridge/SyncMintEvents.js +0 -67
  55. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +0 -164
  56. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +0 -74
  57. package/dist/src/tasks/InteropXGateway/SyncWithdrawtEvents.js +0 -72
  58. package/dist/src/typechain/InteropXGateway.js +0 -2
  59. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +0 -471
  60. package/dist/src/typechain/factories/InteropXGateway__factory.js +0 -265
  61. package/src/abi/interopBridgeToken.json +0 -298
  62. package/src/abi/interopXGateway.json +0 -184
  63. package/src/constants/itokens.ts +0 -10
  64. package/src/gnosis/actions/deposit.ts +0 -63
  65. package/src/gnosis/actions/withdraw.ts +0 -67
  66. package/src/tasks/InteropBridge/SyncBurnEvents.ts +0 -119
  67. package/src/tasks/InteropBridge/SyncMintEvents.ts +0 -99
  68. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +0 -260
  69. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +0 -124
  70. package/src/tasks/InteropXGateway/SyncWithdrawtEvents.ts +0 -105
  71. package/src/typechain/InteropBridgeToken.ts +0 -692
  72. package/src/typechain/InteropXGateway.ts +0 -407
  73. package/src/typechain/factories/InteropBridgeToken__factory.ts +0 -478
  74. package/src/typechain/factories/InteropXGateway__factory.ts +0 -272
@@ -1,67 +1,33 @@
1
1
  import { BaseTask } from "../BaseTask";
2
2
  import Logger from '@/logger';
3
- import { BigNumber, ethers } from "ethers";
3
+ import { ethers, Wallet } from "ethers";
4
4
  import abi from "@/abi";
5
5
  import { Transaction } from "@/db";
6
- import { buildSignatureBytes, getContract, getRpcProviderUrl, Signature } from "@/utils";
6
+ import { buildSignatureBytes, generateGnosisTransaction, generateInteropTransactionHash, getContract, getRpcProviderUrl, LiquidityError, Signature } from "@/utils";
7
7
  import { addresses } from "@/constants";
8
+ import { Op } from "sequelize";
8
9
  import { ChainId } from "@/types";
9
10
  import config from "@/config";
10
- import { GnosisSafe } from "@/typechain";
11
- import { Op } from "sequelize";
11
+ import { GnosisSafe, InteropXContract } from "@/typechain";
12
12
  import wait from "waait";
13
+ import { buildGnosisAction } from "@/gnosis";
13
14
  import { peerPool, protocol } from "@/net";
14
15
  import { LogDescription } from "ethers/lib/utils";
15
- import { buildGnosisAction } from "@/gnosis";
16
-
17
- const generateGnosisTransaction = async (transactionData: any, safeContract: GnosisSafe) => {
18
- console.log(transactionData);
19
-
20
- let isExecuted = await safeContract.dataHashes(
21
- await safeContract.getTransactionHash(
22
- transactionData.to,
23
- transactionData.value,
24
- transactionData.data,
25
- transactionData.operation,
26
- transactionData.safeTxGas,
27
- transactionData.baseGas,
28
- transactionData.gasPrice,
29
- transactionData.gasToken,
30
- transactionData.refundReceiver,
31
- transactionData.nonce
32
- )
33
- )
34
-
35
- while (isExecuted == 1) {
36
- transactionData.safeTxGas = BigNumber.from(String(transactionData.safeTxGas)).add(1).toString()
37
-
38
- isExecuted = await safeContract.dataHashes(
39
- await safeContract.getTransactionHash(
40
- transactionData.to,
41
- transactionData.value,
42
- transactionData.data,
43
- transactionData.operation,
44
- transactionData.safeTxGas,
45
- transactionData.baseGas,
46
- transactionData.gasPrice,
47
- transactionData.gasToken,
48
- transactionData.refundReceiver,
49
- transactionData.nonce
50
- )
51
- )
52
- }
53
-
54
- return transactionData
55
- }
56
16
 
57
- class ProcessWithdrawEvents extends BaseTask {
17
+ class ProccessBridgeRequestEvents extends BaseTask {
18
+ contractAddress: string;
19
+ safeContractAddress: string;
58
20
  provider: ethers.providers.JsonRpcProvider;
21
+ contract: InteropXContract;
22
+ safeContract: GnosisSafe;
59
23
  chainId: ChainId;
60
- leadNodeOnly = true
24
+ sourceWallet: Wallet;
25
+
26
+ leadNodeOnly: boolean = true;
61
27
 
62
28
  constructor({ chainId }: { chainId: ChainId }) {
63
29
  super({
64
- logger: new Logger("InteropXGateway::ProcessWithdrawEvents"),
30
+ logger: new Logger("InteropXContract::ProccessBridgeRequestEvents"),
65
31
  })
66
32
  this.chainId = chainId;
67
33
  }
@@ -72,19 +38,17 @@ class ProcessWithdrawEvents extends BaseTask {
72
38
  const transaction = await Transaction.findOne({
73
39
  where: {
74
40
  status: 'pending',
75
- sourceStatus: 'success',
76
- targetStatus: 'uninitialised',
77
- action: 'withdraw',
78
- sourceCreatedAt: {
41
+ sourceStatus: 'uninitialised',
42
+ createdAt: {
79
43
  [Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
80
44
  },
81
- targetDelayUntil: {
45
+ sourceDelayUntil: {
82
46
  [Op.or]: {
83
47
  [Op.is]: null,
84
48
  [Op.lt]: new Date(),
85
49
  }
86
50
  },
87
- sourceBlockNumber: {
51
+ requestBlockNumber: {
88
52
  [Op.lt]: blockNumber - 12,
89
53
  },
90
54
  sourceChainId: this.chainId,
@@ -100,34 +64,32 @@ class ProcessWithdrawEvents extends BaseTask {
100
64
  transaction.targetStatus = 'pending';
101
65
  await transaction.save();
102
66
 
103
- // refresh event data?
104
-
105
- const targetChainProvider = new ethers.providers.JsonRpcProvider(
106
- getRpcProviderUrl(transaction.targetChainId as ChainId)
107
- );
108
-
109
- const targetWallet = new ethers.Wallet(config.privateKey!, targetChainProvider);
110
-
111
- const safeAddress = addresses[transaction.targetChainId].gnosisSafe;
112
-
113
-
114
- const safeContract = getContract<GnosisSafe>(
115
- safeAddress,
116
- abi.gnosisSafe,
117
- targetWallet
118
- )
119
-
120
- const ownersThreshold = await safeContract.getThreshold();
67
+ const ownersThreshold = await this.safeContract.getThreshold();
121
68
  await wait(10000);
122
69
 
70
+
123
71
  let data, logs = [];
124
72
 
125
73
  try {
126
- ({ data, logs } = await buildGnosisAction(transaction));
74
+ ({ data, logs } = await buildGnosisAction(transaction, 'source'));
75
+
127
76
  } catch (error) {
128
- console.log(error);
77
+
78
+ if(error instanceof LiquidityError){
79
+ await transaction.save();
80
+ transaction.sourceDelayUntil = new Date(Date.now() + 60 * 5 * 1000);
81
+ transaction.sourceStatus = 'uninitialised'
82
+
83
+ await transaction.save();
84
+
85
+ throw error
86
+ return;
87
+ }
88
+
89
+ transaction.sourceStatus = 'failed';
90
+ transaction.sourceErrors = [error.message];
129
91
  transaction.targetStatus = 'failed';
130
- transaction.targetErrors = [error.message];
92
+
131
93
  transaction.status = 'failed'
132
94
  await transaction.save();
133
95
  protocol.sendTransaction(transaction)
@@ -142,13 +104,13 @@ class ProcessWithdrawEvents extends BaseTask {
142
104
  nonce: '0',
143
105
  operation: "1",
144
106
  refundReceiver: "0x0000000000000000000000000000000000000000",
145
- safeAddress: safeAddress,
107
+ safeAddress: this.safeContractAddress,
146
108
  safeTxGas: "79668",
147
- to: addresses[transaction.targetChainId].multisend,
109
+ to: addresses[transaction.sourceChainId].multisend,
148
110
  value: "0",
149
- }, safeContract);
111
+ }, this.safeContract);
150
112
 
151
- const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
113
+ const owners = await this.safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
152
114
 
153
115
  const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
154
116
 
@@ -170,18 +132,17 @@ class ProcessWithdrawEvents extends BaseTask {
170
132
 
171
133
  if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
172
134
  await transaction.save();
173
- transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
174
- transaction.targetStatus = 'uninitialised'
135
+ transaction.sourceDelayUntil = new Date(Date.now() + 30 * 1000);
136
+ transaction.sourceStatus = 'uninitialised'
175
137
 
176
138
  await transaction.save();
177
139
  const errorMessage = signatures.find(s => !!s.error)?.error;
178
140
  throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
179
141
  }
180
142
 
181
-
182
143
  console.log(`Executing transaction for execution ${transaction.transactionHash}`)
183
144
 
184
- const { data: txData } = await safeContract.populateTransaction.execTransaction(
145
+ const { data: txData } = await this.safeContract.populateTransaction.execTransaction(
185
146
  gnosisTx.to,
186
147
  gnosisTx.value,
187
148
  gnosisTx.data,
@@ -194,20 +155,15 @@ class ProcessWithdrawEvents extends BaseTask {
194
155
  buildSignatureBytes(validSignatures)
195
156
  );
196
157
 
197
- console.log({
198
- from: targetWallet.address,
199
- gasPrice: BigNumber.from(120 * 10 ** 9).toString(),
200
- to: safeAddress,
158
+ const txSent = await this.sourceWallet.sendTransaction({
159
+ from: this.sourceWallet.address,
160
+ gasPrice: ethers.BigNumber.from(120 * 10 ** 9),
161
+ to: this.safeContractAddress,
201
162
  data: txData,
202
163
  })
203
164
 
165
+ console.log(txSent);
204
166
 
205
- const txSent = await targetWallet.sendTransaction({
206
- from: targetWallet.address,
207
- gasPrice: BigNumber.from(120 * 10 ** 9),
208
- to: safeAddress,
209
- data: txData,
210
- })
211
167
 
212
168
  const receipt = await txSent.wait();
213
169
 
@@ -215,21 +171,26 @@ class ProcessWithdrawEvents extends BaseTask {
215
171
 
216
172
  receipt.logs.forEach((log) => {
217
173
  try {
218
- parsedLogs.push(safeContract.interface.parseLog(log));
174
+ parsedLogs.push(this.safeContract.interface.parseLog(log));
219
175
  } catch (e) { }
220
176
  });
221
177
 
222
178
  if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
223
179
  console.log('ExecutionSuccess')
224
- transaction.targetStatus = 'success'
225
- transaction.targetTransactionHash = txSent.hash
226
- transaction.targetLogs = logs
180
+ transaction.sourceStatus = 'success'
181
+ if (txSent.blockNumber)
182
+ transaction.sourceBlockNumber = txSent.blockNumber;
183
+ transaction.sourceTransactionHash = txSent.hash
184
+ transaction.sourceLogs = logs;
227
185
  transaction.status = 'success'
228
186
  await transaction.save();
229
187
  } else {
230
188
  console.log('ExecutionFailure')
231
- transaction.targetStatus = 'failed'
232
- transaction.targetTransactionHash = txSent.hash
189
+ transaction.sourceStatus = 'failed'
190
+ if (txSent.blockNumber)
191
+ transaction.sourceBlockNumber = txSent.blockNumber;
192
+ transaction.sourceTransactionHash = txSent.hash
193
+ transaction.sourceTransactionHash = txSent.hash
233
194
  transaction.status = 'failed'
234
195
  await transaction.save();
235
196
  }
@@ -238,12 +199,29 @@ class ProcessWithdrawEvents extends BaseTask {
238
199
  }
239
200
 
240
201
  async start(): Promise<void> {
202
+ this.contractAddress = addresses[this.chainId].interopXContract;
203
+
241
204
  this.provider = new ethers.providers.JsonRpcProvider(
242
205
  getRpcProviderUrl(this.chainId)
243
206
  );
244
207
 
208
+ this.sourceWallet = new ethers.Wallet(config.privateKey!, this.provider);
209
+
210
+ this.contract = getContract<InteropXContract>(
211
+ this.contractAddress,
212
+ abi.interopXContract,
213
+ this.sourceWallet
214
+ );
215
+
216
+ this.safeContractAddress = addresses[this.chainId].gnosisSafe;
217
+ this.safeContract = getContract<GnosisSafe>(
218
+ this.safeContractAddress,
219
+ abi.gnosisSafe,
220
+ this.sourceWallet
221
+ );
222
+
245
223
  await super.start()
246
224
  }
247
225
  }
248
226
 
249
- export default ProcessWithdrawEvents;
227
+ export default ProccessBridgeRequestEvents;
@@ -0,0 +1,125 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import { ethers } from "ethers";
4
+ import abi from "@/abi";
5
+ import { Transaction } from "@/db";
6
+ import { generateInteropTransactionHash, getContract, getRpcProviderUrl } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { ChainId } from "@/types";
9
+ import config from "@/config";
10
+ import { InteropXContract } from "@/typechain";
11
+ import { Op } from "sequelize";
12
+
13
+ class SyncBridgeCommittedEvents extends BaseTask {
14
+ contractAddress: string;
15
+ provider: ethers.providers.JsonRpcProvider;
16
+ contract: InteropXContract;
17
+ chainId: ChainId;
18
+
19
+ constructor({ chainId }: { chainId: ChainId }) {
20
+ super({
21
+ logger: new Logger("InteropXContract::SyncBridgeCommittedEvents"),
22
+ })
23
+ this.chainId = chainId;
24
+ }
25
+
26
+ async pollHandler() {
27
+ const currentBlock = await this.provider.getBlockNumber();
28
+
29
+ const events = await this.contract.queryFilter(
30
+ this.contract.filters.LogBridgeCommitted(),
31
+ currentBlock - 2000,
32
+ currentBlock,
33
+ );
34
+
35
+ let processedEvents = 0;
36
+
37
+ for (const event of events) {
38
+
39
+ try {
40
+ if (!event.args) {
41
+ continue;
42
+ }
43
+
44
+ const { actionId, bridger, position, sourceChainId, targetChainId, requestTransactionHash, metadata } = event.args;
45
+
46
+ const uniqueIdentifier = {
47
+ actionId,
48
+ bridger,
49
+ requestTransactionHash,
50
+ sourceChainId: sourceChainId,
51
+ targetChainId: targetChainId,
52
+ }
53
+
54
+ let transactionHash = generateInteropTransactionHash(uniqueIdentifier);
55
+
56
+ const transaction = await Transaction.findOne({
57
+ where: {
58
+ transactionHash,
59
+ sourceStatus: 'success',
60
+ committedEvent: {
61
+ [Op.eq]: null,
62
+ }
63
+ }
64
+ });
65
+
66
+ if (!transaction) {
67
+ continue;
68
+ }
69
+ transaction.targetStatus = 'success'
70
+ transaction.targetBlockNumber = event.blockNumber;
71
+ transaction.targetTransactionHash = event.transactionHash
72
+ transaction.committedEvent = {
73
+ actionId,
74
+ bridger,
75
+ position: {
76
+ withdraw: position.withdraw.map((v) => ({
77
+ sourceToken: v.sourceToken,
78
+ targetToken: v.targetToken,
79
+ amount: v.amount.toString()
80
+ })),
81
+ supply: position.supply.map((v) => ({
82
+ sourceToken: v.sourceToken,
83
+ targetToken: v.targetToken,
84
+ amount: v.amount.toString()
85
+ })),
86
+ },
87
+ sourceChainId: sourceChainId,
88
+ targetChainId: targetChainId,
89
+ metadata,
90
+ requestTransactionHash,
91
+ }
92
+
93
+ transaction.status = 'success';
94
+ await transaction.save()
95
+
96
+ this.logger.info(
97
+ `New bridge committed received: ${transactionHash} `
98
+ );
99
+ } catch (error) {
100
+ this.logger.error(error);
101
+ }
102
+ }
103
+
104
+ if (processedEvents > 0)
105
+ this.logger.info(`${processedEvents} events processed`);
106
+ }
107
+
108
+ async start(): Promise<void> {
109
+ this.contractAddress = addresses[this.chainId].interopXContract;
110
+
111
+ this.provider = new ethers.providers.JsonRpcProvider(
112
+ getRpcProviderUrl(this.chainId)
113
+ );
114
+
115
+ this.contract = getContract<InteropXContract>(
116
+ this.contractAddress,
117
+ abi.interopXContract,
118
+ new ethers.Wallet(config.privateKey!, this.provider)
119
+ );
120
+
121
+ await super.start()
122
+ }
123
+ }
124
+
125
+ export default SyncBridgeCommittedEvents;
@@ -0,0 +1,115 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import { ethers } from "ethers";
4
+ import abi from "@/abi";
5
+ import { Transaction } from "@/db";
6
+ import { generateInteropTransactionHash, getContract, getRpcProviderUrl } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { ChainId } from "@/types";
9
+ import config from "@/config";
10
+ import { InteropXContract } from "@/typechain";
11
+
12
+ class SyncBridgeRequestEvents extends BaseTask {
13
+ contractAddress: string;
14
+ provider: ethers.providers.JsonRpcProvider;
15
+ contract: InteropXContract;
16
+ chainId: ChainId;
17
+
18
+ constructor({ chainId }: { chainId: ChainId }) {
19
+ super({
20
+ logger: new Logger("InteropXContract::SyncBridgeRequestEvents"),
21
+ })
22
+ this.chainId = chainId;
23
+ }
24
+
25
+ async pollHandler() {
26
+ const currentBlock = await this.provider.getBlockNumber();
27
+
28
+ const events = await this.contract.queryFilter(
29
+ this.contract.filters.LogBridgeRequest(),
30
+ currentBlock - 2000,
31
+ currentBlock,
32
+ );
33
+
34
+ let processedEvents = 0;
35
+
36
+ for (const event of events) {
37
+
38
+ try {
39
+ if (!event.args) {
40
+ continue;
41
+ }
42
+
43
+ const { actionId, bridger, position, sourceChainId, targetChainId, metadata } = event.args;
44
+
45
+ const uniqueIdentifier = {
46
+ actionId,
47
+ bridger,
48
+ requestTransactionHash: event.transactionHash,
49
+ sourceChainId: sourceChainId.toNumber(),
50
+ targetChainId: targetChainId.toNumber(),
51
+ }
52
+
53
+ let transactionHash = generateInteropTransactionHash(uniqueIdentifier);
54
+
55
+ const transaction = await Transaction.findOne({ where: { transactionHash } });
56
+
57
+ if (transaction) {
58
+ continue;
59
+ }
60
+
61
+ await Transaction.create({
62
+ transactionHash,
63
+ ...uniqueIdentifier,
64
+ requestBlockNumber: event.blockNumber,
65
+ requestEvent: {
66
+ actionId,
67
+ bridger,
68
+ position: {
69
+ withdraw: position.withdraw.map((v) => ({
70
+ sourceToken: v.sourceToken,
71
+ targetToken: v.targetToken,
72
+ amount: v.amount.toString()
73
+ })),
74
+ supply: position.supply.map((v) => ({
75
+ sourceToken: v.sourceToken,
76
+ targetToken: v.targetToken,
77
+ amount: v.amount.toString()
78
+ })),
79
+ },
80
+ sourceChainId: sourceChainId.toNumber(),
81
+ targetChainId: targetChainId.toNumber(),
82
+ metadata,
83
+ }
84
+ })
85
+
86
+ this.logger.info(
87
+ `New bridge request received: ${transactionHash} `
88
+ );
89
+ } catch (error) {
90
+ this.logger.error(error);
91
+ }
92
+ }
93
+
94
+ if (processedEvents > 0)
95
+ this.logger.info(`${processedEvents} events processed`);
96
+ }
97
+
98
+ async start(): Promise<void> {
99
+ this.contractAddress = addresses[this.chainId].interopXContract;
100
+
101
+ this.provider = new ethers.providers.JsonRpcProvider(
102
+ getRpcProviderUrl(this.chainId)
103
+ );
104
+
105
+ this.contract = getContract<InteropXContract>(
106
+ this.contractAddress,
107
+ abi.interopXContract,
108
+ new ethers.Wallet(config.privateKey!, this.provider)
109
+ );
110
+
111
+ await super.start()
112
+ }
113
+ }
114
+
115
+ export default SyncBridgeRequestEvents;
@@ -0,0 +1,121 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import { ethers } from "ethers";
4
+ import abi from "@/abi";
5
+ import { Transaction } from "@/db";
6
+ import { generateInteropTransactionHash, getContract, getRpcProviderUrl } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { ChainId } from "@/types";
9
+ import config from "@/config";
10
+ import { InteropXContract } from "@/typechain";
11
+ import { Op } from "sequelize";
12
+
13
+ class SyncBridgeRequestSentEvents extends BaseTask {
14
+ contractAddress: string;
15
+ provider: ethers.providers.JsonRpcProvider;
16
+ contract: InteropXContract;
17
+ chainId: ChainId;
18
+
19
+ constructor({ chainId }: { chainId: ChainId }) {
20
+ super({
21
+ logger: new Logger("InteropXContract::SyncBridgeRequestSentEvents"),
22
+ })
23
+ this.chainId = chainId;
24
+ }
25
+
26
+ async pollHandler() {
27
+ const currentBlock = await this.provider.getBlockNumber();
28
+
29
+ const events = await this.contract.queryFilter(
30
+ this.contract.filters.LogBridgeRequestSent(),
31
+ currentBlock - 2000,
32
+ currentBlock,
33
+ );
34
+
35
+ let processedEvents = 0;
36
+
37
+ for (const event of events) {
38
+
39
+ try {
40
+ if (!event.args) {
41
+ continue;
42
+ }
43
+
44
+ const { actionId, bridger, position, sourceChainId, targetChainId, requestTransactionHash, metadata } = event.args;
45
+
46
+ const uniqueIdentifier = {
47
+ actionId,
48
+ bridger,
49
+ requestTransactionHash,
50
+ sourceChainId: sourceChainId,
51
+ targetChainId: targetChainId,
52
+ }
53
+
54
+ let transactionHash = generateInteropTransactionHash(uniqueIdentifier);
55
+
56
+ const transaction = await Transaction.findOne({
57
+ where: {
58
+ transactionHash, requestSentEvent: {
59
+ [Op.eq]: null,
60
+ }
61
+ }
62
+ });
63
+
64
+ if (!transaction) {
65
+ continue;
66
+ }
67
+ transaction.sourceStatus = 'success'
68
+ transaction.sourceBlockNumber = event.blockNumber;
69
+ transaction.sourceTransactionHash = event.transactionHash
70
+ transaction.requestSentEvent = {
71
+ actionId,
72
+ bridger,
73
+ position: {
74
+ withdraw: position.withdraw.map((v) => ({
75
+ sourceToken: v.sourceToken,
76
+ targetToken: v.targetToken,
77
+ amount: v.amount.toString()
78
+ })),
79
+ supply: position.supply.map((v) => ({
80
+ sourceToken: v.sourceToken,
81
+ targetToken: v.targetToken,
82
+ amount: v.amount.toString()
83
+ })),
84
+ },
85
+ sourceChainId: sourceChainId,
86
+ targetChainId: targetChainId,
87
+ metadata,
88
+ requestTransactionHash,
89
+ }
90
+ await transaction.save()
91
+
92
+ this.logger.info(
93
+ `New bridge request sent received: ${transactionHash} `
94
+ );
95
+ } catch (error) {
96
+ this.logger.error(error);
97
+ }
98
+ }
99
+
100
+ if (processedEvents > 0)
101
+ this.logger.info(`${processedEvents} events processed`);
102
+ }
103
+
104
+ async start(): Promise<void> {
105
+ this.contractAddress = addresses[this.chainId].interopXContract;
106
+
107
+ this.provider = new ethers.providers.JsonRpcProvider(
108
+ getRpcProviderUrl(this.chainId)
109
+ );
110
+
111
+ this.contract = getContract<InteropXContract>(
112
+ this.contractAddress,
113
+ abi.interopXContract,
114
+ new ethers.Wallet(config.privateKey!, this.provider)
115
+ );
116
+
117
+ await super.start()
118
+ }
119
+ }
120
+
121
+ export default SyncBridgeRequestSentEvents;