@instadapp/interop-x 0.0.0-dev.59f2858 → 0.0.0-dev.59f84ce

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) 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 +2 -4
  6. package/dist/src/abi/interopXContract.json +391 -0
  7. package/dist/src/alias.js +10 -0
  8. package/dist/{api → src/api}/index.js +6 -3
  9. package/dist/{config → src/config}/index.js +11 -1
  10. package/dist/{constants → src/constants}/addresses.js +3 -11
  11. package/dist/{constants → src/constants}/index.js +0 -0
  12. package/dist/{constants → src/constants}/tokens.js +0 -0
  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 -7
  16. package/dist/{db → src/db}/sequelize.js +0 -0
  17. package/dist/src/gnosis/actions/index.js +9 -0
  18. package/dist/src/gnosis/actions/withdraw/index.js +41 -0
  19. package/dist/src/gnosis/index.js +20 -0
  20. package/dist/src/index.js +117 -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 +17 -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 +146 -0
  32. package/dist/src/tasks/InteropXContract/SyncBridgeRequestEvents.js +78 -0
  33. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +55 -0
  34. package/dist/src/tasks/index.js +33 -0
  35. package/dist/{typechain → src/typechain}/Erc20.js +0 -0
  36. package/dist/{typechain → src/typechain}/GnosisSafe.js +0 -0
  37. package/dist/{typechain/InteropBridgeToken.js → src/typechain/InteropXContract.js} +0 -0
  38. package/dist/{typechain → src/typechain}/common.js +0 -0
  39. package/dist/{typechain → src/typechain}/factories/Erc20__factory.js +0 -0
  40. package/dist/{typechain → src/typechain}/factories/GnosisSafe__factory.js +0 -0
  41. package/dist/src/typechain/factories/InteropXContract__factory.js +526 -0
  42. package/dist/src/typechain/factories/index.js +12 -0
  43. package/dist/{typechain → src/typechain}/index.js +3 -5
  44. package/dist/{types.js → src/types.js} +0 -0
  45. package/dist/src/utils/index.js +168 -0
  46. package/package.json +10 -6
  47. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  48. package/src/abi/index.ts +2 -4
  49. package/src/abi/interopXContract.json +391 -0
  50. package/src/alias.ts +6 -0
  51. package/src/api/index.ts +5 -2
  52. package/src/config/index.ts +11 -1
  53. package/src/constants/addresses.ts +3 -11
  54. package/src/db/models/transaction.ts +61 -19
  55. package/src/gnosis/actions/index.ts +5 -0
  56. package/src/gnosis/actions/withdraw/index.ts +56 -0
  57. package/src/gnosis/index.ts +19 -0
  58. package/src/index.ts +95 -24
  59. package/src/net/peer/index.ts +9 -7
  60. package/src/net/pool/index.ts +41 -11
  61. package/src/net/protocol/dial/SignatureDialProtocol.ts +19 -13
  62. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  63. package/src/net/protocol/index.ts +67 -1
  64. package/src/tasks/AutoUpdateTask.ts +82 -0
  65. package/src/tasks/BaseTask.ts +14 -4
  66. package/src/tasks/InteropXContract/ProcessBridgeRequestEvents.ts +208 -0
  67. package/src/tasks/InteropXContract/SyncBridgeRequestEvents.ts +116 -0
  68. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +67 -0
  69. package/src/tasks/index.ts +16 -4
  70. package/src/typechain/InteropXContract.ts +524 -0
  71. package/src/typechain/factories/InteropXContract__factory.ts +533 -0
  72. package/src/typechain/factories/index.ts +1 -2
  73. package/src/typechain/index.ts +2 -4
  74. package/src/utils/index.ts +118 -7
  75. package/tsconfig.json +7 -2
  76. package/dist/abi/interopBridgeToken.json +0 -286
  77. package/dist/abi/interopXGateway.json +0 -184
  78. package/dist/index.js +0 -66
  79. package/dist/tasks/InteropXGateway/SyncDepositEvents.js +0 -66
  80. package/dist/tasks/index.js +0 -27
  81. package/dist/typechain/InteropXGateway.js +0 -2
  82. package/dist/typechain/factories/InteropBridgeToken__factory.js +0 -459
  83. package/dist/typechain/factories/InteropXGateway__factory.js +0 -265
  84. package/dist/typechain/factories/index.js +0 -14
  85. package/dist/utils/index.js +0 -101
  86. package/src/abi/interopBridgeToken.json +0 -286
  87. package/src/abi/interopXGateway.json +0 -184
  88. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +0 -111
  89. package/src/typechain/InteropBridgeToken.ts +0 -686
  90. package/src/typechain/InteropXGateway.ts +0 -407
  91. package/src/typechain/factories/InteropBridgeToken__factory.ts +0 -466
  92. package/src/typechain/factories/InteropXGateway__factory.ts +0 -272
