@instadapp/interop-x 0.0.0-dev.1abc1ca → 0.0.0-dev.2c0a756
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.
- package/bin/interop-x +1 -1
- package/dist/package.json +9 -5
- package/dist/src/abi/interopBridgeToken.json +21 -9
- package/dist/src/abi/interopXGateway.json +11 -11
- package/dist/src/api/index.js +3 -3
- package/dist/src/config/index.js +11 -1
- package/dist/src/constants/addresses.js +1 -1
- package/dist/src/constants/itokens.js +1 -1
- package/dist/src/index.js +69 -7
- package/dist/src/net/peer/index.js +8 -3
- package/dist/src/net/pool/index.js +32 -9
- package/dist/src/net/protocol/dial/SignatureDialProtocol.js +11 -4
- package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +28 -0
- package/dist/src/net/protocol/index.js +41 -1
- package/dist/src/tasks/AutoUpdateTask.js +70 -0
- package/dist/src/tasks/BaseTask.js +12 -4
- package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +146 -0
- package/dist/src/tasks/InteropBridge/SyncBurnEvents.js +71 -0
- package/dist/src/tasks/InteropBridge/SyncMintEvents.js +66 -0
- package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +45 -23
- package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +6 -7
- package/dist/src/tasks/InteropXGateway/SyncWithdrawtEvents.js +71 -0
- package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
- package/dist/src/tasks/index.js +29 -0
- package/dist/src/typechain/factories/InteropBridgeToken__factory.js +23 -11
- package/dist/src/typechain/factories/InteropXGateway__factory.js +14 -14
- package/dist/src/utils/index.js +111 -10
- package/package.json +9 -5
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/abi/interopBridgeToken.json +21 -9
- package/src/abi/interopXGateway.json +11 -11
- package/src/api/index.ts +2 -2
- package/src/config/index.ts +11 -1
- package/src/constants/addresses.ts +1 -1
- package/src/constants/itokens.ts +1 -1
- package/src/index.ts +90 -9
- package/src/net/peer/index.ts +9 -7
- package/src/net/pool/index.ts +41 -11
- package/src/net/protocol/dial/SignatureDialProtocol.ts +12 -4
- package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +31 -0
- package/src/net/protocol/index.ts +57 -1
- package/src/tasks/AutoUpdateTask.ts +82 -0
- package/src/tasks/BaseTask.ts +14 -4
- package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +231 -0
- package/src/tasks/InteropBridge/SyncBurnEvents.ts +121 -0
- package/src/tasks/InteropBridge/SyncMintEvents.ts +98 -0
- package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +57 -32
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +8 -10
- package/src/tasks/InteropXGateway/SyncWithdrawtEvents.ts +103 -0
- package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
- package/src/tasks/index.ts +44 -2
- package/src/typechain/InteropBridgeToken.ts +23 -17
- package/src/typechain/InteropXGateway.ts +13 -13
- package/src/typechain/factories/InteropBridgeToken__factory.ts +23 -11
- package/src/typechain/factories/InteropXGateway__factory.ts +14 -14
- package/src/utils/index.ts +146 -12
@@ -0,0 +1,70 @@
|
|
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 await_spawn_1 = __importDefault(require("await-spawn"));
|
9
|
+
const child_process_1 = require("child_process");
|
10
|
+
const config_1 = __importDefault(require("@/config"));
|
11
|
+
const waait_1 = __importDefault(require("waait"));
|
12
|
+
const package_json_1 = __importDefault(require("../../package.json"));
|
13
|
+
const currentVersion = package_json_1.default.version;
|
14
|
+
const tag = config_1.default.staging ? 'dev' : 'latest';
|
15
|
+
class AutoUpdateTask extends BaseTask_1.BaseTask {
|
16
|
+
constructor() {
|
17
|
+
super({
|
18
|
+
logger: new logger_1.default("AutoUpdateTask"),
|
19
|
+
});
|
20
|
+
this.pollIntervalMs = 60 * 10 * 1000;
|
21
|
+
}
|
22
|
+
prePollHandler() {
|
23
|
+
return config_1.default.autoUpdate && !config_1.default.isLeadNode();
|
24
|
+
}
|
25
|
+
async getInstalledVersion() {
|
26
|
+
try {
|
27
|
+
const stdout = await (0, await_spawn_1.default)('npm', ['-g', 'ls', '--depth=0', '--json']);
|
28
|
+
return JSON.parse(stdout.toString()).dependencies[package_json_1.default.name].version;
|
29
|
+
}
|
30
|
+
catch (error) {
|
31
|
+
this.logger.error(error);
|
32
|
+
return currentVersion;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
async getLatestVersion() {
|
36
|
+
try {
|
37
|
+
const stdout = await (0, await_spawn_1.default)('npm', ['view', `${package_json_1.default.name}@${tag}`, 'version']);
|
38
|
+
return stdout.toString().trim();
|
39
|
+
}
|
40
|
+
catch (error) {
|
41
|
+
this.logger.error(error);
|
42
|
+
return currentVersion;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
async pollHandler() {
|
46
|
+
const version = await this.getLatestVersion();
|
47
|
+
if (version === currentVersion) {
|
48
|
+
return;
|
49
|
+
}
|
50
|
+
this.logger.warn(`New version ${version} available.`);
|
51
|
+
this.logger.info('Updating...');
|
52
|
+
await (0, await_spawn_1.default)('npm', ['-g', 'install', `@instadapp/interop-x@${tag}`, '-f']);
|
53
|
+
await (0, waait_1.default)(5000);
|
54
|
+
if (version !== await this.getInstalledVersion()) {
|
55
|
+
this.logger.warn(`failed to install ${version}, retrying in 5 minutes`);
|
56
|
+
return;
|
57
|
+
}
|
58
|
+
this.logger.warn(`Installed version ${version}`);
|
59
|
+
this.logger.warn(`Restarting...`);
|
60
|
+
// TODO: its restarting in the bg, but it should be in the fg
|
61
|
+
const subprocess = (0, child_process_1.spawn)(process.argv[0], process.argv.slice(1), {
|
62
|
+
cwd: process.cwd(),
|
63
|
+
stdio: "inherit",
|
64
|
+
// shell: process.env.SHELL,
|
65
|
+
});
|
66
|
+
subprocess.unref();
|
67
|
+
process.exit();
|
68
|
+
}
|
69
|
+
}
|
70
|
+
exports.default = AutoUpdateTask;
|
@@ -14,6 +14,7 @@ class BaseTask extends events_1.default {
|
|
14
14
|
this.started = false;
|
15
15
|
this.pollIntervalMs = 10 * 1000;
|
16
16
|
this.leadNodeOnly = false;
|
17
|
+
this.exceptLeadNode = false;
|
17
18
|
this.logger = logger !== null && logger !== void 0 ? logger : new logger_1.default('BaseTask');
|
18
19
|
}
|
19
20
|
async pollCheck() {
|
@@ -28,16 +29,23 @@ class BaseTask extends events_1.default {
|
|
28
29
|
}
|
29
30
|
}
|
30
31
|
catch (err) {
|
31
|
-
this.logger.error(`poll check error
|
32
|
+
this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`);
|
32
33
|
}
|
33
34
|
await this.postPollHandler();
|
34
35
|
}
|
35
36
|
}
|
36
37
|
prePollHandler() {
|
37
|
-
if (
|
38
|
-
|
38
|
+
if (config_1.default.isMaintenanceMode()) {
|
39
|
+
this.logger.warn('Maintenance mode is enabled. Skipping task.');
|
40
|
+
return false;
|
39
41
|
}
|
40
|
-
|
42
|
+
if (this.exceptLeadNode) {
|
43
|
+
return !config_1.default.isLeadNode();
|
44
|
+
}
|
45
|
+
if (this.leadNodeOnly) {
|
46
|
+
return config_1.default.isLeadNode();
|
47
|
+
}
|
48
|
+
return true;
|
41
49
|
}
|
42
50
|
async pollHandler() {
|
43
51
|
this.logger.warn('pollHandler not implemented');
|
@@ -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 config_1 = __importDefault(require("@/config"));
|
14
|
+
const sequelize_1 = require("sequelize");
|
15
|
+
const waait_1 = __importDefault(require("waait"));
|
16
|
+
const net_1 = require("@/net");
|
17
|
+
const generateGnosisTransaction = async (transactionData, safeContract) => {
|
18
|
+
console.log(transactionData);
|
19
|
+
let isExecuted = await safeContract.dataHashes(await safeContract.getTransactionHash(transactionData.to, transactionData.value, transactionData.data, transactionData.operation, transactionData.safeTxGas, transactionData.baseGas, transactionData.gasPrice, transactionData.gasToken, transactionData.refundReceiver, transactionData.nonce));
|
20
|
+
while (isExecuted == 1) {
|
21
|
+
transactionData.safeTxGas = ethers_1.BigNumber.from(String(transactionData.safeTxGas)).add(1).toString();
|
22
|
+
isExecuted = await safeContract.dataHashes(await safeContract.getTransactionHash(transactionData.to, transactionData.value, transactionData.data, transactionData.operation, transactionData.safeTxGas, transactionData.baseGas, transactionData.gasPrice, transactionData.gasToken, transactionData.refundReceiver, transactionData.nonce));
|
23
|
+
}
|
24
|
+
return transactionData;
|
25
|
+
};
|
26
|
+
class ProcessWithdrawEvents extends BaseTask_1.BaseTask {
|
27
|
+
constructor({ chainId }) {
|
28
|
+
super({
|
29
|
+
logger: new logger_1.default("InteropXGateway::ProcessWithdrawEvents"),
|
30
|
+
});
|
31
|
+
this.leadNodeOnly = true;
|
32
|
+
this.chainId = chainId;
|
33
|
+
}
|
34
|
+
async pollHandler() {
|
35
|
+
var _a;
|
36
|
+
const blockNumber = await this.provider.getBlockNumber();
|
37
|
+
const transaction = await db_1.Transaction.findOne({
|
38
|
+
where: {
|
39
|
+
status: 'pending',
|
40
|
+
sourceStatus: 'success',
|
41
|
+
targetStatus: 'uninitialised',
|
42
|
+
action: 'withdraw',
|
43
|
+
sourceCreatedAt: {
|
44
|
+
[sequelize_1.Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
|
45
|
+
},
|
46
|
+
targetDelayUntil: {
|
47
|
+
[sequelize_1.Op.or]: {
|
48
|
+
[sequelize_1.Op.is]: null,
|
49
|
+
[sequelize_1.Op.lt]: new Date(),
|
50
|
+
}
|
51
|
+
},
|
52
|
+
sourceBlockNumber: {
|
53
|
+
[sequelize_1.Op.lt]: blockNumber - 12,
|
54
|
+
},
|
55
|
+
sourceChainId: this.chainId,
|
56
|
+
}
|
57
|
+
});
|
58
|
+
if (!transaction) {
|
59
|
+
return;
|
60
|
+
}
|
61
|
+
console.log(`Processing transaction ${transaction.transactionHash}`);
|
62
|
+
transaction.targetStatus = 'pending';
|
63
|
+
await transaction.save();
|
64
|
+
// refresh event data?
|
65
|
+
const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(transaction.targetChainId));
|
66
|
+
const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
|
67
|
+
const safeAddress = constants_1.addresses[transaction.targetChainId].gnosisSafe;
|
68
|
+
const safeContract = (0, utils_1.getContract)(safeAddress, abi_1.default.gnosisSafe, targetWallet);
|
69
|
+
const ownersThreshold = await safeContract.getThreshold();
|
70
|
+
await (0, waait_1.default)(10000);
|
71
|
+
let gnosisTx = await generateGnosisTransaction({
|
72
|
+
baseGas: "0",
|
73
|
+
data: await (0, utils_1.buildDataForTransaction)(transaction),
|
74
|
+
gasPrice: "0",
|
75
|
+
gasToken: "0x0000000000000000000000000000000000000000",
|
76
|
+
nonce: '0',
|
77
|
+
operation: "1",
|
78
|
+
refundReceiver: "0x0000000000000000000000000000000000000000",
|
79
|
+
safeAddress: safeAddress,
|
80
|
+
safeTxGas: "79668",
|
81
|
+
to: constants_1.addresses[transaction.targetChainId].multisend,
|
82
|
+
value: "0",
|
83
|
+
}, safeContract);
|
84
|
+
const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
|
85
|
+
const ownerPeerIds = net_1.peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id);
|
86
|
+
console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
|
87
|
+
console.log(ownerPeerIds);
|
88
|
+
const signatures = await net_1.protocol.requestSignatures({
|
89
|
+
type: 'source',
|
90
|
+
transactionHash: transaction.transactionHash,
|
91
|
+
safeTxGas: gnosisTx.safeTxGas,
|
92
|
+
safeNonce: gnosisTx.nonce
|
93
|
+
}, ownerPeerIds);
|
94
|
+
const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x');
|
95
|
+
console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
|
96
|
+
if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
|
97
|
+
await transaction.save();
|
98
|
+
transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
|
99
|
+
transaction.targetStatus = 'uninitialised';
|
100
|
+
await transaction.save();
|
101
|
+
const errorMessage = (_a = signatures.find(s => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
|
102
|
+
throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
|
103
|
+
}
|
104
|
+
console.log(`Executing transaction for execution ${transaction.transactionHash}`);
|
105
|
+
const { data: txData } = await 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));
|
106
|
+
console.log({
|
107
|
+
from: targetWallet.address,
|
108
|
+
gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9).toString(),
|
109
|
+
to: safeAddress,
|
110
|
+
data: txData,
|
111
|
+
});
|
112
|
+
const txSent = await targetWallet.sendTransaction({
|
113
|
+
from: targetWallet.address,
|
114
|
+
gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9),
|
115
|
+
to: safeAddress,
|
116
|
+
data: txData,
|
117
|
+
});
|
118
|
+
const receipt = await txSent.wait();
|
119
|
+
const parsedLogs = [];
|
120
|
+
receipt.logs.forEach((log) => {
|
121
|
+
try {
|
122
|
+
parsedLogs.push(safeContract.interface.parseLog(log));
|
123
|
+
}
|
124
|
+
catch (e) { }
|
125
|
+
});
|
126
|
+
if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
|
127
|
+
console.log('ExecutionSuccess');
|
128
|
+
transaction.targetStatus = 'success';
|
129
|
+
transaction.targetTransactionHash = txSent.hash;
|
130
|
+
transaction.status = 'success';
|
131
|
+
await transaction.save();
|
132
|
+
}
|
133
|
+
else {
|
134
|
+
console.log('ExecutionFailure');
|
135
|
+
transaction.targetStatus = 'failed';
|
136
|
+
transaction.targetTransactionHash = txSent.hash;
|
137
|
+
transaction.status = 'failed';
|
138
|
+
await transaction.save();
|
139
|
+
}
|
140
|
+
}
|
141
|
+
async start() {
|
142
|
+
this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
|
143
|
+
await super.start();
|
144
|
+
}
|
145
|
+
}
|
146
|
+
exports.default = ProcessWithdrawEvents;
|
@@ -0,0 +1,71 @@
|
|
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 config_1 = __importDefault(require("@/config"));
|
13
|
+
class SyncBurnEvents extends BaseTask_1.BaseTask {
|
14
|
+
constructor({ chainId, itokenAddress }) {
|
15
|
+
super({
|
16
|
+
logger: new logger_1.default("InteropBridgeToken::SyncBurnEvents"),
|
17
|
+
});
|
18
|
+
this.chainId = chainId;
|
19
|
+
this.itokenAddress = itokenAddress;
|
20
|
+
}
|
21
|
+
async pollHandler() {
|
22
|
+
const currentBlock = await this.provider.getBlockNumber();
|
23
|
+
const events = await this.contract.queryFilter(this.contract.filters.Burn(), currentBlock - 2000, currentBlock);
|
24
|
+
let processedEvents = 0;
|
25
|
+
for (const event of events) {
|
26
|
+
try {
|
27
|
+
if (!event.args) {
|
28
|
+
continue;
|
29
|
+
}
|
30
|
+
const { to, amount, sourceChainId, targetChainId } = event.args;
|
31
|
+
const uniqueIdentifier = {
|
32
|
+
action: 'withdraw',
|
33
|
+
submitTransactionHash: event.transactionHash,
|
34
|
+
sourceChainId: sourceChainId,
|
35
|
+
targetChainId: targetChainId,
|
36
|
+
};
|
37
|
+
if (await db_1.Transaction.findOne({ where: uniqueIdentifier })) {
|
38
|
+
continue;
|
39
|
+
}
|
40
|
+
const tx = await event.getTransaction();
|
41
|
+
await db_1.Transaction.create(Object.assign(Object.assign({}, uniqueIdentifier), { transactionHash: (0, utils_1.generateInteropTransactionHash)(uniqueIdentifier), from: tx.from, to, submitTransactionHash: event.transactionHash, submitBlockNumber: event.blockNumber,
|
42
|
+
// submit & source are the same
|
43
|
+
sourceTransactionHash: event.transactionHash, sourceBlockNumber: event.blockNumber, sourceStatus: "success", targetStatus: "uninitialised", submitEvent: {
|
44
|
+
to,
|
45
|
+
amount: amount.toString(),
|
46
|
+
itoken: this.itokenAddress,
|
47
|
+
sourceChainId: sourceChainId,
|
48
|
+
targetChainId: targetChainId,
|
49
|
+
}, sourceEvent: {
|
50
|
+
to,
|
51
|
+
amount: amount.toString(),
|
52
|
+
itoken: this.itokenAddress,
|
53
|
+
sourceChainId: sourceChainId,
|
54
|
+
targetChainId: targetChainId,
|
55
|
+
}, status: "pending" }));
|
56
|
+
this.logger.info(`Withdraw queued: ${event.transactionHash} ${event.blockNumber}`);
|
57
|
+
}
|
58
|
+
catch (error) {
|
59
|
+
this.logger.error(error);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
if (processedEvents > 0)
|
63
|
+
this.logger.info(`${processedEvents} events processed`);
|
64
|
+
}
|
65
|
+
async start() {
|
66
|
+
this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
|
67
|
+
this.contract = (0, utils_1.getContract)(this.itokenAddress, abi_1.default.interopBridgeToken, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
|
68
|
+
await super.start();
|
69
|
+
}
|
70
|
+
}
|
71
|
+
exports.default = SyncBurnEvents;
|
@@ -0,0 +1,66 @@
|
|
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 config_1 = __importDefault(require("@/config"));
|
13
|
+
class SyncMintEvents extends BaseTask_1.BaseTask {
|
14
|
+
constructor({ chainId, itokenAddress }) {
|
15
|
+
super({
|
16
|
+
logger: new logger_1.default("InteropBridgeToken::SyncMintEvents"),
|
17
|
+
});
|
18
|
+
this.chainId = chainId;
|
19
|
+
this.itokenAddress = itokenAddress;
|
20
|
+
}
|
21
|
+
async pollHandler() {
|
22
|
+
const currentBlock = await this.provider.getBlockNumber();
|
23
|
+
const events = await this.contract.queryFilter(this.contract.filters.Mint(), currentBlock - 300, currentBlock);
|
24
|
+
for (const event of events) {
|
25
|
+
try {
|
26
|
+
if (!event.args) {
|
27
|
+
continue;
|
28
|
+
}
|
29
|
+
const { sourceChainId, targetChainId, amount, to, submitTransactionHash } = event.args;
|
30
|
+
const uniqueIdentifier = {
|
31
|
+
action: 'deposit',
|
32
|
+
submitTransactionHash: submitTransactionHash,
|
33
|
+
sourceChainId: sourceChainId,
|
34
|
+
targetChainId: targetChainId,
|
35
|
+
};
|
36
|
+
const transaction = await db_1.Transaction.findOne({ where: uniqueIdentifier });
|
37
|
+
if (!transaction) {
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
const tx = await event.getTransaction();
|
41
|
+
transaction.targetStatus = 'success';
|
42
|
+
transaction.targetErrors = [];
|
43
|
+
transaction.targetTransactionHash = tx.hash;
|
44
|
+
transaction.targetEvent = {
|
45
|
+
sourceChainId,
|
46
|
+
targetChainId,
|
47
|
+
amount: amount.toString(),
|
48
|
+
to,
|
49
|
+
submitTransactionHash
|
50
|
+
};
|
51
|
+
transaction.status = 'success';
|
52
|
+
await transaction.save();
|
53
|
+
this.logger.info(`Mint confirmation received: ${transaction.transactionHash} `);
|
54
|
+
}
|
55
|
+
catch (error) {
|
56
|
+
this.logger.error(error);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
async start() {
|
61
|
+
this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
|
62
|
+
this.contract = (0, utils_1.getContract)(this.itokenAddress, abi_1.default.interopBridgeToken, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
|
63
|
+
await super.start();
|
64
|
+
}
|
65
|
+
}
|
66
|
+
exports.default = SyncMintEvents;
|
@@ -15,6 +15,7 @@ const sequelize_1 = require("sequelize");
|
|
15
15
|
const waait_1 = __importDefault(require("waait"));
|
16
16
|
const net_1 = require("@/net");
|
17
17
|
const generateGnosisTransaction = async (transactionData, safeContract) => {
|
18
|
+
console.log(transactionData);
|
18
19
|
let isExecuted = await safeContract.dataHashes(await safeContract.getTransactionHash(transactionData.to, transactionData.value, transactionData.data, transactionData.operation, transactionData.safeTxGas, transactionData.baseGas, transactionData.gasPrice, transactionData.gasToken, transactionData.refundReceiver, transactionData.nonce));
|
19
20
|
while (isExecuted == 1) {
|
20
21
|
transactionData.safeTxGas = ethers_1.BigNumber.from(String(transactionData.safeTxGas)).add(1).toString();
|
@@ -37,30 +38,52 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
|
|
37
38
|
where: {
|
38
39
|
status: 'pending',
|
39
40
|
sourceStatus: 'success',
|
41
|
+
targetStatus: 'uninitialised',
|
40
42
|
action: 'deposit',
|
41
43
|
sourceCreatedAt: {
|
42
44
|
[sequelize_1.Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
|
43
45
|
},
|
46
|
+
targetDelayUntil: {
|
47
|
+
[sequelize_1.Op.or]: {
|
48
|
+
[sequelize_1.Op.is]: null,
|
49
|
+
[sequelize_1.Op.lt]: new Date(),
|
50
|
+
}
|
51
|
+
},
|
44
52
|
sourceBlockNumber: {
|
45
53
|
[sequelize_1.Op.lt]: blockNumber - 12,
|
46
|
-
}
|
54
|
+
},
|
55
|
+
sourceChainId: this.chainId,
|
47
56
|
}
|
48
57
|
});
|
49
58
|
if (!transaction) {
|
50
59
|
return;
|
51
60
|
}
|
52
|
-
|
61
|
+
console.log(`Processing transaction ${transaction.transactionHash}`);
|
62
|
+
transaction.targetStatus = 'pending';
|
53
63
|
await transaction.save();
|
54
64
|
// refresh event data?
|
55
65
|
const targetChainProvider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(transaction.targetChainId));
|
56
66
|
const targetWallet = new ethers_1.ethers.Wallet(config_1.default.privateKey, targetChainProvider);
|
57
67
|
const safeAddress = constants_1.addresses[transaction.targetChainId].gnosisSafe;
|
58
|
-
const safeContract =
|
68
|
+
const safeContract = (0, utils_1.getContract)(safeAddress, abi_1.default.gnosisSafe, targetWallet);
|
59
69
|
const ownersThreshold = await safeContract.getThreshold();
|
60
70
|
await (0, waait_1.default)(10000);
|
71
|
+
let data;
|
72
|
+
try {
|
73
|
+
data = await (0, utils_1.buildDataForTransaction)(transaction);
|
74
|
+
}
|
75
|
+
catch (error) {
|
76
|
+
console.log(error);
|
77
|
+
transaction.targetStatus = 'failed';
|
78
|
+
transaction.targetErrors = [error.message];
|
79
|
+
transaction.status = 'failed';
|
80
|
+
await transaction.save();
|
81
|
+
net_1.protocol.sendTransaction(transaction);
|
82
|
+
return;
|
83
|
+
}
|
61
84
|
let gnosisTx = await generateGnosisTransaction({
|
62
85
|
baseGas: "0",
|
63
|
-
data
|
86
|
+
data,
|
64
87
|
gasPrice: "0",
|
65
88
|
gasToken: "0x0000000000000000000000000000000000000000",
|
66
89
|
nonce: '0',
|
@@ -74,6 +97,7 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
|
|
74
97
|
const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
|
75
98
|
const ownerPeerIds = net_1.peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id);
|
76
99
|
console.log(`Collecting signatures for execution ${transaction.transactionHash}`);
|
100
|
+
console.log(ownerPeerIds);
|
77
101
|
const signatures = await net_1.protocol.requestSignatures({
|
78
102
|
type: 'source',
|
79
103
|
transactionHash: transaction.transactionHash,
|
@@ -84,33 +108,23 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
|
|
84
108
|
console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
|
85
109
|
if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
|
86
110
|
await transaction.save();
|
87
|
-
transaction.
|
88
|
-
transaction.
|
111
|
+
transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
|
112
|
+
transaction.targetStatus = 'uninitialised';
|
89
113
|
await transaction.save();
|
90
114
|
const errorMessage = (_a = signatures.find(s => !!s.error)) === null || _a === void 0 ? void 0 : _a.error;
|
91
115
|
throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
|
92
116
|
}
|
93
|
-
const execTransactionParams = [
|
94
|
-
gnosisTx.to,
|
95
|
-
gnosisTx.value,
|
96
|
-
gnosisTx.data,
|
97
|
-
gnosisTx.operation,
|
98
|
-
gnosisTx.safeTxGas,
|
99
|
-
gnosisTx.baseGas,
|
100
|
-
gnosisTx.gasPrice,
|
101
|
-
gnosisTx.gasToken,
|
102
|
-
gnosisTx.refundReceiver,
|
103
|
-
(0, utils_1.buildSignatureBytes)(validSignatures),
|
104
|
-
];
|
105
117
|
console.log(`Executing transaction for execution ${transaction.transactionHash}`);
|
118
|
+
const { data: txData } = await 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));
|
106
119
|
console.log({
|
107
|
-
|
120
|
+
from: targetWallet.address,
|
121
|
+
gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9).toString(),
|
122
|
+
to: safeAddress,
|
123
|
+
data: txData,
|
108
124
|
});
|
109
|
-
const { data: txData } = await 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));
|
110
125
|
const txSent = await targetWallet.sendTransaction({
|
111
126
|
from: targetWallet.address,
|
112
127
|
gasPrice: ethers_1.BigNumber.from(120 * 10 ** 9),
|
113
|
-
gasLimit: ethers_1.BigNumber.from(6000000),
|
114
128
|
to: safeAddress,
|
115
129
|
data: txData,
|
116
130
|
});
|
@@ -124,16 +138,24 @@ class ProcessDepositEvents extends BaseTask_1.BaseTask {
|
|
124
138
|
});
|
125
139
|
if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
|
126
140
|
console.log('ExecutionSuccess');
|
141
|
+
transaction.targetStatus = 'success';
|
142
|
+
transaction.targetTransactionHash = txSent.hash;
|
143
|
+
transaction.status = 'success';
|
144
|
+
await transaction.save();
|
127
145
|
}
|
128
146
|
else {
|
129
147
|
console.log('ExecutionFailure');
|
148
|
+
transaction.targetStatus = 'failed';
|
149
|
+
transaction.targetTransactionHash = txSent.hash;
|
150
|
+
transaction.status = 'failed';
|
151
|
+
await transaction.save();
|
130
152
|
}
|
153
|
+
net_1.protocol.sendTransaction(transaction);
|
131
154
|
}
|
132
155
|
async start() {
|
133
|
-
this.logger.info(`Starting execution watcher on interop chain`);
|
134
156
|
this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
|
135
157
|
this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
|
136
|
-
this.contract =
|
158
|
+
this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
|
137
159
|
await super.start();
|
138
160
|
}
|
139
161
|
}
|
@@ -31,8 +31,8 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
|
|
31
31
|
const uniqueIdentifier = {
|
32
32
|
action: 'deposit',
|
33
33
|
submitTransactionHash: event.transactionHash,
|
34
|
-
sourceChainId: sourceChainId
|
35
|
-
targetChainId: targetChainId
|
34
|
+
sourceChainId: sourceChainId,
|
35
|
+
targetChainId: targetChainId,
|
36
36
|
};
|
37
37
|
if (await db_1.Transaction.findOne({ where: uniqueIdentifier })) {
|
38
38
|
continue;
|
@@ -45,17 +45,17 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
|
|
45
45
|
sourceChainId: sourceChainId.toString(),
|
46
46
|
targetChainId: targetChainId.toString(),
|
47
47
|
token: token,
|
48
|
-
|
48
|
+
amount: amount.toString(),
|
49
49
|
vnonce: vnonce.toString(),
|
50
50
|
}, sourceEvent: {
|
51
51
|
user,
|
52
52
|
sourceChainId: sourceChainId.toString(),
|
53
53
|
targetChainId: targetChainId.toString(),
|
54
54
|
token: token,
|
55
|
-
|
55
|
+
amount: amount.toString(),
|
56
56
|
vnonce: vnonce.toString(),
|
57
57
|
}, status: "pending" }));
|
58
|
-
this.logger.info(`
|
58
|
+
this.logger.info(`Deposit queued: ${event.transactionHash} ${event.blockNumber}`);
|
59
59
|
}
|
60
60
|
catch (error) {
|
61
61
|
this.logger.error(error);
|
@@ -65,10 +65,9 @@ class SyncDepositEvents extends BaseTask_1.BaseTask {
|
|
65
65
|
this.logger.info(`${processedEvents} events processed`);
|
66
66
|
}
|
67
67
|
async start() {
|
68
|
-
this.logger.info(`Starting execution watcher on interop chain`);
|
69
68
|
this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
|
70
69
|
this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
|
71
|
-
this.contract =
|
70
|
+
this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
|
72
71
|
await super.start();
|
73
72
|
}
|
74
73
|
}
|
@@ -0,0 +1,71 @@
|
|
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 SyncWithdrawEvents extends BaseTask_1.BaseTask {
|
15
|
+
constructor({ chainId }) {
|
16
|
+
super({
|
17
|
+
logger: new logger_1.default("InteropXGateway::SyncWithdrawEvents"),
|
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.LogGatewayWithdraw(), currentBlock - 300, currentBlock);
|
24
|
+
let processedEvents = 0;
|
25
|
+
for (const event of events) {
|
26
|
+
try {
|
27
|
+
if (!event.args) {
|
28
|
+
continue;
|
29
|
+
}
|
30
|
+
const { user, token, amount, sourceChainId, targetChainId, transactionHash } = event.args;
|
31
|
+
const uniqueIdentifier = {
|
32
|
+
action: 'withdraw',
|
33
|
+
submitTransactionHash: transactionHash,
|
34
|
+
sourceChainId: sourceChainId,
|
35
|
+
targetChainId: targetChainId,
|
36
|
+
};
|
37
|
+
const transaction = await db_1.Transaction.findOne({ where: uniqueIdentifier });
|
38
|
+
if (!transaction) {
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
const tx = await event.getTransaction();
|
42
|
+
transaction.targetStatus = 'success';
|
43
|
+
transaction.targetErrors = [];
|
44
|
+
transaction.targetTransactionHash = tx.hash;
|
45
|
+
transaction.targetEvent = {
|
46
|
+
user,
|
47
|
+
token,
|
48
|
+
amount: amount.toString(),
|
49
|
+
sourceChainId,
|
50
|
+
targetChainId,
|
51
|
+
transactionHash,
|
52
|
+
};
|
53
|
+
transaction.status = 'success';
|
54
|
+
await transaction.save();
|
55
|
+
this.logger.info(`Witdraw confirmation received: ${transaction.transactionHash} `);
|
56
|
+
}
|
57
|
+
catch (error) {
|
58
|
+
this.logger.error(error);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
if (processedEvents > 0)
|
62
|
+
this.logger.info(`${processedEvents} events processed`);
|
63
|
+
}
|
64
|
+
async start() {
|
65
|
+
this.contractAddress = constants_1.addresses[this.chainId].interopXGateway;
|
66
|
+
this.provider = new ethers_1.ethers.providers.JsonRpcProvider((0, utils_1.getRpcProviderUrl)(this.chainId));
|
67
|
+
this.contract = (0, utils_1.getContract)(this.contractAddress, abi_1.default.interopXGateway, new ethers_1.ethers.Wallet(config_1.default.privateKey, this.provider));
|
68
|
+
await super.start();
|
69
|
+
}
|
70
|
+
}
|
71
|
+
exports.default = SyncWithdrawEvents;
|