Skip to content

Commit

Permalink
feat: continue with staking poc
Browse files Browse the repository at this point in the history
  • Loading branch information
icfor committed Jan 11, 2024
1 parent 91fa42a commit c07f2db
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 44 deletions.
2 changes: 1 addition & 1 deletion public/locales/en/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"available": "Available",
"memo": "Memo",
"memoError": {
"tooLong": ""
"tooLongMemo": "Please enter a smaller memo"
},
"success": "Your amount has been staked",
"title": "Stake with Forbole",
Expand Down
2 changes: 1 addition & 1 deletion public/locales/zh-CN/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"available": "Available",
"memo": "Memo",
"memoError": {
"tooLong": ""
"tooLongMemo": "Please enter a smaller memo"
},
"success": "Your amount has been staked",
"title": "Stake with forbole",
Expand Down
2 changes: 1 addition & 1 deletion public/locales/zh-HK/staking.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"available": "Available",
"memo": "Memo",
"memoError": {
"tooLong": ""
"tooLongMemo": "Please enter a smaller memo"
},
"success": "Your amount has been staked",
"title": "Stake with forbole",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import useTranslation from "next-translate/useTranslation";
import { useContext, useState } from "react";
import { useContext } from "react";

import HighlightButton from "@src/components/highlight-button";
import { toastSuccess } from "@src/components/notification";
Expand All @@ -9,7 +9,6 @@ import {
setSelectedAccount,
} from "@src/screens/staking/lib/context";
import { claimRewards } from "@src/screens/staking/lib/context/operations";
import { ChainId } from "@src/screens/staking/lib/context/types";

import ModalBase from "./modal_base";
import NetworksSelect from "./networks_select";
Expand All @@ -18,10 +17,6 @@ const ClaimRewardsModal = () => {
const { setState: setStakingState, state: stakingState } =
useContext(StakingContext);

const [selectedNetwork, setSelectedNetwork] = useState<ChainId>(
ChainId.CosmosHubTestnet,
);

const { t } = useTranslation("staking");

const { selectedAccount, selectedAction } = stakingState;
Expand All @@ -34,7 +29,7 @@ const ClaimRewardsModal = () => {
open={isOpen}
title={t("claimRewards.title")}
>
<NetworksSelect setValue={setSelectedNetwork} value={selectedNetwork} />
<NetworksSelect variant="accounts" />
<div>
<div>Gas Fee</div>
<div>0.002 ATOM</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
}

.modal {
min-height: 100vh;
overflow: auto;
}

Expand Down
70 changes: 53 additions & 17 deletions src/screens/staking/components/staking_section/networks_select.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import MenuItem from "@mui/material/MenuItem";
import Select from "@mui/material/Select";
import { useContext } from "react";

import {
ChainId,
chainIdToNetworkKey,
} from "@src/screens/staking/lib/context/types";
import type { Account, ChainId } from "@src/screens/staking/lib/context/types";
import { chainIdToNetworkKey } from "@src/screens/staking/lib/context/types";
import { getNetworkInfo } from "@src/utils/network_info";

import {
StakingContext,
getSelectedAccount,
setSelectedAccount,
} from "../../lib/context";
import { sortAccounts } from "../../lib/context/formatters";
import * as styles from "./networks_select.module.scss";

const ITEM_HEIGHT = 48;
Expand All @@ -23,13 +28,12 @@ const MenuProps = {
},
};

// @TODO: Get this from context (so testnets are filtered out)
const networks = Object.values(ChainId).sort();

type NetworkItemProps = {
value: ChainId;
};

const SEPARATOR = "____";