@@ -0,0 +1,146 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("../BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const ethers_1 = require("ethers");
9
+ const abi_1 = __importDefault(require("@/abi"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
+ const sequelize_1 = require("sequelize");
14
+ const config_1 = __importDefault(require("@/config"));
15
+ const waait_1 = __importDefault(require("waait"));
16
+ const gnosis_1 = require("@/gnosis");
17
+ const net_1 = require("@/net");
18
+ class ProccessBridgeRequestEvents extends BaseTask_1.BaseTask {
19
+ constructor({ chainId }) {
20
+ super({
21
+ logger: new logger_1.default("InteropXContract::ProccessBridgeRequestEvents"),
22
+ });
23
+ this.leadNodeOnly = true;
24
+ this.chainId = chainId;
25
+ }
26
+ async pollHandler() {
27
+ var _a;
28
+ const blockNumber = await this.provider.getBlockNumber();
29
+ const transaction = await db_1.Transaction.findOne({
30
+ where: {
31
+ status: 'pending',
32
+ sourceStatus: 'uninitialised',
33
+ createdAt: {
34
+ [sequelize_1.Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
35
+ },
36
+ sourceDelayUntil: {
37
+ [sequelize_1.Op.or]: {
38
+ [sequelize_1.Op.is]: null,
39
+ [sequelize_1.Op.lt]: new Date(),
40
+ }
41
+ },
42
+ requestBlockNumber: {
43
+ [sequelize_1.Op.lt]: blockNumber - 12,
44
+ },
45
+ sourceChainId: this.chainId,
46
+ }
47
+ });
48
+ if (!transaction) {
49
+ return;
50
+ }
51
+ console.log(`Processing transaction ${transaction.transactionHash}`);
52
+ transaction.targetStatus = 'pending';
53
+ await transaction.save();
54
+ const ownersThreshold = await this.safeContract.getThreshold();
55
+ await (0, waait_1.default)(10000);
56
+ let data, logs = [];
57
+ try {
58
+ ({ data, logs } = await (0, gnosis_1.buildGnosisAction)(transaction));
59
+ }
60
+ catch (error) {
61
+ console.log(error);
62
+ transaction.sourceStatus = 'failed';
63
+ transaction.sourceErrors = [error.message];
64
+ transaction.targetStatus = 'failed';
65
+ transaction.status = 'failed';
66
+ await transaction.save();
67
+ net_1.protocol.sendTransaction(transaction);
68
+ return;
69
+ }
70
+ let gnosisTx = await (0, utils_1.generateGnosisTransaction)({
71
+ baseGas: "0",
72
+ data,
73
+ gasPrice: "0",
74
+ gasToken: "0x0000000000000000000000000000000000000000",
75
+ nonce: '0',
76
+ operation: "1",
77
+ refundReceiver: "0x0000000000000000000000000000000000000000",
78
+ safeAddress: this.safeContractAddress,
79
+ safeTxGas: "79668",
80
+ to: constants_1.addresses[transaction.sourceChainId].multisend,
81
+ value: "0",
82
+ }, this.safeContract);
83
+ const owners = await this.safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
84
+ const ownerPeerIds = net_1.peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id);
85
+ console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
86
+ console.log(ownerPeerIds);
87
+ const signatures = await net_1.protocol.requestSignatures({
88
+ type: 'source',
89
+ transactionHash: transaction.transactionHash,
90
+ safeTxGas: gnosisTx.safeTxGas,
91
+ safeNonce: gnosisTx.nonce
92
+ }, ownerPeerIds);
93
+ const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x');
94
+ console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
95
+ if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
96
+ await transaction.save();
97
+ transaction.sourceDelayUntil = new Date(Date.now() + 30 * 1000);
98
+ transaction.sourceStatus = 'uninitialised';
99
+ await transaction.save();
100
+ const errorMessage = (_a = signatures.find(s => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
101
+ throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
102
+ }
103
+ console.log(`Executing transaction for execution ${transaction.transactionHash}`);
104
+ const { data: txData } = await this.safeContract.populateTransaction.execTransaction(gnosisTx.to, gnosisTx.value, gnosisTx.data, gnosisTx.operation, gnosisTx.safeTxGas, gnosisTx.baseGas, gnosisTx.gasPrice, gnosisTx.gasToken, gnosisTx.refundReceiver, (0, utils_1.buildSignatureBytes)(validSignatures));
105
+ const txSent = await this.sourceWallet.sendTransaction({
106
+ from: this.sourceWallet.address,
107
+ gasPrice: ethers_1.ethers.BigNumber.from(120 * 10 ** 9),
108
+ to: this.safeContractAddress,
109
+ data: txData,
110
+ });
111
+ const receipt = await txSent.wait();
112
+ const parsedLogs = [];
113
+ receipt.logs.forEach((log) => {
114
+ try {
115
+ parsedLogs.push(this.safeContract.interface.parseLog(log));
116
+ }
117
+ catch (e) { }
118
+ });
119
+ if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
120
+ console.log('ExecutionSuccess');
121
+ transaction.targetStatus = 'success';
122
+ transaction.targetTransactionHash = txSent.hash;
123
+ transaction.targetLogs = logs;
124
+ transaction.status = 'success';
125
+ await transaction.save();
126
+ }
127
+ else {
128
+ console.log('ExecutionFailure');
129
+ transaction.targetStatus = 'failed';
130
+ transaction.targetTransactionHash = txSent.hash;
131
+ transaction.status = 'failed';
132
+ await transaction.save();
133
+ }
134
+ net_1.protocol.sendTransaction(transaction);
135
+ }
136
+ async start() {
137
+ this.contractAddress = constants_1.addresses[this.chainId].interopXContract;
138
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
139
+ this.sourceWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider);
140
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXContract, this.sourceWallet);
141
+ this.safeContractAddress = constants_1.addresses[this.chainId].gnosisSafe;
142
+ this.safeContract = (0, utils_1.getContract)(this.safeContractAddress, abi_1.default.gnosisSafe, this.sourceWallet);
143
+ await super.start();
144
+ }
145
+ }
146
+ exports.default = ProccessBridgeRequestEvents;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("../BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const ethers_1 = require("ethers");
9
+ const abi_1 = __importDefault(require("@/abi"));
10
+ const db_1 = require("@/db");
11
+ const utils_1 = require("@/utils");
12
+ const constants_1 = require("@/constants");
13
+ const config_1 = __importDefault(require("@/config"));
14
+ class SyncBridgeRequestEvents extends BaseTask_1.BaseTask {
15
+ constructor({ chainId }) {
16
+ super({
17
+ logger: new logger_1.default("InteropXContract::SyncBridgeRequestEvents"),
18
+ });
19
+ this.chainId = chainId;
20
+ }
21
+ async pollHandler() {
22
+ const currentBlock = await this.provider.getBlockNumber();
23
+ const events = await this.contract.queryFilter(this.contract.filters.LogBridgeRequest(), currentBlock - 2000, currentBlock);
24
+ let processedEvents = 0;
25
+ for (const event of events) {
26
+ try {
27
+ if (!event.args) {
28
+ continue;
29
+ }
30
+ const { bridger, position, sourceChainId, targetChainId, metadata } = event.args;
31
+ const uniqueIdentifier = {
32
+ action: 'withdraw',
33
+ bridger,
34
+ requestTransactionHash: event.transactionHash,
35
+ sourceChainId: sourceChainId.toNumber(),
36
+ targetChainId: targetChainId.toNumber(),
37
+ };
38
+ let transactionHash = (0, utils_1.generateInteropTransactionHash)(uniqueIdentifier);
39
+ const transaction = await db_1.Transaction.findOne({ where: { transactionHash } });
40
+ if (transaction) {
41
+ return;
42
+ }
43
+ await db_1.Transaction.create(Object.assign(Object.assign({ transactionHash }, uniqueIdentifier), { position,
44
+ metadata, requestBlockNumber: event.blockNumber, requestEvent: {
45
+ bridger,
46
+ position: {
47
+ withdraw: position.withdraw.map((v) => ({
48
+ sourceToken: v.sourceToken,
49
+ targetToken: v.targetToken,
50
+ amount: v.amount.toString()
51
+ })),
52
+ supply: position.supply.map((v) => ({
53
+ sourceToken: v.sourceToken,
54
+ targetToken: v.targetToken,
55
+ amount: v.amount.toString()
56
+ })),
57
+ },
58
+ sourceChainId: sourceChainId.toNumber(),
59
+ targetChainId: targetChainId.toNumber(),
60
+ metadata,
61
+ } }));
62
+ this.logger.info(`New bridge request received: ${transactionHash} `);
63
+ }
64
+ catch (error) {
65
+ this.logger.error(error);
66
+ }
67
+ }
68
+ if (processedEvents > 0)
69
+ this.logger.info(`${processedEvents} events processed`);
70
+ }
71
+ async start() {
72
+ this.contractAddress = constants_1.addresses[this.chainId].interopXContract;
73
+ this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
74
+ this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXContract, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
75
+ await super.start();
76
+ }
77
+ }
78
+ exports.default = SyncBridgeRequestEvents;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const BaseTask_1 = require("../BaseTask");
7
+ const logger_1 = __importDefault(require("@/logger"));
8
+ const net_1 = require("@/net");
9
+ const db_1 = require("@/db");
10
+ const sequelize_1 = require("sequelize");
11
+ class SyncTransactionStatusTask extends BaseTask_1.BaseTask {
12
+ constructor() {
13
+ super({
14
+ logger: new logger_1.default("SyncTransactionStatusTask"),
15
+ });
16
+ this.pollIntervalMs = 60 * 1000;
17
+ this.exceptLeadNode = true;
18
+ }
19
+ async pollHandler() {
20
+ // if transaction is pending for more than 1 hour, check lead node for status
21
+ const leadNode = net_1.peerPool.getLeadPeer();
22
+ if (!leadNode) {
23
+ return;
24
+ }
25
+ const transaction = await db_1.Transaction.findOne({
26
+ where: {
27
+ status: 'pending',
28
+ sourceCreatedAt: {
29
+ [sequelize_1.Op.gte]: new Date(Date.now() - 60 * 60 * 1000),
30
+ },
31
+ }
32
+ });
33
+ if (!transaction) {
34
+ return;
35
+ }
36
+ this.logger.info(`Requesting transaction status for ${transaction.transactionHash}`);
37
+ const transactionStatus = await net_1.protocol.requestTransactionStatus(transaction.transactionHash, leadNode.id);
38
+ if (!transactionStatus) {
39
+ return;
40
+ }
41
+ this.logger.info(`Received transaction status for ${transaction.transactionHash}`);
42
+ transaction.sourceStatus = transactionStatus.sourceStatus;
43
+ transaction.sourceTransactionHash = transactionStatus.sourceTransactionHash;
44
+ transaction.sourceErrors = transactionStatus.sourceErrors;
45
+ transaction.sourceLogs = transactionStatus.sourceLogs;
46
+ transaction.targetStatus = transactionStatus.targetStatus;
47
+ transaction.targetTransactionHash = transactionStatus.targetTransactionHash;
48
+ transaction.targetErrors = transactionStatus.targetErrors;
49
+ transaction.targetLogs = transactionStatus.targetLogs;
50
+ transaction.status = transactionStatus.status;
51
+ await transaction.save();
52
+ this.logger.info(`Updated transaction status for ${transaction.transactionHash}`);
53
+ }
54
+ }
55
+ exports.default = SyncTransactionStatusTask;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Tasks = void 0;
7
+ const AutoUpdateTask_1 = __importDefault(require("./AutoUpdateTask"));
8
+ const SyncBridgeRequestEvents_1 = __importDefault(require("./InteropXContract/SyncBridgeRequestEvents"));
9
+ const ProcessBridgeRequestEvents_1 = __importDefault(require("./InteropXContract/ProcessBridgeRequestEvents"));
10
+ class Tasks {
11
+ constructor() {
12
+ this.tasks = [
13
+ // new SyncTransactionStatusTask(),
14
+ new AutoUpdateTask_1.default(),
15
+ // InteropXContract
16
+ new SyncBridgeRequestEvents_1.default({ chainId: 137 }),
17
+ new SyncBridgeRequestEvents_1.default({ chainId: 43114 }),
18
+ new ProcessBridgeRequestEvents_1.default({ chainId: 137 }),
19
+ new ProcessBridgeRequestEvents_1.default({ chainId: 43114 }),
20
+ ];
21
+ }
22
+ async start() {
23
+ for (const task of this.tasks) {
24
+ try {
25
+ task.start();
26
+ }
27
+ catch (error) {
28
+ console.error(`Error starting task: ${task.constructor.name}`);
29
+ }
30
+ }
31
+ }
32
+ }
33
+ exports.Tasks = Tasks;
File without changes
File without changes
File without changes