Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/data #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ next-env.d.ts
yarn.lock
public
package-lock.json
/src/hooks/useTranslation.ts
169 changes: 88 additions & 81 deletions src/app/restake/fund/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";
import React, { useState, ChangeEvent } from "react";
import { useSearchParams } from "next/navigation";
import useTransaction from "@/hooks/useTransaction";
import { STAKE_CONTRACT_MESSAGES } from "@/lib/Message/stakeMessages";
import toast from "react-hot-toast";
Expand All @@ -11,51 +10,56 @@ import {
} from "@/lib/address";
import { Button } from "@/components/ui/moving-border";
import { TOKEN_CONTRACT_MESSAGES } from "@/lib/Message/token";

import { STAKE_QUERY_MESSAGES } from "@/lib/Query/stakeQuery";
import { useChain } from "@cosmos-kit/react";
import { CHAIN_NAME, getChainLogo } from "@/lib/utils";

export default function Staking() {
const { sendTransaction, fetchQuery } = useTransaction();
const [state, setState] = useState<string>("");
const [queryData, setQueryData] = React.useState();
const { status, address } = useChain(CHAIN_NAME);
const [state, setState] = useState<string>("deposit");
const [queryData, setQueryData] = React.useState()
const [exchange, setExchange] = useState("1");
const [amount, setAmount] = useState<string>("0");
const [open, setOpen] = useState(state); // withdraw
const [withdrawAmount, setWithdrawAmount] = useState<string>("0");
const [unstakedAmount, setUnstakedAmount] = useState<string>("0");
const [withdrawStatus, setWithdrawStatus] = useState<boolean>(true);
const [termsAccepted, setTermsAccepted] = useState<boolean>(false);
const getQueryDataFromContract = async () => {
// if (address) {

try {
const result = await fetchQuery(
STAKE_CONTRACT_ADDRESS,
STAKE_QUERY_MESSAGES.restake(
"nibi1hzty850q3vnew33yuft82j0v5fazyvfcescxhs"
)
);
setQueryData(result);
console.log("queryData", result);
} catch (error) {
console.log(error);
const getQueryDataFromContract = async () => {
if (address) {
console.log("address restake", address)

try {
const result2 = await fetchQuery(
STAKE_CONTRACT_ADDRESS,
STAKE_QUERY_MESSAGES.staker(address)
);
setQueryData(result2)
setUnstakedAmount(convertToNibi(result2?.amount_staked_stnibi)) //burn
setWithdrawAmount(convertToNibi(result2?.amount_restaked_rstnibi)) // withdraw
console.log("result2", result2);
} catch (error) {
console.log("error:", error);
}
}
};

const convertToNibi = (value: string): string => {
const valueAsNumber = parseFloat(value);
const dividedValue = valueAsNumber / Math.pow(10, 6);
return dividedValue.toString();
}

React.useEffect(() => {
const searchParams = new URLSearchParams(window.location.search);
setState(searchParams.get("state") || "");
}, []);

React.useEffect(() => {
getQueryDataFromContract();
}, []);

console.log("queryresult", queryData);

if (queryData) {
setAmount(queryData);
console.log("if is called");
}
}, [address]);

const handleTabOpen = (tabCategory: string) => {
setOpen(tabCategory);
Expand All @@ -73,16 +77,12 @@ export default function Staking() {
const transfer = async (event: { preventDefault: () => void }) => {
event.preventDefault();

const amountAsNumber = parseFloat(withdrawAmount);
const amountAsNumber = parseFloat(amount);
const multipliedAmount = amountAsNumber * Math.pow(10, 6);

const toastId = toast.loading("Transferring...");
console.log(
"transfering",
stNIBITOKEN_CONTRACT_ADDRESS,
"withdrawAmount",
withdrawAmount
);

console.log("transfering", stNIBITOKEN_CONTRACT_ADDRESS, "withdrawAmount", withdrawAmount, "amount", amount, "multipliedAmount", multipliedAmount)
const tx = await sendTransaction(
stNIBITOKEN_CONTRACT_ADDRESS,
TOKEN_CONTRACT_MESSAGES.transfer(
Expand All @@ -96,7 +96,7 @@ export default function Staking() {
console.log("transfer tx", tx);
})
.catch((err) => {
console.log("Transfer Failed");
console.log("Transfer Failed", err);
});
};

Expand All @@ -107,12 +107,8 @@ export default function Staking() {
const multipliedAmount = amountAsNumber * Math.pow(10, 6);

const toastId = toast.loading("Transferring...");
console.log(
"transfering",
stNIBITOKEN_CONTRACT_ADDRESS,
"withdrawAmount",
withdrawAmount
);

console.log("transfering", stNIBITOKEN_CONTRACT_ADDRESS, "withdrawAmount", withdrawAmount, "amount", amount, "multipliedAmount", multipliedAmount)
const tx = await sendTransaction(
rstNIBI_TOKEN_CONTRACT_ADDRESS,
TOKEN_CONTRACT_MESSAGES.transfer(
Expand All @@ -126,39 +122,52 @@ export default function Staking() {
console.log("transfer tx", tx);
})
.catch((err) => {
console.log("Transfer Failed");
console.log("Transfer Failed", err);
toast.dismiss(toastId);
});
};

const restake_deposit = async (event: { preventDefault: () => void }) => {
const restake_deposit = async (event: { preventDefault: () => void; }) => {
event.preventDefault();
transfer(event);
// transfer(event);
const toastId = toast.loading("restaking...");
console.log("unstake", amount, "exchange", exchange);
console.log("restake", amount, "exchange", exchange)
const amountAsNumber = parseFloat(amount);
const multipliedAmount = amountAsNumber * Math.pow(10, 6);

try {
console.log("try block")
const tx = await sendTransaction(
stNIBITOKEN_CONTRACT_ADDRESS,
TOKEN_CONTRACT_MESSAGES.increase_allowance("", multipliedAmount.toString(), null)
)

toast.dismiss(toastId);
toast.success(`Staked ${amount} NIBI successfully`);
console.log("restake tx", tx)
}
catch (error) {
console.log("error:", error);
toast.dismiss(toastId);
}
};

const burnrestake = async (event: { preventDefault: () => void; }) => {
event.preventDefault();
const toastId = toast.loading("buring...");

const tx = await sendTransaction(
stNIBITOKEN_CONTRACT_ADDRESS,
TOKEN_CONTRACT_MESSAGES.increase_allowance("", amount, "")
STAKE_CONTRACT_ADDRESS,
STAKE_CONTRACT_MESSAGES.burnrestakenibi()
)
.then((res) => {
toast.dismiss(toastId);
toast.success(`ReStaked ${amount} NIBI successfully`);
})
.catch((err) => {
"ReStaking Failed";
toast.dismiss(toastId);
});
};
toast.success(`Unstaked ${withdrawAmount} NIBI successfully`);
console.log("unstake sendFrom tx", tx)
}
)
}

// const burnrestake = async (event: { preventDefault: () => void; }) => {
// event.preventDefault();
// const tx = await sendTransaction(
// STAKE_CONTRACT_ADDRESS,
// STAKE_CONTRACT_MESSAGES.burnrestakenibi()
// )
// .then((res) => {
// toast.dismiss(toastId);
// toast.success(`Unstaked ${unstakeAmount} NIBI successfully`);
// console.log("unstake sendFrom tx", tx)

// })
// .catch((err) => {
Expand All @@ -167,11 +176,12 @@ export default function Staking() {
// });
// };

const withdraw_restaked = async (event: { preventDefault: () => void }) => {
const withdraw_restaked = async (event: { preventDefault: () => void; }) => {
event.preventDefault();

const toastId = toast.loading("restaking...");
console.log("unstake", amount, "exchange", exchange);
transferRestake(event);
const toastId = toast.loading("Withdrawing...");
console.log("withdraw", amount, "exchange", exchange)
const tx = await sendTransaction(
STAKE_CONTRACT_ADDRESS,
STAKE_CONTRACT_MESSAGES.withdraw_liquidity()
Expand All @@ -181,9 +191,10 @@ export default function Staking() {
toast.success(`Withdraw ${amount} NIBI successfully`);
})
.catch((err) => {
"UnStaking Failed";
console.log("Withdraw Failed", err);
toast.dismiss(toastId);
});
// await burnrestake(event);
};

return (
Expand All @@ -193,23 +204,21 @@ export default function Staking() {
<div className="flex flex-row w-full items-center gap-4">
<div
className={`w-1/2 py-4 px-1 md:px-4 text-sm font-semibold md:text-base lg:px-12 hover:underline-offset-8
rounded-2xl text-center transition-all delay-75 text-black focus:ring focus:ring-blue-400 cursor-pointer
${
open === "deposit"
? "bg-base-200 drop-shadow-2xl text-black font-semibold"
: " "
}`}
rounded-2xl text-center transition-all delay-75 text-black focus:ring focus:ring-blue-400 cursor-pointer
${open === "deposit"
? " bg-purple-100 drop-shadow-2xl text-black font-semibold"
: " "
}`}
>
<button onClick={() => handleTabOpen("deposit")}>Deposit</button>
</div>

<div
className={`w-1/2 py-4 px-4 text-sm md:text-base lg:px-12 hover:underline-offset-8
text-center rounded-2xl transition-all delay-75 text-black cursor-pointer ${
open === "withdraw"
? "bg-base-200 drop-shadow-2xl text-black font-semibold "
: " "
}`}
text-center rounded-2xl transition-all delay-75 text-black cursor-pointer ${open === "withdraw"
? " bg-purple-100 drop-shadow-2xl text-black font-semibold "
: " "
}`}
>
<button onClick={() => handleTabOpen("withdraw")}>
Withdraw
Expand Down Expand Up @@ -266,7 +275,7 @@ export default function Staking() {
type="submit"
className="bg-black dark:bg-slate-900 text-white text-lg font-bold py-4 px-4 dark:text-black border-blue-700 "
>
Stake
Restake
</Button>
</form>
</div>
Expand Down Expand Up @@ -366,6 +375,4 @@ export default function Staking() {
</main>
);
}
function fetchQuery(STAKE_CONTRACT_ADDRESS: string, arg1: any) {
throw new Error("Function not implemented.");
}

34 changes: 21 additions & 13 deletions src/components/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,39 @@ const Dashboard = () => {
}

const calculateRestakedPoints = () => {
const restakedPoint = parseFloat(restaked) + parseFloat(delegated);
const restakedPoint = (parseFloat(restaked) + parseFloat(delegated)) * 2;
setRestakedPoints(restakedPoint.toString());
}

const getQueryDataFromContract = async () => {
if (address === undefined) return;
try {
const result2 = await fetchQuery(
STAKE_CONTRACT_ADDRESS,
STAKE_QUERY_MESSAGES.staker(address)
);
console.log("result2", result2);
setDelegated(convertToNibi(result2?.amount_restaked_rstnibi));
setRestaked(convertToNibi(result2?.amount_staked_stnibi));
calculateRestakedPoints();

const Historyresult = await fetchQuery(
STAKE_CONTRACT_ADDRESS,
STAKE_QUERY_MESSAGES.all_history(1, 10)
);
setHistoryQueryData(Historyresult)
console.log("Historyresult", Historyresult);

const Stakeresult = await fetchQuery(
STAKE_CONTRACT_ADDRESS,
STAKE_QUERY_MESSAGES.staker(address)
);
setStakeQueryData(Stakeresult)
const StNIBI = Stakeresult?.amount_staked_stnibi?.toString();
const NIBI = Stakeresult?.amount_staked_unibi?.toString();

console.log("NIBI: ", NIBI, "StNIBI: ", StNIBI)
setDelegated(convertToNibi(NIBI));
console.log("Stakeresult", Stakeresult);
// const Stakeresult = await fetchQuery(
// STAKE_CONTRACT_ADDRESS,
// STAKE_QUERY_MESSAGES.staker(address)
// );
// setStakeQueryData(Stakeresult)
// const StNIBI = Stakeresult?.amount_staked_stnibi?.toString();
// const NIBI = Stakeresult?.amount_staked_unibi?.toString();
// console.log("NIBI: ", NIBI, "StNIBI: ", StNIBI)
// setDelegated(convertToNibi(NIBI));
// console.log("Stakeresult", Stakeresult);

// const Restakeresult = await fetchQuery(
// STAKE_CONTRACT_ADDRESS,
Expand All @@ -69,7 +77,7 @@ const Dashboard = () => {
// setRestakeQueryData(Stakeresult)
// const stNIBI = Restakeresult?.stnibi_amount?.toString();
// console.log("stNIBI: ", stNIBI)
setRestaked(convertToNibi(StNIBI));
// setRestaked(convertToNibi(StNIBI));
// console.log("Restakeresult", Restakeresult);
// const Rewardresult = await fetchQuery(
// REWARD_DISPATCHER_CONTRACT_ADDRESS,
Expand Down
Loading