Skip to content

Commit

Permalink
feat: add staking history items (#4690)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwoktung authored Jun 6, 2024
1 parent 3b150d3 commit c210b6f
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 118 deletions.
43 changes: 43 additions & 0 deletions packages/kit-bg/src/services/ServiceStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
IAprItem,
IAprToken,
ILidoEthOverview,
ILidoHistoryItem,
ILidoMaticOverview,
IServerEvmTransaction,
IStakeTag,
Expand Down Expand Up @@ -62,6 +63,26 @@ class ServiceStaking extends ServiceBase {
return resp.data.data;
}

@backgroundMethod()
public async getLidoEthHistory({
accountId,
networkId,
}: {
accountId: string;
networkId: string;
}) {
const accountAddress =
await this.backgroundApi.serviceAccount.getAccountAddressForApi({
networkId,
accountId,
});
const client = await this.getClient(EServiceEndpointEnum.Earn);
const resp = await client.get<{
data: ILidoHistoryItem[];
}>(`/earn/v1/lido-eth/history`, { params: { accountAddress, networkId } });
return resp.data.data;
}

@backgroundMethod()
public async buildLidoEthStakingTransaction({
amount,
Expand Down Expand Up @@ -168,6 +189,28 @@ class ServiceStaking extends ServiceBase {
return resp.data.data;
}

@backgroundMethod()
public async getLidoMaticHistory({
accountId,
networkId,
}: {
accountId: string;
networkId: string;
}) {
const accountAddress =
await this.backgroundApi.serviceAccount.getAccountAddressForApi({
networkId,
accountId,
});
const client = await this.getClient(EServiceEndpointEnum.Earn);
const resp = await client.get<{
data: ILidoHistoryItem[];
}>(`/earn/v1/lido-matic/history`, {
params: { accountAddress, networkId },
});
return resp.data.data;
}

@backgroundMethod()
public async buildLidoMaticStakingTransaction({
amount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type ILidoApproveBaseStakeProps = {
onConfirm?: (amount: string) => Promise<void>;
};

const fieldTitleProps = { color: '$textSubdued', size: '$bodyLg' } as const;

export const LidoApproveBaseStake = ({
price,
balance,
Expand Down Expand Up @@ -172,13 +174,15 @@ export const LidoApproveBaseStake = ({
<SizableText>
<NumberSizeableText
formatter="value"
size="$bodyLgMedium"
formatterOptions={{ tokenSymbol: token.symbol }}
>
{amountBN.toFixed()}
</NumberSizeableText>
(
<NumberSizeableText
formatter="value"
size="$bodyLgMedium"
formatterOptions={{ currency: symbol }}
>
{amountBN.multipliedBy(price).toFixed()}
Expand Down Expand Up @@ -239,44 +243,36 @@ export const LidoApproveBaseStake = ({
<Stack>
<YStack>
{estAnnualRewards ? (
<ListItem
title="Est. annual rewards"
titleProps={{ color: '$textSubdued' }}
>
<ListItem title="Est. annual rewards" titleProps={fieldTitleProps}>
{estAnnualRewards}
</ListItem>
) : null}
{receivingTokenAmount ? (
<ListItem
title="Est. receive"
titleProps={{ color: '$textSubdued' }}
>
<ListItem title="Est. receive" titleProps={fieldTitleProps}>
<SizableText>
<NumberSizeableText
formatter="balance"
size="$bodyLgMedium"
formatterOptions={{ tokenSymbol: receivingTokenSymbol }}
>
{receivingTokenAmount}
</NumberSizeableText>
</SizableText>
</ListItem>
) : null}
<ListItem title="APR" titleProps={{ color: '$textSubdued' }}>
<ListItem title="APR" titleProps={fieldTitleProps}>
<ListItem.Text
primary={`${apr}%`}
primaryTextProps={{ color: '$textSuccess' }}
/>
</ListItem>
<ListItem title="Protocol" titleProps={{ color: '$textSubdued' }}>
<ListItem title="Protocol" titleProps={fieldTitleProps}>
<XStack space="$2" alignItems="center">
<Token size="sm" tokenImageUri={LIDO_LOGO_URI} />
<SizableText size="$bodyMdMedium">Lido</SizableText>
<Token size="xs" tokenImageUri={LIDO_LOGO_URI} />
<SizableText size="$bodyLgMedium">Lido</SizableText>
</XStack>
</ListItem>
<ListItem
title="Stake Release Period"
titleProps={{ color: '$textSubdued' }}
>
<ListItem title="Stake Release Period" titleProps={fieldTitleProps}>
<ListItem.Text primary="< 4 Days" />
</ListItem>
</YStack>
Expand Down
3 changes: 1 addition & 2 deletions packages/kit/src/views/Staking/components/LidoFAQs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ const LidoFAQ = ({ question, answer }: ILidoFAQProps) => {
return (
<YStack>
<YStack>
<XStack mb="$2">
<XStack mb="$2" onPress={onToggle}>
<XStack flex={1}>
<SizableText size="$headingMd">{question}</SizableText>
</XStack>
<XStack>
<IconButton
onPress={onToggle}
variant="tertiary"
icon={show ? 'ChevronTopSmallOutline' : 'ChevronDownSmallOutline'}
/>
Expand Down
95 changes: 95 additions & 0 deletions packages/kit/src/views/Staking/components/LidoHistory/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { useCallback, useMemo } from 'react';

import { groupBy } from 'lodash';

import {
Empty,
NumberSizeableText,
SectionList,
YStack,
} from '@onekeyhq/components';
import { ListItem } from '@onekeyhq/kit/src/components/ListItem';
import { formatDate } from '@onekeyhq/shared/src/utils/dateUtils';
import type { ILidoHistoryItem } from '@onekeyhq/shared/types/staking';

type ILidoHistoryProps = {
items: ILidoHistoryItem[];
};

export const LidoHistory = ({ items }: ILidoHistoryProps) => {
const sections = useMemo(() => {
const result = groupBy(items, (item) =>
formatDate(new Date(item.timestamp * 1000), { hideTimeForever: true }),
);
return Object.entries(result)
.map(([title, data]) => ({ title, data }))
.sort((a, b) => b.data[0].timestamp - a.data[0].timestamp);
}, [items]);

const renderItem = useCallback(
({ item }: { item: ILidoHistoryItem }) => (
<ListItem
avatarProps={{
src: item.receive?.token.logoURI ?? item.send?.token.logoURI,
}}
title={item.label}
subtitle="Lido"
>
<YStack alignItems="flex-end">
{item.receive ? (
<NumberSizeableText
size="$bodyLgMedium"
formatter="balance"
formatterOptions={{ tokenSymbol: item.receive.token.symbol }}
>
{`+${item.receive.amount}`}
</NumberSizeableText>
) : null}
{item.send ? (
<NumberSizeableText
size="$bodyMd"
formatter="balance"
color="$textSubdued"
formatterOptions={{ tokenSymbol: item.send.token.symbol }}
>
{`-${item.send.amount}`}
</NumberSizeableText>
) : null}
</YStack>
</ListItem>
),
[],
);

const renderSectionHeader = useCallback(
({
section,
}: {
section: {
title: string;
};
}) => (
<SectionList.SectionHeader
title={section.title}
justifyContent="space-between"
/>
),
[],
);

return (
<SectionList
estimatedItemSize="$14"
sections={sections}
renderItem={renderItem}
renderSectionHeader={renderSectionHeader}
ListEmptyComponent={
<Empty
icon="ClockTimeHistoryOutline"
title="No Transactions Yet"
description="Your transaction will appear here"
/>
}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ShouldUnderstand = ({
}: IShouldUnderstandProps) => (
<YStack flex={1}>
<ScrollView maxHeight={560}>
<YStack p="$5">
<YStack py="$5">
<Stack>
<Image w="$14" h="$14" src={LIDO_LOGO_URI} />
<YStack mt="$5">
Expand Down
28 changes: 12 additions & 16 deletions packages/kit/src/views/Staking/components/LidoStake/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type ILidoStakeProps = {
onConfirm?: (amount: string) => Promise<void>;
};

const fieldTitleProps = { color: '$textSubdued', size: '$bodyLg' } as const;

export const LidoStake = ({
price,
balance,
Expand Down Expand Up @@ -106,13 +108,15 @@ export const LidoStake = ({
<XStack space="$1">
<SizableText>
<NumberSizeableText
size="$bodyLgMedium"
formatter="balance"
formatterOptions={{ tokenSymbol }}
>
{amountBN.toFixed()}
</NumberSizeableText>
(
<NumberSizeableText
size="$bodyLgMedium"
formatter="value"
formatterOptions={{ currency: symbol }}
>
Expand Down Expand Up @@ -166,42 +170,34 @@ export const LidoStake = ({
<Stack>
<YStack>
{estAnnualRewards ? (
<ListItem
title="Est. annual rewards"
titleProps={{ color: '$textSubdued' }}
>
<ListItem title="Est. annual rewards" titleProps={fieldTitleProps}>
{estAnnualRewards}
</ListItem>
) : null}
{amountValue ? (
<ListItem
title="Est. receive"
titleProps={{ color: '$textSubdued' }}
>
<ListItem title="Est. receive" titleProps={fieldTitleProps}>
<NumberSizeableText
formatter="balance"
size="$bodyLgMedium"
formatterOptions={{ tokenSymbol: stTokenSymbol }}
>
{amountValue}
</NumberSizeableText>
</ListItem>
) : null}
<ListItem title="APR" titleProps={{ color: '$textSubdued' }}>
<ListItem title="APR" titleProps={fieldTitleProps}>
<ListItem.Text
primary={`${apr}%`}
primaryTextProps={{ color: '$textSuccess' }}
/>
</ListItem>
<ListItem title="Protocol" titleProps={{ color: '$textSubdued' }}>
<ListItem title="Protocol" titleProps={fieldTitleProps}>
<XStack space="$2" alignItems="center">
<Token size="sm" tokenImageUri={LIDO_LOGO_URI} />
<SizableText size="$bodyMdMedium">Lido</SizableText>
<Token size="xs" tokenImageUri={LIDO_LOGO_URI} />
<SizableText size="$bodyLgMedium">Lido</SizableText>
</XStack>
</ListItem>
<ListItem
title="Stake Release Period"
titleProps={{ color: '$textSubdued' }}
>
<ListItem title="Stake Release Period" titleProps={fieldTitleProps}>
<ListItem.Text primary="< 4 Days" />
</ListItem>
</YStack>
Expand Down
Loading

0 comments on commit c210b6f

Please sign in to comment.