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

fix: permissioned upload condition #327

Merged
merged 8 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Features

- [#327](https://github.com/alleslabs/celatone-frontend/pull/327) Update logic to enable upload wasm code
- [#317](https://github.com/alleslabs/celatone-frontend/pull/317) Add amplitude for proposal list page and pagination

### Improvements
Expand Down
41 changes: 0 additions & 41 deletions src/lib/components/SwitchToTestnet.tsx

This file was deleted.

98 changes: 63 additions & 35 deletions src/lib/pages/deploy/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { AlertProps } from "@chakra-ui/react";
import {
Alert,
AlertDescription,
Expand All @@ -6,31 +7,71 @@ import {
Button,
Text,
} from "@chakra-ui/react";
import { useWallet } from "@cosmos-kit/react";
import { useRouter } from "next/router";
import { useEffect } from "react";
import { useEffect, useMemo } from "react";

import {
useInternalNavigate,
useSelectChain,
useCurrentNetwork,
} from "lib/app-provider";
import { useInternalNavigate } from "lib/app-provider";
import { ButtonCard } from "lib/components/ButtonCard";
import { ConnectWalletAlert } from "lib/components/ConnectWalletAlert";
import { CustomIcon } from "lib/components/icon";
import { Loading } from "lib/components/Loading";
import { Stepper } from "lib/components/stepper";
import WasmPageContainer from "lib/components/WasmPageContainer";
import { getChainNameByNetwork } from "lib/data";
import { AmpEvent, AmpTrack } from "lib/services/amplitude";
import { useUploadAccessParams } from "lib/services/proposalService";
import type { HumanAddr } from "lib/types";
import { AccessConfigPermission } from "lib/types";

const getAlertContent = (
enabled: boolean
): { variant: AlertProps["variant"]; icon: JSX.Element; description: string } =>
enabled
? {
variant: "success",
icon: (
<CustomIcon
name="check-circle-solid"
color="success.main"
boxSize={4}
/>
),
description: "Your address is allowed to directly upload Wasm files",
}
: {
variant: "violet",
icon: (
<CustomIcon
name="info-circle-solid"
color="violet.light"
boxSize={4}
/>
),
description:
"The current network is a permissioned CosmWasm network. Only whitelisted addresses can directly upload Wasm files.",
};

const Deploy = () => {
const { isMainnet } = useCurrentNetwork();
const router = useRouter();
const navigate = useInternalNavigate();
const selectChain = useSelectChain();
const { address } = useWallet();
const { data, isFetching } = useUploadAccessParams();

const isPermissionedNetwork =
data?.permission !== AccessConfigPermission.EVERYBODY;

const enableUpload = useMemo(() => {
if (!isPermissionedNetwork) return true;
return Boolean(data?.addresses?.includes(address as HumanAddr));
}, [data, address, isPermissionedNetwork]);

useEffect(() => {
if (router.isReady) AmpTrack(AmpEvent.TO_DEPLOY);
}, [router.isReady]);

if (isFetching) return <Loading />;

const { variant, icon, description } = getAlertContent(enableUpload);
return (
<WasmPageContainer>
<Text variant="body1" color="text.dark" mb={3} fontWeight={700}>
Expand All @@ -40,45 +81,32 @@ const Deploy = () => {
<Heading as="h5" variant="h5" my="48px">
Select Deploy Option
</Heading>
{isMainnet && (
<Alert variant="violet" mb="16px" alignItems="flex-start" gap="1">
<CustomIcon name="info-circle-solid" boxSize="20px" />
<AlertDescription>
Uploading new Wasm files on permissioned chains is coming soon to
Celatone. Currently, you can upload codes and instantiate contracts
without permission on testnet.
</AlertDescription>
<ConnectWalletAlert
subtitle="You need to connect wallet to proceed this action"
mb={4}
/>
{address && (
<Alert variant={variant} mb={4} alignItems="flex-start" gap={2}>
{icon}
<AlertDescription>{description}</AlertDescription>
</Alert>
)}
<ButtonCard
title="Upload new WASM File"
description={
isMainnet ? (
<Flex fontSize="14px" gap={1}>
<Text color="text.disabled">
Currently available on testnet only.
</Text>
<Text
color="honeydew.main"
_hover={{ textDecoration: "underline" }}
cursor="pointer"
onClick={() => selectChain(getChainNameByNetwork("testnet"))}
>
Switch to testnet
</Text>
</Flex>
) : (
"Store a new Wasm file on-chain"
)
isPermissionedNetwork
? "Available for whitelisted addresses only"
: "Store a new Wasm file on-chain"
}
disabled={isMainnet}
disabled={!enableUpload || !address}
onClick={() => navigate({ pathname: "/upload" })}
mb="16px"
/>
<ButtonCard
title="Use existing Code IDs"
description="Input code ID or select from previously stored or saved codes"
onClick={() => navigate({ pathname: "/instantiate" })}
disabled={!address}
/>
<Flex justify="center" w="100%" mt="32px">
<Button
Expand Down
7 changes: 3 additions & 4 deletions src/lib/services/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ export const fetchGovVotingParams = (

export interface UploadAccess {
permission: AccessConfigPermission;
address: Addr;
addresses?: Addr[];
}

export const fetchGovUploadAccessParams = async (
lcdEndpoint: string
): Promise<UploadAccess> =>
axios
.get(
`${lcdEndpoint}/cosmos/params/v1beta1/params?subspace=wasm&key=uploadAccess`
)
.then(({ data }) => JSON.parse(data.param.value));
.get(`${lcdEndpoint}/cosmwasm/wasm/v1/codes/params`)
.then(({ data }) => data.params.code_upload_access);
9 changes: 9 additions & 0 deletions src/lib/services/proposalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,12 @@ export const useGovParams = (): UseQueryResult<{
keepPreviousData: true,
});
};

export const useUploadAccessParams = (): UseQueryResult<UploadAccess> => {
const lcdEndpoint = useLCDEndpoint();
return useQuery(
["upload_access", lcdEndpoint],
() => fetchGovUploadAccessParams(lcdEndpoint),
{ keepPreviousData: true, refetchOnWindowFocus: false }
);
};