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

refactor: funds filtering and sorting logic, make the whole contract … #181

Merged
merged 1 commit into from
Feb 10, 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 @@ -114,6 +114,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

- [#181](https://github.com/alleslabs/celatone-frontend/pull/181) Refactor funds filtering logic and sort by denom, enable clicking the whole row of contract list when its readOnly
- [#171](https://github.com/alleslabs/celatone-frontend/pull/171) Change gql query for better speed, remove unwrap and default values, and better handle data rendering
- [#177](https://github.com/alleslabs/celatone-frontend/pull/177) Handle instantiate render: tx hash, proposal, genesis, and data not available case
- [#176](https://github.com/alleslabs/celatone-frontend/pull/176) Adjust contract table fraction, limit tag render, full ellipsis contract desc/name length
Expand Down
32 changes: 25 additions & 7 deletions src/lib/components/table/EditableCell.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { InfoIcon } from "@chakra-ui/icons";
import type { TextProps } from "@chakra-ui/react";
import {
Flex,
Text,
Expand All @@ -17,13 +18,27 @@ interface EditableCellProps {
defaultValue: string;
maxLength: number;
tooltip?: string;
isReadOnly?: boolean;
onSave?: (value?: string) => void;
}

const getInputValueTextProps = (
isShowInputValue: boolean,
inputValue: string,
defaultValue: string
): Pick<TextProps, "fontWeight" | "color" | "children"> => {
if (isShowInputValue) {
return { fontWeight: 600, color: "text.main", children: inputValue };
}
return { fontWeight: 400, color: "text.dark", children: defaultValue };
};

export const EditableCell = ({
initialValue = "",
defaultValue,
maxLength,
tooltip,
isReadOnly,
onSave,
}: EditableCellProps) => {
const [inputValue, setInputValue] = useState(initialValue);
Expand Down Expand Up @@ -141,26 +156,29 @@ export const EditableCell = ({
maxW="full"
align="center"
gap={2}
onClick={(e) => e.stopPropagation()}
onClick={(e) => {
if (!isReadOnly) e.stopPropagation();
}}
>
<Text
variant="body2"
className="ellipsis"
maxW="full"
fontWeight={isShowInputValue ? "600" : "400"}
color={isShowInputValue ? "text.main" : "text.dark"}
onMouseOver={handleMouseEnterText}
ref={textRef}
>
{isShowInputValue ? inputValue : defaultValue}
</Text>
{...getInputValueTextProps(
isShowInputValue,
inputValue,
defaultValue
)}
/>
{showName && (
<Text
variant="body2"
top="-16px"
left="-16px"
borderRadius="8px"
bg="pebble.800"
bg={isReadOnly ? "pebble.700" : "pebble.800"}
whiteSpace="nowrap"
p={4}
position="absolute"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface ContractListReadOnlyTableProps {
onContractSelect: (addr: ContractAddr) => void;
}

const TEMPLATE_COLUMNS = "160px 1fr 220px 160px";
const TEMPLATE_COLUMNS = "160px 280px 260px 1fr";

export const ContractListReadOnlyTable = ({
contracts = [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const ContractNameCell = ({
defaultValue={contractLocalInfo.label}
maxLength={MAX_CONTRACT_NAME_LENGTH}
tooltip={contractLocalInfo.description}
isReadOnly={isReadOnly}
onSave={!isReadOnly ? onSave : undefined}
/>
);
Expand Down
8 changes: 6 additions & 2 deletions src/lib/pages/contract-list/components/table/TagsCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const TagsCell = ({
gap={1}
color="text.dark"
opacity={isHover ? "0" : "1"}
onClick={(e) => e.stopPropagation()}
onClick={(e) => {
if (!isReadOnly) e.stopPropagation();
}}
>
{tags.length ? (
<Tag
Expand Down Expand Up @@ -69,7 +71,9 @@ export const TagsCell = ({
w="340px"
top="-16px"
left="-16px"
onClick={(e) => e.stopPropagation()}
onClick={(e) => {
if (!isReadOnly) e.stopPropagation();
}}
zIndex="dropdown"
>
<Flex
Expand Down
27 changes: 12 additions & 15 deletions src/lib/pages/execute/components/ExecuteArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ import JsonInput from "lib/components/json/JsonInput";
import { useContractStore } from "lib/hooks";
import { useTxBroadcast } from "lib/providers/tx-broadcast";
import type { Activity } from "lib/stores/contract";
import type { ComposedMsg, ContractAddr, HumanAddr, Token } from "lib/types";
import type { ComposedMsg, ContractAddr, HumanAddr } from "lib/types";
import { MsgType } from "lib/types";
import { composeMsg, jsonPrettify, jsonValidate, microfy } from "lib/utils";
import {
composeMsg,
fabricateFunds,
jsonPrettify,
jsonValidate,
} from "lib/utils";

const CodeSnippet = dynamic(() => import("lib/components/modal/CodeSnippet"), {
ssr: false,
Expand Down Expand Up @@ -96,12 +101,7 @@ export const ExecuteArea = ({ control, setValue, cmds }: ExecuteAreaProps) => {
});

const proceed = useCallback(async () => {
const funds = assets
.filter((asset) => asset.amount && asset.denom)
.map((asset) => ({
...asset,
amount: microfy(asset.amount as Token).toFixed(0),
}));
const funds = fabricateFunds(assets);

const stream = await executeTx({
onTxSucceed: (userKey: string, activity: Activity) => {
Expand All @@ -126,13 +126,7 @@ export const ExecuteArea = ({ control, setValue, cmds }: ExecuteAreaProps) => {
if (enableExecute) {
setError(undefined);

const funds = assets
.filter((asset) => asset.amount && asset.denom)
.map((asset) => ({
...asset,
amount: microfy(asset.amount as Token).toFixed(0),
}))
.filter((asset) => asset.amount !== "0");
const funds = fabricateFunds(assets);

const composedMsg = composeMsg(MsgType.EXECUTE, {
sender: address as HumanAddr,
Expand Down Expand Up @@ -218,6 +212,9 @@ export const ExecuteArea = ({ control, setValue, cmds }: ExecuteAreaProps) => {
assetOptions={assetOptions}
initialSelected={field.denom}
amountInput={
/**
* @remarks refactor along with instantiate page
*/
<ControllerInput
name={`assets.${idx}.amount`}
control={control}
Expand Down
4 changes: 3 additions & 1 deletion src/lib/pages/execute/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Coin } from "@cosmjs/stargate";

import type { ContractAddr } from "lib/types";

export interface ExecutePageState {
contractAddress: ContractAddr;
initialMsg: string;
assets: { denom: string; amount: string }[];
assets: Coin[];
}
31 changes: 20 additions & 11 deletions src/lib/pages/instantiate/instantiate.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Button, Heading, Text } from "@chakra-ui/react";
import type { InstantiateResult } from "@cosmjs/cosmwasm-stargate";
import type { Coin } from "@cosmjs/stargate";
import { useWallet } from "@cosmos-kit/react";
import { useQuery } from "@tanstack/react-query";
import Long from "long";
Expand Down Expand Up @@ -30,9 +31,9 @@ import { MsgType } from "lib/types";
import {
composeMsg,
demicrofy,
fabricateFunds,
jsonValidate,
libDecode,
microfy,
} from "lib/utils";

import { FailedModal, Footer } from "./component";
Expand Down Expand Up @@ -65,6 +66,7 @@ const Instantiate = ({ onComplete }: InstantiatePageProps) => {
// ------------------STATES------------------//
// ------------------------------------------//
const [simulating, setSimulating] = useState(false);
const [status, setStatus] = useState<FormStatus>({ state: "init" });

// ------------------------------------------//
// ----------------FORM HOOKS----------------//
Expand All @@ -83,7 +85,7 @@ const Instantiate = ({ onComplete }: InstantiatePageProps) => {
label: "",
adminAddress: "",
initMsg: "",
assets: [{ denom: "", amount: "" }],
assets: [{ denom: "", amount: "" }] as Coin[],
simulateError: "",
},
});
Expand All @@ -98,7 +100,6 @@ const Instantiate = ({ onComplete }: InstantiatePageProps) => {
initMsg: watchInitMsg,
simulateError,
} = watch();
const [status, setStatus] = useState<FormStatus>({ state: "init" });

const selectedAssets = watchAssets.map((asset) => asset.denom);

Expand All @@ -107,10 +108,12 @@ const Instantiate = ({ onComplete }: InstantiatePageProps) => {
!codeId ||
!address ||
!!jsonValidate(watchInitMsg) ||
!!formErrors.label ||
Object.keys(formErrors).length > 0 ||
status.state !== "success"
);
}, [codeId, address, watchInitMsg, formErrors.label, status.state]);
// formErrors change doesnt trigger this effect
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [codeId, address, watchInitMsg, Object.keys(formErrors), status.state]);

const assetOptions = useMemo(
() =>
Expand Down Expand Up @@ -157,12 +160,7 @@ const Instantiate = ({ onComplete }: InstantiatePageProps) => {
const proceed = useCallback(() => {
handleSubmit(async ({ adminAddress, label, initMsg, assets }) => {
setSimulating(true);
const funds = assets
.filter((asset) => asset.amount && asset.denom)
.map((asset) => ({
...asset,
amount: microfy(asset.amount as Token).toFixed(0),
}));
const funds = fabricateFunds(assets);
const msg = composeMsg(MsgType.INSTANTIATE, {
sender: address as HumanAddr,
admin: adminAddress as HumanAddr,
Expand Down Expand Up @@ -328,12 +326,23 @@ const Instantiate = ({ onComplete }: InstantiatePageProps) => {
assetOptions={assetOptions}
initialSelected={field.denom}
amountInput={
/**
* @remarks refactor along with execute page
*/
<ControllerInput
name={`assets.${idx}.amount`}
control={control}
label="Amount"
variant="floating"
type="number"
rules={{
pattern: {
// Move to constant
value: /^[0-9]+([.][0-9]{0,6})?$/i,
message: 'Invalid amount. e.g. "100.00"',
},
}}
error={formErrors.assets?.[idx]?.amount?.message}
/>
}
/>
Expand Down
17 changes: 17 additions & 0 deletions src/lib/utils/funds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Coin } from "@cosmjs/stargate";

import type { Token } from "lib/types";

import { microfy } from "./formatter/currency";

export const fabricateFunds = (assets: Coin[]): Coin[] => {
return assets
.filter((asset) => Number(asset.amount) && asset.denom)
.sort(({ denom: aDenom }, { denom: bDenom }) =>
aDenom.localeCompare(bDenom)
)
.map((asset) => ({
...asset,
amount: microfy(asset.amount as Token).toFixed(0),
}));
};
1 change: 1 addition & 0 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from "./countMessages";
export * from "./msgFurtherAction";
export * from "./extractActionValue";
export * from "./mergeRefs";
export * from "./funds";