@instadapp/interop-x 0.0.0-dev.9387bd9 → 0.0.0-dev.9b26e43
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/bin/interop-x +1 -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 +13 -0
- package/dist/src/abi/interopXContract.json +391 -0
- package/dist/src/alias.js +10 -0
- package/dist/src/api/index.js +36 -0
- package/dist/src/config/index.js +31 -0
- package/dist/src/constants/addresses.js +20 -0
- package/dist/{constants → src/constants}/index.js +1 -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 +72 -0
- package/dist/{db → src/db}/sequelize.js +2 -1
- package/dist/src/gnosis/actions/index.js +9 -0
- package/dist/src/gnosis/actions/withdraw/index.js +41 -0
- package/dist/src/gnosis/index.js +20 -0
- package/dist/src/index.js +119 -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 +13 -8
- package/dist/{net → src/net}/pool/index.js +34 -11
- package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +1 -1
- package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +21 -14
- package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
- package/dist/{net → src/net}/protocol/index.js +54 -4
- package/dist/src/tasks/AutoUpdateTask.js +70 -0
- package/dist/{tasks → src/tasks}/BaseTask.js +14 -6
- package/dist/src/tasks/InteropXContract/ProcessBridgeRequestEvents.js +147 -0
- package/dist/src/tasks/InteropXContract/SyncBridgeRequestEvents.js +78 -0
- package/dist/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.js +80 -0
- package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +55 -0
- package/dist/src/tasks/index.js +38 -0
- package/dist/src/typechain/Erc20.js +2 -0
- package/dist/src/typechain/GnosisSafe.js +2 -0
- package/dist/src/typechain/InteropXContract.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/InteropXContract__factory.js +526 -0
- package/dist/src/typechain/factories/index.js +12 -0
- package/dist/src/typechain/index.js +33 -0
- package/dist/{types.js → src/types.js} +0 -0
- package/dist/src/utils/index.js +168 -0
- package/package.json +21 -7
- 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 +9 -0
- package/src/abi/interopXContract.json +391 -0
- package/src/alias.ts +6 -0
- package/src/api/index.ts +36 -0
- package/src/config/index.ts +18 -2
- package/src/constants/addresses.ts +9 -2
- package/src/constants/index.ts +1 -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 +141 -0
- package/src/db/sequelize.ts +2 -1
- package/src/gnosis/actions/index.ts +5 -0
- package/src/gnosis/actions/withdraw/index.ts +56 -0
- package/src/gnosis/index.ts +19 -0
- package/src/index.ts +109 -7
- package/src/net/peer/index.ts +12 -10
- package/src/net/pool/index.ts +43 -13
- package/src/net/protocol/dial/BaseDialProtocol.ts +1 -1
- package/src/net/protocol/dial/SignatureDialProtocol.ts +24 -17
- package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
- package/src/net/protocol/index.ts +70 -4
- package/src/tasks/AutoUpdateTask.ts +82 -0
- package/src/tasks/BaseTask.ts +16 -7
- package/src/tasks/InteropXContract/ProcessBridgeRequestEvents.ts +211 -0
- package/src/tasks/InteropXContract/SyncBridgeRequestEvents.ts +116 -0
- package/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.ts +112 -0
- package/src/tasks/Transactions/SyncTransactionStatusTask.ts +67 -0
- package/src/tasks/index.ts +24 -2
- package/src/typechain/Erc20.ts +491 -0
- package/src/typechain/GnosisSafe.ts +1728 -0
- package/src/typechain/InteropXContract.ts +524 -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/InteropXContract__factory.ts +533 -0
- package/src/typechain/factories/index.ts +6 -0
- package/src/typechain/index.ts +10 -0
- package/src/types.ts +2 -2
- package/src/utils/index.ts +129 -5
- package/tsconfig.json +8 -0
- package/dist/config/index.js +0 -17
- package/dist/constants/addresses.js +0 -13
- package/dist/db/models/execution.js +0 -38
- package/dist/index.js +0 -42
- package/dist/tasks/index.js +0 -19
- package/dist/utils/index.js +0 -89
- package/src/db/models/execution.ts +0 -57
@@ -0,0 +1,141 @@
|
|
1
|
+
import { sequelize } from '@/db/sequelize'
|
2
|
+
import { CreationOptional, InferAttributes, InferCreationAttributes, Model, DataTypes } from 'sequelize';
|
3
|
+
|
4
|
+
export interface IPositionTokenInfo{
|
5
|
+
amount: string;
|
6
|
+
sourceToken: string;
|
7
|
+
targetToken: string;
|
8
|
+
}
|
9
|
+
|
10
|
+
export interface IPosition {
|
11
|
+
supply: IPositionTokenInfo[];
|
12
|
+
withdraw: IPositionTokenInfo[];
|
13
|
+
}
|
14
|
+
|
15
|
+
export class Transaction extends Model<InferAttributes<Transaction>, InferCreationAttributes<Transaction>> {
|
16
|
+
declare id: CreationOptional<number>;
|
17
|
+
|
18
|
+
declare transactionHash: string;
|
19
|
+
|
20
|
+
declare action: string;
|
21
|
+
declare bridger: string;
|
22
|
+
declare requestTransactionHash: string;
|
23
|
+
|
24
|
+
declare requestBlockNumber: number;
|
25
|
+
|
26
|
+
declare sourceChainId: number;
|
27
|
+
declare sourceTransactionHash: CreationOptional<string>;
|
28
|
+
declare sourceBlockNumber: CreationOptional<number>;
|
29
|
+
declare sourceStatus: CreationOptional<string>;
|
30
|
+
declare sourceErrors: CreationOptional<string[]>;
|
31
|
+
declare sourceLogs: CreationOptional<any[]>;
|
32
|
+
declare sourceCreatedAt: CreationOptional<Date>;
|
33
|
+
declare sourceDelayUntil: CreationOptional<Date>;
|
34
|
+
|
35
|
+
declare targetChainId: number;
|
36
|
+
declare targetTransactionHash: CreationOptional<string>;
|
37
|
+
declare targetBlockNumber: CreationOptional<number>;
|
38
|
+
declare targetStatus: CreationOptional<string>;
|
39
|
+
declare targetErrors: CreationOptional<string[]>;
|
40
|
+
declare targetLogs: CreationOptional<any[]>;
|
41
|
+
declare targetCreatedAt: CreationOptional<Date>;
|
42
|
+
declare targetDelayUntil: CreationOptional<Date>;
|
43
|
+
|
44
|
+
declare requestEvent: {
|
45
|
+
bridger: string;
|
46
|
+
metadata: string;
|
47
|
+
position: IPosition;
|
48
|
+
sourceChainId: number;
|
49
|
+
targetChainId: number;
|
50
|
+
};
|
51
|
+
declare requestSentEvent: CreationOptional<{
|
52
|
+
bridger: string;
|
53
|
+
metadata: string;
|
54
|
+
position: IPosition;
|
55
|
+
sourceChainId: number;
|
56
|
+
targetChainId: number;
|
57
|
+
requestTransactionHash: string;
|
58
|
+
}>;
|
59
|
+
declare committedEvent: CreationOptional<any>;
|
60
|
+
declare completedEvent: CreationOptional<any>;
|
61
|
+
|
62
|
+
declare position: any;
|
63
|
+
declare metadata: CreationOptional<any>;
|
64
|
+
|
65
|
+
declare status: CreationOptional<string>;
|
66
|
+
|
67
|
+
declare createdAt: CreationOptional<Date>;
|
68
|
+
declare updatedAt: CreationOptional<Date>;
|
69
|
+
}
|
70
|
+
|
71
|
+
Transaction.init({
|
72
|
+
id: {
|
73
|
+
type: DataTypes.INTEGER,
|
74
|
+
autoIncrement: true,
|
75
|
+
primaryKey: true
|
76
|
+
},
|
77
|
+
|
78
|
+
requestTransactionHash: DataTypes.NUMBER,
|
79
|
+
requestBlockNumber: DataTypes.NUMBER,
|
80
|
+
|
81
|
+
transactionHash: DataTypes.STRING,
|
82
|
+
action: DataTypes.STRING,
|
83
|
+
bridger: DataTypes.STRING,
|
84
|
+
|
85
|
+
sourceChainId: DataTypes.NUMBER,
|
86
|
+
sourceTransactionHash: DataTypes.STRING,
|
87
|
+
sourceBlockNumber: DataTypes.NUMBER,
|
88
|
+
sourceStatus: {
|
89
|
+
type: DataTypes.STRING,
|
90
|
+
defaultValue: 'uninitialised'
|
91
|
+
},
|
92
|
+
sourceErrors: {
|
93
|
+
type: DataTypes.JSON,
|
94
|
+
// defaultValue: [],
|
95
|
+
},
|
96
|
+
sourceLogs: {
|
97
|
+
type: DataTypes.JSON,
|
98
|
+
// defaultValue: [],
|
99
|
+
},
|
100
|
+
sourceCreatedAt: {
|
101
|
+
type: DataTypes.DATE,
|
102
|
+
defaultValue: Date.now()
|
103
|
+
},
|
104
|
+
sourceDelayUntil: DataTypes.STRING,
|
105
|
+
|
106
|
+
targetChainId: DataTypes.NUMBER,
|
107
|
+
targetTransactionHash: DataTypes.STRING,
|
108
|
+
targetBlockNumber: DataTypes.NUMBER,
|
109
|
+
targetStatus: {
|
110
|
+
type: DataTypes.STRING,
|
111
|
+
defaultValue: 'uninitialised'
|
112
|
+
},
|
113
|
+
targetErrors: {
|
114
|
+
type: DataTypes.JSON,
|
115
|
+
// defaultValue: [],
|
116
|
+
},
|
117
|
+
targetLogs: {
|
118
|
+
type: DataTypes.JSON,
|
119
|
+
// defaultValue: [],
|
120
|
+
},
|
121
|
+
targetCreatedAt: DataTypes.DATE,
|
122
|
+
targetDelayUntil: DataTypes.DATE,
|
123
|
+
|
124
|
+
requestEvent: {
|
125
|
+
type: DataTypes.JSON,
|
126
|
+
allowNull: false
|
127
|
+
},
|
128
|
+
requestSentEvent: DataTypes.JSON,
|
129
|
+
committedEvent: DataTypes.JSON,
|
130
|
+
completedEvent: DataTypes.JSON,
|
131
|
+
|
132
|
+
position: DataTypes.JSON,
|
133
|
+
metadata: DataTypes.JSON,
|
134
|
+
|
135
|
+
status: {
|
136
|
+
type: DataTypes.STRING,
|
137
|
+
defaultValue: 'pending'
|
138
|
+
},
|
139
|
+
createdAt: DataTypes.DATE,
|
140
|
+
updatedAt: DataTypes.DATE,
|
141
|
+
}, { 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',
|
@@ -0,0 +1,56 @@
|
|
1
|
+
import abi from "@/abi";
|
2
|
+
import config from "@/config";
|
3
|
+
import { addresses, tokens } from "@/constants";
|
4
|
+
import { Transaction } from "@/db";
|
5
|
+
import { InteropXContract } from "@/typechain";
|
6
|
+
import { ChainId } from "@/types";
|
7
|
+
import { getContract, getRpcProviderUrl } from "@/utils";
|
8
|
+
import { ethers } from "ethers";
|
9
|
+
import { MetaTransaction, OperationType } from "ethers-multisend";
|
10
|
+
|
11
|
+
export default async function (transaction: Transaction, type: 'source' | 'target') {
|
12
|
+
const transactions: MetaTransaction[] = [];
|
13
|
+
const logs: any[] = [];
|
14
|
+
|
15
|
+
if (transaction.action !== 'withdraw') {
|
16
|
+
throw new Error(`Invalid action: ${transaction.action}`)
|
17
|
+
}
|
18
|
+
|
19
|
+
if (type !== 'source') {
|
20
|
+
throw new Error(`Type not supported: ${type}`)
|
21
|
+
}
|
22
|
+
|
23
|
+
if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
|
24
|
+
throw Error('Cannot build data for pending withdraw transaction');
|
25
|
+
}
|
26
|
+
|
27
|
+
if (!transaction.requestEvent) {
|
28
|
+
throw Error('Cannot build data for transaction without requestEvent');
|
29
|
+
}
|
30
|
+
|
31
|
+
const { bridger, position, sourceChainId, targetChainId, metadata, } = transaction.requestEvent;
|
32
|
+
|
33
|
+
const sourceChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(targetChainId as ChainId));
|
34
|
+
const sourceWallet = new ethers.Wallet(config.privateKey, sourceChainProvider);
|
35
|
+
const contractAddress = addresses[sourceChainId].interopXContract;
|
36
|
+
const contract = getContract<InteropXContract>(contractAddress, abi.interopXContract, sourceWallet);
|
37
|
+
|
38
|
+
const { data } = await contract.populateTransaction.withdrawRequested(
|
39
|
+
bridger,
|
40
|
+
position.withdraw[0].sourceToken,
|
41
|
+
position.withdraw[0].targetToken,
|
42
|
+
position.withdraw[0].amount,
|
43
|
+
targetChainId,
|
44
|
+
transaction.requestTransactionHash,
|
45
|
+
metadata,
|
46
|
+
);
|
47
|
+
|
48
|
+
transactions.push({
|
49
|
+
to: contractAddress,
|
50
|
+
data: data!,
|
51
|
+
value: '0',
|
52
|
+
operation: OperationType.Call,
|
53
|
+
});
|
54
|
+
|
55
|
+
return { transactions, logs }
|
56
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { Transaction } from "@/db";
|
2
|
+
import { encodeMulti } from "ethers-multisend";
|
3
|
+
import actions from "./actions";
|
4
|
+
|
5
|
+
export const buildGnosisAction = async (transaction: Transaction, type: 'source' | 'target') => {
|
6
|
+
// type = type || (transaction.sourceStatus === 'success' ? 'target' : 'source')
|
7
|
+
|
8
|
+
if (actions.hasOwnProperty(transaction.action)) {
|
9
|
+
|
10
|
+
const { transactions, logs } = await actions[transaction.action](transaction, type);
|
11
|
+
|
12
|
+
return {
|
13
|
+
data: encodeMulti(transactions).data,
|
14
|
+
logs
|
15
|
+
};
|
16
|
+
}
|
17
|
+
|
18
|
+
throw new Error(`Unknown action: ${transaction.action}`);
|
19
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,22 +1,87 @@
|
|
1
|
-
import
|
1
|
+
import './alias'
|
2
|
+
import expandHomeDir from "expand-home-dir";
|
3
|
+
import fs from 'fs-extra'
|
4
|
+
|
2
5
|
import dotenv from "dotenv";
|
3
|
-
import
|
6
|
+
import chalk from 'chalk';
|
4
7
|
import { ethers } from "ethers";
|
8
|
+
import packageJson from '../package.json'
|
5
9
|
dotenv.config();
|
10
|
+
|
11
|
+
import Logger from "@/logger";
|
6
12
|
const logger = new Logger('Process')
|
7
13
|
|
8
|
-
|
14
|
+
const GIT_SHORT_HASH = '@GIT_SHORT_HASH@';
|
15
|
+
|
16
|
+
const printUsage = () => {
|
17
|
+
console.log()
|
18
|
+
console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
|
19
|
+
console.log()
|
20
|
+
|
21
|
+
console.log('Usage:')
|
22
|
+
console.log(' interop-x help Show this message')
|
23
|
+
console.log(' interop-x version Print out the installed version of Interop X')
|
24
|
+
|
25
|
+
console.log()
|
26
|
+
|
27
|
+
console.log(' interop-x down Put the node into maintenance mode')
|
28
|
+
console.log(' interop-x up Take the node out of maintenance mode')
|
29
|
+
|
30
|
+
console.log()
|
31
|
+
|
32
|
+
console.log(' PRIVATE_KEY=abcd1234 interop-x Start the node with the given private key')
|
33
|
+
console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x Start the node in staging mode')
|
34
|
+
console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x Start the node in auto update mode')
|
35
|
+
console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x Start the node with custom API host and port')
|
36
|
+
console.log()
|
37
|
+
|
38
|
+
}
|
39
|
+
|
40
|
+
if (process.argv.at(-1) === 'help') {
|
41
|
+
printUsage()
|
42
|
+
process.exit(0)
|
43
|
+
}
|
44
|
+
|
45
|
+
const basePath = expandHomeDir(`~/.interop-x`);
|
46
|
+
|
47
|
+
if (process.argv.at(-1) === 'down') {
|
48
|
+
fs.outputFileSync(basePath + '/maintenance', Date.now().toString())
|
49
|
+
console.log(chalk.red('Maintenance mode enabled'))
|
50
|
+
process.exit(0)
|
51
|
+
}
|
52
|
+
|
53
|
+
if (process.argv.at(-1) === 'up') {
|
54
|
+
fs.removeSync(basePath + '/maintenance')
|
55
|
+
console.log(chalk.green('Maintenance mode disabled'))
|
56
|
+
process.exit(0)
|
57
|
+
}
|
58
|
+
|
9
59
|
|
60
|
+
if (process.argv.at(-1) === 'version') {
|
61
|
+
console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
|
62
|
+
process.exit(0)
|
63
|
+
}
|
64
|
+
|
65
|
+
if (!process.env.PRIVATE_KEY) {
|
66
|
+
console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
|
67
|
+
printUsage()
|
68
|
+
process.exit(1)
|
69
|
+
}
|
10
70
|
try {
|
11
71
|
new ethers.Wallet(process.env.PRIVATE_KEY!)
|
12
72
|
} catch (e) {
|
13
|
-
|
73
|
+
console.error(chalk.bgRed.white('Invalid private key\n'))
|
74
|
+
printUsage()
|
14
75
|
process.exit(1)
|
15
76
|
}
|
16
77
|
|
17
|
-
|
18
|
-
import { startPeer } from "./net";
|
78
|
+
logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
|
19
79
|
|
80
|
+
import { Tasks } from "@/tasks";
|
81
|
+
import { startPeer, protocol, peerPool } from "@/net";
|
82
|
+
import { startApiServer } from '@/api';
|
83
|
+
import { Transaction } from './db';
|
84
|
+
import { shortenHash } from './utils';
|
20
85
|
|
21
86
|
async function main() {
|
22
87
|
|
@@ -24,7 +89,44 @@ async function main() {
|
|
24
89
|
|
25
90
|
const tasks = new Tasks()
|
26
91
|
|
27
|
-
|
92
|
+
setTimeout(() => {
|
93
|
+
tasks.start();
|
94
|
+
}, 10000)
|
95
|
+
|
96
|
+
startApiServer()
|
97
|
+
|
98
|
+
protocol.on('TransactionStatus', async (payload) => {
|
99
|
+
if (!peerPool.isLeadNode(payload.peerId)) {
|
100
|
+
const peer = peerPool.getPeer(payload.peerId)
|
101
|
+
|
102
|
+
if (!peer) {
|
103
|
+
return;
|
104
|
+
}
|
105
|
+
|
106
|
+
logger.info(`ignored transaction status from ${payload.peerId} ${shortenHash(peer.publicAddress)} `)
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
|
110
|
+
const transaction = await Transaction.findOne({ where: { transactionHash: payload.data.transactionHash } })
|
111
|
+
|
112
|
+
if (!transaction) {
|
113
|
+
return;
|
114
|
+
}
|
115
|
+
|
116
|
+
transaction.sourceStatus = payload.data.sourceStatus
|
117
|
+
transaction.sourceTransactionHash = payload.data.sourceTransactionHash
|
118
|
+
transaction.sourceErrors = payload.data.sourceErrors
|
119
|
+
transaction.sourceLogs = payload.data.sourceLogs
|
120
|
+
|
121
|
+
transaction.targetStatus = payload.data.targetStatus
|
122
|
+
transaction.targetTransactionHash = payload.data.targetTransactionHash
|
123
|
+
transaction.targetErrors = payload.data.targetErrors
|
124
|
+
transaction.targetLogs = payload.data.targetLogs
|
125
|
+
|
126
|
+
transaction.status = payload.data.status
|
127
|
+
|
128
|
+
await transaction.save()
|
129
|
+
})
|
28
130
|
}
|
29
131
|
|
30
132
|
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,8 +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 "
|
19
|
-
import config from "
|
18
|
+
import { protocol } from "@/net";
|
19
|
+
import config from "@/config";
|
20
|
+
import chalk from "chalk";
|
20
21
|
|
21
22
|
const logger = new Logger("Peer");
|
22
23
|
|
@@ -80,7 +81,7 @@ export const startPeer = async ({ }: IPeerOptions) => {
|
|
80
81
|
},
|
81
82
|
});
|
82
83
|
|
83
|
-
logger.info("Peer ID:", node.peerId.toB58String());
|
84
|
+
logger.info("Peer ID:", chalk.bold(node.peerId.toB58String()));
|
84
85
|
|
85
86
|
await node.start();
|
86
87
|
|
@@ -88,12 +89,13 @@ export const startPeer = async ({ }: IPeerOptions) => {
|
|
88
89
|
libp2p: node
|
89
90
|
})
|
90
91
|
|
91
|
-
node.on("peer:discovery", (peer) =>
|
92
|
-
logger.log(`Discovered peer ${peer}`)
|
93
|
-
); // peer disc.
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
+
});
|
97
99
|
|
98
100
|
logger.log("Peer discovery started");
|
99
101
|
|
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> {
|
@@ -1,14 +1,15 @@
|
|
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 { signGnosisSafeTx } from "@/utils";
|
6
|
+
import { addresses } from "@/constants";
|
7
|
+
import { ChainId } from "@/types";
|
8
|
+
import { buildGnosisAction } from "@/gnosis";
|
8
9
|
|
9
10
|
export interface ISignatureRequest {
|
10
|
-
type:
|
11
|
-
|
11
|
+
type: 'source' | 'target',
|
12
|
+
transactionHash: string
|
12
13
|
safeTxGas: string
|
13
14
|
safeNonce: string
|
14
15
|
}
|
@@ -21,42 +22,48 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
|
|
21
22
|
protected timeout = 30000;
|
22
23
|
|
23
24
|
constructor(libp2p) {
|
24
|
-
super(libp2p, '/signatures')
|
25
|
+
super(libp2p, '/interop-x/signatures')
|
25
26
|
}
|
26
27
|
|
27
28
|
async response(data: ISignatureRequest): Promise<ISignatureResponse> {
|
29
|
+
console.log({
|
30
|
+
tag: 'SignatureDialProtocol',
|
31
|
+
data
|
32
|
+
})
|
28
33
|
const signer = config.wallet;
|
29
34
|
|
30
|
-
let
|
35
|
+
let transaction: Transaction | null;
|
31
36
|
let maxTimeout = 20000;
|
32
37
|
|
33
38
|
do {
|
34
|
-
|
39
|
+
transaction = await Transaction.findOne({ where: { transactionHash: data.transactionHash } })
|
35
40
|
|
36
|
-
if (!
|
41
|
+
if (!transaction) {
|
37
42
|
await wait(1000);
|
38
43
|
maxTimeout -= 1000;
|
39
44
|
}
|
40
|
-
} while (!
|
45
|
+
} while (!transaction && maxTimeout > 0)
|
41
46
|
|
42
|
-
if (!
|
47
|
+
if (!transaction) {
|
43
48
|
return {
|
44
49
|
signer: signer.address,
|
45
50
|
data: null,
|
46
51
|
error: 'Event not found'
|
47
52
|
};
|
48
53
|
}
|
54
|
+
const { data: gnosisData } = await buildGnosisAction(transaction, data.type);
|
49
55
|
|
50
56
|
const signedData = await signGnosisSafeTx({
|
51
|
-
to: addresses[
|
52
|
-
data:
|
53
|
-
chainId:
|
57
|
+
to: addresses[transaction.sourceChainId].multisend,
|
58
|
+
data: gnosisData,
|
59
|
+
chainId: transaction.sourceChainId as ChainId,
|
54
60
|
safeTxGas: data.safeTxGas,
|
61
|
+
nonce: data.safeNonce,
|
55
62
|
}, { signer });
|
56
63
|
|
57
64
|
return {
|
58
65
|
signer: signer.address,
|
59
|
-
data: signedData
|
66
|
+
data: signedData
|
60
67
|
}
|
61
68
|
}
|
62
69
|
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import { BaseDialProtocol } from "./BaseDialProtocol";
|
2
|
+
import { Transaction } from "@/db";
|
3
|
+
|
4
|
+
export class TransactionStatusDialProtocol extends BaseDialProtocol<string, Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'sourceLogs' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'targetLogs' | '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
|
+
sourceLogs: transaction.sourceLogs,
|
24
|
+
|
25
|
+
targetStatus: transaction.targetStatus,
|
26
|
+
targetTransactionHash: transaction.targetTransactionHash,
|
27
|
+
targetErrors: transaction.targetErrors,
|
28
|
+
targetLogs: transaction.targetLogs,
|
29
|
+
|
30
|
+
status: transaction.status,
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|