Skip to content

Commit

Permalink
fix: remove rawProposerAddress, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpeach committed Jun 10, 2024
1 parent 298fba2 commit 79c4f92
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
6 changes: 4 additions & 2 deletions src/lib/pages/block-details/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export const useBlockDataWithValidatorLcd = (
const validator = useMemo<Nullable<Validator>>(() => {
if (!blockData || !validators) return null;

const { proposerAddress } = blockData.block;
if (!proposerAddress) return null;

const found = validators.find(
(each) =>
consensusPubkeyToHexAddress(each.consensusPubkey) ===
toHex(fromBase64(blockData.rawProposerAddress)).toUpperCase()
toHex(fromBase64(proposerAddress)).toUpperCase()
);
if (!found) return null;
return {
Expand All @@ -43,7 +46,6 @@ export const useBlockDataWithValidatorLcd = (
},
transactions: blockData.transactions,
pagination: blockData.pagination,
rawProposerAddress: blockData.rawProposerAddress,
}
: undefined,
isLoading: isLoadingBlockData || isLoadingValidators,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/services/types/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export const zBlockDataResponseLcd = zBlockLcd
block: BlockData;
transactions: Transaction[];
pagination: Pagination;
rawProposerAddress: string;
}>((val) => {
// 1. Create Tx Hashes
const txHashes = val.block.data.txs.map(createTxHash);
Expand Down Expand Up @@ -144,6 +143,7 @@ export const zBlockDataResponseLcd = zBlockLcd
height: val.block.header.height,
timestamp: val.block.header.time,
proposer: null, // NOTE: Will be filled in the next step
proposerAddress: val.block.header.proposerAddress,
gasLimit: undefined,
gasUsed: undefined,
};
Expand All @@ -152,7 +152,6 @@ export const zBlockDataResponseLcd = zBlockLcd
block,
transactions,
pagination: val.pagination,
rawProposerAddress: val.block.header.proposerAddress,
};
});

Expand Down
1 change: 1 addition & 0 deletions src/lib/types/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Block {
timestamp: Date;
txCount: number;
proposer: Nullable<Validator>;
proposerAddress?: string; // Base64
}

export interface BlockData extends Omit<Block, "txCount"> {
Expand Down
31 changes: 24 additions & 7 deletions src/lib/types/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { z } from "zod";
import { snakeToCamel } from "lib/utils/formatter/snakeToCamel";
import { formatUrl } from "lib/utils/formatter/url";

import type { BechAddr20, ValidatorAddr } from "./addrs";
import { zBechAddr20, zValidatorAddr } from "./addrs";
import { zBig } from "./big";
import type { Nullable, Option } from "./common";
import { zRatio } from "./currency";
import type { Ratio } from "./currency";

Expand All @@ -21,6 +23,26 @@ export const zValidator = z
}));
export type Validator = z.infer<typeof zValidator>;

export type ValidatorData = {
rank: Nullable<number>;
validatorAddress: ValidatorAddr;
accountAddress: BechAddr20;
identity: string;
moniker: string;
details: string;
commissionRate: Ratio<number>;
// NOTE: consensusPubkey is present only in the lcd response
consensusPubkey: Option<{
"@type": string;
key: string;
}>;
isJailed: boolean;
isActive: boolean;
votingPower: Big;
uptime?: number;
website: string;
};

export const zValidatorData = z
.object({
rank: z.number().nullable(),
Expand All @@ -36,16 +58,11 @@ export const zValidatorData = z
uptime: z.number().optional(),
website: z.string(),
})
.transform(({ website, ...val }) => ({
.transform<ValidatorData>(({ website, ...val }) => ({
...snakeToCamel(val),
website: formatUrl(website),
// TODO: add from api
consensusPubkey: {
"@type": "",
key: "",
},
consensusPubkey: undefined,
}));
export type ValidatorData = z.infer<typeof zValidatorData>;

export enum BlockVote {
PROPOSE = "PROPOSE",
Expand Down

0 comments on commit 79c4f92

Please sign in to comment.