@instadapp/interop-x 0.0.0-dev.7a02577 → 0.0.0-dev.80dece2
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/dist/package.json +6 -3
- package/dist/src/constants/itokens.js +1 -1
- package/dist/src/index.js +31 -4
- package/dist/src/net/peer/index.js +6 -2
- package/dist/src/net/pool/index.js +27 -9
- package/dist/src/net/protocol/dial/SignatureDialProtocol.js +11 -4
- package/dist/src/net/protocol/index.js +30 -1
- package/dist/src/tasks/BaseTask.js +1 -1
- package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +147 -0
- package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +70 -0
- package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +32 -22
- package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +4 -4
- package/dist/src/tasks/index.js +13 -0
- package/dist/src/utils/index.js +97 -6
- package/package.json +6 -3
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/constants/itokens.ts +1 -1
- package/src/index.ts +39 -5
- package/src/net/peer/index.ts +7 -6
- package/src/net/pool/index.ts +37 -11
- package/src/net/protocol/dial/SignatureDialProtocol.ts +12 -4
- package/src/net/protocol/index.ts +45 -1
- package/src/tasks/BaseTask.ts +1 -1
- package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +233 -0
- package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +121 -0
- package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +44 -31
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +6 -6
- package/src/tasks/index.ts +19 -2
- package/src/utils/index.ts +130 -8
package/dist/src/tasks/index.js
CHANGED
@@ -4,12 +4,25 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.Tasks = void 0;
|
7
|
+
const ProcessDepositEvents_1 = __importDefault(require("./InteropXGateway/ProcessDepositEvents"));
|
7
8
|
const SyncDepositEvents_1 = __importDefault(require("./InteropXGateway/SyncDepositEvents"));
|
9
|
+
const SyncWithdrawEvents_1 = __importDefault(require("./InteropBridge/SyncWithdrawEvents"));
|
10
|
+
const ProcessWithdrawEvents_1 = __importDefault(require("./InteropBridge/ProcessWithdrawEvents"));
|
8
11
|
class Tasks {
|
9
12
|
constructor() {
|
10
13
|
this.tasks = [
|
11
14
|
new SyncDepositEvents_1.default({
|
12
15
|
chainId: 43114
|
16
|
+
}),
|
17
|
+
new ProcessDepositEvents_1.default({
|
18
|
+
chainId: 43114
|
19
|
+
}),
|
20
|
+
new SyncWithdrawEvents_1.default({
|
21
|
+
chainId: 137,
|
22
|
+
itokenAddress: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
|
23
|
+
}),
|
24
|
+
new ProcessWithdrawEvents_1.default({
|
25
|
+
chainId: 137,
|
13
26
|
})
|
14
27
|
];
|
15
28
|
}
|
package/dist/src/utils/index.js
CHANGED
@@ -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
|
109
|
-
throw new Error(
|
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 =
|
128
|
-
const { data } = await interopBridgeContract.populateTransaction.mint(transaction.submitEvent.
|
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,84 @@ const buildDataForTransaction = async (transaction, type) => {
|
|
134
145
|
});
|
135
146
|
return (0, ethers_multisend_1.encodeMulti)(transactions).data;
|
136
147
|
};
|
137
|
-
exports.
|
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, itoken: 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.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
|
+
// Make sure the contract properties is writable
|
189
|
+
const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
|
190
|
+
if (!desc || desc.writable !== true) {
|
191
|
+
return contract;
|
192
|
+
}
|
193
|
+
return new Proxy(contract, {
|
194
|
+
get(target, prop, receiver) {
|
195
|
+
const value = Reflect.get(target, prop, receiver);
|
196
|
+
if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
|
197
|
+
return async (...args) => {
|
198
|
+
try {
|
199
|
+
return await value.bind(contract)(...args);
|
200
|
+
}
|
201
|
+
catch (error) {
|
202
|
+
throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`);
|
203
|
+
}
|
204
|
+
};
|
205
|
+
}
|
206
|
+
if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
|
207
|
+
const parentProp = String(prop);
|
208
|
+
return new Proxy(value, {
|
209
|
+
get(target, prop, receiver) {
|
210
|
+
const value = Reflect.get(target, prop, receiver);
|
211
|
+
if (typeof value === 'function') {
|
212
|
+
return async (...args) => {
|
213
|
+
try {
|
214
|
+
return await value.bind(contract)(...args);
|
215
|
+
}
|
216
|
+
catch (error) {
|
217
|
+
throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`);
|
218
|
+
}
|
219
|
+
};
|
220
|
+
}
|
221
|
+
}
|
222
|
+
});
|
223
|
+
}
|
224
|
+
return value;
|
225
|
+
},
|
226
|
+
});
|
227
|
+
}
|
228
|
+
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.
|
3
|
+
"version": "0.0.0-dev.80dece2",
|
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,9 @@
|
|
45
46
|
"libp2p-websockets": "^0.16.2",
|
46
47
|
"luxon": "^2.3.2",
|
47
48
|
"module-alias": "^2.2.2",
|
48
|
-
"
|
49
|
+
"patch-package": "^6.4.7",
|
50
|
+
"postinstall-postinstall": "^2.1.0",
|
51
|
+
"sequelize": "6.18.0",
|
49
52
|
"sqlite3": "^5.0.5",
|
50
53
|
"waait": "^1.0.5"
|
51
54
|
},
|
@@ -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;
|
package/src/constants/itokens.ts
CHANGED
package/src/index.ts
CHANGED
@@ -16,7 +16,6 @@ moduleAlias.addAliases({
|
|
16
16
|
})
|
17
17
|
|
18
18
|
moduleAlias();
|
19
|
-
import assert from "assert";
|
20
19
|
import dotenv from "dotenv";
|
21
20
|
import { ethers } from "ethers";
|
22
21
|
import packageJson from '../package.json'
|
@@ -25,16 +24,24 @@ dotenv.config();
|
|
25
24
|
import Logger from "@/logger";
|
26
25
|
const logger = new Logger('Process')
|
27
26
|
|
28
|
-
|
29
|
-
if (process.argv.at(-1) === 'help') {
|
27
|
+
const printUsage = () => {
|
30
28
|
console.log('Usage:')
|
31
29
|
console.log(' PRIVATE_KEY=abcd1234 interop-x')
|
32
30
|
console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x')
|
31
|
+
console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x')
|
32
|
+
}
|
33
|
+
|
34
|
+
if (process.argv.at(-1) === 'help') {
|
35
|
+
printUsage()
|
33
36
|
process.exit(0)
|
34
37
|
}
|
35
38
|
|
36
|
-
assert(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
|
37
39
|
|
40
|
+
if(! process.env.PRIVATE_KEY) {
|
41
|
+
console.error('Please provide a private key\n\n')
|
42
|
+
printUsage()
|
43
|
+
process.exit(1)
|
44
|
+
}
|
38
45
|
try {
|
39
46
|
new ethers.Wallet(process.env.PRIVATE_KEY!)
|
40
47
|
} catch (e) {
|
@@ -45,8 +52,9 @@ try {
|
|
45
52
|
logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
|
46
53
|
|
47
54
|
import { Tasks } from "@/tasks";
|
48
|
-
import { startPeer } from "@/net";
|
55
|
+
import { startPeer, protocol, peerPool } from "@/net";
|
49
56
|
import { startApiServer } from '@/api';
|
57
|
+
import { Transaction } from './db';
|
50
58
|
|
51
59
|
async function main() {
|
52
60
|
|
@@ -57,6 +65,32 @@ async function main() {
|
|
57
65
|
tasks.start();
|
58
66
|
|
59
67
|
startApiServer()
|
68
|
+
|
69
|
+
protocol.on('TransactionStatus', async (payload) => {
|
70
|
+
if (!peerPool.isLeadNode(payload.peerId)) {
|
71
|
+
const peer = peerPool.getPeer(payload.peerId)
|
72
|
+
logger.info(`ignored transaction status from ${payload.peerId} ${peer?.publicAddress} `)
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
|
76
|
+
const transaction = await Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } })
|
77
|
+
|
78
|
+
if (!transaction) {
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
|
82
|
+
transaction.sourceStatus = payload.data.sourceStatus
|
83
|
+
transaction.sourceTransactionHash = payload.data.sourceTransactionHash
|
84
|
+
transaction.sourceErrors = payload.data.sourceErrors
|
85
|
+
|
86
|
+
transaction.targetStatus = payload.data.targetStatus
|
87
|
+
transaction.targetTransactionHash = payload.data.targetTransactionHash
|
88
|
+
transaction.targetErrors = payload.data.targetErrors
|
89
|
+
|
90
|
+
transaction.status = payload.data.status
|
91
|
+
|
92
|
+
await transaction.save()
|
93
|
+
})
|
60
94
|
}
|
61
95
|
|
62
96
|
main()
|
package/src/net/peer/index.ts
CHANGED
@@ -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
|
-
|
95
|
-
|
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
|
|
package/src/net/pool/index.ts
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
import { Event } from "@/types";
|
2
2
|
import config from "@/config";
|
3
|
+
import Logger from "@/logger";
|
4
|
+
import { getAddress } from "ethers/lib/utils";
|
5
|
+
|
6
|
+
|
7
|
+
const logger = new Logger('PeerPool')
|
3
8
|
|
4
9
|
export interface IPeerInfo {
|
5
10
|
id: string;
|
@@ -75,10 +80,15 @@ export class PeerPool {
|
|
75
80
|
* @emits {@link Event.POOL_PEER_ADDED}
|
76
81
|
*/
|
77
82
|
add(peer?: IPeerInfo) {
|
78
|
-
if (peer && peer.id
|
83
|
+
if (peer && peer.id) {
|
84
|
+
const newPeer = !this.pool.get(peer.id);
|
79
85
|
this.pool.set(peer.id, peer)
|
80
86
|
peer.pooled = true
|
81
|
-
|
87
|
+
|
88
|
+
if (newPeer) {
|
89
|
+
config.events.emit(Event.POOL_PEER_ADDED, peer)
|
90
|
+
logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`)
|
91
|
+
}
|
82
92
|
}
|
83
93
|
}
|
84
94
|
|
@@ -92,6 +102,7 @@ export class PeerPool {
|
|
92
102
|
if (this.pool.delete(peer.id)) {
|
93
103
|
peer.pooled = false
|
94
104
|
config.events.emit(Event.POOL_PEER_REMOVED, peer)
|
105
|
+
logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`)
|
95
106
|
}
|
96
107
|
}
|
97
108
|
}
|
@@ -100,7 +111,7 @@ export class PeerPool {
|
|
100
111
|
this.cleanup()
|
101
112
|
|
102
113
|
return this.peers.filter((p) => {
|
103
|
-
if(!p.pooled) return false;
|
114
|
+
if (!p.pooled) return false;
|
104
115
|
|
105
116
|
const now = new Date()
|
106
117
|
|
@@ -113,15 +124,30 @@ export class PeerPool {
|
|
113
124
|
}
|
114
125
|
|
115
126
|
|
116
|
-
|
117
|
-
|
127
|
+
getPeer(id: string){
|
128
|
+
return this.pool.get(id);
|
129
|
+
}
|
118
130
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
}
|
131
|
+
isLeadNode(id: string) {
|
132
|
+
const peer = this.pool.get(id);
|
133
|
+
|
134
|
+
if (!peer) {
|
135
|
+
return false;
|
136
|
+
}
|
137
|
+
|
138
|
+
return getAddress(peer.publicAddress) === getAddress(config.leadNodeAddress)
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
cleanup() {
|
143
|
+
// let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
|
144
|
+
|
145
|
+
// this.peers.forEach((peerInfo) => {
|
146
|
+
// if (peerInfo.updated.getTime() < compDate) {
|
147
|
+
// console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
|
148
|
+
// this.remove(peerInfo)
|
149
|
+
// }
|
150
|
+
// })
|
125
151
|
}
|
126
152
|
}
|
127
153
|
|
@@ -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
|
-
|
52
|
-
|
53
|
-
|
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 {
|
@@ -5,6 +5,7 @@ import { SignatureDialProtocol, ISignatureRequest, ISignatureResponse } from "./
|
|
5
5
|
import { IPeerInfo, peerPool } from "..";
|
6
6
|
import config from "@/config";
|
7
7
|
import { Event } from "@/types";
|
8
|
+
import { Transaction } from "@/db";
|
8
9
|
|
9
10
|
export interface ProtocolOptions {
|
10
11
|
/* Handshake timeout in ms (default: 8000) */
|
@@ -33,7 +34,12 @@ interface PeerInfoEvent extends BaseMessageEvent {
|
|
33
34
|
data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
|
34
35
|
}
|
35
36
|
|
37
|
+
interface TransactionStatusEvent extends BaseMessageEvent {
|
38
|
+
data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
|
39
|
+
}
|
40
|
+
|
36
41
|
declare interface Protocol {
|
42
|
+
on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
|
37
43
|
on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
|
38
44
|
on(event: string, listener: (payload: BaseMessageEvent) => void): this;
|
39
45
|
}
|
@@ -44,7 +50,7 @@ class Protocol extends EventEmitter {
|
|
44
50
|
private protocolMessages: Message[] = [
|
45
51
|
{
|
46
52
|
name: 'PeerInfo',
|
47
|
-
code:
|
53
|
+
code: 0x01,
|
48
54
|
encode: (info: Pick<IPeerInfo, 'publicAddress'>) => [
|
49
55
|
Buffer.from(info.publicAddress),
|
50
56
|
],
|
@@ -52,6 +58,36 @@ class Protocol extends EventEmitter {
|
|
52
58
|
publicAddress: publicAddress.toString(),
|
53
59
|
}),
|
54
60
|
},
|
61
|
+
{
|
62
|
+
name: 'TransactionStatus',
|
63
|
+
code: 0x02,
|
64
|
+
encode: (transaction: Transaction) => [
|
65
|
+
Buffer.from(transaction.transactionHash),
|
66
|
+
|
67
|
+
Buffer.from(transaction.sourceStatus),
|
68
|
+
Buffer.from(transaction.sourceTransactionHash),
|
69
|
+
transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
|
70
|
+
|
71
|
+
Buffer.from(transaction.targetStatus),
|
72
|
+
Buffer.from(transaction.targetTransactionHash),
|
73
|
+
transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
|
74
|
+
|
75
|
+
Buffer.from(transaction.status),
|
76
|
+
],
|
77
|
+
decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]: [Buffer, Buffer, Buffer, Buffer[], Buffer, Buffer, Buffer[], Buffer]) => ({
|
78
|
+
transactionHash: transactionHash.toString(),
|
79
|
+
|
80
|
+
sourceStatus: sourceStatus.toString(),
|
81
|
+
sourceTransactionHash: sourceTransactionHash.toString(),
|
82
|
+
sourceErrors: sourceErrors.map((e) => e.toString()),
|
83
|
+
|
84
|
+
targetStatus: targetStatus.toString(),
|
85
|
+
targetTransactionHash: targetTransactionHash.toString(),
|
86
|
+
targetErrors: targetErrors.map((e) => e.toString()),
|
87
|
+
|
88
|
+
status: status.toString(),
|
89
|
+
}),
|
90
|
+
},
|
55
91
|
];
|
56
92
|
private signature: SignatureDialProtocol;
|
57
93
|
|
@@ -121,6 +157,14 @@ class Protocol extends EventEmitter {
|
|
121
157
|
this.libp2p.pubsub.publish(this.topic, encoded)
|
122
158
|
}
|
123
159
|
|
160
|
+
public sendTransaction(transaction: Transaction) {
|
161
|
+
const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
|
162
|
+
|
163
|
+
const encoded = rlp.encode([message.code, message.encode(transaction)]);
|
164
|
+
|
165
|
+
this.libp2p.pubsub.publish(this.topic, encoded)
|
166
|
+
}
|
167
|
+
|
124
168
|
async requestSignatures(data: ISignatureRequest, peerIds?: string[]) {
|
125
169
|
try {
|
126
170
|
peerIds = peerIds || peerPool.activePeerIds;
|
package/src/tasks/BaseTask.ts
CHANGED
@@ -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
|
40
|
+
this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`)
|
41
41
|
}
|
42
42
|
|
43
43
|
await this.postPollHandler()
|