Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show sbbtc price #143

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions src/components/CoinAmount/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
SkybridgeCoin,
SkybridgeBridge,
estimateSwapRewards,
getSbbtcPrice,
} from '@swingby-protocol/sdk';

import { swingbyTextDisplay } from '../../modules/coin-map';
import { actionSetSwapFormData, useAreCurrenciesValid } from '../../modules/store/swapForm';
import { logger } from '../../modules/logger';
import { useSdkContext } from '../../modules/store/sdkContext';
Expand All @@ -31,6 +33,9 @@ import {
RewardAmountReceiving,
RewardAmountReceivingSmall,
Atag,
SbBtcPriceNotation,
SbBtcPriceNotationSmall,
SbBtcPrice,
} from './styled';
import { CurrencySelector } from './CurrencySelector';

Expand All @@ -55,6 +60,8 @@ export const CoinAmount = ({ variant, resource, 'data-testid': testId }: Props)
const context = useSdkContext();
const { areCurrenciesAndAmountValid, isAmountDesiredValid } = useAreCurrenciesValid({ resource });

const [sbBtcPrice, setSbBtcPrice] = useState('');

const coinsIn = useMemo<SkybridgeCoin[]>(
() =>
getCoinsFor({ context, resource, direction: 'in', bridge: 'btc_skypool' as SkybridgeBridge }),
Expand Down Expand Up @@ -200,6 +207,10 @@ export const CoinAmount = ({ variant, resource, 'data-testid': testId }: Props)
dispatch(actionSetSwapFormData({ amountDesired: debounceAmountDesiredInput }));
}, [debounceAmountDesiredInput, dispatch]);

useEffect(() => {
getSbbtcPrice({ context, bridge: 'btc_skypool' }).then((price) => setSbBtcPrice(price));
}, [context]);

