From 6ba1f45517d7b430430347b9f6f9f88d2ef89482 Mon Sep 17 00:00:00 2001 From: shawn Date: Wed, 3 Apr 2024 12:43:38 +0800 Subject: [PATCH] add DOT balance on AssetHub monitor --- src/config.ts | 5 +++++ src/index.ts | 2 ++ src/monitors/relayChainToken.ts | 13 +++++++++---- src/utils/api.ts | 25 ++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/config.ts b/src/config.ts index 61b98fb..1ec4a21 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,12 +5,17 @@ export const config = { kusama: "wss://kusama-rpc.polkadot.io/", acala: "wss://acala-rpc.dwellir.com", polkadot: "wss://polkadot.api.onfinality.io/public-ws", + assetHub: "wss://statemint-rpc.dwellir.com", }, price: "https://api.polkawallet.io/price-server", ksm: { account: "F7fq1jMZkfuCuoMTyiEVAP2DMpMt18WopgBqTJznLihLNbZ", decimal: 12, }, + assetHub: { + account: "13cKp89Msu7M2PiaCuuGr1BzAsD5V3vaVbDMs3YtjMZHdGwR", + decimal: 10, + }, kar: { prefix: 8, }, diff --git a/src/index.ts b/src/index.ts index f49b593..12168d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,6 +57,8 @@ const runloop = () => { relayChainTokenCheck("Karura", true); relayChainTokenCheck("Acala", true); + + relayChainTokenCheck("AssetHub", true); } }, 1000 * 60 * 60); }; diff --git a/src/monitors/relayChainToken.ts b/src/monitors/relayChainToken.ts index 77be4c8..4a17dfe 100644 --- a/src/monitors/relayChainToken.ts +++ b/src/monitors/relayChainToken.ts @@ -1,17 +1,22 @@ -import { ChainName } from "../types"; import { FixedPointNumber } from "@acala-network/sdk-core"; -import { Logger, getAcaApi, getKarApi, getKsmApi, getPolkaApi, watchDogLog } from "../utils"; +import { Logger, getAcaApi, getAssetHubApi, getKarApi, getKsmApi, getPolkaApi, watchDogLog } from "../utils"; import { config } from "../config"; /// check DOT/KSM balance between parachain-account and total-issuance, /// send [diff-ratio] message. -export const relayChainTokenCheck = async (env: ChainName = "Karura", toSlack = false) => { +export const relayChainTokenCheck = async (env = "Karura", toSlack = false) => { let diff = FixedPointNumber.ZERO; let diffRatio = 0; let msg = ""; const token = env === "Karura" ? "KSM" : "DOT"; const relayChain = token === "KSM" ? "kusama" : "polkadot"; - if (token === "KSM") { + if (env === "AssetHub") { + const hubAccount = await getAssetHubApi().query.system.account(config.assetHub.account); + const hubBalance = FixedPointNumber.fromInner(hubAccount.data.free.toString(), config.assetHub.decimal); + + diffRatio = hubBalance.toNumber() > 0.5 ? 0 : 1; + msg = `- ${token} Balacne in AssetHub Account: ${hubBalance.toNumber(4)}`; + } else if (token === "KSM") { const ksmAccount = await getKsmApi().query.system.account(config.ksm.account); const ksmBalance = FixedPointNumber.fromInner( ksmAccount.data.free.add(ksmAccount.data.reserved).toString(), diff --git a/src/utils/api.ts b/src/utils/api.ts index 8c2b757..9ffa867 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -115,19 +115,41 @@ export class polkaApi { } } +export class assetHubApi { + static instance: assetHubApi | null; + public api: ApiPromise; + constructor() { + const provider = new WsProvider(config.endPoints.assetHub); + this.api = new ApiPromise(options({ provider })); + this.api.isReadyOrError + .then(() => { + Logger.log("assetHubApi is Ready!"); + }) + .catch((err) => { + Logger.pushEvent(API_ERROR, err, "normal", "error"); + }); + if (assetHubApi.instance == null) { + assetHubApi.instance = this; + } + return assetHubApi.instance; + } +} + const apis: Record = {}; -const allNetworks = ["aca", "kar", "dot", "ksm"]; +const allNetworks = ["aca", "kar", "dot", "ksm", "hub"]; let connectedAll = false; export const connectNodes = async () => { apis["aca"] = new acalaApi().api; apis["kar"] = new karuraApi().api; apis["dot"] = new polkaApi().api; apis["ksm"] = new kusamaApi().api; + apis["hub"] = new assetHubApi().api; await apis["aca"].isReady; await apis["kar"].isReady; await apis["dot"].isReady; await apis["ksm"].isReady; + await apis["hub"].isReady; connectedAll = true; }; @@ -162,5 +184,6 @@ export const getAcaApi = () => apis["aca"]; export const getKarApi = () => apis["kar"]; export const getPolkaApi = () => apis["dot"]; export const getKsmApi = () => apis["ksm"]; +export const getAssetHubApi = () => apis["hub"]; export const getApiConnected = () => connectedAll;