Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: use custom RPCs over storage_items read #69

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dist
.env.sisyphos
config/ignore.json
config/perseverance.json
config/sisyphos.json
alerts.yaml
1 change: 1 addition & 0 deletions config/sisyphos.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"flip": {
"enabled": true,
"network": "sisyphos",
"eventLog": false,
"defaultMetrics": [],
"accounts": [],
"skipEvents": [
Expand Down
10 changes: 2 additions & 8 deletions src/metrics/chainflip/gatherGlobalValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ export const gatherGlobalValues = async (context: Context): Promise<void> => {
const { logger, api, registry, metricFailure } = context;

try {
const epoch: any = await api.query.validator.currentEpoch();
global.epochIndex = Number(epoch);
global.epochIndex = Number(context.data.epoch.current_epoch_index);

const epochKey = await api.query.polkadotThresholdSigner.currentKeyEpoch();
const dotAggKeyAddress = await api.query.polkadotThresholdSigner.keys(epochKey.toJSON());

if (dotAggKeyAddress) {
global.dotAggKeyAddress = dotAggKeyAddress.toJSON();
}
global.dotAggKeyAddress = context.data.dot_aggkey;
} catch (err) {
logger.error(err);
}
Expand Down
16 changes: 3 additions & 13 deletions src/metrics/chainflip/gaugeAuthorities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,10 @@ export const gaugeAuthorities = async (context: Context): Promise<void> => {

metricFailure.labels({ metric: metricName }).set(0);

let currentAuthorities: any;
try {
let onlineCounter = 0;

currentAuthorities = await api.query.validator.currentAuthorities();
metric.set(currentAuthorities.toJSON().length);
global.currentAuthorities = currentAuthorities.toJSON().length;
for (const idSs58 of currentAuthorities.toJSON()) {
const result = await makeRpcRequest(api, 'account_info_v2', idSs58);
if (result.is_online) {
onlineCounter++;
}
}
metricOnline.set(onlineCounter);
metric.set(context.data.authorities.authorities);
global.currentAuthorities = context.data.authorities.authorities;
metricOnline.set(context.data.authorities.online_authorities);
} catch (e) {
logger.error(e);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
29 changes: 2 additions & 27 deletions src/metrics/chainflip/gaugeBackupValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,8 @@ export const gaugeBackupValidator = async (context: Context): Promise<void> => {

let currentBackups: any;
try {
let onlineCounter = 0;
let backupSetSize = 0;

currentBackups = await api.query.validator.backups();
const accountInfo: any[] = [];

for (const idSs58 of currentBackups.keys()) {
const result = await makeRpcRequest(api, 'account_info_v2', idSs58);
if (result.is_current_backup) {
accountInfo.push(result);
}
}

accountInfo.sort((a: any, b: any) => {
return Number(BigInt(b.balance) - BigInt(a.balance));
});
const accounts = accountInfo.slice(0, 50);

for (const account of accounts) {
backupSetSize++;
if (account.is_online) {
onlineCounter++;
}
}

metric.set(backupSetSize);
metricOnline.set(onlineCounter);
metric.set(context.data.authorities.backups);
metricOnline.set(context.data.authorities.online_backups);
} catch (e) {
logger.error(e);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
6 changes: 1 addition & 5 deletions src/metrics/chainflip/gaugeBitcoinBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ export const gaugeBitcoinBalance = async (context: Context): Promise<void> => {
metricFailure.labels({ metric: metricName }).set(0);

try {
const bitcoinAvailableUtxos: any = await api.query.environment.bitcoinAvailableUtxos();
const aggregatedAmount = bitcoinAvailableUtxos.reduce((acc: number, utxo: any) => {
return acc + Number(utxo.amount);
}, 0);
metric.set(aggregatedAmount);
metric.set(context.data.btc_utxos.total_balance);
} catch (err) {
logger.error(err);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
4 changes: 1 addition & 3 deletions src/metrics/chainflip/gaugeBlocksPerEpoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ export const gaugeBlocksPerEpoch = async (context: Context): Promise<void> => {
if (registry.getSingleMetric(metricName) === undefined) registry.registerMetric(metric);
metricFailure.labels({ metric: metricName }).set(0);

let blocksPerEpoch: any;
try {
blocksPerEpoch = await api.query.validator.blocksPerEpoch();
metric.set(blocksPerEpoch.toJSON());
metric.set(context.data.epoch.blocks_per_epoch);
} catch (e) {
logger.error(e);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
5 changes: 2 additions & 3 deletions src/metrics/chainflip/gaugeBtcUtxos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export const gaugeBtcUtxos = async (context: Context): Promise<void> => {
metricFailure.labels({ metric: metricName }).set(0);
if (registry.getSingleMetric(metricName) === undefined) registry.registerMetric(metric);
try {
const utxos: any = await api.query.environment.bitcoinAvailableUtxos();
const metricValue: number = utxos.length;
metric.set(metricValue);
const utxos: any = context.data.btc_utxos.count;
metric.set(utxos);
} catch (err) {
logger.error(err);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/chainflip/gaugeCurrentEpochDurationBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const gaugeCurrentEpochDurationBlocks = async (context: Context): Promise
metricFailure.labels({ metric: metricName }).set(0);

try {
const currentEpochStartedAt: number = await api.query.validator.currentEpochStartedAt();
const currentBlockHeight: number = await api.query.system.number();
const currentEpochStartedAt: number = context.data.epoch.current_epoch_started_at;
const currentBlockHeight: number = context.header.number;

const currentEpochDurationBlocks: number = currentBlockHeight - currentEpochStartedAt;
metric.set(currentEpochDurationBlocks);
Expand Down
45 changes: 8 additions & 37 deletions src/metrics/chainflip/gaugeDepositChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,32 @@ const metric: Gauge = new promClient.Gauge({
registers: [],
});

const axios = new Axios({
baseURL: env.PROCESSOR_ENDPOINT,
timeout: 6000,
headers: {
accept: 'application/json',
'cache-control': 'no-cache',
'content-type': 'application/json',
},
});

export const gaugeDepositChannels = async (context: Context): Promise<void> => {
if (context.config.skipMetrics.includes('cf_open_deposit_channels')) {
return;
}
const { logger, api, registry, metricFailure } = context;
logger.debug(`Scraping ${metricName}`);
const config = context.config as FlipConfig;
const { accounts } = config;

if (registry.getSingleMetric(metricName) === undefined) registry.registerMetric(metric);

metricFailure.labels({ metric: metricName }).set(0);

try {
const data = await axios.post(
env.PROCESSOR_ENDPOINT,
`{"query":"query GetOpenSwapChannels {\\n channels: allSwapChannels(\\n condition: {isExpired: false}\\n orderBy: ISSUED_EVENT_ID_DESC\\n ) {\\n nodes {\\n sourceChain\\n }\\n }\\n}"}`,
);

const channels = JSON.parse(data.data).data.channels.nodes;
let ethChannels = 0;
let btcChannels = 0;
let dotChannels = 0;
let arbChannels = 0;
channels.forEach((channel: any) => {
switch (channel.sourceChain) {
case 'Ethereum':
ethChannels++;
break;
case 'Bitcoin':
btcChannels++;
break;
case 'Polkadot':
dotChannels++;
break;
case 'Arbitrum':
arbChannels++;
}
});
// BTC
const btcChannels = context.data.open_deposit_channels.bitcoin;
metric.labels('bitcoin').set(btcChannels);

// DOT
const dotChannels = context.data.open_deposit_channels.polkadot;
metric.labels('polkadot').set(dotChannels);

// ETH
const ethChannels = context.data.open_deposit_channels.ethereum;
metric.labels('ethereum').set(ethChannels);

// ARB
const arbChannels = context.data.open_deposit_channels.arbitrum;
metric.labels('arbitrum').set(arbChannels);
} catch (e) {
logger.error(e);
Expand Down
71 changes: 41 additions & 30 deletions src/metrics/chainflip/gaugeFeeDeficit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,51 @@ export const gaugeFeeDeficit = async (context: Context): Promise<void> => {
if (registry.getSingleMetric(metricName) === undefined) registry.registerMetric(metric);
try {
// ETH fees balance
const feeWitheldEth = Number(
(await api.query.ethereumIngressEgress.withheldTransactionFees('Eth')).toJSON(),
);
const feeSpentEth = await api.query.ethereumBroadcaster.transactionFeeDeficit.entries();
let totalSpent = 0;
feeSpentEth.forEach(([key, element]: [any, any]) => {
totalSpent += Number(element.toJSON());
});
const deficitEth = (feeWitheldEth - totalSpent) / 1e18;
metric.labels('ethereum').set(deficitEth);
const eth_fees = context.data.fee_imbalance.ethereum;
if (Object.hasOwn(eth_fees, 'Deficit')) {
// Deficit case
const metricValue = -(Number(eth_fees.Deficit) / 1e18);
metric.labels('ethereum').set(metricValue);
} else {
// Surplus case
const metricValue = Number(eth_fees.Surplus) / 1e18;
metric.labels('ethereum').set(metricValue);
}

// ARB fees balance
const feeWitheldArb = Number(
(await api.query.arbitrumIngressEgress.withheldTransactionFees('ArbEth')).toJSON(),
);
const feeSpentArb = await api.query.arbitrumBroadcaster.transactionFeeDeficit.entries();
totalSpent = 0;
feeSpentArb.forEach(([key, element]: [any, any]) => {
totalSpent += Number(element.toJSON());
});
const deficitArb = (feeWitheldArb - totalSpent) / 1e18;
metric.labels('arbitrum').set(deficitArb);
const arb_fees = context.data.fee_imbalance.arbitrum;
if (Object.hasOwn(arb_fees, 'Deficit')) {
// Deficit case
const metricValue = -(Number(arb_fees.Deficit) / 1e18);
metric.labels('arbitrum').set(metricValue);
} else {
// Surplus case
const metricValue = Number(arb_fees.Surplus) / 1e18;
metric.labels('arbitrum').set(metricValue);
}

// DOT fees balance
const feeWitheldDot = Number(
(await api.query.polkadotIngressEgress.withheldTransactionFees('Dot')).toJSON(),
);
const feeSpentDot = await api.query.polkadotBroadcaster.transactionFeeDeficit.entries();
totalSpent = 0;
feeSpentDot.forEach(([key, element]: [any, any]) => {
totalSpent += Number(element.toJSON());
});
const deficitDot = (feeWitheldDot - totalSpent) / 1e10;
metric.labels('polkadot').set(deficitDot);
const dot_fees = context.data.fee_imbalance.polkadot;
if (Object.hasOwn(dot_fees, 'Deficit')) {
// Deficit case
const metricValue = -(Number(dot_fees.Deficit) / 1e10);
metric.labels('polkadot').set(metricValue);
} else {
// Surplus case
const metricValue = Number(dot_fees.Deficit) / 1e10;
metric.labels('polkadot').set(metricValue);
}
// BTC fees balance
const btc_fees = context.data.fee_imbalance.bitcoin;
if (Object.hasOwn(btc_fees, 'Deficit')) {
// Deficit case
const metricValue = -(Number(btc_fees.Deficit) / 1e8);
metric.labels('bitcoin').set(metricValue);
} else {
// Surplus case
const metricValue = Number(btc_fees.Deficit) / 1e8;
metric.labels('bitcoin').set(metricValue);
}
} catch (err) {
logger.error(err);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/chainflip/gaugeFlipTotalSupply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const gaugeFlipTotalSupply = async (context: Context): Promise<void> => {
metricFailure.labels({ metric: metricName }).set(0);
if (registry.getSingleMetric(metricName) === undefined) registry.registerMetric(metric);
try {
const totalSupply: bigint = await api.query.flip.totalIssuance();
const totalSupply: bigint = context.data.flip_supply.total_supply;
const metricValue: number = Number(Number(totalSupply) / 10 ** 18);
metric.set(metricValue);
} catch (err) {
Expand Down
3 changes: 1 addition & 2 deletions src/metrics/chainflip/gaugeMinActiveBid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export const gaugeMinActiveBid = async (context: Context): Promise<void> => {
metricFailure.labels({ metric: metricName }).set(0);

try {
const result = await makeRpcRequest(api, 'auction_state');
const { min_active_bid } = result;
const min_active_bid = context.data.epoch.min_active_bid;

const MAB: number = Number(Number(min_active_bid) / 10 ** 18);

Expand Down
20 changes: 8 additions & 12 deletions src/metrics/chainflip/gaugePendingBroadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ export const gaugePendingBroadcast = async (context: Context): Promise<void> =>
metricFailure.labels({ metric: metricName }).set(0);

try {
const dotQueue: any = await api.query.polkadotBroadcaster.pendingBroadcasts();
const dotQueueLenght: number = dotQueue.size;
metric.labels('polkadot').set(dotQueueLenght);
// Polkadot
metric.labels('polkadot').set(context.data.pending_broadcasts.polkadot);

const btcQueue: any = await api.query.bitcoinBroadcaster.pendingBroadcasts();
const btcQueueLenght: number = btcQueue.size;
metric.labels('bitcoin').set(btcQueueLenght);
// Bitcoin
metric.labels('bitcoin').set(context.data.pending_broadcasts.bitcoin);

const ethQueue: any = await api.query.ethereumBroadcaster.pendingBroadcasts();
const ethQueueLenght: number = ethQueue.size;
metric.labels('ethereum').set(ethQueueLenght);
// Ethereum
metric.labels('ethereum').set(context.data.pending_broadcasts.ethereum);

const arbQueue: any = await api.query.arbitrumBroadcaster.pendingBroadcasts();
const arbQueueLenght: number = arbQueue.size;
metric.labels('arbitrum').set(arbQueueLenght);
// Arbitrum
metric.labels('arbitrum').set(context.data.pending_broadcasts.arbitrum);
} catch (err) {
logger.error(err);
metricFailure.labels({ metric: metricName }).set(1);
Expand Down
10 changes: 4 additions & 6 deletions src/metrics/chainflip/gaugePendingRedemptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,12 @@ export const gaugePendingRedemptions = async (context: Context): Promise<void> =

logger.debug(`Scraping ${metricNamePendingRedemption}, ${metricNamePendingRedemptionBalance}`);
try {
const pendingRedemptions = await api.query.funding.pendingRedemptions.entries();
let totalRedemptionBalance: number = 0;
pendingRedemptions.forEach(([key, element]: [any, any]) => {
totalRedemptionBalance += parseInt(element.toJSON().total, 16) / 1e18;
});
const pendingRedemptions = context.data.pending_redemptions.count;
const totalRedemptionBalance: number =
Number(context.data.pending_redemptions.total_balance) / 1e18;

metricPendingRedemptionBalance.set(totalRedemptionBalance);
metricPendingRedemption.set(pendingRedemptions.length);
metricPendingRedemption.set(pendingRedemptions);
} catch (e) {
logger.error(e);
metricFailure.labels({ metric: metricNamePendingRedemption }).set(1);
Expand Down
35 changes: 0 additions & 35 deletions src/metrics/chainflip/gaugeReputation.ts

This file was deleted.

Loading
Loading