-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(suite): add support for Solana staking rewards
- Loading branch information
Showing
10 changed files
with
389 additions
and
5 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,87 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
|
||
import { | ||
EverstakeRewardsEndpointType, | ||
StakeAccountRewards, | ||
StakeRootState, | ||
fetchEverstakeRewards, | ||
selectStakingRewards, | ||
} from '@suite-common/wallet-core'; | ||
import { useDebounce } from '@trezor/react-utils'; | ||
|
||
import { Account } from 'src/types/wallet'; | ||
|
||
const PAGE_SIZE_DEFAULT = 10; | ||
|
||
export const useSolanaRewards = (account: Account) => { | ||
const { data, isLoading } = | ||
useSelector((state: StakeRootState) => selectStakingRewards(state, account.symbol)) || {}; | ||
|
||
const { rewards } = data ?? {}; | ||
const selectedAccountRewards = rewards?.[account.descriptor]; | ||
|
||
const dispatch = useDispatch(); | ||
const debounce = useDebounce(); | ||
|
||
const itemsPerPage = PAGE_SIZE_DEFAULT; | ||
const startPage = 1; | ||
|
||
const [currentPage, setSelectedPage] = useState(startPage); | ||
const [slicedRewards, setSlicedRewards] = useState<StakeAccountRewards[]>([]); | ||
|
||
const startIndex = (currentPage - 1) * itemsPerPage; | ||
const stopIndex = startIndex + itemsPerPage; | ||
|
||
const fetchRewards = useCallback( | ||
async ({ symbol, descriptor }: Account) => { | ||
const controller = new AbortController(); | ||
await debounce(() => { | ||
if (symbol !== 'sol') return; | ||
dispatch( | ||
fetchEverstakeRewards({ | ||
symbol, | ||
endpointType: EverstakeRewardsEndpointType.GetRewards, | ||
address: descriptor, | ||
signal: controller.signal, | ||
}), | ||
); | ||
}); | ||
|
||
return () => controller.abort(); | ||
}, | ||
[dispatch, debounce], | ||
); | ||
|
||
useEffect(() => { | ||
fetchRewards(account); | ||
}, [account, fetchRewards]); | ||
|
||
useEffect(() => { | ||
if (selectedAccountRewards) { | ||
const slicedRewards = selectedAccountRewards?.slice(startIndex, stopIndex); | ||
setSlicedRewards(slicedRewards); | ||
} | ||
}, [currentPage, selectedAccountRewards, startIndex, stopIndex]); | ||
|
||
useEffect(() => { | ||
// reset page on account change | ||
setSelectedPage(startPage); | ||
}, [account.descriptor, account.symbol, startPage]); | ||
|
||
const totalItems = selectedAccountRewards?.length ?? 0; | ||
const showPagination = totalItems > itemsPerPage; | ||
const isLastPage = stopIndex >= totalItems; | ||
|
||
return { | ||
slicedRewards, | ||
isLoading, | ||
currentPage, | ||
setSelectedPage, | ||
totalItems, | ||
itemsPerPage, | ||
showPagination, | ||
isLastPage, | ||
fetchRewards, | ||
}; | ||
}; |
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
163 changes: 163 additions & 0 deletions
163
.../suite/src/views/wallet/staking/components/SolStakingDashboard/components/RewardsList.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,163 @@ | ||
import React, { useRef } from 'react'; | ||
|
||
import { SOLANA_EPOCH_DAYS } from '@suite-common/wallet-constants'; | ||
import { formatNetworkAmount } from '@suite-common/wallet-utils'; | ||
import { | ||
Badge, | ||
Card, | ||
Column, | ||
Icon, | ||
IconButton, | ||
Row, | ||
SkeletonStack, | ||
Text, | ||
Tooltip, | ||
} from '@trezor/components'; | ||
import { spacings } from '@trezor/theme'; | ||
|
||
import { DashboardSection } from 'src/components/dashboard'; | ||
import { | ||
CoinBalance, | ||
FiatValue, | ||
FormattedDate, | ||
HiddenPlaceholder, | ||
Translation, | ||
} from 'src/components/suite'; | ||
import { Pagination } from 'src/components/wallet'; | ||
import { useSolanaRewards } from 'src/hooks/wallet/useSolanaRewards'; | ||
import { Account } from 'src/types/wallet'; | ||
import SkeletonTransactionItem from 'src/views/wallet/transactions/TransactionList/SkeletonTransactionItem'; | ||
import { ColDate } from 'src/views/wallet/transactions/TransactionList/TransactionsGroup/CommonComponents'; | ||
|
||
import { RewardsEmpty } from './RewardsEmpty'; | ||
|
||
interface RewardsListProps { | ||
account: Account; | ||
} | ||
|
||
export const RewardsList = ({ account }: RewardsListProps) => { | ||
const sectionRef = useRef<HTMLDivElement>(null); | ||
|
||
const { | ||
slicedRewards, | ||
isLoading, | ||
currentPage, | ||
setSelectedPage, | ||
totalItems, | ||
itemsPerPage, | ||
showPagination, | ||
isLastPage, | ||
fetchRewards, | ||
} = useSolanaRewards(account); | ||
|
||
const isSolanaMainnet = account.symbol === 'sol'; | ||
|
||
const onPageSelected = (page: number) => { | ||
setSelectedPage(page); | ||
if (sectionRef.current) { | ||
sectionRef.current.scrollIntoView(); | ||
} | ||
}; | ||
|
||
return ( | ||
<DashboardSection | ||
ref={sectionRef} | ||
heading={<Translation id="TR_REWARDS" />} | ||
data-testid="@wallet/accounts/rewards-list" | ||
> | ||
{isLoading ? ( | ||
<SkeletonStack $col $childMargin="0px 0px 16px 0px"> | ||
<SkeletonTransactionItem /> | ||
<SkeletonTransactionItem /> | ||
<SkeletonTransactionItem /> | ||
</SkeletonStack> | ||
) : ( | ||
<> | ||
{slicedRewards?.map(reward => ( | ||
<React.Fragment key={reward.epoch}> | ||
<Row> | ||
<ColDate> | ||
<FormattedDate | ||
value={reward?.time ?? undefined} | ||
day="numeric" | ||
month="long" | ||
year="numeric" | ||
/> | ||
</ColDate> | ||
</Row> | ||
<Card> | ||
<Row | ||
justifyContent="space-between" | ||
margin={{ horizontal: spacings.xs, bottom: spacings.xs }} | ||
> | ||
<Row gap={spacings.xs}> | ||
<Icon name="arrowLineDown" variant="tertiary" /> | ||
<Column> | ||
<Text typographyStyle="body" variant="tertiary"> | ||
<Translation id="TR_REWARD" /> | ||
</Text> | ||
<Tooltip | ||
maxWidth={250} | ||
content={ | ||
<Translation | ||
id="TR_STAKE_REWARDS_TOOLTIP" | ||
values={{ count: SOLANA_EPOCH_DAYS }} | ||
/> | ||
} | ||
> | ||
<Badge size="small"> | ||
<Row gap={spacings.xxs} alignItems="center"> | ||
<Translation | ||
id="TR_STAKE_REWARDS_BAGE" | ||
values={{ count: reward.epoch }} | ||
/> | ||
<Icon name="info" size="small" /> | ||
</Row> | ||
</Badge> | ||
</Tooltip> | ||
</Column> | ||
</Row> | ||
{reward?.amount && ( | ||
<Column alignItems="end"> | ||
<HiddenPlaceholder> | ||
<CoinBalance | ||
value={formatNetworkAmount( | ||
reward?.amount, | ||
account.symbol, | ||
)} | ||
symbol={account.symbol} | ||
/> | ||
</HiddenPlaceholder> | ||
<HiddenPlaceholder> | ||
<Text typographyStyle="hint" variant="tertiary"> | ||
<FiatValue | ||
amount={formatNetworkAmount( | ||
reward?.amount, | ||
account.symbol, | ||
)} | ||
symbol={account.symbol} | ||
/> | ||
</Text> | ||
</HiddenPlaceholder> | ||
</Column> | ||
)} | ||
</Row> | ||
</Card> | ||
</React.Fragment> | ||
))} | ||
</> | ||
)} | ||
|
||
{showPagination && ( | ||
<Pagination | ||
hasPages={true} | ||
currentPage={currentPage} | ||
isLastPage={isLastPage} | ||
perPage={itemsPerPage} | ||
totalItems={totalItems} | ||
onPageSelected={onPageSelected} | ||
/> | ||
)} | ||
</DashboardSection> | ||
); | ||
}; |
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.