-
-
Notifications
You must be signed in to change notification settings - Fork 272
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
8 changed files
with
322 additions
and
3 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
190 changes: 190 additions & 0 deletions
190
.../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,190 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
|
||
import { | ||
EverstakeRewardsEndpointType, | ||
fetchEverstakeRewards, | ||
selectStakingRewards, | ||
StakeAccountRewards, | ||
} from '@suite-common/wallet-core'; | ||
import { formatNetworkAmount } from '@suite-common/wallet-utils'; | ||
import { Badge, Card, Column, Icon, Row, SkeletonStack, Text, Tooltip } from '@trezor/components'; | ||
import { spacings } from '@trezor/theme'; | ||
import { SOLANA_EPOCH_DAYS } from '@suite-common/wallet-constants'; | ||
|
||
import { | ||
CoinBalance, | ||
FiatValue, | ||
FormattedDate, | ||
HiddenPlaceholder, | ||
Translation, | ||
} from 'src/components/suite'; | ||
import { DashboardSection } from 'src/components/dashboard'; | ||
import { useDispatch, useSelector } from 'src/hooks/suite'; | ||
import { Account } from 'src/types/wallet'; | ||
import { Pagination } from 'src/components/wallet'; | ||
import SkeletonTransactionItem from 'src/views/wallet/transactions/TransactionList/SkeletonTransactionItem'; | ||
import { ColDate } from 'src/views/wallet/transactions/TransactionList/TransactionsGroup/CommonComponents'; | ||
|
||
const PAGE_SIZE_DEFAULT = 10; | ||
|
||
interface RewardsListProps { | ||
account: Account; | ||
} | ||
|
||
export const RewardsList = ({ account }: RewardsListProps) => { | ||
const anchor = useSelector(state => state.router.anchor); | ||
const { data, isLoading } = | ||
useSelector(state => selectStakingRewards(state, account?.symbol)) || {}; | ||
|
||
const { rewards } = data ?? {}; | ||
const sectionRef = useRef<HTMLDivElement>(null); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
const perPage = PAGE_SIZE_DEFAULT; | ||
const startPage = 1; | ||
|
||
const [currentPage, setSelectedPage] = useState(startPage); | ||
const [slicedRewards, setSlicedRewards] = useState<StakeAccountRewards[]>([]); | ||
|
||
const startIndex = (currentPage - 1) * perPage; | ||
const stopIndex = startIndex + perPage; | ||
|
||
useEffect(() => { | ||
// Fetch rewards only for the Solana mainnet | ||
if (account.symbol === 'sol') { | ||
dispatch( | ||
fetchEverstakeRewards({ | ||
symbol: account.symbol, | ||
endpointType: EverstakeRewardsEndpointType.GetRewards, | ||
address: account.descriptor, | ||
}), | ||
); | ||
} | ||
}, [anchor, account, dispatch]); | ||
|
||
useEffect(() => { | ||
if (rewards) { | ||
const slicedRewards = rewards?.slice(startIndex, stopIndex); | ||
setSlicedRewards(slicedRewards); | ||
} | ||
}, [currentPage, rewards, startIndex, stopIndex]); | ||
|
||
useEffect(() => { | ||
// reset page on account change | ||
setSelectedPage(startPage); | ||
}, [account.descriptor, account.symbol, startPage]); | ||
|
||
const totalItems = rewards?.length ?? 0; | ||
const showPagination = totalItems > perPage; | ||
const isLastPage = stopIndex >= totalItems; | ||
|
||
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={perPage} | ||
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
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.