This repository has been archived by the owner on Jan 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
458179f
commit de81198
Showing
6 changed files
with
178 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,82 @@ | ||
import { getChainConfig } from "@/utils"; | ||
import { commissionWeightedPower, getChainConfig, notifyTransaction } from "@/utils"; | ||
import BondMoreTokenModal from "./bond-more-token-modal"; | ||
import { useApp } from "@/hooks"; | ||
import { useApp, useStaking } from "@/hooks"; | ||
import { useAccount, useBalance, usePublicClient, useWalletClient } from "wagmi"; | ||
import { useCallback, useState } from "react"; | ||
import { notification } from "./notification"; | ||
|
||
export default function BondMoreKtonModal({ | ||
commission, | ||
isOpen, | ||
onClose = () => undefined, | ||
}: { | ||
commission: string; | ||
isOpen: boolean; | ||
onClose?: () => void; | ||
}) { | ||
const { activeChain } = useApp(); | ||
const { address } = useAccount(); | ||
const { isStakingV2, calcExtraPower } = useStaking(); | ||
|
||
const { data: walletClient } = useWalletClient(); | ||
const publicClient = usePublicClient(); | ||
|
||
const [inputAmount, setInputAmount] = useState(0n); | ||
const [busy, setBusy] = useState(false); | ||
|
||
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 if (walletClient && publicClient) { | ||
setBusy(true); | ||
const { contract, explorer } = getChainConfig(activeChain); | ||
|
||
try { | ||
const abi = isStakingV2 | ||
? (await import("@/config/abi/staking-v2.json")).default | ||
: (await import("@/config/abi/staking.json")).default; | ||
|
||
const hash = await walletClient.writeContract({ | ||
address: contract.staking.address, | ||
abi, | ||
functionName: "stake", | ||
args: [0n, inputAmount, []], | ||
}); | ||
const receipt = await publicClient.waitForTransactionReceipt({ hash }); | ||
|
||
if (receipt.status === "success") { | ||
setInputAmount(0n); | ||
onClose(); | ||
} | ||
notifyTransaction(receipt, explorer); | ||
} catch (err) { | ||
console.error(err); | ||
notification.error({ description: (err as Error).message }); | ||
} | ||
|
||
setBusy(false); | ||
} | ||
}, [activeChain, inputAmount, ktonBalance?.value, isStakingV2, walletClient, publicClient, onClose]); | ||
|
||
return ( | ||
ktonToken && <BondMoreTokenModal isOpen={isOpen} symbol={ktonToken.symbol} onClose={onClose} onCancel={onClose} /> | ||
ktonToken && ( | ||
<BondMoreTokenModal | ||
isOpen={isOpen} | ||
symbol={ktonToken.symbol} | ||
decimals={ktonToken.decimals} | ||
power={commissionWeightedPower(calcExtraPower(0n, inputAmount), commission)} | ||
balance={ktonBalance?.value || 0n} | ||
busy={busy} | ||
disabled={inputAmount <= 0n} | ||
isReset={inputAmount <= 0} | ||
onClose={onClose} | ||
onBond={handleBond} | ||
onCancel={onClose} | ||
onChange={setInputAmount} | ||
/> | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,72 @@ | ||
import { useStaking } from "@/hooks"; | ||
import BalanceInput from "./balance-input"; | ||
import Modal from "./modal"; | ||
|
||
export default function BondMoreTokenModal({ | ||
isOpen, | ||
symbol, | ||
decimals, | ||
balance, | ||
power, | ||
busy, | ||
disabled, | ||
isReset, | ||
onCancel = () => undefined, | ||
onBond = () => undefined, | ||
onClose = () => undefined, | ||
onChange = () => undefined, | ||
}: { | ||
isOpen: boolean; | ||
symbol: string; | ||
decimals: number; | ||
balance: bigint; | ||
power: bigint; | ||
busy?: boolean; | ||
disabled?: boolean; | ||
isReset?: boolean; | ||
onCancel?: () => void; | ||
onBond?: () => void; | ||
onClose?: () => void; | ||
onChange?: (amount: bigint) => void; | ||
}) { | ||
const { isStakingV2 } = useStaking(); | ||
return ( | ||
<Modal | ||
title={`Bond More ${symbol}`} | ||
isOpen={isOpen} | ||
onCancel={onCancel} | ||
onClose={onClose} | ||
onOk={isStakingV2 ? undefined : onBond} | ||
maskClosable={false} | ||
okText="Bond" | ||
className="lg:w-[25rem]" | ||
busy={busy} | ||
disabled={disabled} | ||
> | ||
<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> | ||
{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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters