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

Commit

Permalink
Merge pull request #79 from darwinia-network/jay/adapt-staking-upgrade
Browse files Browse the repository at this point in the history
Adapt staking upgrade
  • Loading branch information
wuminzhe authored Mar 15, 2024
2 parents be17fa2 + de81198 commit a47dfd3
Show file tree
Hide file tree
Showing 49 changed files with 23,222 additions and 9,505 deletions.
31,791 changes: 22,689 additions & 9,102 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
"dependencies": {
"@apollo/client": "^3.8.0",
"@floating-ui/react": "^0.24.5",
"@polkadot/api": "^10.9.1",
"@rainbow-me/rainbowkit": "^1.3.3",
"@polkadot/api": "^10.12.2",
"@rainbow-me/rainbowkit": "^2.0.2",
"@tanstack/react-query": "^5.27.5",
"date-fns": "^2.30.0",
"date-fns-tz": "^2.0.0",
"graphql": "^16.7.1",
"next": "13.4.8",
"next": "^13.5.6",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-jazzicon": "^1.0.4",
"react-transition-group": "^4.4.5",
"viem": "^1.21.4",
"wagmi": "^1.4.13"
"viem": "^2.8.5",
"wagmi": "^2.5.7"
},
"devDependencies": {
"@types/node": "^20.4.0",
Expand Down
5 changes: 5 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Darwinia Staking",
"description": "Darwinia and Crab network staking apps",
"icons": [{ "src": "icon.svg", "sizes": "any" }]
}
8 changes: 5 additions & 3 deletions src/app/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const fontJetBrainsMono = JetBrains_Mono({ subsets: ["latin", "latin-ext"] });

export const metadata = {
title: "Darwinia Staking",
description: "Darwinia and Crab network staking app",
description: "Darwinia and Crab network staking apps",
manifest: "/manifest.json",
};

