Skip to content

Commit

Permalink
chore(eth-staking): use blockbook for staking actions
Browse files Browse the repository at this point in the history
  • Loading branch information
shotgunofdeath committed Mar 4, 2024
1 parent b33ab5b commit 7f137a7
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 90 deletions.
3 changes: 0 additions & 3 deletions packages/suite-desktop-core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export const allowedDomains = [
'trezor-cardano-mainnet.blockfrost.io',
'trezor-cardano-preview.blockfrost.io',
'blockfrost.dev',
'eth-goerli.public.blastapi.io',
'ethereum-holesky.publicnode.com',
'mainnet.infura.io',
'eth-api-b2c-stage.everstake.one',
'eth-api-b2c.everstake.one',
];
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test-unit:watch": "yarn g:jest -o --watch"
},
"dependencies": {
"@everstake/wallet-sdk": "^0.3.39",
"@everstake/wallet-sdk": "^0.3.40",
"@formatjs/intl": "2.10.0",
"@hookform/resolvers": "3.3.4",
"@mobily/ts-belt": "^3.13.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ import {
// @ts-expect-error
import { Ethereum } from '@everstake/wallet-sdk';
import { MIN_ETH_FOR_WITHDRAWALS } from 'src/constants/suite/ethStaking';
import { NetworkSymbol } from '@suite-common/wallet-config';

const calculate = (
availableBalance: string,
output: ExternalOutput,
feeLevel: FeeLevel,
compareWithAmount = true,
symbol: NetworkSymbol,
): PrecomposedTransaction => {
const feeInSatoshi = calculateEthFee(
toWei(feeLevel.feePerUnit, 'gwei'),
Expand All @@ -64,10 +66,14 @@ const calculate = (
new BigNumber(feeInSatoshi).gt(availableBalance) ||
(compareWithAmount && totalSpent.isGreaterThan(availableBalance))
) {
const error = 'AMOUNT_IS_NOT_ENOUGH';
const error = 'TR_STAKE_NOT_ENOUGH_FUNDS';

// errorMessage declared later
return { type: 'error', error, errorMessage: { id: error } } as const;
return {
type: 'error',
error,
errorMessage: { id: error, values: { symbol: symbol.toUpperCase() } },
} as const;
}

const payloadData = {
Expand Down Expand Up @@ -152,7 +158,7 @@ export const composeTransaction =
const wrappedResponse: PrecomposedLevels = {};
const compareWithAmount = formValues.ethereumStakeType === 'stake';
const response = predefinedLevels.map(level =>
calculate(availableBalance, output, level, compareWithAmount),
calculate(availableBalance, output, level, compareWithAmount, account.symbol),
);
response.forEach((tx, index) => {
const feeLabel = predefinedLevels[index].label as FeeLevel['label'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const GreenTxt = styled.span`
`;

const StyledWarning = styled(Warning)`
margin-top: ${spacingsPx.sm};
margin: ${spacingsPx.sm} 0 ${spacingsPx.sm} 0;
justify-content: flex-start;
`;

Expand Down Expand Up @@ -83,16 +83,16 @@ export const ClaimEthForm = () => {
</TxtRight>
</AmountInfo>

{errors[CRYPTO_INPUT] && (
<StyledWarning variant="destructive">{errors[CRYPTO_INPUT]?.message}</StyledWarning>
)}

<FeesInfo
transactionInfo={transactionInfo}
symbol={symbol}
helperText={<Translation id="TR_STAKE_PAID_FROM_BALANCE" />}
/>

{errors[CRYPTO_INPUT] && (
<StyledWarning variant="destructive">{errors[CRYPTO_INPUT]?.message}</StyledWarning>
)}

<ClaimingPeriodWrapper>
<GreyP>
<Translation id="TR_STAKE_CLAIMING_PERIOD" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,29 @@ export const UnstakeEthForm = () => {

return (
<form onSubmit={handleSubmit(signTx)}>
{canClaim && (
<StyledWarning variant="info">
<Translation
id="TR_STAKE_CAN_CLAIM_WARNING"
values={{
amount: claimableAmount,
symbol: symbol.toUpperCase(),
br: <br />,
}}
/>
</StyledWarning>
)}

<Options symbol={symbol} />

<WarningsWrapper>
{errors[CRYPTO_INPUT] && (
<StyledWarning variant="destructive">
{errors[CRYPTO_INPUT]?.message}
</StyledWarning>
)}
</WarningsWrapper>

<DividerWrapper>
<Divider />
</DividerWrapper>
Expand All @@ -82,27 +103,6 @@ export const UnstakeEthForm = () => {
helperText={<Translation id="TR_STAKE_PAID_FROM_BALANCE" />}
/>

<WarningsWrapper>
{errors[CRYPTO_INPUT] && (
<StyledWarning variant="destructive">
{errors[CRYPTO_INPUT]?.message}
</StyledWarning>
)}

{canClaim && (
<StyledWarning variant="info">
<Translation
id="TR_STAKE_CAN_CLAIM_WARNING"
values={{
amount: claimableAmount,
symbol: symbol.toUpperCase(),
br: <br />,
}}
/>
</StyledWarning>
)}
</WarningsWrapper>

<UpToDaysWrapper>
{!Number.isNaN(unstakingPeriod) && (
<>
Expand Down
6 changes: 0 additions & 6 deletions packages/suite/src/constants/suite/ethStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ export const MIN_ETH_AMOUNT_FOR_STAKING = new BigNumber(0.1);
export const MIN_ETH_FOR_WITHDRAWALS = new BigNumber(0.03);
export const MIN_ETH_BALANCE_FOR_STAKING = MIN_ETH_AMOUNT_FOR_STAKING.plus(MIN_ETH_FOR_WITHDRAWALS);

// source is a required parameter for some functions in the Everstake Wallet SDK.
// This parameter is used for some contract calls.
// It is a constant which allows the SDK to define which app calls its functions.
// Each app which integrates the SDK has its own source, e.g. source for Trezor Suite is '1'.
export const WALLET_SDK_SOURCE = '1';

// Used when Everstake pool stats are not available from the API.
export const BACKUP_ETH_APY = '4.13';

Expand Down
17 changes: 13 additions & 4 deletions packages/suite/src/hooks/suite/useEverstakePoolStats.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import BigNumber from 'bignumber.js';
import { BACKUP_ETH_APY } from 'src/constants/suite/ethStaking';
import { BACKUP_ETH_APY, STAKE_SYMBOLS } from 'src/constants/suite/ethStaking';
import { selectEnabledNetworks } from 'src/reducers/wallet/settingsReducer';
import { useSelector } from './useSelector';

export const useEverstakePoolStats = () => {
const enabledNetworks = useSelector(selectEnabledNetworks);
const areEthNetworksEnabled = useMemo(
() => enabledNetworks.some(symbol => STAKE_SYMBOLS.includes(symbol)),
[enabledNetworks],
);
const [poolStats, setPoolStats] = useState<{ ethApy: string; nextRewardPayout: number | null }>(
{
ethApy: BACKUP_ETH_APY,
Expand All @@ -12,6 +19,8 @@ export const useEverstakePoolStats = () => {
const [isPoolStatsLoading, setIsPoolStatsLoading] = useState(false);

useEffect(() => {
if (!areEthNetworksEnabled) return;

const abortController = new AbortController();

const getEverstakePoolStats = async () => {
Expand Down Expand Up @@ -39,7 +48,7 @@ export const useEverstakePoolStats = () => {
ethApy: new BigNumber(stats.apr)
.times(100)
.toPrecision(3, BigNumber.ROUND_DOWN),
nextRewardPayout: Math.round(stats.next_reward_payout_in / 60 / 60 / 24),
nextRewardPayout: Math.ceil(stats.next_reward_payout_in / 60 / 60 / 24),
});
} catch (e) {
if (!abortController.signal.aborted) {
Expand All @@ -55,7 +64,7 @@ export const useEverstakePoolStats = () => {
return () => {
abortController.abort();
};
}, []);
}, [areEthNetworksEnabled]);

return {
ethApy: poolStats.ethApy,
Expand Down
4 changes: 4 additions & 0 deletions packages/suite/src/support/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8778,4 +8778,8 @@ export default defineMessages({
id: 'TR_STAKE_CLAIM_IN_NEXT_BLOCK',
defaultMessage: 'in the next block',
},
TR_STAKE_NOT_ENOUGH_FUNDS: {
id: 'TR_STAKE_NOT_ENOUGH_FUNDS',
defaultMessage: 'Not enough {symbol} to pay network fees',
},
});
Loading

0 comments on commit 7f137a7

Please sign in to comment.