@instadapp/interop-x 0.0.0-dev.6ea4ee5 → 0.0.0-dev.733ff78

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. package/bin/interop-x +1 -1
  2. package/dist/package.json +73 -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 +0 -0
  6. package/dist/{abi → src/abi}/interopBridgeToken.json +21 -9
  7. package/dist/{abi → src/abi}/interopXGateway.json +11 -11
  8. package/dist/src/api/index.js +36 -0
  9. package/dist/{config → src/config}/index.js +11 -1
  10. package/dist/{constants → src/constants}/addresses.js +1 -9
  11. package/dist/{constants → src/constants}/index.js +1 -0
  12. package/dist/src/constants/itokens.js +13 -0
  13. package/dist/{constants → src/constants}/tokens.js +0 -0
  14. package/dist/{db → src/db}/index.js +0 -0
  15. package/dist/{db → src/db}/models/index.js +0 -0
  16. package/dist/{db → src/db}/models/transaction.js +11 -1
  17. package/dist/{db → src/db}/sequelize.js +1 -1
  18. package/dist/src/gnosis/actions/deposit.js +48 -0
  19. package/dist/src/gnosis/actions/index.js +11 -0
  20. package/dist/src/gnosis/actions/withdraw.js +50 -0
  21. package/dist/src/gnosis/index.js +20 -0
  22. package/dist/src/index.js +133 -0
  23. package/dist/{logger → src/logger}/index.js +0 -0
  24. package/dist/{net → src/net}/index.js +0 -0
  25. package/dist/{net → src/net}/peer/index.js +8 -3
  26. package/dist/{net → src/net}/pool/index.js +32 -9
  27. package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +0 -0
  28. package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +17 -12
  29. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
  30. package/dist/{net → src/net}/protocol/index.js +51 -1
  31. package/dist/src/tasks/AutoUpdateTask.js +70 -0
  32. package/dist/{tasks → src/tasks}/BaseTask.js +12 -4
  33. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +162 -0
  34. package/dist/src/tasks/InteropBridge/SyncBurnEvents.js +71 -0
  35. package/dist/src/tasks/InteropBridge/SyncMintEvents.js +67 -0
  36. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +164 -0
  37. package/dist/{tasks → src/tasks}/InteropXGateway/SyncDepositEvents.js +18 -23
  38. package/dist/src/tasks/InteropXGateway/SyncWithdrawtEvents.js +72 -0
  39. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +55 -0
  40. package/dist/src/tasks/index.js +55 -0
  41. package/dist/{typechain → src/typechain}/Erc20.js +0 -0
  42. package/dist/{typechain → src/typechain}/GnosisSafe.js +0 -0
  43. package/dist/{typechain → src/typechain}/InteropBridgeToken.js +0 -0
  44. package/dist/{typechain → src/typechain}/InteropXGateway.js +0 -0
  45. package/dist/{typechain → src/typechain}/common.js +0 -0
  46. package/dist/{typechain → src/typechain}/factories/Erc20__factory.js +0 -0
  47. package/dist/{typechain → src/typechain}/factories/GnosisSafe__factory.js +0 -0
  48. package/dist/{typechain → src/typechain}/factories/InteropBridgeToken__factory.js +23 -11
  49. package/dist/{typechain → src/typechain}/factories/InteropXGateway__factory.js +14 -14
  50. package/dist/{typechain → src/typechain}/factories/index.js +0 -0
  51. package/dist/{typechain → src/typechain}/index.js +0 -0
  52. package/dist/{types.js → src/types.js} +0 -0
  53. package/dist/{utils → src/utils}/index.js +62 -6
  54. package/package.json +12 -5
  55. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  56. package/src/abi/interopBridgeToken.json +21 -9
  57. package/src/abi/interopXGateway.json +11 -11
  58. package/src/api/index.ts +36 -0
  59. package/src/config/index.ts +11 -1
  60. package/src/constants/addresses.ts +1 -9
  61. package/src/constants/index.ts +1 -0
  62. package/src/constants/itokens.ts +10 -0
  63. package/src/db/models/transaction.ts +18 -4
  64. package/src/db/sequelize.ts +1 -1
  65. package/src/gnosis/actions/deposit.ts +63 -0
  66. package/src/gnosis/actions/index.ts +7 -0
  67. package/src/gnosis/actions/withdraw.ts +67 -0
  68. package/src/gnosis/index.ts +19 -0
  69. package/src/index.ts +99 -8
  70. package/src/net/peer/index.ts +9 -7
  71. package/src/net/pool/index.ts +41 -11
  72. package/src/net/protocol/dial/SignatureDialProtocol.ts +19 -13
  73. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  74. package/src/net/protocol/index.ts +67 -1
  75. package/src/tasks/AutoUpdateTask.ts +82 -0
  76. package/src/tasks/BaseTask.ts +14 -4
  77. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +249 -0
  78. package/src/tasks/InteropBridge/SyncBurnEvents.ts +119 -0
  79. package/src/tasks/InteropBridge/SyncMintEvents.ts +99 -0
  80. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +260 -0
  81. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +25 -15
  82. package/src/tasks/InteropXGateway/SyncWithdrawtEvents.ts +105 -0
  83. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +67 -0
  84. package/src/tasks/index.ts +43 -2
  85. package/src/typechain/InteropBridgeToken.ts +23 -17
  86. package/src/typechain/InteropXGateway.ts +13 -13
  87. package/src/typechain/factories/InteropBridgeToken__factory.ts +23 -11
  88. package/src/typechain/factories/InteropXGateway__factory.ts +14 -14
  89. package/src/utils/index.ts +76 -7
  90. package/dist/index.js +0 -63
  91. package/dist/tasks/index.js +0 -27
