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: load all contract detail #37

Merged
merged 6 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ interface AppContextInterface<
ContractAddress,
Constants extends AppConstants = AppConstants
> {
chainName: string;
chainId: string;
songwongtp marked this conversation as resolved.
Show resolved Hide resolved
chainGasPrice: ChainGasPrice;
contractAddress: ContractAddress;
constants: Constants;
Expand All @@ -48,6 +50,8 @@ interface AppContextInterface<

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const AppContext = createContext<AppContextInterface<any, any>>({
chainName: "",
chainId: "",
chainGasPrice: { denom: "", gasPrice: "0" as U<Token> },
contractAddress: {},
constants: { gasAdjustment: 0 },
Expand All @@ -69,6 +73,11 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
const { setCodeUserKey, isCodeUserKeyExist } = useCodeStore();
const { setContractUserKey, isContractUserKeyExist } = useContractStore();

const chainId = useMemo(() => {
if (!currentChainRecord) return "";
return currentChainRecord.chain.chain_id;
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
}, [currentChainRecord]);

const chainGasPrice = useMemo(() => {
if (
!currentChainRecord ||
Expand Down Expand Up @@ -99,12 +108,15 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
AppContextInterface<ContractAddress, Constants>
>(() => {
return {
chainName: currentChainName,
chainId,
chainGasPrice,
contractAddress: contractAddress(currentChainName),
constants,
...chainBoundStates,
};
}, [
chainId,
chainGasPrice,
contractAddress,
currentChainName,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/modal/code/SaveNewCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
MAX_CODE_DESCRIPTION_LENGTH,
} from "lib/data";
import { useCodeStore, useEndpoint, useUserKey } from "lib/hooks";
import { getCodeIdInfo } from "lib/services/contract";
import { getCodeIdInfo } from "lib/services/code";

interface ModalProps {
buttonProps: ButtonProps;
Expand Down
8 changes: 4 additions & 4 deletions src/lib/components/modal/contract/SaveNewContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "lib/data";
import { useContractStore, useEndpoint } from "lib/hooks";
import { useHandleContractSave } from "lib/hooks/useHandleSave";
import { queryContractWithTime } from "lib/services/contract";
import { queryInstantiateInfo } from "lib/services/contract";
import type { Option, RpcContractError } from "lib/types";
import { formatSlugName } from "lib/utils";

Expand Down Expand Up @@ -62,8 +62,8 @@ export function SaveNewContract({ list, buttonProps }: SaveNewContractProps) {

// TODO: Abstract query
const { refetch } = useQuery(
["query", "contractWithTime", contractAddress],
async () => queryContractWithTime(endpoint, contractAddress),
["query", "instantiateInfo", contractAddress],
async () => queryInstantiateInfo(endpoint, contractAddress),
{
enabled: false,
retry: false,
Expand All @@ -72,7 +72,7 @@ export function SaveNewContract({ list, buttonProps }: SaveNewContractProps) {
onSuccess(data) {
setInstantiator(data.instantiator);
setLabel(data.label);
setCreated(data.created);
setCreated(data.createdTime);
setName(data.label);
setStatus({
state: "success",
Expand Down
11 changes: 11 additions & 0 deletions src/lib/data/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ export const getInstantiatedListByUserQueryDocument = graphql(`
}
}
`);

export const getInstantiateDetailByContractQueryDocument = graphql(`
query getInstantiateDetailByContractQueryDocument($contractAddr: String!) {
contracts(where: { address: { _eq: $contractAddr } }) {
init_msg
transaction {
hash
}
}
}
`);
5 changes: 5 additions & 0 deletions src/lib/gql/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const documents = {
types.GetInstantiatedCountByUserQueryDocumentDocument,
"\n query getInstantiatedListByUserQueryDocument($walletAddr: String!) {\n contracts(\n where: { transaction: { account: { address: { _eq: $walletAddr } } } }\n limit: 500\n offset: 0\n order_by: { transaction: { block: { timestamp: desc } } }\n ) {\n label\n address\n transaction {\n block {\n timestamp\n }\n }\n }\n }\n":
types.GetInstantiatedListByUserQueryDocumentDocument,
"\n query getInstantiateDetailByContractQueryDocument($contractAddr: String!) {\n contracts(where: { address: { _eq: $contractAddr } }) {\n init_msg\n transaction {\n hash\n }\n }\n }\n":
types.GetInstantiateDetailByContractQueryDocumentDocument,
};

export function graphql(
Expand All @@ -25,6 +27,9 @@ export function graphql(
export function graphql(
source: "\n query getInstantiatedListByUserQueryDocument($walletAddr: String!) {\n contracts(\n where: { transaction: { account: { address: { _eq: $walletAddr } } } }\n limit: 500\n offset: 0\n order_by: { transaction: { block: { timestamp: desc } } }\n ) {\n label\n address\n transaction {\n block {\n timestamp\n }\n }\n }\n }\n"
): typeof documents["\n query getInstantiatedListByUserQueryDocument($walletAddr: String!) {\n contracts(\n where: { transaction: { account: { address: { _eq: $walletAddr } } } }\n limit: 500\n offset: 0\n order_by: { transaction: { block: { timestamp: desc } } }\n ) {\n label\n address\n transaction {\n block {\n timestamp\n }\n }\n }\n }\n"];
export function graphql(
source: "\n query getInstantiateDetailByContractQueryDocument($contractAddr: String!) {\n contracts(where: { address: { _eq: $contractAddr } }) {\n init_msg\n transaction {\n hash\n }\n }\n }\n"
): typeof documents["\n query getInstantiateDetailByContractQueryDocument($contractAddr: String!) {\n contracts(where: { address: { _eq: $contractAddr } }) {\n init_msg\n transaction {\n hash\n }\n }\n }\n"];

export function graphql(source: string): unknown;
export function graphql(source: string) {
Expand Down
98 changes: 98 additions & 0 deletions src/lib/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4722,6 +4722,19 @@ export type GetInstantiatedListByUserQueryDocumentQuery = {
}>;
};

export type GetInstantiateDetailByContractQueryDocumentQueryVariables = Exact<{
contractAddr: Scalars["String"];
}>;

export type GetInstantiateDetailByContractQueryDocumentQuery = {
__typename?: "query_root";
contracts: Array<{
__typename?: "contracts";
init_msg?: string | null;
transaction?: { __typename?: "transactions"; hash: any } | null;
}>;
};

export const GetCodeListByUserQueryDocument = {
kind: "Document",
definitions: [
Expand Down Expand Up @@ -5200,3 +5213,88 @@ export const GetInstantiatedListByUserQueryDocumentDocument = {
GetInstantiatedListByUserQueryDocumentQuery,
GetInstantiatedListByUserQueryDocumentQueryVariables
>;
export const GetInstantiateDetailByContractQueryDocumentDocument = {
kind: "Document",
definitions: [
{
kind: "OperationDefinition",
operation: "query",
name: {
kind: "Name",
value: "getInstantiateDetailByContractQueryDocument",
},
variableDefinitions: [
{
kind: "VariableDefinition",
variable: {
kind: "Variable",
name: { kind: "Name", value: "contractAddr" },
},
type: {
kind: "NonNullType",
type: {
kind: "NamedType",
name: { kind: "Name", value: "String" },
},
},
},
],
selectionSet: {
kind: "SelectionSet",
selections: [
{
kind: "Field",
name: { kind: "Name", value: "contracts" },
arguments: [
{
kind: "Argument",
name: { kind: "Name", value: "where" },
value: {
kind: "ObjectValue",
fields: [
{
kind: "ObjectField",
name: { kind: "Name", value: "address" },
value: {
kind: "ObjectValue",
fields: [
{
kind: "ObjectField",
name: { kind: "Name", value: "_eq" },
value: {
kind: "Variable",
name: { kind: "Name", value: "contractAddr" },
},
},
],
},
},
],
},
},
],
selectionSet: {
kind: "SelectionSet",
selections: [
{ kind: "Field", name: { kind: "Name", value: "init_msg" } },
{
kind: "Field",
name: { kind: "Name", value: "transaction" },
selectionSet: {
kind: "SelectionSet",
selections: [
{ kind: "Field", name: { kind: "Name", value: "hash" } },
],
},
},
],
},
},
],
},
},
],
} as unknown as DocumentNode<
GetInstantiateDetailByContractQueryDocumentQuery,
GetInstantiateDetailByContractQueryDocumentQueryVariables
>;
62 changes: 60 additions & 2 deletions src/lib/model/contract.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import type { Coin } from "@cosmjs/stargate";
import { useWallet } from "@cosmos-kit/react";
import { useQuery } from "@tanstack/react-query";

import { useApp } from "lib/app-provider";
import { INSTANTIATED_LIST_NAME } from "lib/data";
import { useContractStore } from "lib/hooks";
import { useContractStore, useEndpoint } from "lib/hooks";
import type { InstantiateInfo, PublicInfo } from "lib/services/contract";
import {
queryPublicInfo,
queryContractBalances,
queryInstantiateInfo,
} from "lib/services/contract";
import {
useInstantiatedCountByUserQuery,
useInstantiateDetailByContractQuery,
useInstantiatedListByUserQuery,
} from "lib/services/contractService";
import type { ContractListInfo } from "lib/stores/contract";
import type { ContractInfo, ContractListInfo } from "lib/stores/contract";
import { formatSlugName } from "lib/utils";

interface ContractDetail {
instantiateInfo: InstantiateInfo | undefined;
contractInfo: ContractInfo | undefined;
publicInfo: PublicInfo | undefined;
balances: Coin[];
initMsg: string;
initTxHash?: string;
initProposalId?: number;
}

export const useInstantiatedByMe = (enable: boolean): ContractListInfo => {
const { address } = useWallet();
const { data: contracts = [] } = useInstantiatedListByUserQuery(
Expand Down Expand Up @@ -48,3 +68,41 @@ export const useInstantiatedMockInfoByMe = (): ContractListInfo => {
isContractRemovable: false,
};
};

export const useContractDetail = (contract: string): ContractDetail => {
const { getContractInfo } = useContractStore();
const { chainName, chainId } = useApp();
songwongtp marked this conversation as resolved.
Show resolved Hide resolved
const endpoint = useEndpoint();

const { data: instantiateInfo } = useQuery(
["query", "instantiateInfo", contract],
async () => queryInstantiateInfo(endpoint, contract)
);
const { data: contractBalances = { balances: [] } } = useQuery(
["query", "contractBalances", contract],
async () => queryContractBalances(endpoint, contract)
);
const { data: publicInfo } = useQuery(
["query", "publicInfo", contract],
async () => queryPublicInfo(chainName, chainId, contract)
);

const contractInfo = getContractInfo(contract);
const {
data: instantiateDetail = {
initMsg: "{}",
},
} = useInstantiateDetailByContractQuery(contract);
// TODO: contract proposal id
const proposalId = undefined;

return {
instantiateInfo,
contractInfo,
publicInfo,
balances: contractBalances.balances,
initMsg: instantiateDetail.initMsg,
initTxHash: instantiateDetail.initTxHash,
initProposalId: proposalId,
};
};
2 changes: 1 addition & 1 deletion src/lib/pages/execute/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const Execute = () => {
if (!contractState) {
try {
const onChainDetail = await queryContract(endpoint, contractAddr);
setContractName(onChainDetail.result?.label);
setContractName(onChainDetail.contract_info.label);
} catch {
setContractName("Invalid Contract");
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/pages/query/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const Query = () => {
if (!contractState) {
try {
const onChainDetail = await queryContract(endpoint, contractAddr);
setName(onChainDetail.result?.label);
setName(onChainDetail.contract_info.label);
} catch {
setName("Invalid Contract");
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/services/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import axios from "axios";

songwongtp marked this conversation as resolved.
Show resolved Hide resolved
export const getCodeIdInfo = async (endpoint: string, id: number) => {
songwongtp marked this conversation as resolved.
Show resolved Hide resolved
const { data } = await axios.get(`${endpoint}/cosmwasm/wasm/v1/code/${id}`);
return data;
};
Loading