@instadapp/interop-x 0.0.0-dev.6ea4ee5 → 0.0.0-dev.73e5298

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 (99) hide show
  1. package/bin/interop-x +1 -1
  2. package/dist/package.json +74 -0
  3. package/dist/{abi → src/abi}/erc20.json +0 -0
  4. package/dist/{abi → src/abi}/gnosisSafe.json +0 -0
  5. package/dist/{abi → src/abi}/index.js +2 -4
  6. package/dist/src/abi/interopXContract.json +454 -0
  7. package/dist/src/alias.js +10 -0
  8. package/dist/src/api/index.js +36 -0
  9. package/dist/{config → src/config}/index.js +11 -1
  10. package/dist/src/constants/addresses.js +20 -0
  11. package/dist/{constants → src/constants}/index.js +0 -0
  12. package/dist/{constants → src/constants}/tokens.js +62 -39
  13. package/dist/{db → src/db}/index.js +0 -0
  14. package/dist/{db → src/db}/models/index.js +0 -0
  15. package/dist/{db → src/db}/models/transaction.js +27 -9
  16. package/dist/{db → src/db}/sequelize.js +1 -1
  17. package/dist/src/gnosis/actions/index.js +9 -0
  18. package/dist/src/gnosis/actions/withdraw/index.js +115 -0
  19. package/dist/src/gnosis/index.js +20 -0
  20. package/dist/src/index.js +119 -0
  21. package/dist/{logger → src/logger}/index.js +0 -0
  22. package/dist/{net → src/net}/index.js +0 -0
  23. package/dist/{net → src/net}/peer/index.js +8 -3
  24. package/dist/{net → src/net}/pool/index.js +32 -9
  25. package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +0 -0
  26. package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +21 -12
  27. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
  28. package/dist/{net → src/net}/protocol/index.js +51 -1
  29. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  30. package/dist/{tasks → src/tasks}/BaseTask.js +12 -4
  31. package/dist/src/tasks/InteropXContract/ProcessBridgeRequestEvents.js +158 -0
  32. package/dist/src/tasks/InteropXContract/SyncBridgeCommittedEvents.js +93 -0
  33. package/dist/src/tasks/InteropXContract/SyncBridgeRequestEvents.js +78 -0
  34. package/dist/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.js +90 -0
  35. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +57 -0
  36. package/dist/src/tasks/index.js +41 -0
  37. package/dist/{typechain → src/typechain}/Erc20.js +0 -0
  38. package/dist/{typechain → src/typechain}/GnosisSafe.js +0 -0
  39. package/dist/{typechain/InteropBridgeToken.js → src/typechain/InteropXContract.js} +0 -0
  40. package/dist/{typechain → src/typechain}/common.js +0 -0
  41. package/dist/{typechain → src/typechain}/factories/Erc20__factory.js +0 -0
  42. package/dist/{typechain → src/typechain}/factories/GnosisSafe__factory.js +0 -0
  43. package/dist/src/typechain/factories/InteropXContract__factory.js +635 -0
  44. package/dist/src/typechain/factories/index.js +12 -0
  45. package/dist/{typechain → src/typechain}/index.js +3 -5
  46. package/dist/{types.js → src/types.js} +0 -0
  47. package/dist/src/utils/index.js +193 -0
  48. package/package.json +23 -15
  49. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  50. package/src/abi/index.ts +2 -4
  51. package/src/abi/interopXContract.json +454 -0
  52. package/src/alias.ts +6 -0
  53. package/src/api/index.ts +36 -0
  54. package/src/config/index.ts +11 -1
  55. package/src/constants/addresses.ts +8 -16
  56. package/src/constants/tokens.ts +63 -40
  57. package/src/db/models/transaction.ts +78 -25
  58. package/src/db/sequelize.ts +1 -1
  59. package/src/gnosis/actions/index.ts +5 -0
  60. package/src/gnosis/actions/withdraw/index.ts +155 -0
  61. package/src/gnosis/index.ts +19 -0
  62. package/src/index.ts +101 -24
  63. package/src/net/peer/index.ts +9 -7
  64. package/src/net/pool/index.ts +41 -11
  65. package/src/net/protocol/dial/SignatureDialProtocol.ts +23 -13
  66. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  67. package/src/net/protocol/index.ts +67 -1
  68. package/src/tasks/AutoUpdateTask.ts +82 -0
  69. package/src/tasks/BaseTask.ts +14 -4
  70. package/src/tasks/InteropXContract/ProcessBridgeRequestEvents.ts +226 -0
  71. package/src/tasks/InteropXContract/SyncBridgeCommittedEvents.ts +125 -0
  72. package/src/tasks/InteropXContract/SyncBridgeRequestEvents.ts +115 -0
  73. package/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.ts +121 -0
  74. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +69 -0
  75. package/src/tasks/index.ts +28 -5
  76. package/src/typechain/InteropXContract.ts +680 -0
  77. package/src/typechain/factories/InteropXContract__factory.ts +642 -0
  78. package/src/typechain/factories/index.ts +1 -2
  79. package/src/typechain/index.ts +2 -4
  80. package/src/utils/index.ts +153 -7
  81. package/tsconfig.json +7 -2
  82. package/dist/abi/interopBridgeToken.json +0 -286
  83. package/dist/abi/interopXGateway.json +0 -184
  84. package/dist/constants/addresses.js +0 -28
  85. package/dist/index.js +0 -63
  86. package/dist/tasks/InteropXGateway/SyncDepositEvents.js +0 -79
  87. package/dist/tasks/index.js +0 -27
  88. package/dist/typechain/InteropXGateway.js +0 -2
  89. package/dist/typechain/factories/InteropBridgeToken__factory.js +0 -459
  90. package/dist/typechain/factories/InteropXGateway__factory.js +0 -265
  91. package/dist/typechain/factories/index.js +0 -14
  92. package/dist/utils/index.js +0 -101
  93. package/src/abi/interopBridgeToken.json +0 -286
  94. package/src/abi/interopXGateway.json +0 -184
  95. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +0 -114
  96. package/src/typechain/InteropBridgeToken.ts +0 -686
  97. package/src/typechain/InteropXGateway.ts +0 -407
  98. package/src/typechain/factories/InteropBridgeToken__factory.ts +0 -466
  99. package/src/typechain/factories/InteropXGateway__factory.ts +0 -272
