Skip to content

Commit

Permalink
feat: work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpeach committed Jan 11, 2023
1 parent da6d5ba commit d74bc78
Show file tree
Hide file tree
Showing 30 changed files with 233 additions and 115 deletions.
7 changes: 5 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ export const CELATONE_APP_CONTRACT_ADDRESS = (
}
};

export const LCD_ENDPOINT: Record<string, string> = {
export const FALLBACK_LCD_ENDPOINT: Record<string, string> = {
osmosis: "https://lcd.osmosis.zone",
osmosistestnet: "https://lcd-test.osmosis.zone",
terra2: "https://phoenix-lcd.terra.dev",
terra2testnet: "https://pisco-lcd.terra.dev",
};

export const MAX_FILE_SIZE = 800_000;
Expand All @@ -50,9 +52,10 @@ export const MSG_TYPE_URL = {

export const CELATONE_CONSTANTS: CelatoneConstants = {
gasAdjustment: 1.6,
lcdEndpoint: LCD_ENDPOINT,
maxFileSize: MAX_FILE_SIZE,
msgTypeUrl: MSG_TYPE_URL,
};

export const DUMMY_MNEMONIC = process.env.NEXT_PUBLIC_DUMMY_MNEMONIC;

export const SUPPORTED_CHAIN = process.env.NEXT_PUBLIC_SUPPORTED_CHAIN;
4 changes: 2 additions & 2 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getExplorerUserAddressUrl,
} from "lib/app-fns/explorer";
import { LoadingOverlay } from "lib/components/LoadingOverlay";
import { DEFAULT_ADDRESS, DEFAULT_CHAIN } from "lib/data";
import { DEFAULT_ADDRESS, getDefaultChainName } from "lib/data";
import { useCodeStore, useContractStore } from "lib/hooks";
import type { ChainGasPrice, Token, U } from "lib/types";
import { formatUserKey } from "lib/utils";
Expand Down Expand Up @@ -113,7 +113,7 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
}, [currentChainName, setCodeUserKey, setContractUserKey]);