@@ -0,0 +1,249 @@
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 { buildSignatureBytes, getContract, getRpcProviderUrl, Signature } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { ChainId } from "@/types";
9
+ import config from "@/config";
10
+ import { GnosisSafe } 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
+ 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
+
57
+ class ProcessWithdrawEvents extends BaseTask {
58
+ provider: ethers.providers.JsonRpcProvider;
59
+ chainId: ChainId;
60
+ leadNodeOnly = true
61
+
62
+ constructor({ chainId }: { chainId: ChainId }) {
63
+ super({
64
+ logger: new Logger("InteropXGateway::ProcessWithdrawEvents"),
65
+ })
66
+ this.chainId = chainId;
67
+ }
68
+
69
+ async pollHandler() {
70
+ const blockNumber = await this.provider.getBlockNumber()
71
+
72
+ const transaction = await Transaction.findOne({
73
+ where: {
74
+ status: 'pending',
75
+ sourceStatus: 'success',
76
+ targetStatus: 'uninitialised',
77
+ action: 'withdraw',
78
+ sourceCreatedAt: {
79
+ [Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
80
+ },
81
+ targetDelayUntil: {
82
+ [Op.or]: {
83
+ [Op.is]: null,
84
+ [Op.lt]: new Date(),
85
+ }
86
+ },
87
+ sourceBlockNumber: {
88
+ [Op.lt]: blockNumber - 12,
89
+ },
90
+ sourceChainId: this.chainId,
91
+ }
92
+ })
93
+
94
+ if (!transaction) {
95
+ return;
96
+ }
97
+
98
+ console.log(`Processing transaction ${transaction.transactionHash}`);
99
+
100
+ transaction.targetStatus = 'pending';
101
+ await transaction.save();
102
+
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();
121
+ await wait(10000);
122
+
123
+ let data, logs = [];
124
+
125
+ try {
126
+ ({ data, logs } = await buildGnosisAction(transaction));
127
+ } catch (error) {
128
+ console.log(error);
129
+ transaction.targetStatus = 'failed';
130
+ transaction.targetErrors = [error.message];
131
+ transaction.status = 'failed'
132
+ await transaction.save();
133
+ protocol.sendTransaction(transaction)
134
+ return;
135
+ }
136
+
137
+ let gnosisTx = await generateGnosisTransaction({
138
+ baseGas: "0",
139
+ data,
140
+ gasPrice: "0",
141
+ gasToken: "0x0000000000000000000000000000000000000000",
142
+ nonce: '0',
143
+ operation: "1",
144
+ refundReceiver: "0x0000000000000000000000000000000000000000",
145
+ safeAddress: safeAddress,
146
+ safeTxGas: "79668",
147
+ to: addresses[transaction.targetChainId].multisend,
148
+ value: "0",
149
+ }, safeContract);
150
+
151
+ const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
152
+
153
+ const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
154
+
155
+ console.log(`Collecting signatures for execution ${transaction.transactionHash}`)
156
+
157
+ console.log(ownerPeerIds);
158
+
159
+ const signatures = await protocol.requestSignatures({
160
+ type: 'source',
161
+ transactionHash: transaction.transactionHash,
162
+ safeTxGas: gnosisTx.safeTxGas,
163
+ safeNonce: gnosisTx.nonce
164
+ }, ownerPeerIds)
165
+
166
+
167
+ const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x') as Signature[];
168
+
169
+ console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
170
+
171
+ if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
172
+ await transaction.save();
173
+ transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
174
+ transaction.targetStatus = 'uninitialised'
175
+
176
+ await transaction.save();
177
+ const errorMessage = signatures.find(s => !!s.error)?.error;
178
+ throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
179
+ }
180
+
181
+
182
+ console.log(`Executing transaction for execution ${transaction.transactionHash}`)
183
+
184
+ const { data: txData } = await safeContract.populateTransaction.execTransaction(
185
+ gnosisTx.to,
186
+ gnosisTx.value,
187
+ gnosisTx.data,
188
+ gnosisTx.operation,
189
+ gnosisTx.safeTxGas,
190
+ gnosisTx.baseGas,
191
+ gnosisTx.gasPrice,
192
+ gnosisTx.gasToken,
193
+ gnosisTx.refundReceiver,
194
+ buildSignatureBytes(validSignatures)
195
+ );
196
+
197
+ console.log({
198
+ from: targetWallet.address,
199
+ gasPrice: BigNumber.from(120 * 10 ** 9).toString(),
200
+ to: safeAddress,
201
+ data: txData,
202
+ })
203
+
204
+
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
+
212
+ const receipt = await txSent.wait();
213
+
214
+ const parsedLogs: LogDescription[] = [];
215
+
216
+ receipt.logs.forEach((log) => {
217
+ try {
218
+ parsedLogs.push(safeContract.interface.parseLog(log));
219
+ } catch (e) { }
220
+ });
221
+
222
+ if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
223
+ console.log('ExecutionSuccess')
224
+ transaction.targetStatus = 'success'
225
+ transaction.targetTransactionHash = txSent.hash
226
+ transaction.targetLogs = logs
227
+ transaction.status = 'success'
228
+ await transaction.save();
229
+ } else {
230
+ console.log('ExecutionFailure')
231
+ transaction.targetStatus = 'failed'
232
+ transaction.targetTransactionHash = txSent.hash
233
+ transaction.status = 'failed'
234
+ await transaction.save();
235
+ }
236
+
237
+ protocol.sendTransaction(transaction)
238
+ }
239
+
240
+ async start(): Promise<void> {
241
+ this.provider = new ethers.providers.JsonRpcProvider(
242
+ getRpcProviderUrl(this.chainId)
243
+ );
244
+
245
+ await super.start()
246
+ }
247
+ }
248
+
249
+ export default ProcessWithdrawEvents;
@@ -0,0 +1,119 @@
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 SyncBurnEvents 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::SyncBurnEvents"),
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, sourceChainId, targetChainId } = event.args;
45
+
46
+ const uniqueIdentifier = {
47
+ action: 'withdraw',
48
+ submitTransactionHash: event.transactionHash,
49
+ sourceChainId: sourceChainId,
50
+ targetChainId: targetChainId,
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
+ submitTransactionHash: event.transactionHash,
66
+ submitBlockNumber: event.blockNumber,
67
+
68
+ // submit & source are the same
69
+ sourceTransactionHash: event.transactionHash,
70
+ sourceBlockNumber: event.blockNumber,
71
+ sourceStatus: "success",
72
+ targetStatus: "uninitialised",
73
+
74
+ submitEvent: {
75
+ to,
76
+ amount: amount.toString(),
77
+ itoken: this.itokenAddress,
78
+ sourceChainId: sourceChainId,
79
+ targetChainId: targetChainId,
80
+ },
81
+
82
+ sourceEvent: {
83
+ to,
84
+ amount: amount.toString(),
85
+ itoken: this.itokenAddress,
86
+ sourceChainId: sourceChainId,
87
+ targetChainId: targetChainId,
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.provider = new ethers.providers.JsonRpcProvider(
106
+ getRpcProviderUrl(this.chainId)
107
+ );
108
+
109
+ this.contract = getContract<InteropBridgeToken>(
110
+ this.itokenAddress,
111
+ abi.interopBridgeToken,
112
+ new ethers.Wallet(config.privateKey!, this.provider)
113
+ );
114
+
115
+ await super.start()
116
+ }
117
+ }
118
+
119
+ export default SyncBurnEvents;
@@ -0,0 +1,99 @@
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 { getContract, getRpcProviderUrl } from "@/utils";
7
+ import { ChainId } from "@/types";
8
+ import config from "@/config";
9
+ import { InteropBridgeToken } from "@/typechain";
10
+
11
+ class SyncMintEvents 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::SyncMintEvents"),
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.Mint(),
31
+ currentBlock - 500,
32
+ currentBlock,
33
+ );
34
+
35
+ for (const event of events) {
36
+
37
+ try {
38
+ if (!event.args) {
39
+ continue;
40
+ }
41
+
42
+ const { sourceChainId, targetChainId, amount, to, submitTransactionHash } = event.args;
43
+
44
+ const uniqueIdentifier = {
45
+ action: 'deposit',
46
+ submitTransactionHash: submitTransactionHash,
47
+ sourceChainId: sourceChainId,
48
+ targetChainId: targetChainId,
49
+
50
+ targetEvent: null
51
+ }
52
+
53
+ const transaction = await Transaction.findOne({ where: uniqueIdentifier });
54
+
55
+ if(! transaction){
56
+ return;
57
+ }
58
+
59
+ const tx = await event.getTransaction()
60
+
61
+ transaction.targetStatus = 'success'
62
+ transaction.targetErrors = []
63
+ transaction.targetTransactionHash = tx.hash
64
+ transaction.targetEvent = {
65
+ sourceChainId,
66
+ targetChainId,
67
+ amount: amount.toString(),
68
+ to,
69
+ submitTransactionHash
70
+ }
71
+ transaction.status = 'success'
72
+
73
+ await transaction.save()
74
+
75
+ this.logger.info(
76
+ `Mint confirmation received: ${transaction.transactionHash} `
77
+ );
78
+ } catch (error) {
79
+ this.logger.error(error);
80
+ }
81
+ }
82
+ }
83
+
84
+ async start(): Promise<void> {
85
+ this.provider = new ethers.providers.JsonRpcProvider(
86
+ getRpcProviderUrl(this.chainId)
87
+ );
88
+
89
+ this.contract = getContract<InteropBridgeToken>(
90
+ this.itokenAddress,
91
+ abi.interopBridgeToken,
92
+ new ethers.Wallet(config.privateKey!, this.provider)
93
+ );
94
+
95
+ await super.start()
96
+ }
97
+ }
98
+
99
+ export default SyncMintEvents;