@instadapp/interop-x 0.0.0-dev.a168c79 → 0.0.0-dev.a8594f9

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.
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.buildDataForTransaction = exports.generateInteropTransactionHash = exports.asyncCallWithTimeout = exports.buildSignatureBytes = exports.getRpcProviderUrl = exports.signGnosisSafeTx = exports.short = exports.http = void 0;
6
+ exports.getContract = exports.buildWithdrawDataForTransaction = exports.buildDepositDataForTransaction = exports.buildDataForTransaction = exports.generateInteropTransactionHash = exports.asyncCallWithTimeout = exports.buildSignatureBytes = exports.getRpcProviderUrl = exports.signGnosisSafeTx = exports.short = exports.http = void 0;
7
7
  /**
8
8
  * @module util
9
9
  */
@@ -104,9 +104,20 @@ const generateInteropTransactionHash = (data) => {
104
104
  exports.generateInteropTransactionHash = generateInteropTransactionHash;
105
105
  const buildDataForTransaction = async (transaction, type) => {
106
106
  type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
107
+ switch (transaction.action) {
108
+ case "deposit":
109
+ return await (0, exports.buildDepositDataForTransaction)(transaction, type);
110
+ case "withdraw":
111
+ return await (0, exports.buildWithdrawDataForTransaction)(transaction, type);
112
+ default:
113
+ throw new Error(`Unknown action: ${transaction.action}`);
114
+ }
115
+ };
116
+ exports.buildDataForTransaction = buildDataForTransaction;
117
+ const buildDepositDataForTransaction = async (transaction, type) => {
107
118
  const transactions = [];
108
- if (transaction.action != 'deposit') {
109
- throw new Error('Invalid action');
119
+ if (transaction.action !== 'deposit') {
120
+ throw new Error(`Invalid action: ${transaction.action}`);
110
121
  }
111
122
  if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
112
123
  throw Error('Cannot build data for pending deposit transaction');
@@ -124,8 +135,8 @@ const buildDataForTransaction = async (transaction, type) => {
124
135
  }
125
136
  const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, exports.getRpcProviderUrl)(transaction.targetChainId));
126
137
  const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
127
- const interopBridgeContract = new ethers_1.ethers.Contract(itoken.address, abi_1.default.interopBridgeToken, targetWallet);
128
- const { data } = await interopBridgeContract.populateTransaction.mint(transaction.submitEvent.to, transaction.submitEvent.amount, transaction.sourceChainId, transaction.sourceTransactionHash);
138
+ const interopBridgeContract = getContract(itoken.address, abi_1.default.interopBridgeToken, targetWallet);
139
+ const { data } = await interopBridgeContract.populateTransaction.mint(transaction.submitEvent.user, ethers_1.ethers.BigNumber.from(transaction.submitEvent.amount.toString()), ethers_1.ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()), transaction.submitTransactionHash);
129
140
  transactions.push({
130
141
  to: itoken.address,
131
142
  data: data,
@@ -134,4 +145,79 @@ const buildDataForTransaction = async (transaction, type) => {
134
145
  });
135
146
  return (0, ethers_multisend_1.encodeMulti)(transactions).data;
136
147
  };