useEffect(() => {
setCurrentChain(DEFAULT_CHAIN);
setCurrentChain(getDefaultChainName());
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/app-provider/hooks/useQueryCmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from "@tanstack/react-query";
import type { AxiosError } from "axios";
import { useState } from "react";

import { useEndpoint } from "lib/hooks";
import { useLCDEndpoint } from "lib/hooks";
import { queryData } from "lib/services/contract";
import type { ContractAddr, RpcQueryError } from "lib/types";

Expand All @@ -11,7 +11,7 @@ interface UseQueryCmdsProps {
}
export const useQueryCmds = ({ contractAddress }: UseQueryCmdsProps) => {
const [queryCmds, setQueryCmds] = useState<[string, string][]>([]);
const endpoint = useEndpoint();
const endpoint = useLCDEndpoint();

const { isFetching } = useQuery(
["query", "cmds", endpoint, contractAddress, '{"": {}}'],
Expand Down
7 changes: 3 additions & 4 deletions src/lib/app-provider/query-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { GraphQLClient } from "graphql-request";

export const GRAPH_URL: Record<string, string> = {
osmosistestnet: "https://osmosis-testnet-graphql.alleslabs.dev/v1/graphql",
/**
* Revisit graphql for terra2 mainnet and keplr key for testnet (currently non-existent)
* Revisit graphql for terra2 mainnet and osmosis mainnet
*/
terra2: "https://terra-testnet-graphql.alleslabs.dev/console",
terra2testnet: "https://terra-testnet-graphql.alleslabs.dev/console",
osmosistestnet: "https://osmosis-testnet-graphql.alleslabs.dev/v1/graphql",
terra2testnet: "https://terra-testnet-graphql.alleslabs.dev/v1/graphql",
};

export const getIndexerGraphClient = (currentChainName: string) =>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/ContractSelectSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useEffect, useMemo } from "react";
import { useForm } from "react-hook-form";
import { MdMode, MdOutlineBookmarkBorder } from "react-icons/md";

import { useContractStore, useEndpoint, useMobile } from "lib/hooks";
import { useContractStore, useLCDEndpoint, useMobile } from "lib/hooks";
import { queryInstantiateInfo } from "lib/services/contract";
import type { ContractInfo } from "lib/stores/contract";
import type { ContractAddr, Option } from "lib/types";
Expand Down Expand Up @@ -103,7 +103,7 @@ export const ContractSelectSection = observer(
({ contractAddress, onContractSelect }: ContractSelectSectionProps) => {
const { getContractInfo } = useContractStore();
const isMobile = useMobile();
const endpoint = useEndpoint();
const endpoint = useLCDEndpoint();

const contractInfo = getContractInfo(contractAddress);
const {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/modal/code/SaveNewCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
getMaxCodeDescriptionLengthError,
MAX_CODE_DESCRIPTION_LENGTH,
} from "lib/data";
import { useCodeStore, useEndpoint } from "lib/hooks";
import { useCodeStore, useLCDEndpoint } from "lib/hooks";
import { getCodeIdInfo } from "lib/services/code";

interface ModalProps {
Expand Down Expand Up @@ -50,7 +50,7 @@ export function SaveNewCodeModal({ buttonProps }: ModalProps) {
const toast = useToast();
const { isCodeIdExist, saveNewCode, updateCodeInfo, getCodeLocalInfo } =
useCodeStore();
const endpoint = useEndpoint();
const endpoint = useLCDEndpoint();

const { refetch, isFetching, isRefetching } = useQuery(
["query", endpoint, codeId],
Expand Down
8 changes: 6 additions & 2 deletions src/lib/components/modal/contract/SaveNewContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { ActionModal } from "lib/components/modal/ActionModal";
import type { OffchainDetail } from "lib/components/OffChainForm";
import { OffChainForm } from "lib/components/OffChainForm";
import { DEFAULT_RPC_ERROR, INSTANTIATED_LIST_NAME } from "lib/data";
import { useContractStore, useEndpoint, useValidateAddress } from "lib/hooks";
import {
useContractStore,
useLCDEndpoint,
useValidateAddress,
} from "lib/hooks";
import { useHandleContractSave } from "lib/hooks/useHandleSave";
import { queryInstantiateInfo } from "lib/services/contract";
import type { ContractAddr, LVPair, RpcQueryError } from "lib/types";
Expand All @@ -31,7 +35,7 @@ interface SaveNewContractProps {
buttonProps: ButtonProps;
}
export function SaveNewContract({ list, buttonProps }: SaveNewContractProps) {
const endpoint = useEndpoint();
const endpoint = useLCDEndpoint();
const { getContractInfo } = useContractStore();
const { validateContractAddress } = useValidateAddress();

Expand Down
8 changes: 6 additions & 2 deletions src/lib/components/modal/select-contract/SelectContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { MdChevronLeft } from "react-icons/md";

import { useCelatoneApp } from "lib/app-provider";
import { DEFAULT_RPC_ERROR } from "lib/data";
import { useContractStore, useEndpoint, useValidateAddress } from "lib/hooks";
import {
useContractStore,
useLCDEndpoint,
useValidateAddress,
} from "lib/hooks";
import { useInstantiatedByMe } from "lib/model/contract";
import { queryContract } from "lib/services/contract";
import type { ContractAddr, RpcQueryError } from "lib/types";
Expand Down Expand Up @@ -55,7 +59,7 @@ export const SelectContract = ({
const contractLists = [useInstantiatedByMe(true), ...getContractLists()];
const contractList = contractLists.find((item) => item.slug === listSlug);

const endpoint = useEndpoint();
const endpoint = useLCDEndpoint();

const resetOnClose = () => {
setListSlug("");
Expand Down
89 changes: 89 additions & 0 deletions src/lib/config/terra2testnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Chain, AssetList } from "@chain-registry/types";

export const terra2testnet: Chain = {
$schema: "../../chain.schema.json",
chain_name: "terra2testnet",
status: "live",
network_type: "testnet",
pretty_name: "Terra Testnet",
chain_id: "pisco-1",
bech32_prefix: "terra",
daemon_name: "terrad",
node_home: "$HOME/.terrad",
slip44: 330,
fees: {
fee_tokens: [
{
denom: "uluna",
fixed_min_gas_price: 0,
low_gas_price: 0.015,
average_gas_price: 0.015,
high_gas_price: 0.015,
},
],
},
staking: {
staking_tokens: [
{
denom: "uluna",
},
],
},
codebase: {
git_repo: "https://github.com/terra-money/core",
recommended_version: "v2.2.0",
compatible_versions: ["v2.2.0"],
cosmos_sdk_version: "0.45.10",
tendermint_version: "0.34.22",
cosmwasm_version: "0.27",
cosmwasm_enabled: true,
},
apis: {
rpc: [
{
address: "https://terra-testnet-rpc.polkachu.com/",
},
],
rest: [
{
address: "https://pisco-lcd.terra.dev/",
},
],
grpc: [],
},
logo_URIs: {
png: "https://raw.githubusercontent.com/cosmos/chain-registry/master/terra2/images/luna.png",
},
keywords: ["testnet"],
};

export const terra2testnetAssets: AssetList = {
$schema: "../../assetlist.schema.json",
chain_name: "terra2testnet",
assets: [
{
description: "The native token of Terra",
denom_units: [
{
denom: "uluna",
exponent: 0,
aliases: [],
},
{
denom: "luna",
exponent: 6,
aliases: [],
},
],
base: "uluna",
name: "Luna",
display: "luna",
symbol: "LUNA",
logo_URIs: {
png: "https://raw.githubusercontent.com/cosmos/chain-registry/master/terra2/images/luna.png",
},
coingecko_id: "terra",
keywords: ["staking"],
},
],
};
23 changes: 23 additions & 0 deletions src/lib/data/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { SUPPORTED_CHAIN } from "env";

export const TERRA_CHAIN_NAMES = ["terra2", "terra2testnet"];

export const OSMOSIS_CHAIN_NAMES = ["osmosis", "osmosistestnet"];

export const ALL_CHAIN_NAMES = [...TERRA_CHAIN_NAMES, ...OSMOSIS_CHAIN_NAMES];

export const getSupportedChainNames = () => {
switch (SUPPORTED_CHAIN) {
case "terra":
return TERRA_CHAIN_NAMES;
case "osmosis":
return OSMOSIS_CHAIN_NAMES;
default:
throw new Error(`Unsupported chain: ${SUPPORTED_CHAIN}`);
}
};

export const getDefaultChainName = () => {
const supportedChainNames = getSupportedChainNames();
return supportedChainNames[0];
};
5 changes: 0 additions & 5 deletions src/lib/data/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,3 @@ export const typeUrlDict = {
};

export const DEFAULT_RPC_ERROR = "Invalid format, or Something went wrong";
// TODO
export const LCD_ENDPOINT: Record<string, string> = {
osmosis: "https://lcd.osmosis.zone",
osmosistestnet: "https://lcd-test.osmosis.zone",
};
6 changes: 0 additions & 6 deletions src/lib/data/graphql.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./constant";
export * from "./wallet";
export * from "./chain";
7 changes: 0 additions & 7 deletions src/lib/data/wallet.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/lib/env.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/lib/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "./store";
export * from "./useMediaQuery";
export * from "./useToast";
export * from "./useEndpoint";
export * from "./useLCDEndpoint";
export * from "./useUserKey";
export * from "./useDummyWallet";
export * from "./useAddress";
8 changes: 8 additions & 0 deletions src/lib/hooks/useAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ const addressLengthMap: {
43: "user_address",
63: "contract_address",
},
terra2: {
44: "user_address",
64: "contract_address",
},
terra2testnet: {
44: "user_address",
64: "contract_address",
},
};

const getAddressTypeByLength = (
Expand Down
8 changes: 0 additions & 8 deletions src/lib/hooks/useEndpoint.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/lib/hooks/useLCDEndpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useWallet } from "@cosmos-kit/react";

import { FALLBACK_LCD_ENDPOINT } from "env";

export const useLCDEndpoint = () => {
const { currentChainRecord, currentChainName } = useWallet();

return (
currentChainRecord?.preferredEndpoints?.rest?.at(0) ??
FALLBACK_LCD_ENDPOINT[currentChainName]
);
};
31 changes: 15 additions & 16 deletions src/lib/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Link from "next/link";
import { FiChevronDown } from "react-icons/fi";

import { WalletSection } from "lib/components/Wallet";
import { CHAIN_NAMES } from "lib/data";
import { getSupportedChainNames } from "lib/data";

const Header = () => {
const { currentChainRecord, setCurrentChain, getChainRecord } = useWallet();
Expand Down Expand Up @@ -64,21 +64,20 @@ const Header = () => {
</Flex>
</MenuButton>
<MenuList>
{CHAIN_NAMES.map((chainName) => {
return (
<MenuItem
key={chainName}
onClick={() => setCurrentChain(chainName)}
flexDirection="column"
alignItems="flex-start"
>
<Text>{getChainRecord(chainName)?.chain.pretty_name}</Text>
<Text color="text.dark" fontSize="sm">
{getChainRecord(chainName)?.chain.chain_id}
</Text>
</MenuItem>
);
})}
{getSupportedChainNames().map((chainName) => (
<MenuItem
key={chainName}
onClick={() => setCurrentChain(chainName)}
disabled={currentChainRecord?.chain.chain_id === chainName}
flexDirection="column"
alignItems="flex-start"
>
<Text>{getChainRecord(chainName)?.chain.pretty_name}</Text>
<Text color="text.dark" fontSize="sm">
{getChainRecord(chainName)?.chain.chain_id}
</Text>
</MenuItem>
))}
</MenuList>
</Menu>
<WalletSection />
Expand Down
Loading

0 comments on commit d74bc78

Please sign in to comment.