@@ -0,0 +1,226 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import { ethers, Wallet } from "ethers";
4
+ import abi from "@/abi";
5
+ import { Transaction } from "@/db";
6
+ import { buildSignatureBytes, generateGnosisTransaction, generateInteropTransactionHash, getContract, getRpcProviderUrl, LiquidityError, Signature } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { Op } from "sequelize";
9
+ import { ChainId } from "@/types";
10
+ import config from "@/config";
11
+ import { GnosisSafe, InteropXContract } from "@/typechain";
12
+ import wait from "waait";
13
+ import { buildGnosisAction } from "@/gnosis";
14
+ import { peerPool, protocol } from "@/net";
15
+ import { LogDescription } from "ethers/lib/utils";
16
+
17
+ class ProccessBridgeRequestEvents extends BaseTask {
18
+ contractAddress: string;
19
+ safeContractAddress: string;
20
+ provider: ethers.providers.JsonRpcProvider;
21
+ contract: InteropXContract;
22
+ safeContract: GnosisSafe;
23
+ chainId: ChainId;
24
+ sourceWallet: Wallet;
25
+
26
+ leadNodeOnly: boolean = true;
27
+
28
+ constructor({ chainId }: { chainId: ChainId }) {
29
+ super({
30
+ logger: new Logger("InteropXContract::ProccessBridgeRequestEvents"),
31
+ })
32
+ this.chainId = chainId;
33
+ }
34
+
35
+ async pollHandler() {
36
+ const blockNumber = await this.provider.getBlockNumber()
37
+
38
+ const transaction = await Transaction.findOne({
39
+ where: {
40
+ status: 'pending',
41
+ sourceStatus: 'uninitialised',
42
+ createdAt: {
43
+ [Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
44
+ },
45
+ sourceDelayUntil: {
46
+ [Op.or]: {
47
+ [Op.is]: null,
48
+ [Op.lt]: new Date(),
49
+ }
50
+ },
51
+ requestBlockNumber: {
52
+ [Op.lt]: blockNumber - 12,
53
+ },
54
+ sourceChainId: this.chainId,
55
+ }
56
+ })
57
+
58
+ if (!transaction) {
59
+ return;
60
+ }
61
+
62
+ console.log(`Processing transaction ${transaction.transactionHash}`);
63
+
64
+ transaction.targetStatus = 'pending';
65
+ await transaction.save();
66
+
67
+ const ownersThreshold = await this.safeContract.getThreshold();
68
+ await wait(10000);
69
+
70
+
71
+ let data, logs = [];
72
+
73
+ try {
74
+ ({ data, logs } = await buildGnosisAction(transaction, 'source'));
75
+
76
+ } catch (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];
91
+ transaction.targetStatus = 'failed';
92
+
93
+ transaction.status = 'failed'
94
+ await transaction.save();
95
+ protocol.sendTransaction(transaction)
96
+ return;
97
+ }
98
+
99
+ let gnosisTx = await generateGnosisTransaction({
100
+ baseGas: "0",
101
+ data,
102
+ gasPrice: "0",
103
+ gasToken: "0x0000000000000000000000000000000000000000",
104
+ nonce: '0',
105
+ operation: "1",
106
+ refundReceiver: "0x0000000000000000000000000000000000000000",
107
+ safeAddress: this.safeContractAddress,
108
+ safeTxGas: "79668",
109
+ to: addresses[transaction.sourceChainId].multisend,
110
+ value: "0",
111
+ }, this.safeContract);
112
+
113
+ const owners = await this.safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
114
+
115
+ const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
116
+
117
+ console.log(`Collecting signatures for execution ${transaction.transactionHash}`)
118
+
119
+ console.log(ownerPeerIds);
120
+
121
+ const signatures = await protocol.requestSignatures({
122
+ type: 'source',
123
+ transactionHash: transaction.transactionHash,
124
+ safeTxGas: gnosisTx.safeTxGas,
125
+ safeNonce: gnosisTx.nonce
126
+ }, ownerPeerIds)
127
+
128
+
129
+ const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x') as Signature[];
130
+
131
+ console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
132
+
133
+ if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
134
+ await transaction.save();
135
+ transaction.sourceDelayUntil = new Date(Date.now() + 30 * 1000);
136
+ transaction.sourceStatus = 'uninitialised'
137
+
138
+ await transaction.save();
139
+ const errorMessage = signatures.find(s => !!s.error)?.error;
140
+ throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
141
+ }
142
+
143
+ console.log(`Executing transaction for execution ${transaction.transactionHash}`)
144
+
145
+ const { data: txData } = await this.safeContract.populateTransaction.execTransaction(
146
+ gnosisTx.to,
147
+ gnosisTx.value,
148
+ gnosisTx.data,
149
+ gnosisTx.operation,
150
+ gnosisTx.safeTxGas,
151
+ gnosisTx.baseGas,
152
+ gnosisTx.gasPrice,
153
+ gnosisTx.gasToken,
154
+ gnosisTx.refundReceiver,
155
+ buildSignatureBytes(validSignatures)
156
+ );
157
+
158
+ const txSent = await this.sourceWallet.sendTransaction({
159
+ from: this.sourceWallet.address,
160
+ gasPrice: ethers.BigNumber.from(120 * 10 ** 9),
161
+ to: this.safeContractAddress,
162
+ data: txData,
163
+ })
164
+
165
+ console.log(txSent);
166
+
167
+
168
+ const receipt = await txSent.wait();
169
+
170
+ const parsedLogs: LogDescription[] = [];
171
+
172
+ receipt.logs.forEach((log) => {
173
+ try {
174
+ parsedLogs.push(this.safeContract.interface.parseLog(log));
175
+ } catch (e) { }
176
+ });
177
+
178
+ if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
179
+ console.log('ExecutionSuccess')
180
+ transaction.sourceStatus = 'success'
181
+ if (txSent.blockNumber)
182
+ transaction.sourceBlockNumber = txSent.blockNumber;
183
+ transaction.sourceTransactionHash = txSent.hash
184
+ transaction.sourceLogs = logs;
185
+ await transaction.save();
186
+ } else {
187
+ console.log('ExecutionFailure')
188
+ transaction.sourceStatus = 'failed'
189
+ if (txSent.blockNumber)
190
+ transaction.sourceBlockNumber = txSent.blockNumber;
191
+ transaction.sourceTransactionHash = txSent.hash
192
+ transaction.sourceTransactionHash = txSent.hash
193
+ transaction.status = 'failed'
194
+ await transaction.save();
195
+ }
196
+
197
+ protocol.sendTransaction(transaction)
198
+ }
199
+
200
+ async start(): Promise<void> {
201
+ this.contractAddress = addresses[this.chainId].interopXContract;
202
+
203
+ this.provider = new ethers.providers.JsonRpcProvider(
204
+ getRpcProviderUrl(this.chainId)
205
+ );
206
+
207
+ this.sourceWallet = new ethers.Wallet(config.privateKey!, this.provider);
208
+
209
+ this.contract = getContract<InteropXContract>(
210
+ this.contractAddress,
211
+ abi.interopXContract,
212
+ this.sourceWallet
213
+ );
214
+
215
+ this.safeContractAddress = addresses[this.chainId].gnosisSafe;
216
+ this.safeContract = getContract<GnosisSafe>(
217
+ this.safeContractAddress,
218
+ abi.gnosisSafe,
219
+ this.sourceWallet
220
+ );
221
+
222
+ await super.start()
223
+ }
224
+ }
225
+
226
+ 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;
@@ -0,0 +1,69 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import config from "@/config";
4
+ import { peerPool, protocol } from "@/net";
5
+ import { Transaction } from "@/db";
6
+ import { Op } from "sequelize";
7
+
8
+ class SyncTransactionStatusTask extends BaseTask {
9
+ pollIntervalMs: number = 60 * 1000
10
+ exceptLeadNode: boolean = true;
11
+
12
+ constructor() {
13
+ super({
14
+ logger: new Logger("SyncTransactionStatusTask"),
15
+ })
16
+ }
17
+
18
+ async pollHandler() {
19
+ // if transaction is pending for more than 1 hour, check lead node for status
20
+ const leadNode = peerPool.getLeadPeer();
21
+
22
+ if (!leadNode) {
23
+ return;
24
+ }
25
+
26
+ const transaction = await Transaction.findOne({
27
+ where: {
28
+ status: {
29
+ [Op.ne] : 'success'
30
+ },
31
+ sourceCreatedAt: {
32
+ [Op.gte]: new Date(Date.now() - 60 * 60 * 1000),
33
+ },
34
+ }
35
+ })
36
+
37
+ if (!transaction) {
38
+ return;
39
+ }
40
+
41
+ this.logger.info(`Requesting transaction status for ${transaction.transactionHash}`)
42
+
43
+ const transactionStatus = await protocol.requestTransactionStatus(transaction.transactionHash, leadNode.id);
44
+
45
+ if (!transactionStatus) {
46
+ return;
47
+ }
48
+
49
+ this.logger.info(`Received transaction status for ${transaction.transactionHash}`)
50
+
51
+ transaction.sourceStatus = transactionStatus.sourceStatus
52
+ transaction.sourceTransactionHash = transactionStatus.sourceTransactionHash
53
+ transaction.sourceErrors = transactionStatus.sourceErrors
54
+ transaction.sourceLogs = transactionStatus.sourceLogs
55
+
56
+ transaction.targetStatus = transactionStatus.targetStatus
57
+ transaction.targetTransactionHash = transactionStatus.targetTransactionHash
58
+ transaction.targetErrors = transactionStatus.targetErrors
59
+ transaction.targetLogs = transactionStatus.targetLogs
60
+
61
+ transaction.status = transactionStatus.status
62
+
63
+ await transaction.save()
64
+
65
+ this.logger.info(`Updated transaction status for ${transaction.transactionHash}`)
66
+ }
67
+ }
68
+
69
+ export default SyncTransactionStatusTask;
@@ -1,18 +1,41 @@
1
1
  import { BaseTask } from "./BaseTask";