export default function RootLayout({ children }: { children: React.ReactNode }) {
Expand Down
16 changes: 12 additions & 4 deletions src/components/balance-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ export default function BalanceInput({
);
}

export function ExtraPower({ power, powerChanges = "more" }: { power: bigint; powerChanges?: PowerChanges }) {
export function ExtraPower({
power,
className,
powerChanges = "more",
}: {
power: bigint;
className?: string;
powerChanges?: PowerChanges;
}) {
return (
<span className="text-xs font-bold text-primary">{`${powerChanges === "more" ? "+" : "-"}${prettyNumber(
power
)} Power`}</span>
<span className={`text-xs font-bold text-primary ${className}`}>{`${
powerChanges === "more" ? "+" : "-"
}${prettyNumber(power)} Power`}</span>
);
}
55 changes: 29 additions & 26 deletions src/components/bond-more-deposit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { commissionWeightedPower, formatBlanace, getChainConfig, notifyTransacti
import { ExtraPower } from "./balance-input";
import { useApp, useStaking } from "@/hooks";
import { notification } from "./notification";
import { writeContract, waitForTransaction } from "@wagmi/core";
import { ChainID } from "@/types";
import { usePublicClient, useWalletClient } from "wagmi";

export default function BondMoreDepositModal({
commission,
Expand All @@ -17,9 +16,12 @@ export default function BondMoreDepositModal({
isOpen: boolean;
onClose?: () => void;
}) {
const { deposits, calcExtraPower } = useStaking();
const { deposits, isStakingV2, calcExtraPower } = useStaking();
const { activeChain } = useApp();

const { data: walletClient } = useWalletClient();
const publicClient = usePublicClient();

const [checkedDeposits, setCheckedDeposits] = useState<number[]>([]);
const [busy, setBusy] = useState(false);

Expand All @@ -39,35 +41,36 @@ export default function BondMoreDepositModal({
const { nativeToken } = getChainConfig(activeChain);

const handleBond = useCallback(async () => {
setBusy(true);
const { contract, explorer } = getChainConfig(activeChain);
if (walletClient && publicClient) {
setBusy(true);
const { contract, explorer } = getChainConfig(activeChain);

try {
const abi =
activeChain === ChainID.CRAB
try {
const abi = isStakingV2
? (await import("@/config/abi/staking-v2.json")).default
: (await import(`@/config/abi/${contract.staking.abiFile}`)).default;
: (await import("@/config/abi/staking.json")).default;

const { hash } = await writeContract({
address: contract.staking.address,
abi,
functionName: "stake",
args: [0n, 0n, checkedDeposits],
});
const receipt = await waitForTransaction({ hash });
const hash = await walletClient.writeContract({
address: contract.staking.address,
abi,
functionName: "stake",
args: [0n, 0n, checkedDeposits],
});
const receipt = await publicClient?.waitForTransactionReceipt({ hash });

if (receipt.status === "success") {
setCheckedDeposits([]);
onClose();
if (receipt.status === "success") {
setCheckedDeposits([]);
onClose();
}
notifyTransaction(receipt, explorer);
} catch (err) {
console.error(err);
notification.error({ description: (err as Error).message });
}
notifyTransaction(receipt, explorer);
} catch (err) {
console.error(err);
notification.error({ description: (err as Error).message });
}

setBusy(false);
}, [activeChain, checkedDeposits, onClose]);
setBusy(false);
}
}, [activeChain, isStakingV2, checkedDeposits, walletClient, publicClient, onClose]);

return (
<Modal
Expand Down
28 changes: 14 additions & 14 deletions src/components/bond-more-kton-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { commissionWeightedPower, getChainConfig, notifyTransaction } from "@/utils";
import BondMoreTokenModal from "./bond-more-token-modal";
import { useApp, useStaking } from "@/hooks";
import { useAccount, useBalance } from "wagmi";
import { useAccount, useBalance, usePublicClient, useWalletClient } from "wagmi";
import { useCallback, useState } from "react";
import { notification } from "./notification";
import { writeContract, waitForTransaction } from "@wagmi/core";
import { ChainID } from "@/types";

export default function BondMoreKtonModal({
commission,
Expand All @@ -18,34 +16,36 @@ export default function BondMoreKtonModal({
}) {
const { activeChain } = useApp();
const { address } = useAccount();
const { calcExtraPower } = useStaking();
const { isStakingV2, calcExtraPower } = useStaking();

const { data: walletClient } = useWalletClient();
const publicClient = usePublicClient();

const [inputAmount, setInputAmount] = useState(0n);
const [busy, setBusy] = useState(false);

const { ktonToken, contract, explorer } = getChainConfig(activeChain);
const { data: ktonBalance } = useBalance({ address, token: ktonToken?.address, watch: true });
const { ktonToken } = getChainConfig(activeChain);
const { data: ktonBalance } = useBalance({ address, token: ktonToken?.address, query: { refetchInterval: 3000 } });

const handleBond = useCallback(async () => {
if ((ktonBalance?.value || 0n) < inputAmount) {
notification.warn({ description: "Your balance is insufficient." });
} else {
} else if (walletClient && publicClient) {
setBusy(true);
const { contract, explorer } = getChainConfig(activeChain);

try {
const abi =
activeChain === ChainID.CRAB
? (await import("@/config/abi/staking-v2.json")).default
: (await import(`@/config/abi/${contract.staking.abiFile}`)).default;
const abi = isStakingV2
? (await import("@/config/abi/staking-v2.json")).default
: (await import("@/config/abi/staking.json")).default;

const { hash } = await writeContract({
const hash = await walletClient.writeContract({
address: contract.staking.address,
abi,
functionName: "stake",
args: [0n, inputAmount, []],
});
const receipt = await waitForTransaction({ hash });
const receipt = await publicClient.waitForTransactionReceipt({ hash });

if (receipt.status === "success") {
setInputAmount(0n);
Expand All @@ -59,7 +59,7 @@ export default function BondMoreKtonModal({

setBusy(false);
}
}, [activeChain, inputAmount, ktonBalance?.value, onClose]);
}, [activeChain, inputAmount, ktonBalance?.value, isStakingV2, walletClient, publicClient, onClose]);

return (
ktonToken && (
Expand Down
26 changes: 13 additions & 13 deletions src/components/bond-more-ring-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { commissionWeightedPower, getChainConfig, notifyTransaction } from "@/utils";
import BondMoreTokenModal from "./bond-more-token-modal";
import { useAccount, useBalance } from "wagmi";
import { useAccount, useBalance, usePublicClient, useWalletClient } from "wagmi";
import { useApp, useStaking } from "@/hooks";
import { useCallback, useState } from "react";
import { notification } from "./notification";
import { writeContract, waitForTransaction } from "@wagmi/core";
import { ChainID } from "@/types";

export default function BondMoreRingModal({
commission,
Expand All @@ -18,8 +16,11 @@ export default function BondMoreRingModal({
}) {
const { activeChain } = useApp();
const { address } = useAccount();
const { data: ringBalance } = useBalance({ address, watch: true });
const { calcExtraPower } = useStaking();
const { data: ringBalance } = useBalance({ address, query: { refetchInterval: 3000 } });
const { isStakingV2, calcExtraPower } = useStaking();

const { data: walletClient } = useWalletClient();
const publicClient = usePublicClient();

const [inputAmount, setInputAmount] = useState(0n);
const [busy, setBusy] = useState(false);
Expand All @@ -29,23 +30,22 @@ export default function BondMoreRingModal({
const handleBond = useCallback(async () => {
if ((ringBalance?.value || 0n) < inputAmount) {
notification.warn({ description: "Your balance is insufficient." });
} else {
} else if (walletClient && publicClient) {
setBusy(true);
const { contract, explorer } = getChainConfig(activeChain);

try {
const abi =
activeChain === ChainID.CRAB
? (await import("@/config/abi/staking-v2.json")).default
: (await import(`@/config/abi/${contract.staking.abiFile}`)).default;
const abi = isStakingV2
? (await import("@/config/abi/staking-v2.json")).default
: (await import("@/config/abi/staking.json")).default;

const { hash } = await writeContract({
const hash = await walletClient.writeContract({
address: contract.staking.address,
abi,
functionName: "stake",
args: [inputAmount, 0n, []],
});
const receipt = await waitForTransaction({ hash });
const receipt = await publicClient.waitForTransactionReceipt({ hash });

if (receipt.status === "success") {
setInputAmount(0n);
Expand All @@ -58,7 +58,7 @@ export default function BondMoreRingModal({

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

return (
<BondMoreTokenModal
Expand Down
38 changes: 27 additions & 11 deletions src/components/bond-more-token-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useStaking } from "@/hooks";
import BalanceInput from "./balance-input";
import Modal from "./modal";

Expand Down Expand Up @@ -28,29 +29,44 @@ export default function BondMoreTokenModal({
onClose?: () => void;
onChange?: (amount: bigint) => void;
}) {
const { isStakingV2 } = useStaking();
return (
<Modal
title={`Bond More ${symbol}`}
isOpen={isOpen}
onCancel={onCancel}
onClose={onClose}
onOk={onBond}
onOk={isStakingV2 ? undefined : onBond}
maskClosable={false}
okText="Bond"
className="lg:w-[25rem]"
busy={busy}
disabled={disabled}
>
<BalanceInput
label="Amount"
boldLabel
decimals={decimals}
symbol={symbol}
balance={balance}
power={power}
isReset={isReset}
onChange={onChange}
/>
{isStakingV2 ? (
<div className="flex flex-col gap-small text-xs font-bold lg:text-sm lg:font-light">
<span className="text-white">{`Please stake ${symbol} in`}</span>
<a
href="https://kton-staking.darwinia.network/"
rel="noopener noreferrer"
target="_blank"
className="text-primary underline transition-opacity hover:opacity-80"
>
https://kton-staking.darwinia.network
</a>
</div>
) : (
<BalanceInput
label="Amount"
boldLabel
decimals={decimals}
symbol={symbol}
balance={balance}
power={power}
isReset={isReset}
onChange={onChange}
/>
)}
</Modal>
);
}
Loading

0 comments on commit a47dfd3

Please sign in to comment.