forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wallet staking bug (MystenLabs#7565)
- Show accurate earned SUI. - Show network average APY - Show total stake SUI on stake button - fix scrolling issue - Update toolip - fix validator name sorting <img width="460" alt="Screenshot 2023-01-24 at 3 37 24 PM" src="https://user-images.githubusercontent.com/8676844/214406427-f886e00d-79b5-4e9a-a17b-e0ddcc0b292d.png"> <img width="486" alt="Screenshot 2023-01-24 at 3 37 33 PM" src="https://user-images.githubusercontent.com/8676844/214406430-8a023c8f-7e00-4a32-abff-4375aa911345.png"> <img width="427" alt="Screenshot 2023-01-24 at 3 48 23 PM" src="https://user-images.githubusercontent.com/8676844/214408461-7f46713d-6a2c-435f-80a2-cb30f6a51f86.png">
- Loading branch information
Showing
19 changed files
with
425 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export function roundFloat(num: number, precision = 4) { | ||
return parseFloat(num.toFixed(precision)); | ||
} |
99 changes: 99 additions & 0 deletions
99
apps/wallet/src/ui/app/pages/home/tokens/TokenIconLink.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useFeature } from '@growthbook/growthbook-react'; | ||
import { SUI_TYPE_ARG, type SuiAddress } from '@mysten/sui.js'; | ||
import cl from 'classnames'; | ||
import { useMemo } from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
|
||
import { DelegatedAPY } from '_app/shared/delegated-apy'; | ||
import { Text } from '_app/shared/text'; | ||
import { useGetDelegatedStake } from '_app/staking/useGetDelegatedStake'; | ||
import Icon from '_components/icon'; | ||
import LoadingIndicator from '_components/loading/LoadingIndicator'; | ||
import { SuiIcons } from '_font-icons/output/sui-icons'; | ||
import { useFormatCoin } from '_hooks'; | ||
import { FEATURES } from '_src/shared/experimentation/features'; | ||
|
||
export function TokenIconLink({ | ||
accountAddress, | ||
}: { | ||
accountAddress: SuiAddress; | ||
}) { | ||
const stakingEnabled = useFeature(FEATURES.STAKING_ENABLED).on; | ||
const { data: delegations, isLoading } = | ||
useGetDelegatedStake(accountAddress); | ||
|
||
const totalActivePendingStake = useMemo(() => { | ||
if (!delegations) return 0n; | ||
return delegations.reduce( | ||
(acc, { staked_sui }) => acc + BigInt(staked_sui.principal.value), | ||
0n | ||
); | ||
}, [delegations]); | ||
|
||
const stakedValidators = useMemo(() => { | ||
if (!delegations) return []; | ||
return delegations.map( | ||
({ staked_sui }) => staked_sui.validator_address | ||
); | ||
}, [delegations]); | ||
|
||
const [formatted, symbol, queryResult] = useFormatCoin( | ||
totalActivePendingStake, | ||
SUI_TYPE_ARG | ||
); | ||
|
||
return ( | ||
<Link | ||
to="/stake" | ||
className={cl( | ||
!stakingEnabled && '!bg-gray-40', | ||
'flex mb-5 rounded-2xl w-full p-3.75 justify-between no-underline bg-sui/10 ' | ||
)} | ||
tabIndex={!stakingEnabled ? -1 : undefined} | ||
> | ||
{isLoading || queryResult.isLoading ? ( | ||
<div className="p-2 w-full flex justify-start items-center h-full"> | ||
<LoadingIndicator /> | ||
</div> | ||
) : ( | ||
<div className="flex gap-2.5 items-center"> | ||
<Icon | ||
icon={SuiIcons.Union} | ||
className={cl( | ||
!stakingEnabled ? 'text-gray-60' : 'text-hero', | ||
'text-heading4 font-normal ' | ||
)} | ||
/> | ||
<div className="flex flex-col gap-1.25"> | ||
<Text | ||
variant="body" | ||
weight="semibold" | ||
color={!stakingEnabled ? 'gray-60' : 'hero'} | ||
> | ||
{totalActivePendingStake | ||
? 'Currently Staked' | ||
: 'Stake & Earn SUI'} | ||
</Text> | ||
{!!totalActivePendingStake && ( | ||
<Text | ||
variant="body" | ||
weight="semibold" | ||
color={!stakingEnabled ? 'gray-60' : 'hero'} | ||
> | ||
{formatted} {symbol} | ||
</Text> | ||
)} | ||
</div> | ||
</div> | ||
)} | ||
<div className="flex"> | ||
{stakingEnabled && ( | ||
<DelegatedAPY stakedValidators={stakedValidators} /> | ||
)} | ||
</div> | ||
</Link> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { type SuiAddress } from '@mysten/sui.js'; | ||
import { useMemo } from 'react'; | ||
|
||
import { calculateAPY } from '../../staking/calculateAPY'; | ||
import { STATE_OBJECT } from '../../staking/usePendingDelegation'; | ||
import { Text } from '_app/shared/text'; | ||
import { IconTooltip } from '_app/shared/tooltip'; | ||
import { validatorsFields } from '_app/staking/validatorsFields'; | ||
import LoadingIndicator from '_components/loading/LoadingIndicator'; | ||
import { roundFloat } from '_helpers'; | ||
import { useGetObject } from '_hooks'; | ||
|
||
const APY_DECIMALS = 3; | ||
|
||
type DelegatedAPYProps = { | ||
stakedValidators: SuiAddress[]; | ||
}; | ||
|
||
export function DelegatedAPY({ stakedValidators }: DelegatedAPYProps) { | ||
const { data, isLoading } = useGetObject(STATE_OBJECT); | ||
|
||
const validatorsData = data && validatorsFields(data); | ||
|
||
const averageNetworkAPY = useMemo(() => { | ||
if (!validatorsData) return 0; | ||
const validators = validatorsData.validators.fields.active_validators; | ||
|
||
let stakedAPYs = 0; | ||
|
||
validators.forEach((validator) => { | ||
if ( | ||
stakedValidators.includes( | ||
validator.fields.delegation_staking_pool.fields | ||
.validator_address | ||
) | ||
) { | ||
stakedAPYs += calculateAPY(validator, +validatorsData.epoch); | ||
} | ||
}); | ||
|
||
const averageAPY = stakedAPYs / stakedValidators.length; | ||
|
||
return roundFloat(averageAPY || 0, APY_DECIMALS); | ||
}, [stakedValidators, validatorsData]); | ||
|
||
if (isLoading) { | ||
return ( | ||
<div className="p-2 w-full flex justify-center items-center h-full"> | ||
<LoadingIndicator /> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className="flex gap-0.5 items-center"> | ||
{averageNetworkAPY > 0 ? ( | ||
<> | ||
<Text variant="body" weight="semibold" color="steel-dark"> | ||
{averageNetworkAPY} | ||
</Text> | ||
<Text | ||
variant="subtitle" | ||
weight="medium" | ||
color="steel-darker" | ||
> | ||
% APY | ||
</Text> | ||
<div className="text-steel items-baseline text-body flex"> | ||
<IconTooltip | ||
tip="The average APY of all validators you are currently staking your SUI on." | ||
placement="top" | ||
/> | ||
</div> | ||
</> | ||
) : ( | ||
<Text variant="subtitle" weight="medium" color="steel-dark"> | ||
-- | ||
</Text> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.