Skip to content

Commit

Permalink
Fix voting power on dashboard & staking panel (#754)
Browse files Browse the repository at this point in the history
* Fix voting power on dashboard & staking panel

* Remove unused imports
  • Loading branch information
bpierre authored Jan 23, 2025
1 parent ba05192 commit 9d7539b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
34 changes: 32 additions & 2 deletions frontend/app/src/liquity-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
useSpYieldGainParameters,
} from "@/src/liquity-stability-pool";
import {
useGovernanceStats,
useGovernanceUser,
useInterestRateBrackets,
useStabilityPool,
useStabilityPoolDeposit,
Expand Down Expand Up @@ -182,7 +184,31 @@ function earnPositionFromGraph(
};
}

export function useAccountVotingPower(account: Address | null, lqtyDiff: bigint = 0n) {
const govUser = useGovernanceUser(account);
const govStats = useGovernanceStats();

return useMemo(() => {
if (!govStats.data || !govUser.data) {
return null;
}

const t = BigInt(Math.floor(Date.now() / 1000));

const { totalLQTYStaked, totalOffset } = govStats.data;
const totalVp = (BigInt(totalLQTYStaked) + lqtyDiff) * t - BigInt(totalOffset);

const { stakedLQTY, stakedOffset } = govUser.data;
const userVp = (BigInt(stakedLQTY) + lqtyDiff) * t - BigInt(stakedOffset);

// pctShare(t) = userVotingPower(t) / totalVotingPower(t)
return dn.div([userVp, 18], [totalVp, 18]);
}, [govUser.data, govStats.data, lqtyDiff]);
}

export function useStakePosition(address: null | Address) {
const votingPower = useAccountVotingPower(address);

const LqtyStaking = getProtocolContract("LqtyStaking");
const LusdToken = getProtocolContract("LusdToken");
const Governance = getProtocolContract("Governance");
Expand All @@ -199,7 +225,7 @@ export function useStakePosition(address: null | Address) {
query: { enabled: Boolean(address) && userProxyAddress.isSuccess },
});

return useReadContracts({
const stakePosition = useReadContracts({
contracts: [
{
...LqtyStaking,
Expand Down Expand Up @@ -250,11 +276,15 @@ export function useStakePosition(address: null | Address) {
eth: dnum18(pendingEthGainResult.result + (userProxyBalance.data?.value ?? 0n)),
lusd: dnum18(pendingLusdGainResult.result + lusdBalanceResult.result),
},
share: dn.gt(totalStaked, 0) ? dn.div(deposit, totalStaked) : dnum18(0),
share: dnum18(0),
};
},
},
});

return stakePosition.data && votingPower
? { ...stakePosition, data: { ...stakePosition.data, share: votingPower } }
: stakePosition;
}

export function useTroveNftUrl(collIndex: null | CollIndex, troveId: null | TroveId) {
Expand Down
21 changes: 9 additions & 12 deletions frontend/app/src/screens/StakeScreen/PanelStaking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import content from "@/src/content";
import { dnumMax } from "@/src/dnum-utils";
import { parseInputFloat } from "@/src/form-utils";
import { fmtnum } from "@/src/formatting";
import { useStakePosition } from "@/src/liquity-utils";
import { useAccountVotingPower, useStakePosition } from "@/src/liquity-utils";
import { useAccount, useBalance } from "@/src/services/Ethereum";
import { usePrice } from "@/src/services/Prices";
import { useTransactionFlow } from "@/src/services/TransactionFlow";
Expand Down Expand Up @@ -37,21 +37,18 @@ export function PanelStaking() {
mode === "withdraw" ? -1 : 1,
);

const updatedShare = useAccountVotingPower(
account.address ?? null,
depositDifference[0],
);

const updatedDeposit = stakePosition.data?.deposit
? dnumMax(
dn.add(stakePosition.data?.deposit, depositDifference),
dn.from(0, 18),
)
: dn.from(0, 18);

const updatedTotalStaked = stakePosition.data?.totalStaked
? dn.add(stakePosition.data.totalStaked, depositDifference)
: null;

const updatedShare = updatedTotalStaked && dn.gt(updatedTotalStaked, 0)
? dn.div(updatedDeposit, updatedTotalStaked)
: dn.from(0, 18);

const lqtyBalance = useBalance(account.address, "LQTY");
const isDepositFilled = parsedValue && dn.gt(parsedValue, 0);
const hasDeposit = stakePosition.data?.deposit && dn.gt(
Expand Down Expand Up @@ -156,7 +153,7 @@ export function PanelStaking() {
/>
}
footer={{
start: (
start: null, /* (
<Field.FooterInfo
label="New voting power"
value={
Expand All @@ -170,7 +167,7 @@ export function PanelStaking() {
</HFlex>
}
/>
),
)*/
}}
/>
<div
Expand Down Expand Up @@ -245,7 +242,7 @@ export function PanelStaking() {
size="large"
wide
onClick={() => {
if (account.address) {
if (account.address && updatedShare) {
txFlow.start({
flowId: mode === "deposit" ? "stakeDeposit" : "unstakeDeposit",
backLink: ["/stake", "Back to stake position"],
Expand Down

0 comments on commit 9d7539b

Please sign in to comment.