Skip to content

Commit

Permalink
add DOT balance on AssetHub monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
RomeroYang committed Apr 3, 2024
1 parent e9cebff commit 6ba1f45
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const runloop = () => {

relayChainTokenCheck("Karura", true);
relayChainTokenCheck("Acala", true);

relayChainTokenCheck("AssetHub", true);
}
}, 1000 * 60 * 60);
};
Expand Down
13 changes: 9 additions & 4 deletions src/monitors/relayChainToken.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
25 changes: 24 additions & 1 deletion src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ApiPromise> = {};
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;
};
Expand Down Expand Up @@ -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;

0 comments on commit 6ba1f45

Please sign in to comment.