137
- exports.buildDataForTransaction = buildDataForTransaction;
148
+ exports.buildDepositDataForTransaction = buildDepositDataForTransaction;
149
+ const buildWithdrawDataForTransaction = async (transaction, type) => {
150
+ const transactions = [];
151
+ if (transaction.action !== 'withdraw') {
152
+ throw new Error(`Invalid action: ${transaction.action}`);
153
+ }
154
+ if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
155
+ throw Error('Cannot build data for pending withdraw transaction');
156
+ }
157
+ if (!transaction.submitEvent) {
158
+ throw Error('Cannot build data for transaction without submitEvent');
159
+ }
160
+ const { to, amount, chainId, itokenAddress } = transaction.submitEvent;
161
+ const itoken = constants_1.itokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === itokenAddress.toLowerCase());
162
+ if (!itoken) {
163
+ throw Error('Cannot build data for transaction without itoken');
164
+ }
165
+ const token = constants_1.tokens[chainId].find(t => t.symbol.toLowerCase() === itoken.symbol.toLowerCase());
166
+ if (!token) {
167
+ throw Error('Cannot build data for transaction without token');
168
+ }
169
+ const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, exports.getRpcProviderUrl)(transaction.targetChainId));
170
+ const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
171
+ const gatewayAddress = constants_1.addresses[chainId].interopXGateway;
172
+ const interopBridgeContract = getContract(gatewayAddress, abi_1.default.interopXGateway, targetWallet);
173
+ const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(ethers_1.ethers.BigNumber.from(amount.toString()), to, token.address, ethers_1.ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()), transaction.submitTransactionHash);
174
+ transactions.push({
175
+ to: gatewayAddress,
176
+ data: data,
177
+ value: '0',
178
+ operation: ethers_multisend_1.OperationType.Call,
179
+ });
180
+ return (0, ethers_multisend_1.encodeMulti)(transactions).data;
181
+ };
182
+ exports.buildWithdrawDataForTransaction = buildWithdrawDataForTransaction;
183
+ function getContract(address, contractInterface, signerOrProvider) {
184
+ if (!ethers_1.ethers.utils.getAddress(address) || address === ethers_1.ethers.constants.AddressZero) {
185
+ throw Error(`Invalid 'address' parameter '${address}'.`);
186
+ }
187
+ const contract = new ethers_1.ethers.Contract(address, contractInterface, signerOrProvider);
188
+ return new Proxy(contract, {
189
+ get(target, prop, receiver) {
190
+ const value = Reflect.get(target, prop, receiver);
191
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
192
+ return async (...args) => {
193
+ try {
194
+ return await value.bind(contract)(...args);
195
+ }
196
+ catch (error) {
197
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`);
198
+ }
199
+ };
200
+ }
201
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
202
+ const parentProp = String(prop);
203
+ return new Proxy(value, {
204
+ get(target, prop, receiver) {
205
+ const value = Reflect.get(target, prop, receiver);
206
+ if (typeof value === 'function') {
207
+ return async (...args) => {
208
+ try {
209
+ return await value.bind(contract)(...args);
210
+ }
211
+ catch (error) {
212
+ throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`);
213
+ }
214
+ };
215
+ }
216
+ }
217
+ });
218
+ }
219
+ return value;
220
+ },
221
+ });
222
+ }
223
+ exports.getContract = getContract;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/interop-x",
3
- "version": "0.0.0-dev.a168c79",
3
+ "version": "0.0.0-dev.a8594f9",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -12,7 +12,8 @@
12
12
  "build": "yarn generate-abi-types && export GIT_REF=$(git rev-parse --short HEAD) && rimraf ./dist && tsc -p tsconfig.json && replace-in-file '@GIT_SHORT_HASH@' $GIT_REF ./dist/**/*.js",
13
13
  "dev": "yarn generate-abi-types && NODE_ENV=development nodemon",
14
14
  "generate-abi-types": "typechain --target=ethers-v5 'src/abi/*.json' --out-dir 'src/typechain'",
15
- "prepublishOnly": "yarn build"
15
+ "prepublishOnly": "yarn build",
16
+ "postinstall": "patch-package"
16
17
  },
