From 4dda03cd9de7c479aa19d2d95d93df0e3d227e2e Mon Sep 17 00:00:00 2001 From: Pham Tu Date: Fri, 2 Feb 2024 11:47:53 +0700 Subject: [PATCH] fix inflation osmo and cosmwasm secret --- package.json | 3 +- src/libs/client.ts | 434 +++++++++++++++++---- src/modules/[chain]/cosmwasm/WasmClient.ts | 68 +--- src/stores/useParamsStore.ts | 2 +- yarn.lock | 37 +- 5 files changed, 375 insertions(+), 169 deletions(-) diff --git a/package.json b/package.json index 104600f2..f1b4533b 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "vue-tsc": "^1.8.27" }, "resolutions": { - "cosmjs-types": "^0.9.0" + "cosmjs-types": "^0.9.0", + "@cosmjs/math": "^0.32.2" } } diff --git a/src/libs/client.ts b/src/libs/client.ts index f1144a99..8d7e8c3b 100644 --- a/src/libs/client.ts +++ b/src/libs/client.ts @@ -1,10 +1,12 @@ import { DEFAULT, fetchData } from '@/libs'; import { PageRequest } from '@/types'; import { - setupWasmExtension, - type WasmExtension, -} from '@cosmjs/cosmwasm-stargate'; -import { fromBase64, fromHex, toBase64, toHex } from '@cosmjs/encoding'; + fromBase64, + fromHex, + toBase64, + toBech32, + toHex, +} from '@cosmjs/encoding'; import type { DecodedTxRaw } from '@cosmjs/proto-signing'; import { decodeTxRaw } from '@cosmjs/proto-signing'; import { @@ -15,7 +17,6 @@ import { setupDistributionExtension, setupGovExtension, setupIbcExtension, - setupMintExtension, setupSlashingExtension, setupStakingExtension, setupTxExtension, @@ -25,13 +26,18 @@ import { type GovExtension, type GovProposalId, type IbcExtension, - type MintExtension, type StakingExtension, type TxExtension, } from '@cosmjs/stargate'; -import type { SlashingExtension } from '@cosmjs/stargate/build/modules'; +import type { + MintParams, + SlashingExtension, +} from '@cosmjs/stargate/build/modules'; import type { BondStatusString } from '@cosmjs/stargate/build/modules/staking/queries'; -import { longify } from '@cosmjs/stargate/build/queryclient'; +import { + decodeCosmosSdkDecFromProto, + longify, +} from '@cosmjs/stargate/build/queryclient'; import { HttpClient, Tendermint37Client, @@ -47,6 +53,13 @@ import { QueryAccountsResponse, QueryClientImpl as AuthQueryClientImpl, } from 'cosmjs-types/cosmos/auth/v1beta1/query'; +import { + QueryAnnualProvisionsResponse, + QueryClientImpl as MintQueryClientImpl, + QueryInflationResponse, + QueryParamsResponse, +} from 'cosmjs-types/cosmos/mint/v1beta1/query'; +import { QueryClientImpl as OsmoMintQueryClientImpl } from 'osmojs/dist/codegen/osmosis/mint/v1beta1/query.rpc.Query'; import { QueryClientImpl as BankQueryClientImpl, QueryParamsResponse as QueryBankParamsResponse, @@ -87,12 +100,17 @@ import { Tx } from 'cosmjs-types/cosmos/tx/v1beta1/tx'; import { QueryAllContractStateResponse, QueryClientImpl as WasmQueryClientImpl, + QueryCodeResponse, QueryCodesResponse, + QueryContractHistoryResponse, + QueryContractInfoResponse, + QueryContractsByCodeResponse, QueryContractsByCreatorResponse, QueryParamsResponse as QueryWasmParamsResponse, + QueryRawContractStateResponse, } from 'cosmjs-types/cosmwasm/wasm/v1/query'; import { QueryClientImpl as SecretWasmQueryClientImpl } from 'secretjs/src/protobuf/secret/compute/v1beta1/query'; - +import { ChainGrpcWasmApi, type PaginationOption } from '@injectivelabs/sdk-ts'; import { toTimestamp } from 'cosmjs-types/helpers'; import type { Event, EventAttribute } from 'cosmjs-types/tendermint/abci/types'; import semver from 'semver'; @@ -106,9 +124,11 @@ import { type AbstractRegistry, type RequestRegistry, } from './registry'; -import { decodeProto } from '@/components/dynamic'; import { convertStr } from './utils'; import { AccessType } from 'cosmjs-types/cosmwasm/wasm/v1/types'; +import type { JsonObject } from '@cosmjs/cosmwasm-stargate'; +import { Decimal } from '@cosmjs/math'; +import { assert } from '@cosmjs/utils'; export const DEFAULT_SDK_VERSION = '0.45.16'; export const LCD_FALLBACK_CHAINS = ['OraiBtcMainnet']; @@ -138,6 +158,44 @@ export interface ExtraQueryProposalsResponse { } export interface ExtraExtension { + readonly mint: { + readonly params: () => Promise; + readonly inflation: () => Promise; + readonly annualProvisions: () => Promise; + }; + readonly wasm: { + readonly contractsByCreator: ( + address: string, + page?: PageRequest + ) => Promise; + readonly contractStates: ( + address: string, + page?: PageRequest + ) => Promise; + readonly wasmParams: () => Promise; + readonly listCode: (page?: PageRequest) => Promise; + readonly listContractsByCodeId: ( + codeId: string, + page?: PageRequest, + secret?: boolean + ) => Promise; + readonly getCode: (id: string) => Promise; + readonly getContractInfo: ( + address: string + ) => Promise; + readonly getContractCodeHistory: ( + address: string, + page?: PageRequest + ) => Promise; + readonly queryContractRaw: ( + address: string, + key: Uint8Array + ) => Promise; + readonly queryContractSmart: ( + address: string, + query: JsonObject + ) => Promise; + }; readonly extra: { readonly accounts: (page?: PageRequest) => Promise; readonly votes: ( @@ -155,20 +213,8 @@ export interface ExtraExtension { readonly totalSupply: ( page?: PageRequest ) => Promise; - readonly listCode: ( - page?: PageRequest, - secret?: boolean - ) => Promise; - readonly wasmParams: () => Promise; + readonly bankParams: () => Promise; - readonly contractsByCreator: ( - address: string, - page?: PageRequest - ) => Promise; - readonly contractStates: ( - address: string, - page?: PageRequest - ) => Promise; readonly validatorDelegations: ( validatorAddr: string, @@ -178,6 +224,7 @@ export interface ExtraExtension { readonly getNodeInfo: () => Promise; }; } + function setupExtraExtension(base: QueryClient) { const rpc = createProtobufRpcClient(base); const authQueryService = new AuthQueryClientImpl(rpc); @@ -185,10 +232,297 @@ function setupExtraExtension(base: QueryClient) { const govQueryServiceV1 = new GovQueryClientImplV1(rpc); const bankQueryService = new BankQueryClientImpl(rpc); const wasmQueryService = new WasmQueryClientImpl(rpc); - const secretWasmQueryClientImpl = new SecretWasmQueryClientImpl(rpc); + const secretWasmQueryService = new SecretWasmQueryClientImpl(rpc); const stakingQueryService = new StakingQueryClientImpl(rpc); - const tmQueryClientImpl = new TmQueryClientImpl(rpc); + const mintQueryService = new MintQueryClientImpl(rpc); + const osmoMintQueryService = new OsmoMintQueryClientImpl(rpc); + const tmQueryService = new TmQueryClientImpl(rpc); + const blockchain = useBlockchain(); return { + mint: { + params: async (): Promise => { + switch (blockchain.chainName) { + case 'osmosis': + const osmoRes = await osmoMintQueryService.params(); + return osmoRes.params; + default: + const res = await mintQueryService.Params(); + return res.params; + } + }, + inflation: async (): Promise => { + switch (blockchain.chainName) { + case 'osmosis': + const res = await osmoMintQueryService.params(); + return Decimal.fromAtomics( + ( + (1 - + Number(res.params.distributionProportions.developerRewards)) * + 1e6 + ).toString(), + 6 + ); + default: + const { inflation } = await mintQueryService.Inflation(); + return decodeCosmosSdkDecFromProto(inflation); + } + }, + annualProvisions: async (): Promise => { + const { annualProvisions } = await mintQueryService.AnnualProvisions(); + return decodeCosmosSdkDecFromProto(annualProvisions); + }, + }, + wasm: { + listCode: async (page?: PageRequest): Promise => { + switch (blockchain.chainName) { + case 'injective': { + let endpoint = blockchain.current?.endpoints.grpc?.[0].address!; + const wasmApi = new ChainGrpcWasmApi(endpoint); + const paginationOption = { + reverse: page?.reverse ?? true, + key: page?.key, + countTotal: false, + limit: page?.limit, + }; + + const { codeInfosList, pagination } = + await wasmApi.fetchContractCodes(paginationOption); + + return { + codeInfos: codeInfosList.map((info) => { + return { + codeId: BigInt(info.codeId), + creator: info.creator, + dataHash: + typeof info.dataHash === 'string' + ? fromBase64(info.dataHash) + : info.dataHash, + instantiatePermission: { + permission: AccessType.ACCESS_TYPE_EVERYBODY, + addresses: [] as string[], + address: '', + }, + }; + }), + pagination: { + nextKey: pagination.next + ? fromBase64(pagination.next) + : new Uint8Array(), + total: BigInt(pagination.total), + }, + }; + } + + case 'secret': { + const res = await secretWasmQueryService.Codes({ + pagination: page?.toPagination(), + }); + + return { + codeInfos: res.code_infos.map((codeInfo) => { + return { + codeId: BigInt(codeInfo.code_id), + creator: codeInfo.creator, + dataHash: fromHex(codeInfo.code_hash), + instantiatePermission: { + permission: AccessType.ACCESS_TYPE_EVERYBODY, + address: '', + addresses: [], + }, + }; + }), + }; + } + default: + return await wasmQueryService.Codes({ + pagination: page?.toPagination(), + }); + } + }, + listContractsByCodeId: async ( + codeId: string, + page?: PageRequest + ): Promise => { + switch (blockchain.chainName) { + case 'secret': { + const res = await secretWasmQueryService.ContractsByCodeId({ + code_id: codeId, + }); + return { + contracts: res.contract_infos.map((c) => c.contract_address), + }; + } + default: + return await wasmQueryService.ContractsByCode({ + codeId: BigInt(codeId), + pagination: page?.toPagination(), + }); + } + }, + wasmParams: async () => { + return await wasmQueryService.Params(); + }, + contractsByCreator: async ( + address: string, + page?: PageRequest + ): Promise => { + switch (blockchain.chainName) { + case 'secret': { + return { + contractAddresses: [], + }; + } + default: + return await wasmQueryService.ContractsByCreator({ + pagination: page?.toPagination(), + creatorAddress: address, + }); + } + }, + contractStates: async ( + address: string, + page?: PageRequest + ): Promise => { + switch (blockchain.chainName) { + case 'secret': { + return { + models: [], + }; + } + default: + return await wasmQueryService.AllContractState({ + pagination: page?.toPagination(), + address, + }); + } + }, + getCode: async (codeId: string): Promise => { + switch (blockchain.chainName) { + case 'secret': { + const res = await secretWasmQueryService.Code({ + code_id: codeId, + }); + const codeInfo = res.code_info!; + return { + codeInfo: { + codeId: BigInt(codeInfo.code_id), + creator: codeInfo.creator, + dataHash: fromHex(codeInfo.code_hash), + instantiatePermission: { + permission: AccessType.ACCESS_TYPE_EVERYBODY, + address: '', + addresses: [], + }, + }, + data: res.wasm, + }; + } + default: + return await wasmQueryService.Code({ + codeId: BigInt(codeId), + }); + } + }, + getContractInfo: async ( + address: string + ): Promise => { + switch (blockchain.chainName) { + case 'secret': { + const res = await secretWasmQueryService.ContractInfo({ + contract_address: address, + }); + const contractInfo = res.contract_info!; + return { + address: res.contract_address, + contractInfo: { + codeId: BigInt(contractInfo.code_id), + creator: toBech32('secret', contractInfo.creator), + admin: contractInfo.admin, + label: contractInfo.label, + created: contractInfo.created + ? { + blockHeight: BigInt(contractInfo.created.block_height), + txIndex: BigInt(contractInfo.created.tx_index), + } + : undefined, + ibcPortId: contractInfo.ibc_port_id, + }, + }; + } + default: + return await wasmQueryService.ContractInfo({ + address, + }); + } + }, + + getContractCodeHistory: async ( + address: string, + page?: PageRequest + ): Promise => { + switch (blockchain.chainName) { + case 'secret': + const res = await secretWasmQueryService.ContractHistory({ + contract_address: address, + }); + return { + entries: res.entries.map((entry) => { + return { + operation: entry.operation, + codeId: BigInt(entry.code_id), + updated: entry.updated + ? { + blockHeight: BigInt(entry.updated.block_height), + txIndex: BigInt(entry.updated.tx_index), + } + : undefined, + msg: entry.msg, + }; + }), + }; + default: + return await wasmQueryService.ContractHistory({ + address, + pagination: page?.toPagination(), + }); + } + }, + queryContractRaw: async ( + address: string, + key: Uint8Array + ): Promise => { + switch (blockchain.chainName) { + case 'secret': + const res = await secretWasmQueryService.QuerySecretContract({ + contract_address: address, + query: key, + }); + return { + data: res.data, + }; + default: + return await wasmQueryService.RawContractState({ + address, + queryData: key, + }); + } + }, + + queryContractSmart: async ( + address: string, + query: JsonObject + ): Promise => { + switch (blockchain.chainName) { + case 'secret': + return {}; + default: + return await wasmQueryService.SmartContractState({ + address, + queryData: query, + }); + } + }, + }, extra: { accounts: async (page?: PageRequest) => { return authQueryService.Accounts({ @@ -225,49 +559,7 @@ function setupExtraExtension(base: QueryClient) { pagination: page?.toPagination(), }); }, - listCode: async ( - page?: PageRequest, - secret = false - ): Promise => { - if (secret) { - const res = await secretWasmQueryClientImpl.Codes({ - pagination: page?.toPagination(), - }); - return { - codeInfos: res.code_infos.map((codeInfo) => { - return { - codeId: BigInt(codeInfo.code_id), - creator: codeInfo.creator, - dataHash: fromHex(codeInfo.code_hash), - instantiatePermission: { - permission: AccessType.ACCESS_TYPE_EVERYBODY, - address: '', - addresses: [], - }, - }; - }), - }; - } - return await wasmQueryService.Codes({ - pagination: page?.toPagination(), - }); - }, - wasmParams: async () => { - return await wasmQueryService.Params(); - }, - contractsByCreator: async (address: string, page?: PageRequest) => { - return await wasmQueryService.ContractsByCreator({ - pagination: page?.toPagination(), - creatorAddress: address, - }); - }, - contractStates: async (address: string, page?: PageRequest) => { - return await wasmQueryService.AllContractState({ - pagination: page?.toPagination(), - address, - }); - }, bankParams: async () => { return await bankQueryService.Params(); }, @@ -282,7 +574,7 @@ function setupExtraExtension(base: QueryClient) { }, getNodeInfo: async () => { - return await tmQueryClientImpl.GetNodeInfo(); + return await tmQueryService.GetNodeInfo(); }, }, }; @@ -298,13 +590,11 @@ export class BaseRestClient { AuthExtension & BankExtension & StakingExtension & - MintExtension & GovExtension & IbcExtension & SlashingExtension & DistributionExtension & TxExtension & - WasmExtension & ExtraExtension; constructor(endpoint: string, registry: R, version?: string) { @@ -326,13 +616,11 @@ export class BaseRestClient { setupAuthExtension, setupBankExtension, setupStakingExtension, - setupMintExtension, setupGovExtension, setupIbcExtension, setupSlashingExtension, setupDistributionExtension, setupTxExtension, - setupWasmExtension, setupExtraExtension ); } @@ -1084,7 +1372,7 @@ export class CosmosRestClient extends BaseRestClient { // return this.request(this.registry.mint_inflation, {}); try { const res = await this.queryClient.mint.inflation(); - + // const res = await this.queryClient.mint.inflation(); return res; } catch (ex) { console.log(ex); diff --git a/src/modules/[chain]/cosmwasm/WasmClient.ts b/src/modules/[chain]/cosmwasm/WasmClient.ts index 472ea6ab..8a2ec8eb 100644 --- a/src/modules/[chain]/cosmwasm/WasmClient.ts +++ b/src/modules/[chain]/cosmwasm/WasmClient.ts @@ -1,4 +1,3 @@ -import { ChainGrpcWasmApi, type PaginationOption } from '@injectivelabs/sdk-ts'; import { BaseRestClient } from '@/libs/client'; import { adapter, type AbstractRegistry, type Request } from '@/libs/registry'; @@ -78,74 +77,32 @@ export class WasmRestClient extends BaseRestClient { // if(!pr) pr = new PageRequest() // const query = `?${pr.toQueryString()}` // return this.request(this.registry.cosmwasm_code, {}, /*query*/); - const blockchain = useBlockchain(); - - if (blockchain.chainName === 'injective') { - let endpoint = blockchain.current?.endpoints.grpc?.[0].address; - if (!endpoint) return; - const wasmApi = new ChainGrpcWasmApi(endpoint); - const paginationOption = { - reverse: page?.reverse ?? true, - key: page?.key, - countTotal: false, - limit: page?.limit, - }; - - const { codeInfosList, pagination } = await wasmApi.fetchContractCodes( - paginationOption - ); - - return { - codeInfos: codeInfosList.map((info) => { - return { - codeId: BigInt(info.codeId), - creator: info.creator, - dataHash: - typeof info.dataHash === 'string' - ? fromBase64(info.dataHash) - : info.dataHash, - instantiatePermission: { - permission: AccessType.ACCESS_TYPE_EVERYBODY, - addresses: [] as string[], - address: '', - }, - }; - }), - pagination: { - nextKey: pagination.next - ? fromBase64(pagination.next) - : new Uint8Array(), - total: BigInt(pagination.total), - }, - }; - } page?.setCountTotal(false); - const res = await this.queryClient.extra.listCode( - page, - blockchain.chainName === 'secret' - ); + const res = await this.queryClient.wasm.listCode(page); return res; } - async getWasmCodeById(code_id: string) { + async getWasmCodeById(codeId: string) { // return this.request(this.registry.cosmwasm_code, { code_id }); // `code_id` is a param in above url - const res = await this.queryClient.wasm.getCode(Number(code_id)); + const res = await this.queryClient.wasm.getCode(codeId); return res; } async getWasmCodeContracts(code_id: string, page?: PageRequest) { // if(!page) page = new PageRequest() // const query = `?${page.toQueryString()}` // return this.request(this.registry.cosmwasm_code_id_contracts, { code_id }); + page?.setCountTotal(false); const res = await this.queryClient.wasm.listContractsByCodeId( - Number(code_id) + code_id, + page ); return res; } async getWasmParams() { // return this.request(this.registry.cosmwasm_param, {}); - const res = await this.queryClient.extra.wasmParams(); + const res = await this.queryClient.wasm.wasmParams(); return res; } async getWasmContractInfo(address: string) { @@ -161,7 +118,7 @@ export class WasmRestClient extends BaseRestClient { // if(!page) page = new PageRequest() // const query = `?${page.toQueryString()}` // return this.request(this.registry.cosmwasm_wasm_contracts_creator, { creator_address }); - const res = await this.queryClient.extra.contractsByCreator( + const res = await this.queryClient.wasm.contractsByCreator( creator_address, page ); @@ -203,15 +160,10 @@ export class WasmRestClient extends BaseRestClient { address: string, page: PageRequest ): Promise { - // const blockchain = useBlockchain(); try { - // if ( - // blockchain.chainName === 'osmosis' || - // blockchain.chainName === 'injective' - // ) { page.setCountTotal(false); - // } - const res = await this.queryClient.extra.contractStates(address, page); + + const res = await this.queryClient.wasm.contractStates(address, page); return res; } catch (ex) { console.log(ex); diff --git a/src/stores/useParamsStore.ts b/src/stores/useParamsStore.ts index 4d95c435..05fcd391 100644 --- a/src/stores/useParamsStore.ts +++ b/src/stores/useParamsStore.ts @@ -159,7 +159,7 @@ export const useParamStore = defineStore('paramstore', { // const chainIndex = this.chain.items.findIndex(x => x.subtitle === 'inflation') // this.chain.items[chainIndex].value = `${percent(res)}%` // }) - const res = await this.getMintParam(); + // const res = await this.getMintParam(); }, async handleSlashingParams() { const res = await this.getSlashingParams(); diff --git a/yarn.lock b/yarn.lock index 2d5b70b5..a84961d9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1507,42 +1507,7 @@ ledger-cosmos-js "^2.1.8" semver "^7.5.2" -"@cosmjs/math@0.27.1": - version "0.27.1" - resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.27.1.tgz#be78857b008ffc6b1ed6fecaa1c4cd5bc38c07d7" - integrity sha512-cHWVjmfIjtRc7f80n7x+J5k8pe+vTVTQ0lA82tIxUgqUvgS6rogPP/TmGtTiZ4+NxWxd11DUISY6gVpr18/VNQ== - dependencies: - bn.js "^5.2.0" - -"@cosmjs/math@^0.29.3", "@cosmjs/math@^0.29.5": - version "0.29.5" - resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.29.5.tgz#722c96e080d6c2b62215ce9f4c70da7625b241b6" - integrity sha512-2GjKcv+A9f86MAWYLUkjhw1/WpRl2R1BTb3m9qPG7lzMA7ioYff9jY5SPCfafKdxM4TIQGxXQlYGewQL16O68Q== - dependencies: - bn.js "^5.2.0" - -"@cosmjs/math@^0.30.0", "@cosmjs/math@^0.30.1": - version "0.30.1" - resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.30.1.tgz#8b816ef4de5d3afa66cb9fdfb5df2357a7845b8a" - integrity sha512-yaoeI23pin9ZiPHIisa6qqLngfnBR/25tSaWpkTm8Cy10MX70UF5oN4+/t1heLaM6SSmRrhk3psRkV4+7mH51Q== - dependencies: - bn.js "^5.2.0" - -"@cosmjs/math@^0.31.1": - version "0.31.1" - resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.31.1.tgz#74c02cf237c2996b77661b636b014168b18d95e6" - integrity sha512-kiuHV6m6DSB8/4UV1qpFhlc4ul8SgLXTGRlYkYiIIP4l0YNeJ+OpPYaOlEgx4Unk2mW3/O2FWYj7Jc93+BWXng== - dependencies: - bn.js "^5.2.0" - -"@cosmjs/math@^0.31.3": - version "0.31.3" - resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.31.3.tgz#767f7263d12ba1b9ed2f01f68d857597839fd957" - integrity sha512-kZ2C6glA5HDb9hLz1WrftAjqdTBb3fWQsRR+Us2HsjAYdeE6M3VdXMsYCP5M3yiihal1WDwAY2U7HmfJw7Uh4A== - dependencies: - bn.js "^5.2.0" - -"@cosmjs/math@^0.32.2": +"@cosmjs/math@0.27.1", "@cosmjs/math@^0.29.3", "@cosmjs/math@^0.29.5", "@cosmjs/math@^0.30.0", "@cosmjs/math@^0.30.1", "@cosmjs/math@^0.31.1", "@cosmjs/math@^0.31.3", "@cosmjs/math@^0.32.2": version "0.32.2" resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.32.2.tgz#4522312769197e132679e4960862bcec0eed4cb8" integrity sha512-b8+ruAAY8aKtVKWSft2IvtCVCUH1LigIlf9ALIiY8n9jtM4kMASiaRbQ/27etnSAInV88IaezKK9rQZrtxTjcw==