|
| 1 | +import Image from "next/image"; |
| 2 | +import type { ComponentProps, ReactNode } from "react"; |
| 3 | + |
| 4 | +import background from "./background.png"; |
| 5 | +import { deposit, withdraw, claim } from "../../api"; |
| 6 | +import { StateType, useTransfer } from "../../hooks/use-transfer"; |
| 7 | +import { Button } from "../Button"; |
| 8 | +import { Modal, ModalButton, ModalPanel } from "../Modal"; |
| 9 | +import { Tokens } from "../Tokens"; |
| 10 | +import { TransferButton } from "../TransferButton"; |
| 11 | + |
| 12 | +type Props = { |
| 13 | + total: bigint; |
| 14 | + locked: bigint; |
| 15 | + unlockSchedule: { |
| 16 | + amount: bigint; |
| 17 | + date: Date; |
| 18 | + }[]; |
| 19 | + lastSlash: |
| 20 | + | { |
| 21 | + amount: bigint; |
| 22 | + date: Date; |
| 23 | + } |
| 24 | + | undefined; |
| 25 | + walletAmount: bigint; |
| 26 | + availableRewards: bigint; |
| 27 | + expiringRewards: { |
| 28 | + amount: bigint; |
| 29 | + expiry: Date; |
| 30 | + }; |
| 31 | + availableToWithdraw: bigint; |
| 32 | +}; |
| 33 | + |
| 34 | +export const AccountSummary = ({ |
| 35 | + locked, |
| 36 | + unlockSchedule, |
| 37 | + lastSlash, |
| 38 | + walletAmount, |
| 39 | + total, |
| 40 | + availableToWithdraw, |
| 41 | + availableRewards, |
| 42 | + expiringRewards, |
| 43 | +}: Props) => ( |
| 44 | + <section className="relative w-full overflow-hidden border border-neutral-600/50 bg-pythpurple-800"> |
| 45 | + <Image |
| 46 | + src={background} |
| 47 | + alt="" |
| 48 | + className="absolute -right-40 h-full object-right [mask-image:linear-gradient(to_right,_transparent,_black_50%)]" |
| 49 | + /> |
| 50 | + <div className="relative flex flex-col items-start justify-between gap-16 px-12 py-20 md:flex-row md:items-center"> |
| 51 | + <div> |
| 52 | + <div className="mb-4 inline-block border border-neutral-600/50 bg-neutral-900 px-4 py-1 text-xs text-neutral-400"> |
| 53 | + Total Balance |
| 54 | + </div> |
| 55 | + <div className="flex flex-row items-center gap-8"> |
| 56 | + <span> |
| 57 | + <Tokens className="text-6xl font-light">{total}</Tokens> |
| 58 | + </span> |
| 59 | + {lastSlash && ( |
| 60 | + <p className="max-w-48 text-sm text-red-600"> |
| 61 | + <Tokens>{lastSlash.amount}</Tokens> were slashed on{" "} |
| 62 | + {lastSlash.date.toLocaleString()} |
| 63 | + </p> |
| 64 | + )} |
| 65 | + </div> |
| 66 | + <div className="mt-8 flex flex-row items-center gap-4"> |
| 67 | + <TransferButton |
| 68 | + actionDescription="Add funds to your balance" |
| 69 | + actionName="Deposit" |
| 70 | + max={walletAmount} |
| 71 | + transfer={deposit} |
| 72 | + /> |
| 73 | + </div> |
| 74 | + {locked > 0n && ( |
| 75 | + <> |
| 76 | + <div className="mt-6 flex flex-row items-center gap-1 text-xl text-pythpurple-100/50"> |
| 77 | + <Tokens>{locked}</Tokens> |
| 78 | + <div>locked</div> |
| 79 | + </div> |
| 80 | + <Modal> |
| 81 | + <ModalButton |
| 82 | + as="button" |
| 83 | + className="mt-1 text-sm text-pythpurple-400 hover:underline" |
| 84 | + > |
| 85 | + Show Unlock Schedule |
| 86 | + </ModalButton> |
| 87 | + <ModalPanel |
| 88 | + title="Unlock Schedule" |
| 89 | + description="Your tokens will become available for withdrawal and for participation in Integrity Staking according to this schedule" |
| 90 | + > |
| 91 | + <div className="border border-neutral-600/50 bg-pythpurple-100/10 px-8 py-6"> |
| 92 | + <table> |
| 93 | + <thead className="font-medium"> |
| 94 | + <tr> |
| 95 | + <td className="pr-12 text-sm text-neutral-400">Date</td> |
| 96 | + <td className="text-sm text-neutral-400">Amount</td> |
| 97 | + </tr> |
| 98 | + </thead> |
| 99 | + <tbody> |
| 100 | + {unlockSchedule.map((unlock, i) => ( |
| 101 | + <tr key={i}> |
| 102 | + <td className="pr-12 text-sm opacity-80"> |
| 103 | + {unlock.date.toLocaleString()} |
| 104 | + </td> |
| 105 | + <td> |
| 106 | + <Tokens>{unlock.amount}</Tokens> |
| 107 | + </td> |
| 108 | + </tr> |
| 109 | + ))} |
| 110 | + </tbody> |
| 111 | + </table> |
| 112 | + </div> |
| 113 | + </ModalPanel> |
| 114 | + </Modal> |
| 115 | + </> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + <div className="flex flex-col items-stretch gap-4 xl:flex-row"> |
| 119 | + <BalanceCategory |
| 120 | + name="Available for Withdrawal" |
| 121 | + amount={availableToWithdraw} |
| 122 | + description="The lesser of the amount you have available to stake in governance & integrity staking" |
| 123 | + action={ |
| 124 | + <TransferButton |
| 125 | + small |
| 126 | + secondary |
| 127 | + actionDescription="Move funds from your account back to your wallet" |
| 128 | + actionName="Withdraw" |
| 129 | + max={availableToWithdraw} |
| 130 | + transfer={withdraw} |
| 131 | + disabled={availableToWithdraw === 0n} |
| 132 | + /> |
| 133 | + } |
| 134 | + /> |
| 135 | + <BalanceCategory |
| 136 | + name="Available Rewards" |
| 137 | + amount={availableRewards} |
| 138 | + description="Rewards you have earned but not yet claimed from the Integrity Staking program" |
| 139 | + action={<ClaimButton disabled={availableRewards === 0n} />} |
| 140 | + {...(expiringRewards.amount > 0n && { |
| 141 | + warning: ( |
| 142 | + <> |
| 143 | + <Tokens>{expiringRewards.amount}</Tokens> will expire on{" "} |
| 144 | + {expiringRewards.expiry.toLocaleDateString()} |
| 145 | + </> |
| 146 | + ), |
| 147 | + })} |
| 148 | + /> |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </section> |
| 152 | +); |
| 153 | + |
| 154 | +type BalanceCategoryProps = { |
| 155 | + name: string; |
| 156 | + amount: bigint; |
| 157 | + description: string; |
| 158 | + action: ReactNode; |
| 159 | + warning?: ReactNode | undefined; |
| 160 | +}; |
| 161 | + |
| 162 | +const BalanceCategory = ({ |
| 163 | + name, |
| 164 | + amount, |
| 165 | + description, |
| 166 | + action, |
| 167 | + warning, |
| 168 | +}: BalanceCategoryProps) => ( |
| 169 | + <div className="flex flex-col justify-between border border-neutral-600/50 bg-pythpurple-800/60 p-6 backdrop-blur"> |
| 170 | + <div> |
| 171 | + <div className="mb-4 inline-block border border-neutral-600/50 bg-neutral-900 px-4 py-1 text-xs text-neutral-400"> |
| 172 | + {name} |
| 173 | + </div> |
| 174 | + <div> |
| 175 | + <Tokens className="text-xl font-light">{amount}</Tokens> |
| 176 | + </div> |
| 177 | + <p className="mt-4 max-w-xs text-sm text-neutral-500">{description}</p> |
| 178 | + </div> |
| 179 | + <div className="mt-4 flex flex-row items-center gap-4"> |
| 180 | + {action} |
| 181 | + {warning && <p className="max-w-xs text-xs text-red-600">{warning}</p>} |
| 182 | + </div> |
| 183 | + </div> |
| 184 | +); |
| 185 | + |
| 186 | +const ClaimButton = ( |
| 187 | + props: Omit< |
| 188 | + ComponentProps<typeof Button>, |
| 189 | + "onClick" | "disabled" | "loading" |
| 190 | + >, |
| 191 | +) => { |
| 192 | + const { state, execute } = useTransfer(claim); |
| 193 | + |
| 194 | + return ( |
| 195 | + <Button |
| 196 | + small |
| 197 | + secondary |
| 198 | + onClick={execute} |
| 199 | + disabled={state.type !== StateType.Base} |
| 200 | + loading={state.type === StateType.Submitting} |
| 201 | + {...props} |
| 202 | + > |
| 203 | + Claim |
| 204 | + </Button> |
| 205 | + ); |
| 206 | +}; |
0 commit comments