Skip to content

Commit

Permalink
Merge pull request #95 from moonshotcollective/feature-unstake
Browse files Browse the repository at this point in the history
Unstake feature for end of active round
  • Loading branch information
farque65 authored Sep 16, 2022
2 parents e6aebde + 4bb9c19 commit 54c01b8
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 28 deletions.
15 changes: 12 additions & 3 deletions packages/react-app/src/components/Rounds.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ const Rounds = ({
address,
readContracts,
writeContracts,
unstake,
migrate,
round,
latestRound,
roundEnded,
mainnetProvider,
unstakeUsers,
userSigner,
targetNetwork,
roundData,
Expand All @@ -32,14 +31,22 @@ const Rounds = ({
const [isModalVisible, setIsModalVisible] = useState(false);
const [stakingType, setStakingType] = useState("self");

// console.log("view new data ", data, error);
const unstake = async amount => {
tx(writeContracts.IDStaking.unstake(round + "", ethers.utils.parseUnits(amount)));
};

const unstakeUsers = async users => {
tx(writeContracts.IDStaking.unstakeUsers(round + "", users));
};

return (
<>
<div className="text-gray-600 body-font">
<StakeItem
icon={<UserOutlined style={{ fontSize: "25px" }} />}
title="Self Staking"
roundEnded={roundEnded}
unstake={unstake}
description="Stake GTC on yourself"
amount={getSelfStakeAmount(roundData)}
buttonText={getSelfStakeAmount(roundData) ? "Modify Stake" : "Stake"}
Expand All @@ -51,6 +58,8 @@ const Rounds = ({

<StakeItemCommunity
icon={<UsergroupAddOutlined style={{ fontSize: "25px" }} />}
roundEnded={roundEnded}
unstakeUsers={unstakeUsers}
title="Community Staking"
description="Stake GTC on other people"
amount={getCommunityStakeAmount(roundData)}
Expand Down
19 changes: 12 additions & 7 deletions packages/react-app/src/components/StakeItem.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import UnstakeButton from "./UnstakeButton";

const StakeItem = ({ icon, title, description, amount, buttonText, buttonHandler }) => {
const StakeItem = ({ icon, roundEnded, unstake, title, description, amount, buttonText, buttonHandler }) => {
return (
<div className="flex items-start md:items-center mx-auto border-b pb-10 mb-10 border-divider flex-col md:flex-row">
<div className="flex flex-1 items-center justify-start">
Expand All @@ -16,12 +17,16 @@ const StakeItem = ({ icon, title, description, amount, buttonText, buttonHandler
<h2 className="text-gray-900 text-lg title-font font-medium mb-0">{amount} GTC</h2>
<span className="leading-relaxed text-base">Staked</span>
</div>
<button
onClick={buttonHandler}
className="flex md:max-w-button w-full justify-center text-white text-center bg-purple-connectPurple border-0 py-2 focus:outline-none hover:bg-indigo-600 rounded-sm text-lg font-miriam-libre"
>
<span>{buttonText}</span>
</button>
{roundEnded ? (
<UnstakeButton amount={amount} handler={() => unstake(amount)} />
) : (
<button
onClick={buttonHandler}
className="flex md:max-w-button w-full justify-center text-white text-center bg-purple-connectPurple border-0 py-2 focus:outline-none hover:bg-indigo-600 rounded-sm text-lg font-miriam-libre"
>
<span>{buttonText}</span>
</button>
)}
</div>
);
};
Expand Down
25 changes: 19 additions & 6 deletions packages/react-app/src/components/StakeItemCommunity.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import React from "react";
// Format User Address
import DisplayAddressEns from "./DisplayAddressEns";
import { formatAmountUnits } from "./StakingModal/utils";
import UnstakeButton from "./UnstakeButton";

const StakeItemCommunity = ({
icon,
title,
roundEnded,
description,
amount,
unstakeUsers,
buttonText,
buttonHandler,
roundData,
mainnetProvider,
}) => {
const unstakeHandler = async () => {
const users = roundData.map(i => i?.to?.address);

await unstakeUsers(users);
};

return (
<div className="border-divider border-b">
<div className="flex items-start md:items-center mx-auto pb-4 flex-col md:flex-row">
Expand All @@ -30,12 +39,16 @@ const StakeItemCommunity = ({
<h2 className="text-gray-900 text-lg title-font font-medium mb-0">{amount} GTC</h2>
<span className="leading-relaxed text-base">Staked</span>
</div>
<button
onClick={buttonHandler}
className="flex md:max-w-button w-full justify-center text-white text-center bg-purple-connectPurple border-0 py-2 focus:outline-none hover:bg-indigo-600 rounded-sm text-lg font-miriam-libre"
>
<span>{buttonText}</span>
</button>
{roundEnded ? (
<UnstakeButton amount={amount} handler={unstakeHandler} />
) : (
<button
onClick={buttonHandler}
className="flex md:max-w-button w-full justify-center text-white text-center bg-purple-connectPurple border-0 py-2 focus:outline-none hover:bg-indigo-600 rounded-sm text-lg font-miriam-libre"
>
<span>{buttonText}</span>
</button>
)}
</div>

{/* List all users staked on */}
Expand Down
18 changes: 18 additions & 0 deletions packages/react-app/src/components/UnstakeButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";

const UnstakeButton = ({ amount = 0, handler }) => {
const noBalance = amount === 0;

return (
<button
onClick={noBalance ? null : handler}
className={`flex md:max-w-button w-full justify-center text-white text-center border-0 py-2 focus:outline-none rounded-sm text-lg font-miriam-libre ${
noBalance ? "bg-grayBtn" : "bg-dustRed"
}`}
>
<span>{noBalance ? "Stake" : "Unstake All"}</span>
</button>
);
};

export default UnstakeButton;
2 changes: 1 addition & 1 deletion packages/react-app/src/contracts/hardhat_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@
"chainId": "5",
"contracts": {
"IDStaking": {
"address": "0xA9757F8bA79341553D6728080A352EB8281d47eF",
"address": "0xe3d7684C87c6dD50602B34D3CDAc99752950c1a1",
"abi": [
{
"inputs": [
Expand Down
16 changes: 5 additions & 11 deletions packages/react-app/src/views/StakeDashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,6 @@ function StakeDashboard({
tx(writeContracts.Token.mintAmount(ethers.utils.parseUnits("1000")));
};

const unstake = async (id, amount) => {
tx(writeContracts.IDStaking.unstake(id + "", ethers.utils.parseUnits(amount)));
};

const unstakeUsers = async (id, users) => {
tx(writeContracts.IDStaking.unstakeUsers(id + "", users));
};

const migrate = async id => {
tx(writeContracts.IDStaking.migrateStake(id + ""));
};
Expand Down Expand Up @@ -131,6 +123,9 @@ function StakeDashboard({
},
});

const roundEndTimestamp = moment.unix((start || zero).add(duration || zero).toString());
const roundEnded = moment.unix(moment().toString()).unix() >= roundEndTimestamp.unix();

return (
<>
<Navbar
Expand Down Expand Up @@ -166,7 +161,7 @@ function StakeDashboard({
{roundInView ? (
<p className="font-miriam-libre text-base text-left mb-4">
{moment.unix((start || zero).toString()).format("MMMM Do YYYY (h:mm:ss a)")} {" - "}
{moment.unix((start || zero).add(duration || zero).toString()).format("MMMM Do YYYY (h:mm:ss a)")}
{roundEndTimestamp.format("MMMM Do YYYY (h:mm:ss a)")}
</p>
) : (
<></>
Expand All @@ -181,12 +176,11 @@ function StakeDashboard({
tx={tx}
key={roundInView}
round={roundInView}
unstake={unstake}
address={address}
migrate={migrate}
roundEnded={roundEnded}
latestRound={latestRound}
tokenSymbol={tokenSymbol}
unstakeUsers={unstakeUsers}
readContracts={readContracts}
writeContracts={writeContracts}
mainnetProvider={mainnetProvider}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-app/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = {
asideBG: "#F6F0FF",
orange: colors.orange,
signout: "#FF4D4F",
grayBtn: "#BFBFBF",
dustRed: "#F5222D",
purple: {
darkpurple: "#0E0333",
connectPurple: "#6F3FF5",
Expand Down

1 comment on commit 54c01b8

@vercel
Copy link

@vercel vercel bot commented on 54c01b8 Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

staging-identity-staking – ./packages/react-app

id-staking.vercel.app
staging-identity-staking-git-dev-id-staking.vercel.app
staging-identity-staking-id-staking.vercel.app

Please sign in to comment.