Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

DIP-6 Adaptation #88

Merged
merged 9 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
20 changes: 17 additions & 3 deletions src/components/balance-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { formatBlanace } from "@/utils";
import Image from "next/image";
import InputLabel from "./input-label";
import { parseUnits } from "viem";
import { useEffect, useRef, useState } from "react";
import { useEffect, useMemo, useRef, useState } from "react";

export default function BalanceInput({
isReset,
balance,
max,
symbol,
decimals,
logoPath,
Expand All @@ -17,6 +18,7 @@ export default function BalanceInput({
}: {
isReset?: boolean;
balance: bigint;
max?: bigint;
symbol: string;
decimals: number;
logoPath?: string;
Expand All @@ -34,6 +36,14 @@ export default function BalanceInput({
}
}, [isReset]);

const placeholder = useMemo(() => {
if (typeof max === "bigint") {
return `Max: ${formatBlanace(max, decimals, { keepZero: false, precision: decimals })}`;
} else {
return `Balance: ${formatBlanace(balance, decimals, { keepZero: false, precision: decimals })}`;
}
}, [balance, decimals, max]);

return (
<div className={`flex flex-col gap-middle ${className}`}>
{label && <InputLabel label={label} bold={boldLabel} />}
Expand All @@ -43,11 +53,15 @@ export default function BalanceInput({
}`}
>
<input
placeholder={`Balance: ${formatBlanace(balance, decimals, { keepZero: false, precision: decimals })}`}
placeholder={placeholder}
className="h-full w-[72%] bg-transparent text-sm font-light focus-visible:outline-none"
onChange={(e) => {
const _hasError = Number.isNaN(Number(e.target.value));
setHasError(_hasError || balance < parseUnits(e.target.value, decimals));
setHasError(
_hasError ||
balance < parseUnits(e.target.value, decimals) ||
(typeof max === "bigint" && max < parseUnits(e.target.value, decimals))
);

if (!_hasError) {
onChange(parseUnits(e.target.value, decimals));
Expand Down
22 changes: 19 additions & 3 deletions src/components/bond-more-deposit-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Key, useCallback, useState } from "react";
import { Key, useCallback, useEffect, useState } from "react";
import Modal from "./modal";
import CheckboxGroup from "./checkbox-group";
import { formatBlanace, getChainConfig, notifyTransaction } from "@/utils";
import { useApp, useStaking } from "@/hooks";
import { useApp, useDip6, useRateLimit, useStaking } from "@/hooks";
import { notification } from "./notification";
import { writeContract, waitForTransaction } from "@wagmi/core";

Expand All @@ -22,6 +22,14 @@ export default function BondMoreDepositModal({
const availableDeposits = deposits.filter(({ inUse }) => !inUse);
const { nativeToken } = getChainConfig(activeChain);

const { isDip6Implemented } = useDip6();
const { availableDeposit, updateRateLimit } = useRateLimit();
useEffect(() => {
if (isOpen) {
updateRateLimit();
}
}, [isOpen, updateRateLimit]);

const handleBond = useCallback(async () => {
setBusy(true);
const { contract, explorer } = getChainConfig(activeChain);
Expand All @@ -37,6 +45,7 @@ export default function BondMoreDepositModal({

if (receipt.status === "success") {
setCheckedDeposits([]);
updateRateLimit();
onClose();
}
notifyTransaction(receipt, explorer);
Expand All @@ -46,7 +55,7 @@ export default function BondMoreDepositModal({
}

setBusy(false);
}, [activeChain, checkedDeposits, onClose]);
}, [activeChain, checkedDeposits, onClose, updateRateLimit]);

return (
<Modal
Expand All @@ -63,6 +72,12 @@ export default function BondMoreDepositModal({
>
{availableDeposits.length ? (
<>
{isDip6Implemented && (
<span className="text-xs font-light text-white/50">
Max in this session: {formatBlanace(availableDeposit, nativeToken.decimals, { keepZero: false })}{" "}
{nativeToken.symbol}
</span>
)}
<CheckboxGroup
options={availableDeposits.map(({ id, value }) => ({
value: id,
Expand All @@ -74,6 +89,7 @@ export default function BondMoreDepositModal({
})} ${nativeToken.symbol}`}</span>
</div>
),
disabled: availableDeposit < value,
}))}
checkedValues={checkedDeposits}
onChange={setCheckedDeposits as (values: Key[]) => void}
Expand Down
1 change: 1 addition & 0 deletions src/components/bond-more-kton-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function BondMoreKtonModal({
symbol={ktonToken.symbol}
decimals={ktonToken.decimals}
balance={ktonBalance?.value || 0n}
max={0n}
busy={busy}
disabled={inputAmount <= 0n}
isReset={inputAmount <= 0}
Expand Down
15 changes: 12 additions & 3 deletions src/components/bond-more-ring-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getChainConfig, notifyTransaction } from "@/utils";
import BondMoreTokenModal from "./bond-more-token-modal";
import { useAccount, useBalance } from "wagmi";
import { useApp } from "@/hooks";
import { useCallback, useState } from "react";
import { useApp, useRateLimit } from "@/hooks";
import { useCallback, useEffect, useState } from "react";
import { notification } from "./notification";
import { writeContract, waitForTransaction } from "@wagmi/core";

Expand All @@ -22,6 +22,13 @@ export default function BondMoreRingModal({

const { nativeToken } = getChainConfig(activeChain);

const { availableDeposit, updateRateLimit } = useRateLimit();
useEffect(() => {
if (isOpen) {
updateRateLimit();
}
}, [isOpen, updateRateLimit]);

const handleBond = useCallback(async () => {
if ((ringBalance?.value || 0n) < inputAmount) {
notification.warn({ description: "Your balance is insufficient." });
Expand All @@ -40,6 +47,7 @@ export default function BondMoreRingModal({

if (receipt.status === "success") {
setInputAmount(0n);
updateRateLimit();
onClose();
}
notifyTransaction(receipt, explorer);
Expand All @@ -49,14 +57,15 @@ export default function BondMoreRingModal({

setBusy(false);
}
}, [activeChain, inputAmount, ringBalance?.value, onClose]);
}, [activeChain, inputAmount, ringBalance?.value, onClose, updateRateLimit]);

return (
<BondMoreTokenModal
isOpen={isOpen}
symbol={nativeToken.symbol}
decimals={nativeToken.decimals}
balance={ringBalance?.value || 0n}
max={availableDeposit}
busy={busy}
disabled={inputAmount <= 0n}
isReset={inputAmount <= 0}
Expand Down
3 changes: 3 additions & 0 deletions src/components/bond-more-token-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default function BondMoreTokenModal({
isOpen,
symbol,
decimals,
max,
balance,
busy,
disabled,
Expand All @@ -17,6 +18,7 @@ export default function BondMoreTokenModal({
isOpen: boolean;
symbol: string;
decimals: number;
max: bigint;
balance: bigint;
busy?: boolean;
disabled?: boolean;
Expand Down Expand Up @@ -60,6 +62,7 @@ export default function BondMoreTokenModal({
decimals={decimals}
symbol={symbol}
balance={balance}
max={max}
isReset={isReset}
onChange={onChange}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/components/checkbox-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Key, ReactElement } from "react";
interface Props {
options: {
label: ReactElement;
disabled?: boolean;
value: Key;
}[];
checkedValues: Key[];
Expand All @@ -13,7 +14,7 @@ interface Props {
export default function CheckboxGroup({ options, checkedValues, className, onChange = () => undefined }: Props) {
return (
<div className={`flex flex-col gap-large ${className}`}>
{options.map(({ label, value }) => {
{options.map(({ label, value, disabled }) => {
const idx = checkedValues.findIndex((v) => v === value);
const checked = idx >= 0;

Expand All @@ -23,6 +24,7 @@ export default function CheckboxGroup({ options, checkedValues, className, onCha
className={`relative h-4 w-4 rounded-sm border transition hover:scale-105 active:scale-95 ${
checked ? "border-primary bg-primary" : "border-white bg-transparent"
}`}
disabled={disabled}
onClick={() => {
const checkeds = [...checkedValues];
if (checked) {
Expand Down
26 changes: 0 additions & 26 deletions src/components/collator-select-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,6 @@ const columns: ColumnType<DataSource>[] = [
title: (
<div className="inline-flex items-center gap-small">
<span className="text-xs font-bold text-white">Total-staked</span>
<Tooltip
content={
<div className="inline-block text-xs font-light text-white">
{`The Collator's total-staked is a dynamic value, inversely proportional to the commission set by
the Collator. Higher commission results in lower total-staked and vice versa. `}
<a
rel="noopener noreferrer"
target="_blank"
className="text-primary hover:underline"
href="https://github.com/darwinia-network/DIPs/blob/main/DIPs/dip-1.md"
>
Learn More
</a>
</div>
}
enabledSafePolygon
contentClassName="w-80"
>
<Image
width={15}
height={14}
alt="Info"
src="/images/help.svg"
className="opacity-60 transition-opacity hover:opacity-100"
/>
</Tooltip>
</div>
),
render: (row) => <span>{prettyNumber(row.power)}</span>,
Expand Down
9 changes: 5 additions & 4 deletions src/components/records-bonded-tokens.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useApp } from "@/hooks";
import { useApp, useDip6 } from "@/hooks";
import { StakingRecordsDataSource } from "@/types";
import { formatBlanace, getChainConfig, notifyTransaction } from "@/utils";
import UnbondingTokenTooltip from "./unbonding-token-tooltip";
Expand All @@ -21,6 +21,7 @@ export default function RecordsBondedTokens({ row }: { row: StakingRecordsDataSo
const [ktonBusy, setKtonBusy] = useState(false);

const { activeChain } = useApp();
const { isDip6Implemented } = useDip6();
const { nativeToken, ktonToken } = getChainConfig(activeChain);

const handleCancelUnbonding = useCallback(
Expand Down Expand Up @@ -103,7 +104,7 @@ export default function RecordsBondedTokens({ row }: { row: StakingRecordsDataSo
<div className="flex flex-col">
{/* ring */}
<div className="flex items-center gap-small">
{row.bondedTokens.unbondingRing.length > 0 ? (
{row.bondedTokens.unbondingRing.length > 0 && !isDip6Implemented ? (
ringBusy ? (
<BusyIcon />
) : (
Expand Down Expand Up @@ -131,7 +132,7 @@ export default function RecordsBondedTokens({ row }: { row: StakingRecordsDataSo
</div>
{/* deposit */}
<div className="flex items-center gap-small">
{row.bondedTokens.unbondingDeposits.length > 0 ? (
{row.bondedTokens.unbondingDeposits.length > 0 && !isDip6Implemented ? (
depositBusy ? (
<BusyIcon />
) : (
Expand Down Expand Up @@ -160,7 +161,7 @@ export default function RecordsBondedTokens({ row }: { row: StakingRecordsDataSo
</div>
{/* kton */}
<div className="flex items-center gap-small">
{row.bondedTokens.unbondingKton.length > 0 ? (
{row.bondedTokens.unbondingKton.length > 0 && !isDip6Implemented ? (
ktonBusy ? (
<BusyIcon />
) : (
Expand Down
22 changes: 19 additions & 3 deletions src/components/unbond-deposit-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Key, useCallback, useState } from "react";
import { Key, useCallback, useEffect, useState } from "react";
import Modal from "./modal";
import CheckboxGroup from "./checkbox-group";
import { formatBlanace, getChainConfig, notifyTransaction } from "@/utils";
import { useApp, useStaking } from "@/hooks";
import { useApp, useDip6, useRateLimit, useStaking } from "@/hooks";
import { notification } from "./notification";
import { writeContract, waitForTransaction } from "@wagmi/core";

Expand All @@ -22,6 +22,14 @@ export default function UnbondDepositModal({
const availableDeposits = deposits.filter(({ id }) => stakedDeposits.includes(id));
const { nativeToken } = getChainConfig(activeChain);

const { isDip6Implemented } = useDip6();
const { availableWithdraw, updateRateLimit } = useRateLimit();
useEffect(() => {
if (isOpen) {
updateRateLimit();
}
}, [isOpen, updateRateLimit]);

const handleUnbond = useCallback(async () => {
setBusy(true);
const { contract, explorer } = getChainConfig(activeChain);
Expand All @@ -37,6 +45,7 @@ export default function UnbondDepositModal({

if (receipt.status === "success") {
setCheckedDeposits([]);
updateRateLimit();
onClose();
}
notifyTransaction(receipt, explorer);
Expand All @@ -46,7 +55,7 @@ export default function UnbondDepositModal({
}

setBusy(false);
}, [activeChain, checkedDeposits, onClose]);
}, [activeChain, checkedDeposits, onClose, updateRateLimit]);

return (
<Modal
Expand All @@ -63,6 +72,12 @@ export default function UnbondDepositModal({
>
{availableDeposits.length ? (
<>
{isDip6Implemented && (
<span className="text-xs font-light text-white/50">
Max in this session: {formatBlanace(availableWithdraw, nativeToken.decimals, { keepZero: false })}{" "}
{nativeToken.symbol}
</span>
)}
<CheckboxGroup
options={availableDeposits.map(({ id, value }) => ({
value: id,
Expand All @@ -74,6 +89,7 @@ export default function UnbondDepositModal({
})} ${nativeToken.symbol}`}</span>
</div>
),
disabled: availableWithdraw < value,
}))}
checkedValues={checkedDeposits}
onChange={setCheckedDeposits as (values: Key[]) => void}
Expand Down
1 change: 1 addition & 0 deletions src/components/unbond-kton-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export default function UnbondKtonModal({
symbol={ktonToken.symbol}
decimals={ktonToken.decimals}
balance={stakedKton}
max={0n}
hackfisher marked this conversation as resolved.
Show resolved Hide resolved
busy={busy}
disabled={inputAmount <= 0n}
isReset={inputAmount <= 0}
Expand Down
Loading
Loading