Skip to content

Commit

Permalink
Fix: change toHuman to toJson when appropriate (#54)
Browse files Browse the repository at this point in the history
* switch from toHuman to toJson when possible

* lint & prettier
  • Loading branch information
marcellorigotti authored May 6, 2024
1 parent 2ab3c3e commit fa0ff12
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/metrics/chainflip/countEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const countEvents = async (context: Context): Promise<void> => {

let error;
if (event.method === 'ExtrinsicFailed') {
error = await getStateChainError(api, event.data.toHuman().dispatchError.Module);
error = await getStateChainError(api, event.data.toJSON()[0].module);
const parsedError = error.data.name.split(':');
metricExtrinsicFailed.labels(`${parsedError[0]}`, `${parsedError[1]}`).inc();
}
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/chainflip/gatherGlobalValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export const gatherGlobalValues = async (context: Context): Promise<void> => {
global.epochIndex = Number(epoch);

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

if (dotAggKeyAddress) {
global.dotAggKeyAddress = dotAggKeyAddress.toHuman();
global.dotAggKeyAddress = dotAggKeyAddress.toJSON();
}
} catch (err) {
logger.error(err);
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/chainflip/gaugeBtcBlockTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export const gaugeBtcBlockTime = async (context: Context): Promise<void> => {
const timestamp: number = Number(await api.query.timestamp.now());
const currentChainState: any = (
await api.query.bitcoinChainTracking.currentChainState()
).toHuman();
).toJSON();
if (currentChainState) {
const currentBtcBlock: number = currentChainState.blockHeight.replace(/,/g, '');
const currentBtcBlock: number = currentChainState.blockHeight;
if (previousTimestamp === 0) {
previousTimestamp = Number(timestamp);
}
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/chainflip/gaugeDotBlockTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const gaugeDotBlockTime = async (context: Context): Promise<void> => {
const timestamp: number = Number(await api.query.timestamp.now());
const currentChainState: any = (
await api.query.polkadotChainTracking.currentChainState()
).toHuman();
).toJSON();
if (currentChainState) {
const currentDotBlock: number = currentChainState.blockHeight.replace(/,/g, '');
const currentDotBlock: number = currentChainState.blockHeight;
if (previousTimestamp === 0) {
previousTimestamp = Number(timestamp);
}
Expand Down
6 changes: 3 additions & 3 deletions src/metrics/chainflip/gaugeEthBlockTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export const gaugeEthBlockTime = async (context: Context): Promise<void> => {
metricFailure.labels({ metric: metricNameError }).set(0);
try {
const timestamp: number = Number(await api.query.timestamp.now());
const currentEthBlock: number = (await api.query.ethereumChainTracking.currentChainState())
.toHuman()
.blockHeight.replace(/,/g, '');
const currentEthBlock: number = (
await api.query.ethereumChainTracking.currentChainState()
).toJSON();
if (previousTimestamp === 0) {
previousTimestamp = Number(timestamp);
}
Expand Down
12 changes: 4 additions & 8 deletions src/metrics/chainflip/gaugeFeeDeficit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,24 @@ export const gaugeFeeDeficit = async (context: Context): Promise<void> => {
try {
// ETH fees balance
const feeWitheldEth = Number(
(await api.query.ethereumIngressEgress.withheldTransactionFees('Eth'))
.toHuman()
.replace(/,/g, ''),
(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.toHuman().replace(/,/g, ''));
totalSpent += Number(element.toJSON());
});
const deficitEth = (feeWitheldEth - totalSpent) / 1e18;
metric.labels('ethereum').set(deficitEth);

// DOT fees balance
const feeWitheldDot = Number(
(await api.query.polkadotIngressEgress.withheldTransactionFees('Dot'))
.toHuman()
.replace(/,/g, ''),
(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.toHuman().replace(/,/g, ''));
totalSpent += Number(element.toJSON());
});
const deficitDot = (feeWitheldDot - totalSpent) / 1e10;
metric.labels('polkadot').set(deficitDot);
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/chainflip/gaugePendingRedemptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const gaugePendingRedemptions = async (context: Context): Promise<void> =
const pendingRedemptions = await api.query.funding.pendingRedemptions.entries();
let totalRedemptionBalance: number = 0;
pendingRedemptions.forEach(([key, element]: [any, any]) => {
totalRedemptionBalance += parseFloat(element.toHuman().total.replace(/,/g, '')) / 1e18;
totalRedemptionBalance += parseInt(element.toJSON().total, 16) / 1e18;
});

metricPendingRedemptionBalance.set(totalRedemptionBalance);
Expand Down
4 changes: 1 addition & 3 deletions src/metrics/chainflip/gaugeWitnessChainTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export const gaugeWitnessChainTracking = async (context: Context): Promise<void>
metricFailure.labels({ metric: metricName }).set(0);
try {
const signedBlock = await api.rpc.chain.getBlock();
const currentBlockNumber = Number(
signedBlock.block.header.number.toHuman().replace(/,/g, ''),
);
const currentBlockNumber = Number(signedBlock.toJSON().block.header.number);
global.currentBlock = currentBlockNumber;
toDelete.forEach((block, labels) => {
if (block <= currentBlockNumber) {
Expand Down
5 changes: 2 additions & 3 deletions src/metrics/chainflip/gaugeWitnessCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ export const gaugeWitnessCount = async (context: Context): Promise<void> => {
metricFailure.labels({ metric: metricName }).set(0);
try {
const signedBlock = await api.rpc.chain.getBlock();
const currentBlockNumber = Number(
signedBlock.block.header.number.toHuman().replace(/,/g, ''),
);
const currentBlockNumber = Number(signedBlock.toJSON().block.header.number);

toDelete.forEach((block, labels) => {
if (block <= currentBlockNumber) {
const values = JSON.parse(labels);
Expand Down
12 changes: 3 additions & 9 deletions src/metrics/chainflip/guageExternalChainsBlockHeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,21 @@ export const gaugeExternalChainsBlockHeight = async (context: Context) => {
try {
// Ethereum
const ethBlockHeight = Number(
(await api.query.ethereumChainTracking.currentChainState())
.toHuman()
.blockHeight.replace(/,/g, ''),
(await api.query.ethereumChainTracking.currentChainState()).toJSON().blockHeight,
);
global.ethHeight = ethBlockHeight;
metric.labels('ethereum').set(ethBlockHeight);

// Bitcoin
const btcBlockHeight = Number(
(await api.query.bitcoinChainTracking.currentChainState())
.toHuman()
.blockHeight.replace(/,/g, ''),
(await api.query.bitcoinChainTracking.currentChainState()).toJSON().blockHeight,
);
global.btcHeight = btcBlockHeight;
metric.labels('bitcoin').set(btcBlockHeight);

// Polkadot
const dotBlockHeight = Number(
(await api.query.polkadotChainTracking.currentChainState())
.toHuman()
.blockHeight.replace(/,/g, ''),
(await api.query.polkadotChainTracking.currentChainState()).toJSON().blockHeight,
);
global.dotHeight = dotBlockHeight;
metric.labels('polkadot').set(dotBlockHeight);
Expand Down

0 comments on commit fa0ff12

Please sign in to comment.