Skip to content

Commit

Permalink
Merge branch '202411-staking-yield' of github.com:the-standard/decent…
Browse files Browse the repository at this point in the history
…ralised-dashboard-fork into beta
  • Loading branch information
ZakMooney committed Nov 8, 2024
2 parents 8dc192c + 763e7ff commit 7960065
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
30 changes: 12 additions & 18 deletions src/components/tst-staking/StakingRewardsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ import CenterLoader from "../ui/CenterLoader";

import ClaimingRewardsModal from "./ClaimingRewardsModal";

const formatUSD = (value) => {
if (value >= 0) {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: 2
}).format(Math.abs(value));
}
};

const StakingRewardsList = ({
stakedSince,
poolRewardsLoading,
Expand All @@ -43,15 +32,17 @@ const StakingRewardsList = ({

const rewardsWithPrices = rewardData.map(reward => {
const useAmount = ethers.formatUnits(reward.amount, reward.decimals);
const useRate = ethers.formatUnits(reward.dailyReward, reward.decimals);
const price = latestPrices[reward.asset] || 1; // Default to 1 for stablecoins
const usdValue = parseFloat(useAmount) * price;
const usdRate = parseFloat(useRate) * price;
return {
...reward,
usdValue: usdValue,
usdRate: usdRate,
price: price
};
})

const rows = rewardsWithPrices || [];

let noRewards = true;
Expand Down Expand Up @@ -87,7 +78,6 @@ const StakingRewardsList = ({
<tr>
<th>Asset</th>
<th>Amount</th>
<th>Value (USD)</th>
<th>Daily Rate Per TST</th>
</tr>
</thead>
Expand All @@ -99,20 +89,24 @@ const StakingRewardsList = ({
const symbol = asset?.asset;
const dailyReward = asset?.dailyReward || 0n;
const value = asset?.usdValue || 0;
const rate = asset?.usdRate || 0;

return(
<tr key={index}>
<td>
{symbol}
</td>
<td>
{ethers.formatUnits(amount, decimals)}
{ethers.formatUnits(amount, decimals)}<br/>
<span className="opacity-50">
~${value.toFixed(8)}
</span>
</td>
<td>
{formatUSD(value)}
</td>
<td className="whitespace-nowrap">
{ethers.formatUnits(dailyReward, decimals)}
{ethers.formatUnits(dailyReward, decimals)}<br/>
<span className="opacity-50">
~${rate.toFixed(8)}
</span>
</td>
</tr>
)}
Expand Down
45 changes: 29 additions & 16 deletions src/components/tst-staking/StakingSummary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,34 +211,47 @@ const StakingSummary = ({
}

function getDaysStaked() {
const start = moment.unix((Number(rawStakedSince)));
let start = moment.unix((Number(rawStakedSince)));
const today = moment();
if (!rawStakedSince) {
start = today;
}
const since = today.diff(start, 'days');
return since;
}

// const calculateSimpleAPY = (stakedAmount, totalRewardsValue, daysStaked) => {
// const dailyEarnings = Number(totalRewardsValue / daysStaked) || 0;
// const annualEarningsPerTST = (dailyEarnings * 365) / Number(stakedAmount) || 0;
// return annualEarningsPerTST * 100;
// }
const calculateSimpleAPY = (stakedAmount, totalRewardsValue, daysStaked) => {
const dailyEarnings = totalRewardsValue / daysStaked;
const annualEarningsPerTST = (dailyEarnings * 365) / stakedAmount;
// console.log(123123, stakedAmount, totalRewardsValue, daysStaked)
return annualEarningsPerTST * 100;
const dailyEarnings = totalRewardsValue || 0;
const annualEarningsPerTST = (dailyEarnings * 365) / Number(stakedAmount) || 0;

return annualEarningsPerTST;
}

const rewardsWithPrices = rewardsData.map(reward => {
const useAmount = ethers.formatUnits(reward.amount, reward.decimals);
const useRate = ethers.formatUnits(reward.dailyReward, reward.decimals);
const price = latestPrices[reward.asset] || 1; // Default to 1 for stablecoins
const usdValue = parseFloat(useAmount) * price;
const usdRate = parseFloat(useRate) * price;
return {
...reward,
usdValue: usdValue,
usdRate: usdRate,
price: price
};
})

const totalValue = rewardsWithPrices.reduce((sum, reward) => sum + reward.usdValue, 0) || 0;

const totalValueUSD = rewardsWithPrices.reduce((sum, reward) => sum + reward.usdValue, 0) || 0;
const totalRateUSD = rewardsWithPrices.reduce((sum, reward) => sum + reward.usdRate, 0) || 0;

const daysStaked = getDaysStaked() || 0;
const dailyEarnings = (totalValue / daysStaked) || 0;
const dailyEarnings = (totalRateUSD / daysStaked) || 0;
const monthlyProjection = (dailyEarnings * 30) || 0;

const currentTier = getCurrentTier(stakedAmount);
Expand All @@ -248,7 +261,7 @@ const StakingSummary = ({
if (nextTier?.minAmount) {
progress = (stakedAmount / nextTier.minAmount) * 100;
}
const apyDisplay = calculateSimpleAPY(stakedAmount, totalValue, daysStaked) + '%' || '0%';
const apyDisplay = calculateSimpleAPY(stakedAmount, totalRateUSD, daysStaked) + '%' || '0%';

let stakeRatio = 'TODO.DOTO';

Expand All @@ -275,7 +288,7 @@ const StakingSummary = ({
Participate in protocol governance
</Typography>

<div className="grid grid-cols-2 gap-4 mb-4">
<div className="grid grid-cols-1 gap-4 mb-4">
<div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
<div className="w-full">
<Typography variant="p">
Expand All @@ -286,7 +299,7 @@ const StakingSummary = ({
</Typography>
</div>
</div>
<div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
{/* <div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
<div className="w-full">
<Typography variant="p">
Historical APY
Expand All @@ -304,7 +317,7 @@ const StakingSummary = ({
0%
</Typography>
</div>
</div>
</div> */}
</div>

<div className="grid grid-cols-1 gap-4 mb-4">
Expand Down Expand Up @@ -343,7 +356,7 @@ const StakingSummary = ({
Total Value Accrued
</Typography>
<Typography variant="h2">
{formatUSD(totalValue)}
{formatUSD(totalValueUSD)}
</Typography>
</div>
</div>
Expand Down Expand Up @@ -381,7 +394,7 @@ const StakingSummary = ({
</Typography>

{/* <div className="grid grid-cols-3 gap-4 mb-4"> */}
<div className="grid grid-cols-2 gap-2 mb-2">
<div className="grid grid-cols-1 gap-2 mb-2">
<div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
<div className="w-full">
<Typography variant="p">
Expand All @@ -392,7 +405,7 @@ const StakingSummary = ({
</Typography>
</div>
</div>
<div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
{/* <div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
<div className="w-full">
<Typography variant="p">
Historical APY
Expand All @@ -410,7 +423,7 @@ const StakingSummary = ({
{apyDisplay}
</Typography>
</div>
</div>
</div> */}
{/* <div className="bg-base-300/40 p-4 rounded-lg w-full flex items-center">
<div className="w-full">
<Typography variant="p">
Expand Down Expand Up @@ -474,7 +487,7 @@ const StakingSummary = ({
Total Value Accrued
</Typography>
<Typography variant="h2">
{formatUSD(totalValue)}
{formatUSD(totalValueUSD)}
</Typography>
<Typography variant="p">
Current market prices
Expand Down

0 comments on commit 7960065

Please sign in to comment.