@instadapp/interop-x 0.0.0-dev.0134f74
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 +76 -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/blockConfirmations.js +8 -0
- package/dist/src/constants/index.js +19 -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 +73 -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/ProcessSubmitSubmitEvents.js +180 -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 +37 -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 +223 -0
- package/package.json +76 -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/blockConfirmations.ts +5 -0
- package/src/constants/index.ts +3 -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 +118 -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 +70 -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/ProcessSubmitSubmitEvents.ts +270 -0
- package/src/tasks/InteropX/SyncLogSubmitEvents.ts +138 -0
- package/src/tasks/Transactions/SyncTransactionStatusTask.ts +70 -0
- package/src/tasks/index.ts +37 -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
@@ -0,0 +1,70 @@
|
|
1
|
+
import { BaseDialProtocol } from "./BaseDialProtocol";
|
2
|
+
import wait from "waait";
|
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";
|
9
|
+
|
10
|
+
export interface ISignatureRequest {
|
11
|
+
type: 'source' | 'target',
|
12
|
+
transactionHash: string
|
13
|
+
safeTxGas: string
|
14
|
+
safeNonce: string
|
15
|
+
chainId: number
|
16
|
+
}
|
17
|
+
export interface ISignatureResponse {
|
18
|
+
signer: string,
|
19
|
+
data?: string | null,
|
20
|
+
error?: string | null,
|
21
|
+
}
|
22
|
+
export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, ISignatureResponse> {
|
23
|
+
protected timeout = 30000;
|
24
|
+
|
25
|
+
constructor(libp2p) {
|
26
|
+
super(libp2p, '/interop-x/signatures')
|
27
|
+
}
|
28
|
+
|
29
|
+
async response(data: ISignatureRequest): Promise<ISignatureResponse> {
|
30
|
+
console.log({
|
31
|
+
tag: 'SignatureDialProtocol',
|
32
|
+
data
|
33
|
+
})
|
34
|
+
const signer = config.wallet;
|
35
|
+
|
36
|
+
let transaction: Transaction | null;
|
37
|
+
let maxTimeout = 20000;
|
38
|
+
|
39
|
+
do {
|
40
|
+
transaction = await Transaction.findOne({ where: { transactionHash: data.transactionHash } })
|
41
|
+
|
42
|
+
if (!transaction) {
|
43
|
+
await wait(1000);
|
44
|
+
maxTimeout -= 1000;
|
45
|
+
}
|
46
|
+
} while (!transaction && maxTimeout > 0)
|
47
|
+
|
48
|
+
if (!transaction) {
|
49
|
+
return {
|
50
|
+
signer: signer.address,
|
51
|
+
data: null,
|
52
|
+
error: 'Event not found'
|
53
|
+
};
|
54
|
+
}
|
55
|
+
const { data: gnosisData } = await buildGnosisAction(transaction, data.type);
|
56
|
+
|
57
|
+
const signedData = await signGnosisSafeTx({
|
58
|
+
to: addresses[data.chainId].multisend,
|
59
|
+
data: gnosisData,
|
60
|
+
chainId: data.chainId as ChainId,
|
61
|
+
safeTxGas: data.safeTxGas,
|
62
|
+
nonce: data.safeNonce,
|
63
|
+
}, { signer });
|
64
|
+
|
65
|
+
return {
|
66
|
+
signer: signer.address,
|
67
|
+
data: signedData
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
@@ -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
|
+
}
|
@@ -0,0 +1,204 @@
|
|
1
|
+
import { EventEmitter } from "stream";
|
2
|
+
import Libp2p from 'libp2p';
|
3
|
+
import { bufferToInt, rlp } from 'ethereumjs-util'
|
4
|
+
import { SignatureDialProtocol, ISignatureRequest, ISignatureResponse } from "./dial/SignatureDialProtocol";
|
5
|
+
import { IPeerInfo, peerPool } from "..";
|
6
|
+
import config from "@/config";
|
7
|
+
import { Event } from "@/types";
|
8
|
+
import { Transaction } from "@/db";
|
9
|
+
import { TransactionStatusDialProtocol } from "./dial/TransactionStatusDialProtocol";
|
10
|
+
|
11
|
+
export interface ProtocolOptions {
|
12
|
+
/* Handshake timeout in ms (default: 8000) */
|
13
|
+
timeout?: number
|
14
|
+
}
|
15
|
+
|
16
|
+
|
17
|
+
export type Message = {
|
18
|
+
name: string
|
19
|
+
code: number
|
20
|
+
payload?: any
|
21
|
+
response?: number
|
22
|
+
encode: Function
|
23
|
+
decode: Function
|
24
|
+
}
|
25
|
+
|
26
|
+
interface BaseMessageEvent {
|
27
|
+
peerId: string
|
28
|
+
name: string
|
29
|
+
code: number
|
30
|
+
data: any
|
31
|
+
}
|
32
|
+
|
33
|
+
|
34
|
+
interface PeerInfoEvent extends BaseMessageEvent {
|
35
|
+
data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
|
36
|
+
}
|
37
|
+
|
38
|
+
interface TransactionStatusEvent extends BaseMessageEvent {
|
39
|
+
data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'sourceLogs' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'targetLogs' | 'status'>
|
40
|
+
}
|
41
|
+
|
42
|
+
declare interface Protocol {
|
43
|
+
on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
|
44
|
+
on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
|
45
|
+
on(event: string, listener: (payload: BaseMessageEvent) => void): this;
|
46
|
+
}
|
47
|
+
|
48
|
+
class Protocol extends EventEmitter {
|
49
|
+
private libp2p: Libp2p;
|
50
|
+
private topic: string;
|
51
|
+
private protocolMessages: Message[] = [
|
52
|
+
{
|
53
|
+
name: 'PeerInfo',
|
54
|
+
code: 0x01,
|
55
|
+
encode: (info: Pick<IPeerInfo, 'publicAddress'>) => [
|
56
|
+
Buffer.from(info.publicAddress),
|
57
|
+
],
|
58
|
+
decode: ([publicAddress]: [Buffer]) => ({
|
59
|
+
publicAddress: publicAddress.toString(),
|
60
|
+
}),
|
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
|
+
transaction.sourceLogs ? transaction.sourceLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
|
72
|
+
|
73
|
+
Buffer.from(transaction.targetStatus),
|
74
|
+
Buffer.from(transaction.targetTransactionHash || ''),
|
75
|
+
transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
|
76
|
+
transaction.targetLogs ? transaction.targetLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
|
77
|
+
|
78
|
+
Buffer.from(transaction.status),
|
79
|
+
],
|
80
|
+
decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, sourceLogs, targetStatus, targetTransactionHash, targetErrors, targetLogs, status]: [Buffer, Buffer, Buffer, Buffer[],[Buffer,Buffer][], Buffer, Buffer, Buffer[],[Buffer,Buffer][], Buffer]) => ({
|
81
|
+
transactionHash: transactionHash.toString(),
|
82
|
+
|
83
|
+
sourceStatus: sourceStatus.toString(),
|
84
|
+
sourceTransactionHash: sourceTransactionHash.toString() || null,
|
85
|
+
sourceErrors: sourceErrors.map((e) => e.toString()),
|
86
|
+
sourceLogs: sourceLogs.map(e => ({
|
87
|
+
type: e[0].toString(),
|
88
|
+
message: e[1].toString(),
|
89
|
+
})),
|
90
|
+
|
91
|
+
targetStatus: targetStatus.toString(),
|
92
|
+
targetTransactionHash: targetTransactionHash.toString() || null,
|
93
|
+
targetErrors: targetErrors.map((e) => e.toString()),
|
94
|
+
targetLogs: targetLogs.map(e => ({
|
95
|
+
type: e[0].toString(),
|
96
|
+
message: e[1].toString(),
|
97
|
+
})),
|
98
|
+
|
99
|
+
status: status.toString(),
|
100
|
+
}),
|
101
|
+
},
|
102
|
+
];
|
103
|
+
private signature: SignatureDialProtocol;
|
104
|
+
private transactionStatus: TransactionStatusDialProtocol;
|
105
|
+
|
106
|
+
|
107
|
+
start({ libp2p, topic = null, }) {
|
108
|
+
this.libp2p = libp2p
|
109
|
+
this.topic = topic || 'itnerop-x-protocol'
|
110
|
+
|
111
|
+
if (this.libp2p.isStarted()) this.init()
|
112
|
+
|
113
|
+
this.on('PeerInfo', (payload) => {
|
114
|
+
|
115
|
+
config.events.emit(Event.PEER_CONNECTED, {
|
116
|
+
id: payload.peerId,
|
117
|
+
publicAddress: payload.data.publicAddress,
|
118
|
+
pooled: false,
|
119
|
+
updated: new Date(),
|
120
|
+
})
|
121
|
+
})
|
122
|
+
|
123
|
+
this.signature = new SignatureDialProtocol(this.libp2p);
|
124
|
+
this.transactionStatus = new TransactionStatusDialProtocol(this.libp2p);
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
init() {
|
129
|
+
this.libp2p.pubsub.subscribe(this.topic)
|
130
|
+
|
131
|
+
this.libp2p.pubsub.on(this.topic, (message) => {
|
132
|
+
try {
|
133
|
+
const [codeBuf, payload]: any = rlp.decode(message.data);
|
134
|
+
|
135
|
+
const code = bufferToInt(codeBuf);
|
136
|
+
|
137
|
+
const protocolMessage = this.protocolMessages.find((m) => m.code === code);
|
138
|
+
if (!protocolMessage) {
|
139
|
+
return;
|
140
|
+
}
|
141
|
+
const decoded = protocolMessage.decode(payload);
|
142
|
+
this.emit(
|
143
|
+
protocolMessage.name,
|
144
|
+
{
|
145
|
+
peerId: message.from,
|
146
|
+
name: protocolMessage.name,
|
147
|
+
code: protocolMessage.code,
|
148
|
+
data: decoded
|
149
|
+
}
|
150
|
+
)
|
151
|
+
|
152
|
+
this.emit('all', {
|
153
|
+
peerId: message.from,
|
154
|
+
name: protocolMessage.name,
|
155
|
+
code: protocolMessage.code,
|
156
|
+
data: decoded,
|
157
|
+
})
|
158
|
+
} catch (err) {
|
159
|
+
console.error(err)
|
160
|
+
}
|
161
|
+
})
|
162
|
+
}
|
163
|
+
|
164
|
+
|
165
|
+
public sendPeerInfo(data: Pick<IPeerInfo, 'publicAddress'>) {
|
166
|
+
const message = this.protocolMessages.find((m) => m.name === 'PeerInfo')!
|
167
|
+
|
168
|
+
const encoded = rlp.encode([message.code, message.encode(data)]);
|
169
|
+
|
170
|
+
this.libp2p.pubsub.publish(this.topic, encoded)
|
171
|
+
}
|
172
|
+
|
173
|
+
public sendTransaction(transaction: Transaction) {
|
174
|
+
const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
|
175
|
+
|
176
|
+
const encoded = rlp.encode([message.code, message.encode(transaction)]);
|
177
|
+
|
178
|
+
this.libp2p.pubsub.publish(this.topic, encoded)
|
179
|
+
}
|
180
|
+
|
181
|
+
async requestSignatures(data: ISignatureRequest, peerIds?: string[]) {
|
182
|
+
try {
|
183
|
+
peerIds = peerIds || peerPool.activePeerIds;
|
184
|
+
const promises = peerIds.map((peerId) => this.signature.send(data, peerId))
|
185
|
+
return (await Promise.allSettled(promises))
|
186
|
+
.map((p) => p.status === 'fulfilled' ? p.value : null)
|
187
|
+
.filter(Boolean) as ISignatureResponse[]
|
188
|
+
} catch (error) {
|
189
|
+
console.log(error);
|
190
|
+
return []
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
async requestTransactionStatus(transactionHash: string, peerId: string) {
|
195
|
+
try {
|
196
|
+
return await this.transactionStatus.send(transactionHash, peerId);
|
197
|
+
} catch (error) {
|
198
|
+
console.log(error);
|
199
|
+
return null
|
200
|
+
}
|
201
|
+
}
|
202
|
+
}
|
203
|
+
|
204
|
+
export const protocol = new Protocol();
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import { BaseTask } from "./BaseTask";
|
2
|
+
import Logger from '@/logger';
|
3
|
+
import spawnAsync from 'await-spawn';
|
4
|
+
import { spawn } from 'child_process'
|
5
|
+
import config from "@/config";
|
6
|
+
import wait from "waait";
|
7
|
+
import packageJson from "../../package.json";
|
8
|
+
|
9
|
+
const currentVersion = packageJson.version;
|
10
|
+
const tag = config.staging ? 'dev' : 'latest';
|
11
|
+
|
12
|
+
class AutoUpdateTask extends BaseTask {
|
13
|
+
pollIntervalMs: number = 60 * 10 * 1000
|
14
|
+
|
15
|
+
constructor() {
|
16
|
+
super({
|
17
|
+
logger: new Logger("AutoUpdateTask"),
|
18
|
+
})
|
19
|
+
}
|
20
|
+
|
21
|
+
prePollHandler(): boolean {
|
22
|
+
return config.autoUpdate && !config.isLeadNode();
|
23
|
+
}
|
24
|
+
|
25
|
+
async getInstalledVersion() {
|
26
|
+
try {
|
27
|
+
const stdout = await spawnAsync('npm', ['-g', 'ls', '--depth=0', '--json'])
|
28
|
+
return JSON.parse(stdout.toString()).dependencies[packageJson.name].version
|
29
|
+
} catch (error) {
|
30
|
+
this.logger.error(error)
|
31
|
+
return currentVersion
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
async getLatestVersion() {
|
36
|
+
try {
|
37
|
+
const stdout = await spawnAsync('npm', ['view', `${packageJson.name}@${tag}`, 'version'])
|
38
|
+
return stdout.toString().trim()
|
39
|
+
} catch (error) {
|
40
|
+
this.logger.error(error)
|
41
|
+
return currentVersion
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
async pollHandler() {
|
46
|
+
const version = await this.getLatestVersion()
|
47
|
+
|
48
|
+
if (version === currentVersion) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
|
52
|
+
this.logger.warn(`New version ${version} available.`)
|
53
|
+
|
54
|
+
this.logger.info('Updating...')
|
55
|
+
|
56
|
+
await spawnAsync('npm', ['-g', 'install', `@instadapp/interop-x@${tag}`, '-f']);
|
57
|
+
|
58
|
+
await wait(5000)
|
59
|
+
|
60
|
+
if (version !== await this.getInstalledVersion()) {
|
61
|
+
this.logger.warn(`failed to install ${version}, retrying in 5 minutes`)
|
62
|
+
return;
|
63
|
+
}
|
64
|
+
|
65
|
+
this.logger.warn(`Installed version ${version}`)
|
66
|
+
this.logger.warn(`Restarting...`)
|
67
|
+
|
68
|
+
|
69
|
+
// TODO: its restarting in the bg, but it should be in the fg
|
70
|
+
const subprocess = spawn(process.argv[0], process.argv.slice(1), {
|
71
|
+
cwd: process.cwd(),
|
72
|
+
stdio: "inherit",
|
73
|
+
// shell: process.env.SHELL,
|
74
|
+
});
|
75
|
+
|
76
|
+
subprocess.unref();
|
77
|
+
|
78
|
+
process.exit()
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
export default AutoUpdateTask;
|
@@ -0,0 +1,85 @@
|
|
1
|
+
import config from "@/config";
|
2
|
+
import EventEmitter from "events";
|
3
|
+
import wait from "waait";
|
4
|
+
import Logger from "@/logger";
|
5
|
+
|
6
|
+
export interface IBaseTask {
|
7
|
+
pollCheck(): Promise<void>
|
8
|
+
prePollHandler(): boolean
|
9
|
+
pollHandler(): Promise<void>
|
10
|
+
postPollHandler(): Promise<void>
|
11
|
+
|
12
|
+
start(): Promise<void>
|
13
|
+
stop(): Promise<void>
|
14
|
+
}
|
15
|
+
|
16
|
+
|
17
|
+
export class BaseTask extends EventEmitter implements IBaseTask {
|
18
|
+
logger: Logger
|
19
|
+
started: boolean = false
|
20
|
+
pollIntervalMs: number = 10 * 1000
|
21
|
+
leadNodeOnly: boolean = false
|
22
|
+
exceptLeadNode: boolean = false
|
23
|
+
|
24
|
+
public constructor({ logger }: { logger?: Logger }) {
|
25
|
+
super()
|
26
|
+
|
27
|
+
this.logger = logger ?? new Logger('BaseTask')
|
28
|
+
}
|
29
|
+
|
30
|
+
async pollCheck() {
|
31
|
+
while (true) {
|
32
|
+
if (!this.started) {
|
33
|
+
return
|
34
|
+
}
|
35
|
+
try {
|
36
|
+
const shouldPoll = this.prePollHandler()
|
37
|
+
if (shouldPoll) {
|
38
|
+
await this.pollHandler()
|
39
|
+
}
|
40
|
+
} catch (err) {
|
41
|
+
this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`)
|
42
|
+
}
|
43
|
+
|
44
|
+
await this.postPollHandler()
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
prePollHandler(): boolean {
|
49
|
+
if(config.isMaintenanceMode()){
|
50
|
+
this.logger.warn('Maintenance mode is enabled. Skipping task.')
|
51
|
+
return false
|
52
|
+
}
|
53
|
+
|
54
|
+
if (this.exceptLeadNode) {
|
55
|
+
return !config.isLeadNode();
|
56
|
+
}
|
57
|
+
|
58
|
+
if (this.leadNodeOnly) {
|
59
|
+
return config.isLeadNode()
|
60
|
+
}
|
61
|
+
|
62
|
+
return true
|
63
|
+
}
|
64
|
+
|
65
|
+
async pollHandler() {
|
66
|
+
this.logger.warn('pollHandler not implemented')
|
67
|
+
}
|
68
|
+
|
69
|
+
async postPollHandler() {
|
70
|
+
await wait(this.pollIntervalMs)
|
71
|
+
}
|
72
|
+
|
73
|
+
async start() {
|
74
|
+
this.started = true
|
75
|
+
try {
|
76
|
+
await this.pollCheck()
|
77
|
+
} catch (err) {
|
78
|
+
this.logger.error(`base task error: ${err.message}\ntrace: ${err.stack}`)
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
async stop() {
|
83
|
+
this.started = false
|
84
|
+
}
|
85
|
+
}
|