@instadapp/interop-x 0.0.0-dev.0159b4f
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 -0
- package/.github/workflows/ci.yml +19 -0
- package/.github/workflows/publish-dev.yml +30 -0
- package/README.md +21 -0
- package/bin/interop-x +2 -0
- package/dist/package.json +75 -0
- package/dist/src/abi/connectors/index.js +36 -0
- package/dist/src/abi/connectors/v1/aave.js +148 -0
- package/dist/src/abi/connectors/v1/aave_claim.js +4 -0
- package/dist/src/abi/connectors/v1/aave_migrate.js +109 -0
- package/dist/src/abi/connectors/v1/aave_polygon_migrate.js +110 -0
- package/dist/src/abi/connectors/v1/aave_stake.js +4 -0
- package/dist/src/abi/connectors/v1/aave_v1_import.js +54 -0
- package/dist/src/abi/connectors/v1/aave_v2.js +230 -0
- package/dist/src/abi/connectors/v1/aave_v2_import.js +59 -0
- package/dist/src/abi/connectors/v1/authority.js +100 -0
- package/dist/src/abi/connectors/v1/basic.js +136 -0
- package/dist/src/abi/connectors/v1/chi.js +36 -0
- package/dist/src/abi/connectors/v1/comp.js +4 -0
- package/dist/src/abi/connectors/v1/compound.js +4 -0
- package/dist/src/abi/connectors/v1/compoundImport.js +69 -0
- package/dist/src/abi/connectors/v1/compoundImport_v2.js +4 -0
- package/dist/src/abi/connectors/v1/compound_old.js +448 -0
- package/dist/src/abi/connectors/v1/curve.js +140 -0
- package/dist/src/abi/connectors/v1/curve_claim.js +63 -0
- package/dist/src/abi/connectors/v1/curve_gauge.js +158 -0
- package/dist/src/abi/connectors/v1/curve_sbtc.js +140 -0
- package/dist/src/abi/connectors/v1/curve_susd.js +140 -0
- package/dist/src/abi/connectors/v1/curve_three.js +79 -0
- package/dist/src/abi/connectors/v1/curve_y.js +140 -0
- package/dist/src/abi/connectors/v1/dsa_migrate_v1_to_v2.js +4 -0
- package/dist/src/abi/connectors/v1/dydx.js +148 -0
- package/dist/src/abi/connectors/v1/dydx_flash.js +52 -0
- package/dist/src/abi/connectors/v1/fee.js +50 -0
- package/dist/src/abi/connectors/v1/gelato.js +1138 -0
- package/dist/src/abi/connectors/v1/index.js +58 -0
- package/dist/src/abi/connectors/v1/instapool.js +439 -0
- package/dist/src/abi/connectors/v1/instapool_v2.js +126 -0
- package/dist/src/abi/connectors/v1/kyber.js +117 -0
- package/dist/src/abi/connectors/v1/maker.js +480 -0
- package/dist/src/abi/connectors/v1/maker_old.js +300 -0
- package/dist/src/abi/connectors/v1/math.js +43 -0
- package/dist/src/abi/connectors/v1/migrate.js +46 -0
- package/dist/src/abi/connectors/v1/oasis.js +198 -0
- package/dist/src/abi/connectors/v1/oneInch.js +160 -0
- package/dist/src/abi/connectors/v1/polygon_bridge.js +4 -0
- package/dist/src/abi/connectors/v1/refinance.js +4 -0
- package/dist/src/abi/connectors/v1/staking.js +220 -0
- package/dist/src/abi/connectors/v1/swerve.js +179 -0
- package/dist/src/abi/connectors/v1/uniswap.js +297 -0
- package/dist/src/abi/connectors/v2/1INCH-A.js +4 -0
- package/dist/src/abi/connectors/v2/1INCH-B.js +4 -0
- package/dist/src/abi/connectors/v2/AAVE-CLAIM-A.js +4 -0
- package/dist/src/abi/connectors/v2/AAVE-CLAIM-B.js +166 -0
- package/dist/src/abi/connectors/v2/AAVE-STAKE-A.js +4 -0
- package/dist/src/abi/connectors/v2/AAVE-V1-A.js +130 -0
- package/dist/src/abi/connectors/v2/AAVE-V1-IMPORT-A.js +4 -0
- package/dist/src/abi/connectors/v2/AAVE-V2-A.js +230 -0
- package/dist/src/abi/connectors/v2/AAVE-V2-IMPORT-A.js +4 -0
- package/dist/src/abi/connectors/v2/AAVE-V2-IMPORT-B.js +225 -0
- package/dist/src/abi/connectors/v2/AUTHORITY-A.js +100 -0
- package/dist/src/abi/connectors/v2/B-COMPOUND-A.js +4 -0
- package/dist/src/abi/connectors/v2/B-LIQUITY-A.js +4 -0
- package/dist/src/abi/connectors/v2/B-MAKERDAO-A.js +4 -0
- package/dist/src/abi/connectors/v2/BASIC-A.js +136 -0
- package/dist/src/abi/connectors/v2/BASIC-B.js +4 -0
- package/dist/src/abi/connectors/v2/BASIC-C.js +4 -0
- package/dist/src/abi/connectors/v2/COMP-A.js +4 -0
- package/dist/src/abi/connectors/v2/COMPOUND-A.js +4 -0
- package/dist/src/abi/connectors/v2/COMPOUND-IMPORT-A.js +4 -0
- package/dist/src/abi/connectors/v2/COMPOUND-IMPORT-B.js +195 -0
- package/dist/src/abi/connectors/v2/G-UNISWAP-A.js +4 -0
- package/dist/src/abi/connectors/v2/GELATO-AAVE-A.js +4 -0
- package/dist/src/abi/connectors/v2/INST-A.js +4 -0
- package/dist/src/abi/connectors/v2/INST-LM-A.js +4 -0
- package/dist/src/abi/connectors/v2/INST-STAKING-A.js +4 -0
- package/dist/src/abi/connectors/v2/INST-STAKING-B.js +4 -0
- package/dist/src/abi/connectors/v2/INSTAPOOL-A.js +4 -0
- package/dist/src/abi/connectors/v2/LIQUITY-A.js +4 -0
- package/dist/src/abi/connectors/v2/MAKERDAO-A.js +4 -0
- package/dist/src/abi/connectors/v2/MAKERDAO-CLAIM-A.js +136 -0
- package/dist/src/abi/connectors/v2/PARASWAP-A.js +4 -0
- package/dist/src/abi/connectors/v2/POLYGON-BRIDGE-A.js +4 -0
- package/dist/src/abi/connectors/v2/REFINANCE-A.js +4 -0
- package/dist/src/abi/connectors/v2/REFLEXER-A.js +4 -0
- package/dist/src/abi/connectors/v2/STAKE-ERC20-A.js +4 -0
- package/dist/src/abi/connectors/v2/UNISWAP-A.js +297 -0
- package/dist/src/abi/connectors/v2/UNISWAP-V2-A.js +4 -0
- package/dist/src/abi/connectors/v2/UNISWAP-V3-A.js +4 -0
- package/dist/src/abi/connectors/v2/UNISWAP-V3-STAKE-A.js +4 -0
- package/dist/src/abi/connectors/v2/WETH-A.js +4 -0
- package/dist/src/abi/connectors/v2/YEARN-VAULT-A.js +4 -0
- package/dist/src/abi/connectors/v2/index.js +89 -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/interopX.json +1436 -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 +17 -0
- package/dist/src/constants/index.js +18 -0
- package/dist/src/constants/tokens.js +130 -0
- package/dist/src/db/index.js +17 -0
- package/dist/src/db/models/index.js +17 -0
- package/dist/src/db/models/transaction.js +72 -0
- package/dist/src/db/sequelize.js +23 -0
- package/dist/src/gnosis/actions/aaveV2/index.js +11 -0
- package/dist/src/gnosis/actions/aaveV2/source.js +62 -0
- package/dist/src/gnosis/actions/aaveV2/target.js +8 -0
- package/dist/src/gnosis/actions/index.js +9 -0
- package/dist/src/gnosis/index.js +20 -0
- package/dist/src/index.js +119 -0
- package/dist/src/logger/index.js +138 -0
- package/dist/src/net/index.js +19 -0
- package/dist/src/net/peer/index.js +109 -0
- package/dist/src/net/pool/index.js +130 -0
- package/dist/src/net/protocol/dial/BaseDialProtocol.js +106 -0
- package/dist/src/net/protocol/dial/SignatureDialProtocol.js +55 -0
- package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
- package/dist/src/net/protocol/index.js +142 -0
- package/dist/src/tasks/AutoUpdateTask.js +70 -0
- package/dist/src/tasks/BaseTask.js +69 -0
- package/dist/src/tasks/InteropX/SyncLogSubmitEvents.js +86 -0
- package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +58 -0
- package/dist/src/tasks/index.js +34 -0
- package/dist/src/typechain/Erc20.js +2 -0
- package/dist/src/typechain/GnosisSafe.js +2 -0
- package/dist/src/typechain/InteropX.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/InteropX__factory.js +1928 -0
- package/dist/src/typechain/factories/index.js +12 -0
- package/dist/src/typechain/index.js +33 -0
- package/dist/src/types.js +21 -0
- package/dist/src/utils/index.js +222 -0
- package/package.json +75 -0
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/abi/connectors/index.ts +14 -0
- package/src/abi/connectors/v1/aave.ts +147 -0
- package/src/abi/connectors/v1/aave_claim.ts +3 -0
- package/src/abi/connectors/v1/aave_migrate.ts +108 -0
- package/src/abi/connectors/v1/aave_polygon_migrate.ts +109 -0
- package/src/abi/connectors/v1/aave_stake.ts +3 -0
- package/src/abi/connectors/v1/aave_v1_import.ts +53 -0
- package/src/abi/connectors/v1/aave_v2.ts +229 -0
- package/src/abi/connectors/v1/aave_v2_import.ts +58 -0
- package/src/abi/connectors/v1/authority.ts +99 -0
- package/src/abi/connectors/v1/basic.ts +135 -0
- package/src/abi/connectors/v1/chi.ts +35 -0
- package/src/abi/connectors/v1/comp.ts +3 -0
- package/src/abi/connectors/v1/compound.ts +3 -0
- package/src/abi/connectors/v1/compoundImport.ts +68 -0
- package/src/abi/connectors/v1/compoundImport_v2.ts +3 -0
- package/src/abi/connectors/v1/compound_old.ts +447 -0
- package/src/abi/connectors/v1/curve.ts +139 -0
- package/src/abi/connectors/v1/curve_claim.ts +62 -0
- package/src/abi/connectors/v1/curve_gauge.ts +157 -0
- package/src/abi/connectors/v1/curve_sbtc.ts +139 -0
- package/src/abi/connectors/v1/curve_susd.ts +139 -0
- package/src/abi/connectors/v1/curve_three.ts +78 -0
- package/src/abi/connectors/v1/curve_y.ts +139 -0
- package/src/abi/connectors/v1/dsa_migrate_v1_to_v2.ts +3 -0
- package/src/abi/connectors/v1/dydx.ts +147 -0
- package/src/abi/connectors/v1/dydx_flash.ts +51 -0
- package/src/abi/connectors/v1/fee.ts +49 -0
- package/src/abi/connectors/v1/gelato.ts +1137 -0
- package/src/abi/connectors/v1/index.ts +42 -0
- package/src/abi/connectors/v1/instapool.ts +438 -0
- package/src/abi/connectors/v1/instapool_v2.ts +125 -0
- package/src/abi/connectors/v1/kyber.ts +116 -0
- package/src/abi/connectors/v1/maker.ts +479 -0
- package/src/abi/connectors/v1/maker_old.ts +299 -0
- package/src/abi/connectors/v1/math.ts +42 -0
- package/src/abi/connectors/v1/migrate.ts +45 -0
- package/src/abi/connectors/v1/oasis.ts +197 -0
- package/src/abi/connectors/v1/oneInch.ts +159 -0
- package/src/abi/connectors/v1/polygon_bridge.ts +3 -0
- package/src/abi/connectors/v1/refinance.ts +3 -0
- package/src/abi/connectors/v1/staking.ts +219 -0
- package/src/abi/connectors/v1/swerve.ts +178 -0
- package/src/abi/connectors/v1/uniswap.ts +297 -0
- package/src/abi/connectors/v2/1INCH-A.ts +3 -0
- package/src/abi/connectors/v2/1INCH-B.ts +3 -0
- package/src/abi/connectors/v2/AAVE-CLAIM-A.ts +3 -0
- package/src/abi/connectors/v2/AAVE-CLAIM-B.ts +165 -0
- package/src/abi/connectors/v2/AAVE-STAKE-A.ts +3 -0
- package/src/abi/connectors/v2/AAVE-V1-A.ts +130 -0
- package/src/abi/connectors/v2/AAVE-V1-IMPORT-A.ts +3 -0
- package/src/abi/connectors/v2/AAVE-V2-A.ts +229 -0
- package/src/abi/connectors/v2/AAVE-V2-IMPORT-A.ts +3 -0
- package/src/abi/connectors/v2/AAVE-V2-IMPORT-B.ts +224 -0
- package/src/abi/connectors/v2/AUTHORITY-A.ts +99 -0
- package/src/abi/connectors/v2/B-COMPOUND-A.ts +3 -0
- package/src/abi/connectors/v2/B-LIQUITY-A.ts +3 -0
- package/src/abi/connectors/v2/B-MAKERDAO-A.ts +3 -0
- package/src/abi/connectors/v2/BASIC-A.ts +135 -0
- package/src/abi/connectors/v2/BASIC-B.ts +3 -0
- package/src/abi/connectors/v2/BASIC-C.ts +3 -0
- package/src/abi/connectors/v2/COMP-A.ts +3 -0
- package/src/abi/connectors/v2/COMPOUND-A.ts +3 -0
- package/src/abi/connectors/v2/COMPOUND-IMPORT-A.ts +3 -0
- package/src/abi/connectors/v2/COMPOUND-IMPORT-B.ts +194 -0
- package/src/abi/connectors/v2/G-UNISWAP-A.ts +3 -0
- package/src/abi/connectors/v2/GELATO-AAVE-A.ts +3 -0
- package/src/abi/connectors/v2/INST-A.ts +3 -0
- package/src/abi/connectors/v2/INST-LM-A.ts +3 -0
- package/src/abi/connectors/v2/INST-STAKING-A.ts +3 -0
- package/src/abi/connectors/v2/INST-STAKING-B.ts +3 -0
- package/src/abi/connectors/v2/INSTAPOOL-A.ts +3 -0
- package/src/abi/connectors/v2/LIQUITY-A.ts +3 -0
- package/src/abi/connectors/v2/MAKERDAO-A.ts +3 -0
- package/src/abi/connectors/v2/MAKERDAO-CLAIM-A.ts +135 -0
- package/src/abi/connectors/v2/PARASWAP-A.ts +3 -0
- package/src/abi/connectors/v2/POLYGON-BRIDGE-A.ts +3 -0
- package/src/abi/connectors/v2/REFINANCE-A.ts +3 -0
- package/src/abi/connectors/v2/REFLEXER-A.ts +3 -0
- package/src/abi/connectors/v2/STAKE-ERC20-A.ts +3 -0
- package/src/abi/connectors/v2/UNISWAP-A.ts +297 -0
- package/src/abi/connectors/v2/UNISWAP-V2-A.ts +3 -0
- package/src/abi/connectors/v2/UNISWAP-V3-A.ts +3 -0
- package/src/abi/connectors/v2/UNISWAP-V3-STAKE-A.ts +3 -0
- package/src/abi/connectors/v2/WETH-A.ts +3 -0
- package/src/abi/connectors/v2/YEARN-VAULT-A.ts +3 -0
- package/src/abi/connectors/v2/index.ts +87 -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/interopX.json +1436 -0
- package/src/alias.ts +6 -0
- package/src/api/index.ts +36 -0
- package/src/config/index.ts +40 -0
- package/src/constants/addresses.ts +14 -0
- package/src/constants/index.ts +2 -0
- package/src/constants/tokens.ts +127 -0
- package/src/db/index.ts +1 -0
- package/src/db/models/index.ts +1 -0
- package/src/db/models/transaction.ts +136 -0
- package/src/db/sequelize.ts +22 -0
- package/src/gnosis/actions/aaveV2/index.ts +9 -0
- package/src/gnosis/actions/aaveV2/source.ts +104 -0
- package/src/gnosis/actions/aaveV2/target.ts +13 -0
- package/src/gnosis/actions/index.ts +5 -0
- package/src/gnosis/index.ts +19 -0
- package/src/index.ts +150 -0
- package/src/logger/index.ts +157 -0
- package/src/net/index.ts +3 -0
- package/src/net/peer/index.ts +121 -0
- package/src/net/pool/index.ts +158 -0
- package/src/net/protocol/dial/BaseDialProtocol.ts +104 -0
- package/src/net/protocol/dial/SignatureDialProtocol.ts +69 -0
- package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
- package/src/net/protocol/index.ts +204 -0
- package/src/tasks/AutoUpdateTask.ts +82 -0
- package/src/tasks/BaseTask.ts +85 -0
- package/src/tasks/InteropX/SyncLogSubmitEvents.ts +138 -0
- package/src/tasks/Transactions/SyncTransactionStatusTask.ts +70 -0
- package/src/tasks/index.ts +33 -0
- package/src/typechain/Erc20.ts +491 -0
- package/src/typechain/GnosisSafe.ts +1728 -0
- package/src/typechain/InteropX.ts +1216 -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/InteropX__factory.ts +1932 -0
- package/src/typechain/factories/index.ts +6 -0
- package/src/typechain/index.ts +10 -0
- package/src/types.ts +39 -0
- package/src/utils/index.ts +365 -0
- package/tsconfig.json +35 -0
package/src/index.ts
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
import './alias'
|
2
|
+
import expandHomeDir from "expand-home-dir";
|
3
|
+
import fs from 'fs-extra'
|
4
|
+
|
5
|
+
import dotenv from "dotenv";
|
6
|
+
import chalk from 'chalk';
|
7
|
+
import { ethers } from "ethers";
|
8
|
+
import packageJson from '../package.json'
|
9
|
+
dotenv.config();
|
10
|
+
|
11
|
+
import Logger from "@/logger";
|
12
|
+
const logger = new Logger('Process')
|
13
|
+
|
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
|
+
|
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
|
+
}
|
70
|
+
try {
|
71
|
+
new ethers.Wallet(process.env.PRIVATE_KEY!)
|
72
|
+
} catch (e) {
|
73
|
+
console.error(chalk.bgRed.white('Invalid private key\n'))
|
74
|
+
printUsage()
|
75
|
+
process.exit(1)
|
76
|
+
}
|
77
|
+
|
78
|
+
logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
|
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';
|
85
|
+
|
86
|
+
async function main() {
|
87
|
+
|
88
|
+
startPeer({})
|
89
|
+
|
90
|
+
const tasks = new Tasks()
|
91
|
+
|
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
|
+
})
|
130
|
+
}
|
131
|
+
|
132
|
+
main()
|
133
|
+
.then(() => {
|
134
|
+
}).catch(err => {
|
135
|
+
console.error(err);
|
136
|
+
});
|
137
|
+
|
138
|
+
process.on('SIGINT', () => {
|
139
|
+
logger.debug('received SIGINT signal. exiting.')
|
140
|
+
process.exit(0)
|
141
|
+
})
|
142
|
+
|
143
|
+
process.on('SIGTERM', () => {
|
144
|
+
logger.debug('received SIGTERM signal. exiting.')
|
145
|
+
process.exit(0)
|
146
|
+
})
|
147
|
+
|
148
|
+
process.on('unhandledRejection', (reason: Error, p: Promise<any>) => {
|
149
|
+
logger.error('unhandled rejection: promise:', p, 'reason:', reason)
|
150
|
+
})
|
@@ -0,0 +1,157 @@
|
|
1
|
+
import chalk from 'chalk'
|
2
|
+
import { DateTime } from 'luxon'
|
3
|
+
|
4
|
+
export type Options = {
|
5
|
+
tag?: string
|
6
|
+
prefix?: string
|
7
|
+
color: string
|
8
|
+
}
|
9
|
+
|
10
|
+
type AdditionalDataLabel = {
|
11
|
+
id?: string
|
12
|
+
root?: string
|
13
|
+
}
|
14
|
+
|
15
|
+
export enum LogLevels {
|
16
|
+
Critical,
|
17
|
+
Error,
|
18
|
+
Warn,
|
19
|
+
Info,
|
20
|
+
Log,
|
21
|
+
Debug
|
22
|
+
}
|
23
|
+
|
24
|
+
const logLevelColors: { [key: string]: string } = {
|
25
|
+
[LogLevels.Critical]: 'red',
|
26
|
+
[LogLevels.Error]: 'red',
|
27
|
+
[LogLevels.Warn]: 'yellow',
|
28
|
+
[LogLevels.Info]: 'blue',
|
29
|
+
[LogLevels.Log]: 'white',
|
30
|
+
[LogLevels.Debug]: 'white'
|
31
|
+
}
|
32
|
+
|
33
|
+
let logLevel = LogLevels.Debug
|
34
|
+
export const setLogLevel = (_logLevel: LogLevels | string) => {
|
35
|
+
if (typeof _logLevel === 'string') {
|
36
|
+
const mapping: { [key: string]: number } = {
|
37
|
+
error: LogLevels.Error,
|
38
|
+
warn: LogLevels.Warn,
|
39
|
+
info: LogLevels.Info,
|
40
|
+
debug: LogLevels.Debug
|
41
|
+
}
|
42
|
+
_logLevel = mapping[_logLevel]
|
43
|
+
}
|
44
|
+
logLevel = _logLevel
|
45
|
+
}
|
46
|
+
|
47
|
+
class Logger {
|
48
|
+
private readonly tag: string = ''
|
49
|
+
private readonly prefix: string = ''
|
50
|
+
private readonly options: any = {}
|
51
|
+
enabled: boolean = true
|
52
|
+
|
53
|
+
setEnabled(enabled: boolean) {
|
54
|
+
this.enabled = enabled
|
55
|
+
}
|
56
|
+
|
57
|
+
constructor(
|
58
|
+
tag: Partial<Options> | string = '',
|
59
|
+
opts: Partial<Options> = {
|
60
|
+
color: 'white'
|
61
|
+
}
|
62
|
+
) {
|
63
|
+
if (tag instanceof Object) {
|
64
|
+
opts = tag
|
65
|
+
tag = opts.tag ?? ''
|
66
|
+
}
|
67
|
+
if (opts.prefix) {
|
68
|
+
this.prefix = `<${opts.prefix}>`
|
69
|
+
}
|
70
|
+
if (tag) {
|
71
|
+
if (opts.color) {
|
72
|
+
//@ts-ignore
|
73
|
+
this.tag = chalk[opts.color](`[${tag}]`)
|
74
|
+
} else {
|
75
|
+
this.tag = `[${tag}]`
|
76
|
+
}
|
77
|
+
}
|
78
|
+
if (process.env.DISABLE_LOGGER) {
|
79
|
+
this.enabled = false
|
80
|
+
}
|
81
|
+
this.options = opts
|
82
|
+
}
|
83
|
+
|
84
|
+
create(additionalDataLabel: AdditionalDataLabel): Logger {
|
85
|
+
let label: string
|
86
|
+
if (additionalDataLabel.id) {
|
87
|
+
label = `id: ${additionalDataLabel.id}`
|
88
|
+
} else {
|
89
|
+
label = `root: ${additionalDataLabel.root}`
|
90
|
+
}
|
91
|
+
|
92
|
+
return new Logger(
|
93
|
+
this.options.tag,
|
94
|
+
Object.assign({}, this.options, {
|
95
|
+
prefix: `${this.options.prefix ? `${this.options.prefix} ` : ''}${label}`
|
96
|
+
})
|
97
|
+
)
|
98
|
+
}
|
99
|
+
|
100
|
+
get timestamp(): string {
|
101
|
+
return DateTime.now().toISO()
|
102
|
+
}
|
103
|
+
|
104
|
+
headers(logLevelEnum: LogLevels): string[] {
|
105
|
+
const keys = Object.keys(LogLevels)
|
106
|
+
const logLevelName = keys[logLevelEnum + keys.length / 2].toUpperCase()
|
107
|
+
//@ts-ignore
|
108
|
+
const coloredLogLevel = chalk[logLevelColors[logLevelEnum]](
|
109
|
+
logLevelName.padEnd(5, ' ')
|
110
|
+
)
|
111
|
+
return [this.timestamp, coloredLogLevel, this.tag, this.prefix]
|
112
|
+
}
|
113
|
+
|
114
|
+
critical = (...input: any[]) => {
|
115
|
+
if (!this.enabled) return
|
116
|
+
console.error(...this.headers(LogLevels.Critical), ...input)
|
117
|
+
}
|
118
|
+
|
119
|
+
debug = (...input: any[]) => {
|
120
|
+
if (!this.enabled) return
|
121
|
+
if (logLevel !== LogLevels.Debug) {
|
122
|
+
return
|
123
|
+
}
|
124
|
+
console.debug(...this.headers(LogLevels.Debug), ...input)
|
125
|
+
}
|
126
|
+
|
127
|
+
error = (...input: any[]) => {
|
128
|
+
if (!this.enabled) return
|
129
|
+
console.error(...this.headers(LogLevels.Error), ...input)
|
130
|
+
}
|
131
|
+
|
132
|
+
info = (...input: any[]) => {
|
133
|
+
if (!this.enabled) return
|
134
|
+
if (!(logLevel === LogLevels.Debug || logLevel === LogLevels.Info)) {
|
135
|
+
return
|
136
|
+
}
|
137
|
+
console.info(...this.headers(LogLevels.Info), ...input)
|
138
|
+
}
|
139
|
+
|
140
|
+
log = (...input: any[]) => {
|
141
|
+
if (!this.enabled) return
|
142
|
+
if (logLevel < LogLevels.Info) {
|
143
|
+
return
|
144
|
+
}
|
145
|
+
console.log(...this.headers(LogLevels.Log), ...input)
|
146
|
+
}
|
147
|
+
|
148
|
+
warn = (...input: any[]) => {
|
149
|
+
if (!this.enabled) return
|
150
|
+
if (logLevel < LogLevels.Warn) {
|
151
|
+
return
|
152
|
+
}
|
153
|
+
console.warn(...this.headers(LogLevels.Warn), ...input)
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
export default Logger
|
package/src/net/index.ts
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
import Libp2p from "libp2p";
|
2
|
+
import TCP from "libp2p-tcp";
|
3
|
+
//@ts-ignore
|
4
|
+
import WS from "libp2p-websockets";
|
5
|
+
//@ts-ignore
|
6
|
+
import Mplex from "libp2p-mplex";
|
7
|
+
import { NOISE } from "libp2p-noise";
|
8
|
+
import Logger from "@/logger";
|
9
|
+
import Bootstrap from "libp2p-bootstrap";
|
10
|
+
import wait from "waait";
|
11
|
+
import Gossipsub from "@achingbrain/libp2p-gossipsub";
|
12
|
+
//@ts-ignore
|
13
|
+
import MulticastDNS from "libp2p-mdns";
|
14
|
+
//@ts-ignore
|
15
|
+
import KadDHT from "libp2p-kad-dht";
|
16
|
+
//@ts-ignore
|
17
|
+
import PubsubPeerDiscovery from "libp2p-pubsub-peer-discovery";
|
18
|
+
import { protocol } from "@/net";
|
19
|
+
import config from "@/config";
|
20
|
+
import chalk from "chalk";
|
21
|
+
|
22
|
+
const logger = new Logger("Peer");
|
23
|
+
|
24
|
+
let node: Libp2p;
|
25
|
+
|
26
|
+
// Known peers addresses
|
27
|
+
const bootstrapMultiaddrs = [
|
28
|
+
'/ip4/164.92.249.133/tcp/15001/ws/p2p/QmVGmkqRn8FEw4PQz83L8Mun24ALxXB2fEyS5aEfT2gGqz',
|
29
|
+
'/ip4/164.92.249.133/tcp/15000/p2p/QmVGmkqRn8FEw4PQz83L8Mun24ALxXB2fEyS5aEfT2gGqz',
|
30
|
+
];
|
31
|
+
|
32
|
+
interface IPeerOptions {
|
33
|
+
}
|
34
|
+
|
35
|
+
export const startPeer = async ({ }: IPeerOptions) => {
|
36
|
+
node = await Libp2p.create({
|
37
|
+
addresses: {
|
38
|
+
listen: ["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/tcp/0/ws"],
|
39
|
+
},
|
40
|
+
modules: {
|
41
|
+
transport: [TCP, WS],
|
42
|
+
streamMuxer: [Mplex],
|
43
|
+
connEncryption: [NOISE],
|
44
|
+
peerDiscovery: [MulticastDNS, Bootstrap, PubsubPeerDiscovery],
|
45
|
+
pubsub: Gossipsub,
|
46
|
+
dht: KadDHT,
|
47
|
+
},
|
48
|
+
|
49
|
+
config: {
|
50
|
+
dht: {
|
51
|
+
enabled: true,
|
52
|
+
},
|
53
|
+
peerDiscovery: {
|
54
|
+
autoDial: true, // Auto connect to discovered peers (limited by ConnectionManager minConnections)
|
55
|
+
// The `tag` property will be searched when creating the instance of your Peer Discovery service.
|
56
|
+
// The associated object, will be passed to the service when it is instantiated.
|
57
|
+
[Bootstrap.tag]: {
|
58
|
+
enabled: true,
|
59
|
+
interval: 60e3,
|
60
|
+
list: bootstrapMultiaddrs, // provide array of multiaddrs
|
61
|
+
},
|
62
|
+
[MulticastDNS.tag]: {
|
63
|
+
interval: 20e3,
|
64
|
+
enabled: true,
|
65
|
+
},
|
66
|
+
[PubsubPeerDiscovery.tag]: {
|
67
|
+
interval: 1000,
|
68
|
+
enabled: true,
|
69
|
+
},
|
70
|
+
},
|
71
|
+
relay: {
|
72
|
+
enabled: true,
|
73
|
+
autoRelay: {
|
74
|
+
enabled: true,
|
75
|
+
maxListeners: 10,
|
76
|
+
},
|
77
|
+
},
|
78
|
+
},
|
79
|
+
peerStore: {
|
80
|
+
persistence: true,
|
81
|
+
},
|
82
|
+
});
|
83
|
+
|
84
|
+
logger.info("Peer ID:", chalk.bold(node.peerId.toB58String()));
|
85
|
+
|
86
|
+
await node.start();
|
87
|
+
|
88
|
+
protocol.start({
|
89
|
+
libp2p: node
|
90
|
+
})
|
91
|
+
|
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
|
+
});
|
99
|
+
|
100
|
+
logger.log("Peer discovery started");
|
101
|
+
|
102
|
+
await wait(1000);
|
103
|
+
|
104
|
+
|
105
|
+
setInterval(() => protocol.sendPeerInfo({
|
106
|
+
publicAddress: config.wallet.address,
|
107
|
+
}), 5000)
|
108
|
+
};
|
109
|
+
|
110
|
+
export const stopPeer = async () => {
|
111
|
+
if (node) {
|
112
|
+
// stop libp2p
|
113
|
+
await node.stop();
|
114
|
+
logger.log("libp2p has stopped");
|
115
|
+
}
|
116
|
+
|
117
|
+
process.exit(0);
|
118
|
+
};
|
119
|
+
|
120
|
+
process.on("SIGTERM", stopPeer);
|
121
|
+
process.on("SIGINT", stopPeer);
|
@@ -0,0 +1,158 @@
|
|
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')
|
10
|
+
|
11
|
+
export interface IPeerInfo {
|
12
|
+
id: string;
|
13
|
+
publicAddress: string;
|
14
|
+
pooled: boolean;
|
15
|
+
updated: Date;
|
16
|
+
}
|
17
|
+
|
18
|
+
export class PeerPool {
|
19
|
+
public PEERS_CLEANUP_TIME_LIMIT = 1
|
20
|
+
private pool: Map<string, IPeerInfo>
|
21
|
+
|
22
|
+
|
23
|
+
constructor() {
|
24
|
+
this.pool = new Map<string, IPeerInfo>()
|
25
|
+
|
26
|
+
config.events.on(Event.PEER_CONNECTED, (peer) => {
|
27
|
+
this.connected(peer)
|
28
|
+
})
|
29
|
+
|
30
|
+
config.events.on(Event.PEER_DISCONNECTED, (peer) => {
|
31
|
+
this.disconnected(peer)
|
32
|
+
})
|
33
|
+
}
|
34
|
+
|
35
|
+
|
36
|
+
/**
|
37
|
+
* Connected peers
|
38
|
+
*/
|
39
|
+
get peers(): IPeerInfo[] {
|
40
|
+
const connectedPeers: IPeerInfo[] = Array.from(this.pool.values())
|
41
|
+
return connectedPeers
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Number of peers in pool
|
46
|
+
*/
|
47
|
+
get size() {
|
48
|
+
return this.peers.length
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Return true if pool contains the specified peer
|
53
|
+
* @param peer peer object or id
|
54
|
+
*/
|
55
|
+
contains(peer: IPeerInfo | string): boolean {
|
56
|
+
if (typeof peer !== 'string') {
|
57
|
+
peer = peer.id
|
58
|
+
}
|
59
|
+
return !!this.pool.get(peer)
|
60
|
+
}
|
61
|
+
|
62
|
+
/**
|
63
|
+
* Handler for peer connections
|
64
|
+
* @param peer peer
|
65
|
+
*/
|
66
|
+
private connected(peer: IPeerInfo) {
|
67
|
+
if (this.size >= config.maxPeers) return
|
68
|
+
this.add(peer)
|
69
|
+
}
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Handler for peer disconnections
|
73
|
+
* @param peer peer
|
74
|
+
*/
|
75
|
+
private disconnected(peer: IPeerInfo) {
|
76
|
+
this.remove(peer)
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Add peer to pool
|
81
|
+
* @param peer peer
|
82
|
+
* @emits {@link Event.POOL_PEER_ADDED}
|
83
|
+
*/
|
84
|
+
add(peer?: IPeerInfo) {
|
85
|
+
if (peer && peer.id) {
|
86
|
+
const newPeer = !this.pool.get(peer.id);
|
87
|
+
this.pool.set(peer.id, peer)
|
88
|
+
peer.pooled = true
|
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
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Remove peer from pool
|
99
|
+
* @param peer peer
|
100
|
+
* @emits {@link Event.POOL_PEER_REMOVED}
|
101
|
+
*/
|
102
|
+
remove(peer?: IPeerInfo) {
|
103
|
+
if (peer && peer.id) {
|
104
|
+
if (this.pool.delete(peer.id)) {
|
105
|
+
peer.pooled = false
|
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`)
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
get activePeers() {
|
113
|
+
this.cleanup()
|
114
|
+
|
115
|
+
return this.peers.filter((p) => {
|
116
|
+
if (!p.pooled) return false;
|
117
|
+
|
118
|
+
const now = new Date()
|
119
|
+
|
120
|
+
return (now.getTime() - p.updated.getTime()) < 1000 * 10 // 10 seconds
|
121
|
+
})
|
122
|
+
}
|
123
|
+
|
124
|
+
get activePeerIds() {
|
125
|
+
return this.activePeers.map((p) => p.id)
|
126
|
+
}
|
127
|
+
|
128
|
+
getPeer(id: string){
|
129
|
+
return this.pool.get(id);
|
130
|
+
}
|
131
|
+
|
132
|
+
isLeadNode(id: string) {
|
133
|
+
const peer = this.pool.get(id);
|
134
|
+
|
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
|
+
// })
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
export const peerPool = new PeerPool()
|
@@ -0,0 +1,104 @@
|
|
1
|
+
import pipe from 'it-pipe';
|
2
|
+
import Libp2p from 'libp2p';
|
3
|
+
import PeerId from 'peer-id';
|
4
|
+
import { asyncCallWithTimeout } from '@/utils';
|
5
|
+
import wait from 'waait';
|
6
|
+
|
7
|
+
export class BaseDialProtocol<TRequest extends any, TResponse extends any> {
|
8
|
+
protected libp2p: Libp2p;
|
9
|
+
protected protocol: string;
|
10
|
+
protected timeout = 20000;
|
11
|
+
|
12
|
+
constructor(libp2p: Libp2p, protocol: string) {
|
13
|
+
this.libp2p = libp2p
|
14
|
+
this.protocol = protocol
|
15
|
+
|
16
|
+
this.libp2p.handle(this.protocol, this.handle.bind(this))
|
17
|
+
}
|
18
|
+
|
19
|
+
encode(data: TRequest) {
|
20
|
+
return JSON.stringify(data)
|
21
|
+
}
|
22
|
+
|
23
|
+
encodeResponse(data: TResponse): String | Buffer {
|
24
|
+
return JSON.stringify(data)
|
25
|
+
}
|
26
|
+
|
27
|
+
decode(data: Buffer) {
|
28
|
+
return JSON.parse(data.toString())
|
29
|
+
}
|
30
|
+
|
31
|
+
decodeResponse(data: Buffer): any {
|
32
|
+
return JSON.parse(data.toString())
|
33
|
+
}
|
34
|
+
|
35
|
+
async response(data: TRequest): Promise<TResponse> {
|
36
|
+
return data as TResponse;
|
37
|
+
}
|
38
|
+
|
39
|
+
async handle({ connection, stream }) {
|
40
|
+
const instance = this;
|
41
|
+
|
42
|
+
try {
|
43
|
+
await pipe(stream, async function (source) {
|
44
|
+
for await (const message of source) {
|
45
|
+
const response = instance.encodeResponse(
|
46
|
+
await instance.response(
|
47
|
+
instance.decode(message)
|
48
|
+
)
|
49
|
+
)
|
50
|
+
|
51
|
+
await pipe(
|
52
|
+
[response],
|
53
|
+
stream
|
54
|
+
);
|
55
|
+
}
|
56
|
+
});
|
57
|
+
} catch (err) {
|
58
|
+
console.error(err);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
async send(data: TRequest, peerId: string) {
|
63
|
+
return await asyncCallWithTimeout<TResponse>(new Promise(async (resolve, reject) => {
|
64
|
+
try {
|
65
|
+
let connection = this.libp2p.connectionManager.get(PeerId.createFromB58String(peerId))
|
66
|
+
|
67
|
+
if (!connection) {
|
68
|
+
|
69
|
+
await wait(5000)
|
70
|
+
|
71
|
+
connection = this.libp2p.connectionManager.get(PeerId.createFromB58String(peerId))
|
72
|
+
|
73
|
+
|
74
|
+
if (!connection) {
|
75
|
+
|
76
|
+
await wait(3000)
|
77
|
+
|
78
|
+
connection = this.libp2p.connectionManager.get(PeerId.createFromB58String(peerId))
|
79
|
+
|
80
|
+
if (!connection) {
|
81
|
+
throw new Error('No connection available')
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
const { stream } = await connection.newStream([this.protocol])
|
89
|
+
|
90
|
+
const instance = this;
|
91
|
+
|
92
|
+
pipe([this.encode(data)], stream, async function (source) {
|
93
|
+
for await (const message of source) {
|
94
|
+
resolve(instance.decodeResponse(message))
|
95
|
+
stream.close()
|
96
|
+
}
|
97
|
+
});
|
98
|
+
} catch (error) {
|
99
|
+
reject(error)
|
100
|
+
}
|
101
|
+
}), this.timeout)
|
102
|
+
|
103
|
+
}
|
104
|
+
}
|