const NetworkItem = ({ value }: NetworkItemProps) => {
const networkName = chainIdToNetworkKey[value];
const networkInfo = networkName ? getNetworkInfo(networkName) : "";
Expand All @@ -46,29 +50,61 @@ const NetworkItem = ({ value }: NetworkItemProps) => {
};

type Props = {
setValue: (value: ChainId) => void;
value: ChainId;
variant: "accounts";
};

const NetworksSelect = ({ setValue, value }: Props) => {
const NetworksSelect = ({ variant }: Props) => {
const { setState: setStakingState, state: stakingState } =
useContext(StakingContext);

const selectedAccount = getSelectedAccount(stakingState);

if (!variant || !selectedAccount) return null;

const wallet = stakingState.wallets[selectedAccount.wallet];

if (!wallet) return null;

const allAccounts = Object.values(wallet)
.reduce((acc, chain) => {
acc.push(...chain.accounts);

return acc;
}, [] as Account[])
.sort(sortAccounts);

const handleChange = (event: any) => {
setValue(event.target.value);
const [address, chainId] = event.target.value.split(SEPARATOR);

setSelectedAccount(setStakingState, {
address,
chainId,
wallet: selectedAccount.wallet,
});
};

const selectedItem = [selectedAccount.address, selectedAccount.chainId].join(
SEPARATOR,
);

return (
<div className={styles.control}>
<Select
IconComponent={IconComponent}
MenuProps={MenuProps}
className={styles.select}
onChange={handleChange}
value={value as string}
value={selectedItem}
>
{networks.map((network) => (
<MenuItem key={network} value={network}>
<NetworkItem value={network} />
</MenuItem>
))}
{allAccounts.map((account) => {
const item = [account.address, account.chainId].join(SEPARATOR);

return (
<MenuItem key={item} value={item}>
<NetworkItem value={account.chainId} />
</MenuItem>
);
})}
</Select>
</div>
);
Expand Down
14 changes: 2 additions & 12 deletions src/screens/staking/components/staking_section/staking_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
NetworkInfo,
TStakingContext,
} from "@src/screens/staking/lib/context/types";
import { ChainId } from "@src/screens/staking/lib/context/types";

import Label from "./label";
import ModalBase, { ModalError } from "./modal_base";
Expand All @@ -40,10 +39,6 @@ const StakingModal = () => {
const [isLoading, setIsLoading] = useState(false);
const { selectedAccount, selectedAction } = stakingState;

const [selectedNetwork, setSelectedNetwork] = useState<ChainId>(
ChainId.CosmosHubTestnet,
);

const [networkInfo, setNetworkInfo] = useState<NetworkInfo | null>(null);
const [amount, setAmount] = useState("");
const [amountError, setAmountError] = useState("");
Expand Down Expand Up @@ -98,12 +93,7 @@ const StakingModal = () => {
title={t("stakingModal.title")}
>
<div className={styles.wrapper}>
<div>
<NetworksSelect
setValue={setSelectedNetwork}
value={selectedNetwork}
/>
</div>
<div>{selectedAccount && <NetworksSelect variant="accounts" />}</div>
{networkInfo?.apy && (
<div className={styles.row}>
<Label className={styles.apy}>
Expand Down Expand Up @@ -166,7 +156,7 @@ const StakingModal = () => {
if (!memo) return "";

if (memo.length > 256) {
return t("stakingModal.memoError.tooLong");
return t("stakingModal.memoError.tooLongMemo");
}
})() as string;

Expand Down
14 changes: 10 additions & 4 deletions src/screens/staking/components/staking_section/unstaking_modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import {
setSelectedAccount,
syncAccountData,
} from "@src/screens/staking/lib/context";
import { formatDenom } from "@src/screens/staking/lib/context/formatters";
import {
formatDenom,
resolveDenom,
} from "@src/screens/staking/lib/context/formatters";
import { unstake } from "@src/screens/staking/lib/context/operations";
import { MAX_MEMO } from "@src/screens/staking/lib/context/types";

import Label from "./label";
import ModalBase, { ModalError } from "./modal_base";
Expand Down Expand Up @@ -136,7 +140,9 @@ const UnstakingModal = () => {
setAmount(e.target.value);
}}
placeholder={t("unstakingModal.amount.placeholder")}
rightText="ATOM"
{...(account?.info?.delegation?.denom && {
rightText: resolveDenom(account.info.delegation?.denom),
})}
value={amount}
/>
{!!amountError && <ModalError>{amountError}</ModalError>}
Expand All @@ -148,8 +154,8 @@ const UnstakingModal = () => {
const newMemoError = (() => {
if (!memo) return "";

if (memo.length > 256) {
return t("stakingModal.memoError.tooLong");
if (memo.length > MAX_MEMO) {
return t("stakingModal.memoError.tooLongMemo");
}
})() as string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ChainId } from "@src/screens/staking/lib/context";
import type { ChainId } from "@src/screens/staking/lib/context/types";

const baseUrl = process.env.NEXT_PUBLIC_STAKING_API as string;

Expand Down
2 changes: 2 additions & 0 deletions src/screens/staking/lib/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { networks } from "@src/utils/network_info";
export const ENABLE_TESTNETS =
process.env.NEXT_PUBLIC_STAKING_ENABLE_TESTNETS === "true";

export const MAX_MEMO = 256;

export enum WalletId {
Keplr = "keplr",
}
Expand Down

0 comments on commit c07f2db

Please sign in to comment.