17
18
  "nodemonConfig": {
18
19
  "watch": [
@@ -45,7 +46,7 @@
45
46
  "libp2p-websockets": "^0.16.2",
46
47
  "luxon": "^2.3.2",
47
48
  "module-alias": "^2.2.2",
48
- "sequelize": "^6.19.0",
49
+ "sequelize": "6.18.0",
49
50
  "sqlite3": "^5.0.5",
50
51
  "waait": "^1.0.5"
51
52
  },
@@ -59,6 +60,8 @@
59
60
  "@types/fs-extra": "^9.0.13",
60
61
  "@types/node": "^17.0.17",
61
62
  "nodemon": "^2.0.15",
63
+ "patch-package": "^6.4.7",
64
+ "postinstall-postinstall": "^2.1.0",
62
65
  "replace-in-file": "^6.3.2",
63
66
  "rimraf": "^3.0.2",
64
67
  "ts-node": "^10.5.0",
@@ -0,0 +1,13 @@
1
+ diff --git a/node_modules/@ethersproject/properties/lib/index.js b/node_modules/@ethersproject/properties/lib/index.js
2
+ index 41e0b52..4c7a9e3 100644
3
+ --- a/node_modules/@ethersproject/properties/lib/index.js
4
+ +++ b/node_modules/@ethersproject/properties/lib/index.js
5
+ @@ -44,7 +44,7 @@ function defineReadOnly(object, name, value) {
6
+ Object.defineProperty(object, name, {
7
+ enumerable: true,
8
+ value: value,
9
+ - writable: false,
10
+ + writable: true,
11
+ });
12
+ }
13
+ exports.defineReadOnly = defineReadOnly;
@@ -2,7 +2,7 @@ export const itokens = {
2
2
  1: [],
3
3
  137: [
4
4
  {
5
- address: '0x6c20F03598d5ABF729348E2868b0ff5e8A48aB1F',
5
+ address: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
6
6
  symbol: 'USDC',
7
7
  }
8
8
  ],
@@ -88,12 +88,13 @@ export const startPeer = async ({ }: IPeerOptions) => {
88
88
  libp2p: node
89
89
  })
90
90
 
91
- node.on("peer:discovery", (peer) =>
92
- logger.log(`Discovered peer ${peer}`)
93
- ); // peer disc.
94
- node.connectionManager.on("peer:connect", (connection) =>
95
- logger.log(`Connected to ${connection.remotePeer.toB58String()}`)
96
- );
91
+ node.on("peer:discovery", (peer) => {
92
+ // logger.log(`Discovered peer ${peer}`)
93
+ }); // peer disc.
94
+
95
+ node.connectionManager.on("peer:connect", (connection) => {
96
+ // logger.log(`Connected to ${connection.remotePeer.toB58String()}`)
97
+ });
97
98
 
98
99
  logger.log("Peer discovery started");
99
100
 
@@ -1,5 +1,9 @@
1
1
  import { Event } from "@/types";
2
2
  import config from "@/config";
3
+ import Logger from "@/logger";
4
+
5
+
6
+ const logger = new Logger('PeerPool')
3
7
 
4
8
  export interface IPeerInfo {
5
9
  id: string;
@@ -75,10 +79,15 @@ export class PeerPool {
75
79
  * @emits {@link Event.POOL_PEER_ADDED}
76
80
  */
77
81
  add(peer?: IPeerInfo) {
78
- if (peer && peer.id && !this.pool.get(peer.id)) {
82
+ if (peer && peer.id) {
83
+ const newPeer = !this.pool.get(peer.id);
79
84
  this.pool.set(peer.id, peer)
80
85
  peer.pooled = true
81
- config.events.emit(Event.POOL_PEER_ADDED, peer)
86
+
87
+ if(newPeer) {
88
+ config.events.emit(Event.POOL_PEER_ADDED, peer)
89
+ logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`)
90
+ }
82
91
  }
83
92
  }
84
93
 
@@ -92,6 +101,7 @@ export class PeerPool {
92
101
  if (this.pool.delete(peer.id)) {
93
102
  peer.pooled = false
94
103
  config.events.emit(Event.POOL_PEER_REMOVED, peer)
104
+ logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`)
95
105
  }
96
106
  }
97
107
  }
@@ -114,14 +124,14 @@ export class PeerPool {
114
124
 
115
125
 
116
126
  cleanup() {
117
- let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
118
-
119
- this.peers.forEach((peerInfo) => {
120
- if (peerInfo.updated.getTime() < compDate) {
121
- console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
122
- this.remove(peerInfo)
123
- }
124
- })
127
+ // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
128
+
129
+ // this.peers.forEach((peerInfo) => {
130
+ // if (peerInfo.updated.getTime() < compDate) {
131
+ // console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
132
+ // this.remove(peerInfo)
133
+ // }
134
+ // })
125
135
  }
126
136
  }
127
137
 
@@ -47,12 +47,20 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
47
47
  };
48
48
  }
49
49
 
50
+ console.log("signing:", {
51
+ to: addresses[transaction.targetChainId].multisend,
52
+ data: await buildDataForTransaction(transaction, data.type),
53
+ chainId: transaction.targetChainId as ChainId,
54
+ safeTxGas: data.safeTxGas,
55
+ nonce: data.safeNonce,
56
+ });
57
+
50
58
  const signedData = await signGnosisSafeTx({
51
- //TODO: chain id depends on event type
52
- to: addresses[transaction.sourceChainId].multisend,
53
- data: buildDataForTransaction(transaction, data.type),
54
- chainId: transaction.sourceChainId as ChainId,
59
+ to: addresses[transaction.targetChainId].multisend,
60
+ data: await buildDataForTransaction(transaction, data.type),
61
+ chainId: transaction.targetChainId as ChainId,
55
62
  safeTxGas: data.safeTxGas,
63
+ nonce: data.safeNonce,
56
64
  }, { signer });
57
65
 
58
66
  return {
@@ -37,7 +37,7 @@ export class BaseTask extends EventEmitter implements IBaseTask {
37
37
  await this.pollHandler()
38
38
  }
39
39
  } catch (err) {
40
- this.logger.error(`poll check error: ${err.message}\ntrace: ${err.stack}`)
40
+ this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`)
41
41
  }
42
42
 
43
43
  await this.postPollHandler()
@@ -0,0 +1,231 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import { BigNumber, ethers } from "ethers";
4
+ import abi from "@/abi";
5
+ import { Transaction } from "@/db";
6
+ import { buildDataForTransaction, buildSignatureBytes, getContract, getRpcProviderUrl, Signature } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { ChainId } from "@/types";
9
+ import config from "@/config";
10
+ import { GnosisSafe, InteropXGateway } from "@/typechain";
11
+ import { Op } from "sequelize";
12
+ import wait from "waait";
13
+ import { peerPool, protocol } from "@/net";
14
+ import { LogDescription } from "ethers/lib/utils";
15
+
16
+ const generateGnosisTransaction = async (transactionData: any, safeContract: GnosisSafe) => {
17
+ console.log(transactionData);
18
+
19
+ let isExecuted = await safeContract.dataHashes(
20
+ await safeContract.getTransactionHash(
21
+ transactionData.to,
22
+ transactionData.value,
23
+ transactionData.data,
24
+ transactionData.operation,
25
+ transactionData.safeTxGas,
26
+ transactionData.baseGas,
27
+ transactionData.gasPrice,
28
+ transactionData.gasToken,
29
+ transactionData.refundReceiver,
30
+ transactionData.nonce
31
+ )
32
+ )
33
+
34
+ while (isExecuted == 1) {
35
+ transactionData.safeTxGas = BigNumber.from(String(transactionData.safeTxGas)).add(1).toString()
36
+
37
+ isExecuted = await safeContract.dataHashes(
38
+ await safeContract.getTransactionHash(
39
+ transactionData.to,
40
+ transactionData.value,
41
+ transactionData.data,
42
+ transactionData.operation,
43
+ transactionData.safeTxGas,
44
+ transactionData.baseGas,
45
+ transactionData.gasPrice,
46
+ transactionData.gasToken,
47
+ transactionData.refundReceiver,
48
+ transactionData.nonce
49
+ )
50
+ )
51
+ }
52
+
53
+ return transactionData
54
+ }
55
+
56
+ class ProcessWithdrawEvents extends BaseTask {
57
+ provider: ethers.providers.JsonRpcProvider;
58
+ chainId: ChainId;
59
+ leadNodeOnly = true
60
+
61
+ constructor({ chainId }: { chainId: ChainId }) {
62
+ super({
63
+ logger: new Logger("InteropXGateway::ProcessWithdrawEvents"),
64
+ })
65
+ this.chainId = chainId;
66
+ }
67
+
68
+ async pollHandler() {
69
+ const blockNumber = await this.provider.getBlockNumber()
70
+
71
+ const transaction = await Transaction.findOne({
72
+ where: {
73
+ status: 'pending',
74
+ sourceStatus: 'success',
75
+ targetStatus: 'uninitialised',
76
+ action: 'withdraw',
77
+ sourceCreatedAt: {
78
+ [Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
79
+ },
80
+ targetDelayUntil: {
81
+ [Op.or]: {
82
+ [Op.is]: null,
83
+ [Op.lt]: new Date(),
84
+ }
85
+ },
86
+ sourceBlockNumber: {
87
+ [Op.lt]: blockNumber - 12,
88
+ },
89
+ sourceChainId: this.chainId,
90
+ }
91
+ })
92
+
93
+ if (!transaction) {
94
+ return;
95
+ }
96
+
97
+ console.log(`Processing transaction ${transaction.transactionHash}`);
98
+
99
+ transaction.targetStatus = 'pending';
100
+ await transaction.save();
101
+
102
+ // refresh event data?
103
+
104
+ const targetChainProvider = new ethers.providers.JsonRpcProvider(
105
+ getRpcProviderUrl(transaction.targetChainId as ChainId)
106
+ );
107
+
108
+ const targetWallet = new ethers.Wallet(config.privateKey!, targetChainProvider);
109
+
110
+ const safeAddress = addresses[transaction.targetChainId].gnosisSafe;
111
+
112
+
113
+ const safeContract = getContract<GnosisSafe>(
114
+ safeAddress,
115
+ abi.gnosisSafe,
116
+ targetWallet
117
+ )
118
+
119
+ const ownersThreshold = await safeContract.getThreshold();
120
+ await wait(10000);
121
+
122
+ let gnosisTx = await generateGnosisTransaction({
123
+ baseGas: "0",
124
+ data: await buildDataForTransaction(transaction),
125
+ gasPrice: "0",
126
+ gasToken: "0x0000000000000000000000000000000000000000",
127
+ nonce: '0',
128
+ operation: "1",
129
+ refundReceiver: "0x0000000000000000000000000000000000000000",
130
+ safeAddress: safeAddress,
131
+ safeTxGas: "79668",
132
+ to: addresses[transaction.targetChainId].multisend,
133
+ value: "0",
134
+ }, safeContract);
135
+
136
+ const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
137
+
138
+ const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
139
+
140
+ console.log(`Collecting signatures for execution ${transaction.transactionHash}`)
141
+
142
+ console.log(ownerPeerIds);
143
+
144
+ const signatures = await protocol.requestSignatures({
145
+ type: 'source',
146
+ transactionHash: transaction.transactionHash,
147
+ safeTxGas: gnosisTx.safeTxGas,
148
+ safeNonce: gnosisTx.nonce
149
+ }, ownerPeerIds)
150
+
151
+
152
+ const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x') as Signature[];
153
+
154
+ console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
155
+
156
+ if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
157
+ await transaction.save();
158
+ transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
159
+ transaction.targetStatus = 'uninitialised'
160
+
161
+ await transaction.save();
162
+ const errorMessage = signatures.find(s => !!s.error)?.error;
163
+ throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
164
+ }
165
+
166
+
167
+ console.log(`Executing transaction for execution ${transaction.transactionHash}`)
168
+
169
+ const { data: txData } = await safeContract.populateTransaction.execTransaction(
170
+ gnosisTx.to,
171
+ gnosisTx.value,
172
+ gnosisTx.data,
173
+ gnosisTx.operation,
174
+ gnosisTx.safeTxGas,
175
+ gnosisTx.baseGas,
176
+ gnosisTx.gasPrice,
177
+ gnosisTx.gasToken,
178
+ gnosisTx.refundReceiver,
179
+ buildSignatureBytes(validSignatures)
180
+ );
181
+
182
+ console.log({
183
+ from: targetWallet.address,
184
+ gasPrice: BigNumber.from(120 * 10 ** 9).toString(),
185
+ to: safeAddress,
186
+ data: txData,
187
+ })
188
+
189
+
190
+ const txSent = await targetWallet.sendTransaction({
191
+ from: targetWallet.address,
192
+ gasPrice: BigNumber.from(120 * 10 ** 9),
193
+ to: safeAddress,
194
+ data: txData,
195
+ })
196
+
197
+ const receipt = await txSent.wait();
198
+
199
+ const parsedLogs: LogDescription[] = [];
200
+
201
+ receipt.logs.forEach((log) => {
202
+ try {
203
+ parsedLogs.push(safeContract.interface.parseLog(log));
204
+ } catch (e) { }
205
+ });
206
+
207
+ if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
208
+ console.log('ExecutionSuccess')
209
+ transaction.targetStatus = 'success'
210
+ transaction.status = 'success'
211
+ await transaction.save();
212
+ } else {
213
+ console.log('ExecutionFailure')
214
+ transaction.targetStatus = 'failed'
215
+ transaction.status = 'failed'
216
+ await transaction.save();
217
+ }
218
+ }
219
+
220
+ async start(): Promise<void> {
221
+ this.logger.info(`Starting execution watcher on interop chain`);
222
+
223
+ this.provider = new ethers.providers.JsonRpcProvider(
224
+ getRpcProviderUrl(this.chainId)
225
+ );
226
+
227
+ await super.start()
228
+ }
229
+ }
230
+
231
+ export default ProcessWithdrawEvents;
@@ -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 { ChainId } from "@/types";
8
+ import config from "@/config";
9
+ import { InteropBridgeToken } from "@/typechain";
10
+
11
+ class SyncWithdrawEvents extends BaseTask {
12
+ contractAddress: string;
13
+ provider: ethers.providers.JsonRpcProvider;
14
+ contract: InteropBridgeToken;
15
+ chainId: ChainId;
16
+ itokenAddress: string;
17
+
18
+ constructor({ chainId, itokenAddress }: { chainId: ChainId, itokenAddress: string }) {
19
+ super({
20
+ logger: new Logger("InteropBridgeToken::SyncWithdrawEvents"),
21
+ })
22
+ this.chainId = chainId;
23
+ this.itokenAddress = itokenAddress;
24
+ }
25
+
26
+ async pollHandler() {
27
+ const currentBlock = await this.provider.getBlockNumber();
28
+
29
+ const events = await this.contract.queryFilter(
30
+ this.contract.filters.Burn(),
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 { to, amount, chainId } = event.args;
45
+
46
+ const uniqueIdentifier = {
47
+ action: 'withdraw',
48
+ submitTransactionHash: event.transactionHash,
49
+ sourceChainId:this.chainId,
50
+ targetChainId: chainId.toNumber(),
51
+ }
52
+
53
+ if (await Transaction.findOne({ where: uniqueIdentifier })) {
54
+ continue;
55
+ }
56
+
57
+ const tx = await event.getTransaction()
58
+
59
+ await Transaction.create({
60
+ ...uniqueIdentifier,
61
+ transactionHash: generateInteropTransactionHash(uniqueIdentifier),
62
+ from: tx.from,
63
+ to,
64
+
65
+
66
+ submitTransactionHash: event.transactionHash,
67
+ submitBlockNumber: event.blockNumber,
68
+
69
+ // submit & source are the same
70
+ sourceTransactionHash: event.transactionHash,
71
+ sourceBlockNumber: event.blockNumber,
72
+ sourceStatus: "success",
73
+
74
+ targetStatus: "uninitialised",
75
+
76
+ submitEvent: {
77
+ to,
78
+ amount: amount.toString(),
79
+ itoken: this.itokenAddress,
80
+ chainId: chainId.toString()
81
+ },
82
+
83
+ sourceEvent: {
84
+ to,
85
+ amount: amount.toString(),
86
+ itoken: this.itokenAddress,
87
+ chainId: chainId.toString(),
88
+ },
89
+ status: "pending",
90
+ })
91
+
92
+ this.logger.info(
93
+ `Withdraw queued: ${event.transactionHash} ${event.blockNumber}`
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.logger.info(`Starting execution watcher on interop chain`);
106
+
107
+ this.provider = new ethers.providers.JsonRpcProvider(
108
+ getRpcProviderUrl(this.chainId)
109
+ );
110
+
111
+ this.contract = getContract<InteropBridgeToken>(
112
+ this.itokenAddress,
113
+ abi.interopBridgeToken,
114
+ new ethers.Wallet(config.privateKey!, this.provider)
115
+ );
116
+
117
+ await super.start()
118
+ }
119
+ }
120
+
121
+ export default SyncWithdrawEvents;