return (
<CoinAmountContainer variant={variant} data-testid={buildTestId('')}>
{variant === 'vertical' && (
Expand Down Expand Up @@ -229,11 +240,18 @@ export const CoinAmount = ({ variant, resource, 'data-testid': testId }: Props)
<>
<SwapVertical onClick={reverseCurrency} />
<SwapFeeLabel variant={variant}>
{feeTotal !== '' && (
{resource === 'swap' && feeTotal !== '' && amountReceiving && (
<>
-{feeTotal}
<br />
<SwapFeeLabelSmall>({feeBridgePercent}% + network fees)</SwapFeeLabelSmall>
<SwapFeeLabelSmall>({feeBridgePercent}% + miner fees)</SwapFeeLabelSmall>
</>
)}
{resource !== 'swap' && amountReceiving && (
<>
-0.00000
<br />
<SwapFeeLabelSmall>(0.00% + miner fees)</SwapFeeLabelSmall>
</>
)}
</SwapFeeLabel>
Expand All @@ -257,10 +275,10 @@ export const CoinAmount = ({ variant, resource, 'data-testid': testId }: Props)
) : isAmountReceivingValid ? (
<>
<FormattedNumber value={Number(amountReceiving)} maximumFractionDigits={4} />
{rewardAmountReceiving !== '' && (
{variant === 'vertical' && resource === 'swap' && rewardAmountReceiving !== '' && (
<>
<br />
<RewardAmountReceiving>
<RewardAmountReceiving variant={variant}>
+
<FormattedNumber
value={Number(rewardAmountReceiving)}
Expand All @@ -278,19 +296,73 @@ export const CoinAmount = ({ variant, resource, 'data-testid': testId }: Props)
</RewardAmountReceiving>
</>
)}
{variant === 'vertical' &&
resource !== 'swap' &&
[currencyDeposit, currencyReceiving].some((currency) =>
currency.includes('sbBTC'),
) && (
<>
<br />
<SbBtcPriceNotation variant={variant}>
sbBTC price =&nbsp;
<SbBtcPrice>
<FormattedNumber value={Number(sbBtcPrice)} maximumFractionDigits={6} />
</SbBtcPrice>
&nbsp;
{swingbyTextDisplay(
currencyDeposit.includes('sbBTC') ? currencyReceiving : currencyDeposit,
)}
</SbBtcPriceNotation>
</>
)}
</>
) : null}
</AmountReceiving>

{variant === 'banner' && (
<SwapFeeLabel variant={variant}>
{feeTotal !== '' && (
{resource === 'swap' && feeTotal !== '' && amountReceiving && (
<SwapFeeLabelSmall>
-{feeTotal}&nbsp;({feeBridgePercent}% + network fees)
-{feeTotal}&nbsp;({feeBridgePercent}% + miner fees)
</SwapFeeLabelSmall>
)}
{resource !== 'swap' && amountReceiving && (
<SwapFeeLabelSmall>-0.00000&nbsp;(0.00% + miner fees)</SwapFeeLabelSmall>
)}
</SwapFeeLabel>
)}
{variant === 'banner' && resource === 'swap' && rewardAmountReceiving !== '' && (
<RewardAmountReceiving variant={variant}>
+
<FormattedNumber value={Number(rewardAmountReceiving)} maximumFractionDigits={4} />
&nbsp;SWINGBY
<br />
<RewardAmountReceivingSmall>
({rebateRate}%{' '}
<Atag href={rebalanceRewardsUrl} rel="noopener noreferrer" target="_blank">
rebalance rewards
</Atag>
)
</RewardAmountReceivingSmall>
</RewardAmountReceiving>
)}
{variant === 'banner' &&
resource !== 'swap' &&
[currencyDeposit, currencyReceiving].some((currency) => currency.includes('sbBTC')) &&
amountReceiving && (
<SbBtcPriceNotation variant={variant}>
<SbBtcPriceNotationSmall>
sbBTC price =&nbsp;
<SbBtcPrice>
<FormattedNumber value={Number(sbBtcPrice)} maximumFractionDigits={6} />
</SbBtcPrice>
&nbsp;
{swingbyTextDisplay(
currencyDeposit.includes('sbBTC') ? currencyReceiving : currencyDeposit,
)}
</SbBtcPriceNotationSmall>
</SbBtcPriceNotation>
)}
</CoinAmountContainer>
);
};
60 changes: 51 additions & 9 deletions src/components/CoinAmount/styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ const wideBanner = css`

const bannerSwapFeeLabel = css`
margin: 0;
margin-right: ${({ theme }) => rem(-theme.pulsar.size.house)};
min-height: ${({ theme }) => rem(theme.pulsar.size.house)};
white-space: nowrap;
`;
Expand Down Expand Up @@ -113,13 +112,15 @@ export const SwapFeeLabel = styled.span<{ variant: Variant }>`
min-height: ${({ theme }) => rem(theme.pulsar.size.town)};
font-size: ${({ theme }) => rem(theme.pulsar.size.closet)};
color: ${({ theme }) => theme.pulsar.color.text.accent};
${({ variant }) => variant === 'banner' && bannerSwapFeeLabel};

place-self: stretch;
grid-column: 2 / 3;

${({ variant }) => variant === 'banner' && bannerSwapFeeLabel};
${({ variant }) =>
variant === 'vertical' &&
`
@media ${StylingConstants.mediaVerticalWideWidth} {
grid-column: 2 / 2;
grid-column: 1 / 2;
}

@media ${StylingConstants.mediaLayout.widgetSmall} {
Expand All @@ -132,7 +133,7 @@ export const SwapFeeLabel = styled.span<{ variant: Variant }>`

@media ${StylingConstants.mediaWebsiteWideWidth} {
grid-column: 3 / 4;
}
}`}
`;

export const SwapFeeLabelSmall = styled.span`
Expand All @@ -147,14 +148,25 @@ export const AmountReceiving = styled.span`
position: relative;
`;

export const RewardAmountReceiving = styled.span`
export const RewardAmountReceiving = styled.span<{ variant: Variant }>`
display: block;
margin-top: ${({ theme }) => rem(theme.pulsar.size.box)};
margin-bottom: ${({ theme }) => rem(-theme.pulsar.size.street)};
margin-right: ${({ theme }) => rem(-theme.pulsar.size.room)};
font-size: ${({ theme }) => rem(theme.pulsar.size.closet)};
font-weight: 500;
color: ${({ theme }) => theme.pulsar.color.text.accent};

${({ variant }) =>
variant === 'banner' &&
`
grid-column: 5 / 6;
place-self: stretch;
`}
${({ variant, theme }) =>
variant === 'vertical' &&
`
margin-top: ${rem(theme.pulsar.size.box)};
margin-bottom: ${rem(-theme.pulsar.size.street)};
margin-right: ${rem(-theme.pulsar.size.room)};
`}
`;

export const RewardAmountReceivingSmall = styled.span`
Expand All @@ -165,3 +177,33 @@ export const Atag = styled.a`
text-decoration: none;
color: ${({ theme }) => theme.pulsar.color.primary.active} !important;
`;

export const SbBtcPriceNotation = styled.span<{ variant: Variant }>`
display: block;
font-weight: 500;
white-space: nowrap;
color: ${({ theme }) => theme.pulsar.color.text.accent};
font-size: ${({ theme }) => rem(theme.pulsar.size.closet)};

${({ variant }) =>
variant === 'banner' &&
`
grid-column: 5 / 6;
place-self: stretch;
`}
${({ variant, theme }) =>
variant === 'vertical' &&
`
margin-top: ${rem(theme.pulsar.size.box)};
margin-bottom: ${rem(-theme.pulsar.size.street)};
margin-right: ${rem(-theme.pulsar.size.room)};
`}
`;

export const SbBtcPriceNotationSmall = styled.span`
font-size: 0.8em;
`;

export const SbBtcPrice = styled.span`
color: ${({ theme }) => theme.pulsar.color.primary.active} !important;
`;
Loading