From 3521209eb6c222216fd06bf80bbaaea10ae77c1b Mon Sep 17 00:00:00 2001 From: Elliot Braem <16282460+elliotBraem@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:28:45 -0400 Subject: [PATCH] add staking widget (#497) --- widget/page/stake/Index.tsx | 231 ++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 widget/page/stake/Index.tsx diff --git a/widget/page/stake/Index.tsx b/widget/page/stake/Index.tsx new file mode 100644 index 00000000..c74f19cd --- /dev/null +++ b/widget/page/stake/Index.tsx @@ -0,0 +1,231 @@ +const { Tailwind } = VM.require("uiisnear.near/widget/tailwind"); +const { Button, ButtonConf } = VM.require("uiisnear.near/widget/button"); +const { Input } = VM.require("uiisnear.near/widget/input"); +const { Label } = VM.require("uiisnear.near/widget/label"); +const { Switch } = VM.require("uiisnear.near/widget/switch"); +const { Alert, AlertTitle } = VM.require("uiisnear.near/widget/alert"); +const { ClassnameConf } = VM.require("uiisnear.near/widget/utils"); +const { + Card, + CardHeader, + CardFooter, + CardTitle, + CardDescription, + CardContent, + cardClassname, + cardFooterClassname, +} = VM.require("uiisnear.near/widget/card"); +const { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } = + VM.require("uiisnear.near/widget/select"); + +if (Tailwind == undefined) return ""; +if (ButtonConf == undefined) return ""; +if (ClassnameConf == undefined) return ""; + +const [buttonCancel, setButtonCancel] = useState(""); +const [card, setCard] = useState(""); +const [cardFooter, setCardFooter] = useState(""); +const [amount, setAmount] = useState(null); +const [isUnstakeSelected, setSelected] = useState(false); + +if (buttonCancel === "") + return ; + +if (card === "") { + let className = `${cardClassname} max-w-lg sm:w-96`; + return ; +} + +if (cardFooter === "") { + let className = `${cardFooterClassname} flex justify-between`; + return ; +} + +if (ButtonConf == undefined) return ""; +const [buttonLink, setButtonLink] = useState(""); + +if (buttonLink === "") + return ; + +const account = "builddao.poolv1.near"; + +function onStake() { + Near.call({ + contractName: account, + methodName: "deposit_and_stake", + args: {}, + deposit: Big(amount).mul(Big(10).pow(24)).toFixed(), + gas: 200000000000000, + }); +} + +function onUnstake() { + Near.call({ + contractName: account, + methodName: "unstake", + args: { + amount: Big(amount).mul(Big(10).pow(24)).toFixed(), + }, + gas: 200000000000000, + }); +} + +function onWithdraw() { + Near.call({ + contractName: account, + methodName: "withdraw_all", + args: {}, + gas: 200000000000000, + }); +} +const userLoggedIn = context.accountId; + +if (!userLoggedIn) { + return ( + +
+ + Please login to continue! + } /> + +
+
+ ); +} + +const balanceResp = fetch( + `https://api3.nearblocks.io/v1/account/${userLoggedIn}` +); +const nearBalance = Big(balanceResp?.body?.account?.[0]?.amount ?? "0") + .div(Big(10).pow(24)) + .toFixed(4); + +function convertAmountToReadableFormat(value) { + return Big(value ?? "0") + .div(1e24) + .toFixed(4); +} + +const stakedBalanceResp = Near.view(account, "get_account_staked_balance", { + account_id: userLoggedIn, +}); +const stakedBalance = convertAmountToReadableFormat(stakedBalanceResp); +const unstakedBalanceResp = Near.view(account, "get_account_unstaked_balance", { + account_id: userLoggedIn, +}); +const unstakedBalance = convertAmountToReadableFormat(unstakedBalanceResp); +const allowedToWithdraw = Near.view( + account, + "is_account_unstaked_balance_available", + { account_id: userLoggedIn } +); + +const Container = styled.div` + .text-red { + color: red; + } +`; + +return ( + + + + + + {isUnstakeSelected ? "Unstake NEAR from" : "Stake NEAR to"} the + Build DAO treasury + + +
+ setSelected(e)} + /> + +
+
+
+ +
+
+ + setAmount(e.target.value)} + /> +

+ {!isUnstakeSelected ? ( + `Balance: ${nearBalance} NEAR` + ) : ( +

+ Staked Balance: {stakedBalance} NEAR + {parseFloat(unstakedBalance) > 0 && ( +
+ Unstaked Balance: {unstakedBalance} NEAR + {allowedToWithdraw && ( + + )} +
+ )} +
+ )} +

+
+ {!isUnstakeSelected && + parseFloat(amount ?? "0") > parseFloat(nearBalance ?? "0") && ( +

+ Stake amount is more than your balance +

+ )} + {isUnstakeSelected && + parseFloat(amount ?? "0") > parseFloat(stakedBalance ?? "0") && ( +

+ Unstake amount is more than your staked balance +

+ )} +
+
+ + {!isUnstakeSelected ? ( + + ) : ( + + )} + + + + +
+
+
+);