clean code

This commit is contained in:
liangping 2023-04-04 12:48:39 +08:00
parent c89bdd06b4
commit c30a652465
14 changed files with 33 additions and 108 deletions

View File

@ -1,7 +1,5 @@
import {fromBase64, fromBech32, fromHex, toBase64, toBech32, toHex} from '@cosmjs/encoding'
import { Ripemd160, sha256 } from '@cosmjs/crypto'
import { cosmos } from '@ping-pub/codegen'
import type { PubKey } from '@ping-pub/codegen/src/cosmos/crypto/ed25519/keys'
export function decodeAddress(address: string) {
return fromBech32(address)
@ -25,41 +23,27 @@ export function valoperToPrefix(valoper?: string) {
}
return toBech32(prefix.replace('valoper', ''), data)
}
export function decodeKey(consensusPubkey: {typeUrl: string, value: Uint8Array}) {
let raw = null
if (consensusPubkey.typeUrl === '/cosmos.crypto.ed25519.PubKey') {
const pubkey = cosmos.crypto.ed25519.PubKey.decode(consensusPubkey.value)
return pubkey
}
if (consensusPubkey.typeUrl === '/cosmos.crypto.secp256k1.PubKey') {
const pubkey = cosmos.crypto.secp256k1.PubKey.decode(consensusPubkey.value)
return pubkey
}
return raw
}
export function consensusPubkeyToHexAddress(consensusPubkey?: {typeUrl: string, value: Uint8Array}) {
export function consensusPubkeyToHexAddress(consensusPubkey?: {"@type": string, key: string}) {
if(!consensusPubkey) return ""
let raw = ""
if (consensusPubkey.typeUrl === '/cosmos.crypto.ed25519.PubKey') {
const pubkey = decodeKey(consensusPubkey)
if(pubkey) return toHex(sha256(pubkey.key)).slice(0, 40).toUpperCase()
if (consensusPubkey['@type'] === '/cosmos.crypto.ed25519.PubKey') {
const pubkey = fromBase64(consensusPubkey.key)
if(pubkey) return toHex(sha256(pubkey)).slice(0, 40).toUpperCase()
}
if (consensusPubkey.typeUrl === '/cosmos.crypto.secp256k1.PubKey') {
const pubkey = decodeKey(consensusPubkey)
if(pubkey) return toHex(new Ripemd160().update(sha256(pubkey.key)).digest())
if (consensusPubkey['@type'] === '/cosmos.crypto.secp256k1.PubKey') {
const pubkey = fromBase64(consensusPubkey.key)
if(pubkey) return toHex(new Ripemd160().update(sha256(pubkey)).digest())
}
return raw
}
export function pubKeyToValcons(consensusPubkey: {typeUrl: string, value: Uint8Array}, prefix: string) {
const pubkey = decodeKey(consensusPubkey)
export function pubKeyToValcons(consensusPubkey: {"@type": string, key: string}, prefix: string) {
const pubkey = fromBase64(consensusPubkey.key)
if(pubkey) {
const addressData = sha256(pubkey.key).slice(0, 20)
const addressData = sha256(pubkey).slice(0, 20)
return toBech32(`${prefix}valcons`, addressData)
}
}

View File

@ -1,38 +0,0 @@
import type { LCDClient } from "@osmonauts/lcd";
import type { QueryAnnualProvisionsRequest, QueryAnnualProvisionsResponseSDKType, QueryInflationRequest, QueryInflationResponseSDKType, QueryParamsRequest, QueryParamsResponseSDKType } from '@ping-pub/codegen/src/cosmos/mint/v1beta1/query'
export class OsmosisMintClient {
req: LCDClient;
constructor({
requestClient
}: {
requestClient: LCDClient;
}) {
this.req = requestClient;
this.params = this.params.bind(this);
this.inflation = this.inflation.bind(this);
this.annualProvisions = this.annualProvisions.bind(this);
}
/* Params returns the total set of minting parameters. */
async params(_params: QueryParamsRequest = {}): Promise<QueryParamsResponseSDKType> {
const endpoint = `cosmos/mint/v1beta1/params`;
return await this.req.get<QueryParamsResponseSDKType>(endpoint);
}
/* Inflation returns the current minting inflation value. */
async inflation(_params: QueryInflationRequest = {}): Promise<QueryInflationResponseSDKType> {
const endpoint = `cosmos/mint/v1beta1/inflation`;
return await this.req.get<QueryInflationResponseSDKType>(endpoint);
}
/* AnnualProvisions current minting annual provisions value. */
async annualProvisions(_params: QueryAnnualProvisionsRequest = {}): Promise<QueryAnnualProvisionsResponseSDKType> {
const endpoint = `cosmos/mint/v1beta1/annual_provisions`;
return await this.req.get<QueryAnnualProvisionsResponseSDKType>(endpoint);
}
}

View File

@ -1,4 +1,3 @@
export * from './address'
export * from './client'
export * from './http'
export * from './misc'

View File

@ -1,6 +1,5 @@
import { sha256 } from "@cosmjs/crypto";
import { toHex } from "@cosmjs/encoding";
import { PageRequest } from "@ping-pub/codegen/src/cosmos/base/query/v1beta1/pagination";
export function newPageRequest(param: {
key?: Uint8Array,
@ -9,9 +8,7 @@ export function newPageRequest(param: {
countTotal?: boolean,
reverse?: boolean
}) {
return PageRequest.fromPartial(
param
)
return param
}
export function hashTx(raw: Uint8Array) {

View File

@ -9,14 +9,6 @@ export interface Request<T> {
adapter: (source: any) => T
}
interface User {
}
interface Post {
}
// use snake style, since the all return object use snake style.
export interface RequestRegistry {
auth_params: Request<any>
@ -66,9 +58,9 @@ export interface RequestRegistry {
base_tendermint_validatorsets_latest: Request<any>;
base_tendermint_validatorsets_height: Request<any>;
tx_txs: Request<any>;
tx_txs_block: Request<any>;
tx_hash: Request<any>;
tx_txs: Request<Txs>;
tx_txs_block: Request<Txs>;
tx_hash: Request<Tx>;
}

View File

@ -1,16 +1,15 @@
import { defineStore } from "pinia";
import { decodeTxRaw, type DecodedTxRaw } from '@cosmjs/proto-signing'
import { createBaseClientForChain } from "@/libs/client";
import { useBlockchain } from "@/stores";
import type { GetLatestBlockResponse, GetLatestBlockResponseSDKType } from "@ping-pub/codegen/src/cosmos/base/tendermint/v1beta1/query";
import { hashTx } from "@/libs";
import type { Block } from "@/types";
export const useBlockModule = defineStore('blockModule', {
state: () => {
return {
latest: {} as GetLatestBlockResponse,
current: {} as GetLatestBlockResponse,
recents: [] as GetLatestBlockResponse[]
latest: {} as Block,
current: {} as Block,
recents: [] as Block[]
}
},
getters: {

View File

@ -2,7 +2,6 @@ import { useBlockchain, useCoingecko, useBaseStore, useBankStore, useFormatter,
import { useDistributionStore } from "@/stores/useDistributionStore";
import { useMintStore } from "@/stores/useMintStore";
import { useStakingStore } from "@/stores/useStakingStore";
import { ProposalStatus, type ProposalSDKType, Proposal } from "@ping-pub/codegen/src/cosmos/gov/v1beta1/gov";
import numeral from "numeral";
import { defineStore } from "pinia";

View File

@ -1,13 +1,12 @@
<script setup lang="ts">
import { useBlockchain, useFormatter, useMintStore, useStakingStore } from '@/stores';
import type { QueryValidatorResponseSDKType } from '@ping-pub/codegen/src/cosmos/staking/v1beta1/query';
import { onMounted } from 'vue';
import { useRoute } from 'vue-router';
import ValidatorCommissionRate from '@/components/ValidatorCommissionRate.vue'
import type { Coin, DecCoin } from '@ping-pub/codegen/src/cosmos/base/v1beta1/coin';
import { consensusPubkeyToHexAddress, operatorAddressToAccount, pubKeyToValcons, valoperToPrefix } from '@/libs';
import { computed } from '@vue/reactivity';
import type { GetTxsEventResponse } from '@ping-pub/codegen/src/cosmos/tx/v1beta1/service';
import type { Coin } from '@/types';
import type { Txs } from '@/types/Txs';
const props = defineProps(['validator', 'chain'])
@ -17,11 +16,11 @@ const format = useFormatter()
const validator: string = props.validator
const v = ref({} as QueryValidatorResponseSDKType)
const v = ref({})
const cache = JSON.parse(localStorage.getItem('avatars')||'{}')
const avatars = ref( cache || {} )
const identity = ref("")
const rewards = ref([] as DecCoin[])
const rewards = ref([] as Coin[])
const addresses = ref({} as {
account: string
operAddress: string
@ -38,7 +37,7 @@ staking.fetchValidatorDelegation(validator, addresses.value.account).then(x => {
}
})
const txs = ref({} as GetTxsEventResponse)
const txs = ref({} as Txs)
blockchain.rpc.txs([`message.sender='${addresses.value.account}'`]).then(x => {
console.log("txs", x)

View File

@ -2,7 +2,6 @@
import { useBaseStore, useFormatter, useStakingStore } from '@/stores';
import { toBase64, toHex } from '@cosmjs/encoding';
import { pubkeyToAddress } from '@cosmjs/tendermint-rpc';
import type { ValidatorSDKType } from '@ping-pub/codegen/src/cosmos/staking/v1beta1/staking';
import { computed } from '@vue/reactivity';
import { onMounted, ref, type DebuggerEvent } from 'vue';
import { consensusPubkeyToHexAddress } from '@/libs'
@ -14,7 +13,7 @@ const avatars = ref( cache || {} )
const latest = ref({} as Record<string, number>)
const yesterday = ref({} as Record<string, number>)
const tab = ref('active')
const unbondList = ref([] as ValidatorSDKType[])
const unbondList = ref([])
const base = useBaseStore()
onMounted(()=> {
fetchChange(0)

View File

@ -1,3 +1,6 @@
<script lang="ts" setup>
</script>
<template>
<div>Hello wallet</div>
</template>

View File

@ -4,9 +4,6 @@ import ChainSummary from '@/components/ChainSummary.vue';
import { computed, ref } from 'vue';
import { useBlockchain } from '@/stores';
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { QueryClient, setupBankExtension, setupDistributionExtension, setupGovExtension, setupMintExtension, setupStakingExtension } from "@cosmjs/stargate";
const dashboard = useDashboard()
dashboard.$subscribe((mutation, state) => {

View File

@ -1,6 +1,5 @@
import { defineStore } from "pinia";
import { useDashboard, type ChainConfig, type Endpoint, EndpointType } from "./useDashboard";
import { LCDClient } from '@osmonauts/lcd'
import type { VerticalNavItems } from '@/@layouts/types'
import { useRouter } from "vue-router";
import { useStakingStore } from "./useStakingStore";

View File

@ -1,6 +1,5 @@
import { defineStore } from "pinia";
import { useBlockchain } from "./useBlockchain";
import Long from "long";
import numeral from "numeral";
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration'
@ -9,8 +8,8 @@ import updateLocale from 'dayjs/plugin/updateLocale'
import utc from 'dayjs/plugin/utc'
import localeData from 'dayjs/plugin/localeData'
import { useStakingStore } from "./useStakingStore";
import { fromBech32, toBase64, toHex } from "@cosmjs/encoding";
import { consensusPubkeyToHexAddress, operatorAddressToAccount } from "@/libs";
import { toHex } from "@cosmjs/encoding";
import { consensusPubkeyToHexAddress } from "@/libs";
dayjs.extend(localeData)
dayjs.extend(duration)

View File

@ -1,16 +1,13 @@
import { defineStore } from "pinia";
import { useBlockchain } from "./useBlockchain";
import { createGovRestClientForChain } from "@/libs/client";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";
import { QueryClient } from "@cosmjs/stargate";
export const useGovStore = defineStore('govStore', {
state: () => {
return {
params: {
deposit: {} as DepositParams,
voting: {} as VotingParams,
tally: {} as TallyParams,
deposit: {},
voting: {},
tally: {},
}
}
},