@instadapp/interop-x 0.0.0-dev.30b0db0 → 0.0.0-dev.326bdca

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