@instadapp/interop-x 0.0.0-dev.0ee2ee3 → 0.0.0-dev.160118e
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/.env.example +2 -1
- package/dist/package.json +73 -0
- package/dist/src/abi/erc20.json +350 -0
- package/dist/src/abi/gnosisSafe.json +747 -0
- package/dist/src/abi/index.js +15 -0
- package/dist/src/abi/interopBridgeToken.json +286 -0
- package/dist/src/abi/interopXGateway.json +184 -0
- package/dist/src/api/index.js +33 -0
- package/dist/{config → src/config}/index.js +6 -1
- package/dist/src/constants/addresses.js +20 -0
- package/dist/{constants → src/constants}/index.js +2 -0
- package/dist/src/constants/itokens.js +13 -0
- package/dist/src/constants/tokens.js +107 -0
- package/dist/{db → src/db}/index.js +0 -0
- package/dist/{db → src/db}/models/index.js +1 -1
- package/dist/src/db/models/transaction.js +54 -0
- package/dist/{db → src/db}/sequelize.js +2 -1
- package/dist/src/index.js +107 -0
- package/dist/{logger → src/logger}/index.js +0 -0
- package/dist/{net → src/net}/index.js +0 -0
- package/dist/{net → src/net}/peer/index.js +15 -6
- package/dist/{net → src/net}/pool/index.js +34 -11
- package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +1 -1
- package/dist/src/net/protocol/dial/SignatureDialProtocol.1.js +28 -0
- package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +22 -14
- package/dist/{net → src/net}/protocol/index.js +44 -4
- package/dist/src/tasks/AutoUpdateTask.js +47 -0
- package/dist/{tasks → src/tasks}/BaseTask.js +10 -6
- package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +146 -0
- package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +69 -0
- package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +149 -0
- package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +74 -0
- package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
- package/dist/src/tasks/index.js +44 -0
- package/dist/src/typechain/Erc20.js +2 -0
- package/dist/src/typechain/GnosisSafe.js +2 -0
- package/dist/src/typechain/InteropBridgeToken.js +2 -0
- package/dist/src/typechain/InteropXGateway.js +2 -0
- package/dist/src/typechain/common.js +2 -0
- package/dist/src/typechain/factories/Erc20__factory.js +367 -0
- package/dist/src/typechain/factories/GnosisSafe__factory.js +1174 -0
- package/dist/src/typechain/factories/InteropBridgeToken__factory.js +459 -0
- package/dist/src/typechain/factories/InteropXGateway__factory.js +265 -0
- package/dist/src/typechain/factories/index.js +14 -0
- package/dist/src/typechain/index.js +35 -0
- package/dist/{types.js → src/types.js} +0 -0
- package/dist/src/utils/index.js +238 -0
- package/package.json +20 -6
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/abi/erc20.json +350 -0
- package/src/abi/gnosisSafe.json +747 -0
- package/src/abi/index.ts +11 -0
- package/src/abi/interopBridgeToken.json +286 -0
- package/src/abi/interopXGateway.json +184 -0
- package/src/api/index.ts +33 -0
- package/src/config/index.ts +9 -1
- package/src/constants/addresses.ts +10 -3
- package/src/constants/index.ts +2 -0
- package/src/constants/itokens.ts +10 -0
- package/src/constants/tokens.ts +104 -0
- package/src/db/index.ts +1 -1
- package/src/db/models/index.ts +1 -1
- package/src/db/models/transaction.ts +96 -0
- package/src/db/sequelize.ts +2 -1
- package/src/index.ts +96 -6
- package/src/net/peer/index.ts +17 -9
- package/src/net/pool/index.ts +43 -13
- package/src/net/protocol/dial/BaseDialProtocol.ts +1 -1
- package/src/net/protocol/dial/SignatureDialProtocol.1.ts +31 -0
- package/src/net/protocol/dial/SignatureDialProtocol.ts +26 -17
- package/src/net/protocol/index.ts +61 -5
- package/src/tasks/AutoUpdateTask.ts +61 -0
- package/src/tasks/BaseTask.ts +11 -7
- package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +231 -0
- package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +119 -0
- package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +243 -0
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +124 -0
- package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
- package/src/tasks/index.ts +26 -1
- package/src/typechain/Erc20.ts +491 -0
- package/src/typechain/GnosisSafe.ts +1728 -0
- package/src/typechain/InteropBridgeToken.ts +686 -0
- package/src/typechain/InteropXGateway.ts +407 -0
- package/src/typechain/common.ts +44 -0
- package/src/typechain/factories/Erc20__factory.ts +368 -0
- package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
- package/src/typechain/factories/InteropBridgeToken__factory.ts +466 -0
- package/src/typechain/factories/InteropXGateway__factory.ts +272 -0
- package/src/typechain/factories/index.ts +7 -0
- package/src/typechain/index.ts +12 -0
- package/src/types.ts +2 -2
- package/src/utils/index.ts +207 -4
- package/tsconfig.json +3 -0
- package/dist/constants/addresses.js +0 -13
- package/dist/db/models/execution.js +0 -38
- package/dist/index.js +0 -34
- package/dist/tasks/index.js +0 -19
- package/dist/utils/index.js +0 -89
- package/src/db/models/execution.ts +0 -57
package/src/db/models/index.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export * from './
|
1
|
+
export * from './transaction';
|
@@ -0,0 +1,96 @@
|
|
1
|
+
import { sequelize } from '@/db/sequelize'
|
2
|
+
import { CreationOptional, InferAttributes, InferCreationAttributes, Model, DataTypes } from 'sequelize';
|
3
|
+
|
4
|
+
export class Transaction extends Model<InferAttributes<Transaction>, InferCreationAttributes<Transaction>> {
|
5
|
+
declare id: CreationOptional<number>;
|
6
|
+
|
7
|
+
declare transactionHash: string;
|
8
|
+
declare action: string;
|
9
|
+
declare from: string;
|
10
|
+
declare to: string;
|
11
|
+
|
12
|
+
declare submitTransactionHash: string;
|
13
|
+
declare submitBlockNumber: number;
|
14
|
+
|
15
|
+
declare sourceChainId: number;
|
16
|
+
declare sourceTransactionHash: CreationOptional<string>;
|
17
|
+
declare sourceBlockNumber: CreationOptional<number>;
|
18
|
+
declare sourceStatus: string;
|
19
|
+
declare sourceErrors: CreationOptional<string[]>;
|
20
|
+
declare sourceCreatedAt: CreationOptional<Date>;
|
21
|
+
declare sourceDelayUntil: CreationOptional<Date>;
|
22
|
+
|
23
|
+
declare targetChainId: number;
|
24
|
+
declare targetTransactionHash: CreationOptional<string>;
|
25
|
+
declare targetBlockNumber: CreationOptional<number>;
|
26
|
+
declare targetStatus: string;
|
27
|
+
declare targetErrors: CreationOptional<string[]>;
|
28
|
+
declare targetCreatedAt: CreationOptional<Date>;
|
29
|
+
declare targetDelayUntil: CreationOptional<Date>;
|
30
|
+
|
31
|
+
declare submitEvent: any;
|
32
|
+
declare sourceEvent: CreationOptional<any>;
|
33
|
+
declare targetEvent: CreationOptional<any>;
|
34
|
+
|
35
|
+
declare metadata: CreationOptional<any>;
|
36
|
+
|
37
|
+
declare status: string;
|
38
|
+
|
39
|
+
declare createdAt: CreationOptional<Date>;
|
40
|
+
declare updatedAt: CreationOptional<Date>;
|
41
|
+
}
|
42
|
+
|
43
|
+
Transaction.init({
|
44
|
+
id: {
|
45
|
+
type: DataTypes.INTEGER,
|
46
|
+
autoIncrement: true,
|
47
|
+
primaryKey: true
|
48
|
+
},
|
49
|
+
|
50
|
+
submitTransactionHash: DataTypes.NUMBER,
|
51
|
+
submitBlockNumber: DataTypes.NUMBER,
|
52
|
+
|
53
|
+
transactionHash: DataTypes.STRING,
|
54
|
+
action: DataTypes.STRING,
|
55
|
+
|
56
|
+
from: DataTypes.STRING,
|
57
|
+
to: DataTypes.STRING,
|
58
|
+
|
59
|
+
sourceChainId: DataTypes.NUMBER,
|
60
|
+
sourceTransactionHash: DataTypes.STRING,
|
61
|
+
sourceBlockNumber: DataTypes.NUMBER,
|
62
|
+
sourceStatus: DataTypes.STRING,
|
63
|
+
sourceErrors: {
|
64
|
+
type: DataTypes.JSON,
|
65
|
+
// defaultValue: [],
|
66
|
+
},
|
67
|
+
sourceCreatedAt: {
|
68
|
+
type: DataTypes.DATE,
|
69
|
+
defaultValue: Date.now()
|
70
|
+
},
|
71
|
+
sourceDelayUntil: DataTypes.STRING,
|
72
|
+
|
73
|
+
targetChainId: DataTypes.NUMBER,
|
74
|
+
targetTransactionHash: DataTypes.STRING,
|
75
|
+
targetBlockNumber: DataTypes.NUMBER,
|
76
|
+
targetStatus: DataTypes.STRING,
|
77
|
+
targetErrors: {
|
78
|
+
type: DataTypes.JSON,
|
79
|
+
// defaultValue: [],
|
80
|
+
},
|
81
|
+
targetCreatedAt: DataTypes.DATE,
|
82
|
+
targetDelayUntil: DataTypes.DATE,
|
83
|
+
|
84
|
+
submitEvent: DataTypes.JSON,
|
85
|
+
sourceEvent: DataTypes.JSON,
|
86
|
+
targetEvent: DataTypes.JSON,
|
87
|
+
|
88
|
+
metadata: DataTypes.JSON,
|
89
|
+
|
90
|
+
status: {
|
91
|
+
type: DataTypes.STRING,
|
92
|
+
defaultValue: 'pending'
|
93
|
+
},
|
94
|
+
createdAt: DataTypes.DATE,
|
95
|
+
updatedAt: DataTypes.DATE,
|
96
|
+
}, { sequelize, tableName: 'transactions' });
|
package/src/db/sequelize.ts
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
//@ts-ignore
|
2
|
+
import config from "@/config";
|
2
3
|
import expandHomeDir from "expand-home-dir";
|
3
4
|
|
4
5
|
import { Sequelize } from 'sequelize';
|
5
6
|
|
6
|
-
const basePath = expandHomeDir(
|
7
|
+
const basePath = expandHomeDir(`~/.interop-x/data/${config.publicAddress}/${config.staging ? 'staging' : ''}`);
|
7
8
|
|
8
9
|
export const sequelize = new Sequelize({
|
9
10
|
dialect: 'sqlite',
|
package/src/index.ts
CHANGED
@@ -1,14 +1,71 @@
|
|
1
|
-
import
|
1
|
+
import moduleAlias from 'module-alias';
|
2
|
+
|
3
|
+
moduleAlias.addAliases({
|
4
|
+
"@/": __dirname + "/",
|
5
|
+
"@/logger": __dirname + "/logger",
|
6
|
+
"@/tasks": __dirname + "/tasks",
|
7
|
+
"@/utils": __dirname + "/utils",
|
8
|
+
"@/api": __dirname + "/api",
|
9
|
+
"@/net": __dirname + "/net",
|
10
|
+
"@/db": __dirname + "/db",
|
11
|
+
"@/config": __dirname + "/config",
|
12
|
+
"@/types": __dirname + "/types",
|
13
|
+
"@/abi": __dirname + "/abi",
|
14
|
+
"@/constants": __dirname + "/constants",
|
15
|
+
"@/typechain": __dirname + "/typechain"
|
16
|
+
})
|
17
|
+
|
18
|
+
moduleAlias();
|
2
19
|
import dotenv from "dotenv";
|
3
|
-
import
|
4
|
-
import
|
5
|
-
import
|
20
|
+
import chalk from 'chalk';
|
21
|
+
import { ethers } from "ethers";
|
22
|
+
import packageJson from '../package.json'
|
6
23
|
dotenv.config();
|
7
24
|
|
8
|
-
|
9
|
-
|
25
|
+
import Logger from "@/logger";
|
10
26
|
const logger = new Logger('Process')
|
11
27
|
|
28
|
+
const printUsage = () => {
|
29
|
+
console.log('Usage:')
|
30
|
+
console.log(' PRIVATE_KEY=abcd1234 interop-x')
|
31
|
+
console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x')
|
32
|
+
console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x')
|
33
|
+
console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x')
|
34
|
+
}
|
35
|
+
|
36
|
+
if (process.argv.at(-1) === 'help') {
|
37
|
+
printUsage()
|
38
|
+
process.exit(0)
|
39
|
+
}
|
40
|
+
|
41
|
+
const GIT_SHORT_HASH = '@GIT_SHORT_HASH@';
|
42
|
+
|
43
|
+
if (process.argv.at(-1) === 'version') {
|
44
|
+
console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
|
45
|
+
process.exit(0)
|
46
|
+
}
|
47
|
+
|
48
|
+
if(! process.env.PRIVATE_KEY) {
|
49
|
+
console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
|
50
|
+
printUsage()
|
51
|
+
process.exit(1)
|
52
|
+
}
|
53
|
+
try {
|
54
|
+
new ethers.Wallet(process.env.PRIVATE_KEY!)
|
55
|
+
} catch (e) {
|
56
|
+
console.error(chalk.bgRed.white('Invalid private key\n'))
|
57
|
+
printUsage()
|
58
|
+
process.exit(1)
|
59
|
+
}
|
60
|
+
|
61
|
+
logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
|
62
|
+
|
63
|
+
import { Tasks } from "@/tasks";
|
64
|
+
import { startPeer, protocol, peerPool } from "@/net";
|
65
|
+
import { startApiServer } from '@/api';
|
66
|
+
import { Transaction } from './db';
|
67
|
+
import { shortenHash } from './utils';
|
68
|
+
|
12
69
|
async function main() {
|
13
70
|
|
14
71
|
startPeer({})
|
@@ -16,6 +73,39 @@ async function main() {
|
|
16
73
|
const tasks = new Tasks()
|
17
74
|
|
18
75
|
tasks.start();
|
76
|
+
|
77
|
+
startApiServer()
|
78
|
+
|
79
|
+
protocol.on('TransactionStatus', async (payload) => {
|
80
|
+
if (!peerPool.isLeadNode(payload.peerId)) {
|
81
|
+
const peer = peerPool.getPeer(payload.peerId)
|
82
|
+
|
83
|
+
if(! peer) {
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
|
87
|
+
logger.info(`ignored transaction status from ${payload.peerId} ${shortenHash(peer.publicAddress)} `)
|
88
|
+
return;
|
89
|
+
}
|
90
|
+
|
91
|
+
const transaction = await Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } })
|
92
|
+
|
93
|
+
if (!transaction) {
|
94
|
+
return;
|
95
|
+
}
|
96
|
+
|
97
|
+
transaction.sourceStatus = payload.data.sourceStatus
|
98
|
+
transaction.sourceTransactionHash = payload.data.sourceTransactionHash
|
99
|
+
transaction.sourceErrors = payload.data.sourceErrors
|
100
|
+
|
101
|
+
transaction.targetStatus = payload.data.targetStatus
|
102
|
+
transaction.targetTransactionHash = payload.data.targetTransactionHash
|
103
|
+
transaction.targetErrors = payload.data.targetErrors
|
104
|
+
|
105
|
+
transaction.status = payload.data.status
|
106
|
+
|
107
|
+
await transaction.save()
|
108
|
+
})
|
19
109
|
}
|
20
110
|
|
21
111
|
main()
|
package/src/net/peer/index.ts
CHANGED
@@ -5,7 +5,7 @@ import WS from "libp2p-websockets";
|
|
5
5
|
//@ts-ignore
|
6
6
|
import Mplex from "libp2p-mplex";
|
7
7
|
import { NOISE } from "libp2p-noise";
|
8
|
-
import Logger from "
|
8
|
+
import Logger from "@/logger";
|
9
9
|
import Bootstrap from "libp2p-bootstrap";
|
10
10
|
import wait from "waait";
|
11
11
|
import Gossipsub from "@achingbrain/libp2p-gossipsub";
|
@@ -15,7 +15,9 @@ import MulticastDNS from "libp2p-mdns";
|
|
15
15
|
import KadDHT from "libp2p-kad-dht";
|
16
16
|
//@ts-ignore
|
17
17
|
import PubsubPeerDiscovery from "libp2p-pubsub-peer-discovery";
|
18
|
-
import { protocol } from "
|
18
|
+
import { protocol } from "@/net";
|
19
|
+
import config from "@/config";
|
20
|
+
import chalk from "chalk";
|
19
21
|
|
20
22
|
const logger = new Logger("Peer");
|
21
23
|
|
@@ -79,7 +81,7 @@ export const startPeer = async ({ }: IPeerOptions) => {
|
|
79
81
|
},
|
80
82
|
});
|
81
83
|
|
82
|
-
logger.info("Peer ID:", node.peerId.toB58String());
|
84
|
+
logger.info("Peer ID:", chalk.bold(node.peerId.toB58String()));
|
83
85
|
|
84
86
|
await node.start();
|
85
87
|
|
@@ -87,16 +89,22 @@ export const startPeer = async ({ }: IPeerOptions) => {
|
|
87
89
|
libp2p: node
|
88
90
|
})
|
89
91
|
|
90
|
-
node.on("peer:discovery", (peer) =>
|
91
|
-
logger.log(`Discovered peer ${peer}`)
|
92
|
-
); // peer disc.
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
node.on("peer:discovery", (peer) => {
|
93
|
+
// logger.log(`Discovered peer ${peer}`)
|
94
|
+
}); // peer disc.
|
95
|
+
|
96
|
+
node.connectionManager.on("peer:connect", (connection) => {
|
97
|
+
// logger.log(`Connected to ${connection.remotePeer.toB58String()}`)
|
98
|
+
});
|
96
99
|
|
97
100
|
logger.log("Peer discovery started");
|
98
101
|
|
99
102
|
await wait(1000);
|
103
|
+
|
104
|
+
|
105
|
+
setInterval(() => protocol.sendPeerInfo({
|
106
|
+
publicAddress: config.wallet.address,
|
107
|
+
}), 5000)
|
100
108
|
};
|
101
109
|
|
102
110
|
export const stopPeer = async () => {
|
package/src/net/pool/index.ts
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
import { Event } from "
|
2
|
-
import config from "
|
1
|
+
import { Event } from "@/types";
|
2
|
+
import config from "@/config";
|
3
|
+
import Logger from "@/logger";
|
4
|
+
import { getAddress } from "ethers/lib/utils";
|
5
|
+
import { shortenHash } from "@/utils";
|
6
|
+
import chalk from "chalk";
|
7
|
+
|
8
|
+
|
9
|
+
const logger = new Logger('PeerPool')
|
3
10
|
|
4
11
|
export interface IPeerInfo {
|
5
12
|
id: string;
|
@@ -75,10 +82,15 @@ export class PeerPool {
|
|
75
82
|
* @emits {@link Event.POOL_PEER_ADDED}
|
76
83
|
*/
|
77
84
|
add(peer?: IPeerInfo) {
|
78
|
-
if (peer && peer.id
|
85
|
+
if (peer && peer.id) {
|
86
|
+
const newPeer = !this.pool.get(peer.id);
|
79
87
|
this.pool.set(peer.id, peer)
|
80
88
|
peer.pooled = true
|
81
|
-
|
89
|
+
|
90
|
+
if (newPeer) {
|
91
|
+
config.events.emit(Event.POOL_PEER_ADDED, peer)
|
92
|
+
logger.info(`Peer ${chalk.bold(shortenHash(peer.id, 16))} with address ${chalk.bold(shortenHash(peer.publicAddress))} added to pool`)
|
93
|
+
}
|
82
94
|
}
|
83
95
|
}
|
84
96
|
|
@@ -92,6 +104,7 @@ export class PeerPool {
|
|
92
104
|
if (this.pool.delete(peer.id)) {
|
93
105
|
peer.pooled = false
|
94
106
|
config.events.emit(Event.POOL_PEER_REMOVED, peer)
|
107
|
+
logger.info(`Peer ${chalk.bold(shortenHash(peer.id, 16))} with address ${chalk.bold(shortenHash(peer.publicAddress))} removed from pool`)
|
95
108
|
}
|
96
109
|
}
|
97
110
|
}
|
@@ -100,7 +113,7 @@ export class PeerPool {
|
|
100
113
|
this.cleanup()
|
101
114
|
|
102
115
|
return this.peers.filter((p) => {
|
103
|
-
if(!p.pooled) return false;
|
116
|
+
if (!p.pooled) return false;
|
104
117
|
|
105
118
|
const now = new Date()
|
106
119
|
|
@@ -112,16 +125,33 @@ export class PeerPool {
|
|
112
125
|
return this.activePeers.map((p) => p.id)
|
113
126
|
}
|
114
127
|
|
128
|
+
getPeer(id: string){
|
129
|
+
return this.pool.get(id);
|
130
|
+
}
|
115
131
|
|
116
|
-
|
117
|
-
|
132
|
+
isLeadNode(id: string) {
|
133
|
+
const peer = this.pool.get(id);
|
118
134
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
135
|
+
if (!peer) {
|
136
|
+
return false;
|
137
|
+
}
|
138
|
+
|
139
|
+
return getAddress(peer.publicAddress) === getAddress(config.leadNodeAddress)
|
140
|
+
}
|
141
|
+
|
142
|
+
getLeadPeer() {
|
143
|
+
return this.peers.find((p) => this.isLeadNode(p.id))
|
144
|
+
}
|
145
|
+
|
146
|
+
cleanup() {
|
147
|
+
// let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
|
148
|
+
|
149
|
+
// this.peers.forEach((peerInfo) => {
|
150
|
+
// if (peerInfo.updated.getTime() < compDate) {
|
151
|
+
// console.log(`Peer ${peerInfo.id} idle for ${this.PEERS_CLEANUP_TIME_LIMIT} minutes`)
|
152
|
+
// this.remove(peerInfo)
|
153
|
+
// }
|
154
|
+
// })
|
125
155
|
}
|
126
156
|
}
|
127
157
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import pipe from 'it-pipe';
|
2
2
|
import Libp2p from 'libp2p';
|
3
3
|
import PeerId from 'peer-id';
|
4
|
-
import { asyncCallWithTimeout } from '
|
4
|
+
import { asyncCallWithTimeout } from '@/utils';
|
5
5
|
import wait from 'waait';
|
6
6
|
|
7
7
|
export class BaseDialProtocol<TRequest extends any, TResponse extends any> {
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { BaseDialProtocol } from "./BaseDialProtocol";
|
2
|
+
import { Transaction } from "@/db";
|
3
|
+
|
4
|
+
export class TransactionStatusDialProtocol extends BaseDialProtocol<string, Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'> | null> {
|
5
|
+
protected timeout = 30000;
|
6
|
+
|
7
|
+
constructor(libp2p) {
|
8
|
+
super(libp2p, '/interop-x/transaction-status')
|
9
|
+
}
|
10
|
+
|
11
|
+
async response(transactionHash: string){
|
12
|
+
const transaction = await Transaction.findOne({ where: { transactionHash } })
|
13
|
+
|
14
|
+
if(! transaction){
|
15
|
+
return null
|
16
|
+
}
|
17
|
+
return {
|
18
|
+
transactionHash: transaction.transactionHash,
|
19
|
+
|
20
|
+
sourceStatus: transaction.sourceStatus,
|
21
|
+
sourceTransactionHash: transaction.sourceTransactionHash,
|
22
|
+
sourceErrors: transaction.sourceErrors,
|
23
|
+
|
24
|
+
targetStatus: transaction.targetStatus,
|
25
|
+
targetTransactionHash: transaction.targetTransactionHash,
|
26
|
+
targetErrors: transaction.targetErrors,
|
27
|
+
|
28
|
+
status: transaction.status,
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
@@ -1,14 +1,14 @@
|
|
1
|
-
import { signGnosisSafeTx } from "../../../utils";
|
2
1
|
import { BaseDialProtocol } from "./BaseDialProtocol";
|
3
2
|
import wait from "waait";
|
4
|
-
import
|
5
|
-
import
|
6
|
-
import {
|
7
|
-
import {
|
3
|
+
import config from "@/config";
|
4
|
+
import { Transaction } from "@/db";
|
5
|
+
import { buildDataForTransaction, signGnosisSafeTx } from "@/utils";
|
6
|
+
import { addresses } from "@/constants";
|
7
|
+
import { ChainId } from "@/types";
|
8
8
|
|
9
9
|
export interface ISignatureRequest {
|
10
|
-
type:
|
11
|
-
|
10
|
+
type: 'source' | 'target' ,
|
11
|
+
transactionHash: string
|
12
12
|
safeTxGas: string
|
13
13
|
safeNonce: string
|
14
14
|
}
|
@@ -21,25 +21,25 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
|
|
21
21
|
protected timeout = 30000;
|
22
22
|
|
23
23
|
constructor(libp2p) {
|
24
|
-
super(libp2p, '/signatures')
|
24
|
+
super(libp2p, '/interop-x/signatures')
|
25
25
|
}
|
26
26
|
|
27
27
|
async response(data: ISignatureRequest): Promise<ISignatureResponse> {
|
28
28
|
const signer = config.wallet;
|
29
29
|
|
30
|
-
let
|
30
|
+
let transaction: Transaction | null;
|
31
31
|
let maxTimeout = 20000;
|
32
32
|
|
33
33
|
do {
|
34
|
-
|
34
|
+
transaction = await Transaction.findOne({ where: { transactionHash: data.transactionHash } })
|
35
35
|
|
36
|
-
if (!
|
36
|
+
if (!transaction) {
|
37
37
|
await wait(1000);
|
38
38
|
maxTimeout -= 1000;
|
39
39
|
}
|
40
|
-
} while (!
|
40
|
+
} while (!transaction && maxTimeout > 0)
|
41
41
|
|
42
|
-
if (!
|
42
|
+
if (!transaction) {
|
43
43
|
return {
|
44
44
|
signer: signer.address,
|
45
45
|
data: null,
|
@@ -47,16 +47,25 @@ 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
|
-
to: addresses[
|
52
|
-
data:
|
53
|
-
chainId:
|
59
|
+
to: addresses[transaction.targetChainId].multisend,
|
60
|
+
data: await buildDataForTransaction(transaction, data.type),
|
61
|
+
chainId: transaction.targetChainId as ChainId,
|
54
62
|
safeTxGas: data.safeTxGas,
|
63
|
+
nonce: data.safeNonce,
|
55
64
|
}, { signer });
|
56
65
|
|
57
66
|
return {
|
58
67
|
signer: signer.address,
|
59
|
-
data: signedData
|
68
|
+
data: signedData
|
60
69
|
}
|
61
70
|
}
|
62
71
|
}
|
@@ -3,8 +3,10 @@ import Libp2p from 'libp2p';
|
|
3
3
|
import { bufferToInt, rlp } from 'ethereumjs-util'
|
4
4
|
import { SignatureDialProtocol, ISignatureRequest, ISignatureResponse } from "./dial/SignatureDialProtocol";
|
5
5
|
import { IPeerInfo, peerPool } from "..";
|
6
|
-
import config from "config";
|
7
|
-
import { Event } from "types";
|
6
|
+
import config from "@/config";
|
7
|
+
import { Event } from "@/types";
|
8
|
+
import { Transaction } from "@/db";
|
9
|
+
import { TransactionStatusDialProtocol } from "./dial/SignatureDialProtocol.1";
|
8
10
|
|
9
11
|
export interface ProtocolOptions {
|
10
12
|
/* Handshake timeout in ms (default: 8000) */
|
@@ -33,7 +35,12 @@ interface PeerInfoEvent extends BaseMessageEvent {
|
|
33
35
|
data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
|
34
36
|
}
|
35
37
|
|
38
|
+
interface TransactionStatusEvent extends BaseMessageEvent {
|
39
|
+
data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
|
40
|
+
}
|
41
|
+
|
36
42
|
declare interface Protocol {
|
43
|
+
on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
|
37
44
|
on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
|
38
45
|
on(event: string, listener: (payload: BaseMessageEvent) => void): this;
|
39
46
|
}
|
@@ -44,7 +51,7 @@ class Protocol extends EventEmitter {
|
|
44
51
|
private protocolMessages: Message[] = [
|
45
52
|
{
|
46
53
|
name: 'PeerInfo',
|
47
|
-
code:
|
54
|
+
code: 0x01,
|
48
55
|
encode: (info: Pick<IPeerInfo, 'publicAddress'>) => [
|
49
56
|
Buffer.from(info.publicAddress),
|
50
57
|
],
|
@@ -52,13 +59,44 @@ class Protocol extends EventEmitter {
|
|
52
59
|
publicAddress: publicAddress.toString(),
|
53
60
|
}),
|
54
61
|
},
|
62
|
+
{
|
63
|
+
name: 'TransactionStatus',
|
64
|
+
code: 0x02,
|
65
|
+
encode: (transaction: Transaction) => [
|
66
|
+
Buffer.from(transaction.transactionHash),
|
67
|
+
|
68
|
+
Buffer.from(transaction.sourceStatus),
|
69
|
+
Buffer.from(transaction.sourceTransactionHash),
|
70
|
+
transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
|
71
|
+
|
72
|
+
Buffer.from(transaction.targetStatus),
|
73
|
+
Buffer.from(transaction.targetTransactionHash),
|
74
|
+
transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
|
75
|
+
|
76
|
+
Buffer.from(transaction.status),
|
77
|
+
],
|
78
|
+
decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]: [Buffer, Buffer, Buffer, Buffer[], Buffer, Buffer, Buffer[], Buffer]) => ({
|
79
|
+
transactionHash: transactionHash.toString(),
|
80
|
+
|
81
|
+
sourceStatus: sourceStatus.toString(),
|
82
|
+
sourceTransactionHash: sourceTransactionHash.toString(),
|
83
|
+
sourceErrors: sourceErrors.map((e) => e.toString()),
|
84
|
+
|
85
|
+
targetStatus: targetStatus.toString(),
|
86
|
+
targetTransactionHash: targetTransactionHash.toString(),
|
87
|
+
targetErrors: targetErrors.map((e) => e.toString()),
|
88
|
+
|
89
|
+
status: status.toString(),
|
90
|
+
}),
|
91
|
+
},
|
55
92
|
];
|
56
93
|
private signature: SignatureDialProtocol;
|
94
|
+
private transactionStatus: TransactionStatusDialProtocol;
|
57
95
|
|
58
96
|
|
59
97
|
start({ libp2p, topic = null, }) {
|
60
98
|
this.libp2p = libp2p
|
61
|
-
this.topic = topic || 'protocol'
|
99
|
+
this.topic = topic || 'itnerop-x-protocol'
|
62
100
|
|
63
101
|
if (this.libp2p.isStarted()) this.init()
|
64
102
|
|
@@ -73,6 +111,7 @@ class Protocol extends EventEmitter {
|
|
73
111
|
})
|
74
112
|
|
75
113
|
this.signature = new SignatureDialProtocol(this.libp2p);
|
114
|
+
this.transactionStatus = new TransactionStatusDialProtocol(this.libp2p);
|
76
115
|
}
|
77
116
|
|
78
117
|
|
@@ -113,7 +152,7 @@ class Protocol extends EventEmitter {
|
|
113
152
|
}
|
114
153
|
|
115
154
|
|
116
|
-
public sendPeerInfo(data:
|
155
|
+
public sendPeerInfo(data: Pick<IPeerInfo, 'publicAddress'>) {
|
117
156
|
const message = this.protocolMessages.find((m) => m.name === 'PeerInfo')!
|
118
157
|
|
119
158
|
const encoded = rlp.encode([message.code, message.encode(data)]);
|
@@ -121,6 +160,14 @@ class Protocol extends EventEmitter {
|
|
121
160
|
this.libp2p.pubsub.publish(this.topic, encoded)
|
122
161
|
}
|
123
162
|
|
163
|
+
public sendTransaction(transaction: Transaction) {
|
164
|
+
const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
|
165
|
+
|
166
|
+
const encoded = rlp.encode([message.code, message.encode(transaction)]);
|
167
|
+
|
168
|
+
this.libp2p.pubsub.publish(this.topic, encoded)
|
169
|
+
}
|
170
|
+
|
124
171
|
async requestSignatures(data: ISignatureRequest, peerIds?: string[]) {
|
125
172
|
try {
|
126
173
|
peerIds = peerIds || peerPool.activePeerIds;
|
@@ -133,6 +180,15 @@ class Protocol extends EventEmitter {
|
|
133
180
|
return []
|
134
181
|
}
|
135
182
|
}
|
183
|
+
|
184
|
+
async requestTransactionStatus(transactionHash: string, peerId: string) {
|
185
|
+
try {
|
186
|
+
return await this.transactionStatus.send(transactionHash, peerId);
|
187
|
+
} catch (error) {
|
188
|
+
console.log(error);
|
189
|
+
return null
|
190
|
+
}
|
191
|
+
}
|
136
192
|
}
|
137
193
|
|
138
194
|
export const protocol = new Protocol();
|