2
- import SyncInteropXGatewayDepositEvents from "./InteropXGateway/SyncDepositEvents";
2
+ import wait from "waait";
3
+
4
+ import SyncTransactionStatusTask from "./Transactions/SyncTransactionStatusTask";
5
+
6
+ import AutoUpdateTask from "./AutoUpdateTask";
7
+
8
+ import SyncBridgeRequestEvents from "./InteropXContract/SyncBridgeRequestEvents";
9
+ import ProccessBridgeRequestEvents from "./InteropXContract/ProcessBridgeRequestEvents";
10
+ import SyncBridgeRequestSentEvents from "./InteropXContract/SyncBridgeRequestSentEvents";
11
+ import SyncBridgeCommittedEvents from "./InteropXContract/SyncBridgeCommittedEvents";
3
12
 
4
13
  export class Tasks {
5
-
14
+
6
15
  tasks: BaseTask[] = [
7
- new SyncInteropXGatewayDepositEvents({
8
- chainId: 43114
9
- })
16
+ // new SyncTransactionStatusTask(),
17
+ new AutoUpdateTask(),
18
+
19
+ // InteropXContract
20
+
21
+ new SyncBridgeRequestEvents({ chainId: 137 }),
22
+ new SyncBridgeRequestEvents({ chainId: 43114 }),
23
+
24
+ new SyncBridgeRequestSentEvents({ chainId: 137 }),
25
+ new SyncBridgeRequestSentEvents({ chainId: 43114 }),
26
+
27
+ new SyncBridgeCommittedEvents({ chainId: 137 }),
28
+ new SyncBridgeCommittedEvents({ chainId: 43114 }),
29
+
30
+ new ProccessBridgeRequestEvents({ chainId: 137 }),
31
+ new ProccessBridgeRequestEvents({ chainId: 43114 }),
10
32
  ];
11
33
 
12
34
  async start() {
13
35
  for (const task of this.tasks) {
14
36
  try {
15
37
  task.start();
38
+ await wait(1000)
16
39
  } catch (error) {
17
40
  console.error(`Error starting task: ${task.constructor.name}`);
18
41
  }