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

chore(suite): add priority fee network feature #16342

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
21 changes: 21 additions & 0 deletions packages/blockchain-link-types/src/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,33 @@ export interface GetFiatRatesTickersList {
};
}

export interface PriorityFeeEstimationDetails {
maxFeePerGas: string;
maxPriorityFeePerGas: string;
minWaitTimeEstimate: number;
maxWaitTimeEstimate: number;
}

export type FeeTrend = 'up' | 'down';

export interface EstimateFee {
type: typeof RESPONSES.ESTIMATE_FEE;
payload: {
feePerUnit: string;
feePerTx?: string;
feeLimit?: string;
eip1559?: {
tomasklim marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be imported from Blockbook

baseFeePerGas: string;
low: PriorityFeeEstimationDetails;
medium: PriorityFeeEstimationDetails;
high: PriorityFeeEstimationDetails;
networkCongestion: number;
latestPriorityFeeRange: string[];
historicalPriorityFeeRange: string[];
historicalBaseFeeRange: string[];
priorityFeeTrend: FeeTrend;
baseFeeTrend: FeeTrend;
};
}[];
}

Expand Down
50 changes: 37 additions & 13 deletions packages/connect/src/api/bitcoin/Fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,43 @@ export class FeeLevels {
async loadMisc(blockchain: Blockchain) {
try {
const [response] = await blockchain.estimateFee({ blocks: [1] });
// misc coins should have only one FeeLevel (normal)
this.levels[0] = {
...this.levels[0],
...response,
// validate `feePerUnit` from the backend
// should be lower than `coinInfo.maxFee` and higher than `coinInfo.minFee`
// xrp sends values from 1 to very high number occasionally
// see: https://github.com/trezor/trezor-suite/blob/develop/packages/blockchain-link/src/workers/ripple/index.ts#L316
feePerUnit: Math.min(
this.coinInfo.maxFee,
Math.max(this.coinInfo.minFee, parseInt(response.feePerUnit, 10)),
).toString(),
};
if (response.eip1559) {
const eip1559LevelKeys = ['low', 'medium', 'high'] as const;

const { eip1559 } = response;
const eip1559Levels = eip1559LevelKeys.map(levelKey => {
const level = eip1559[levelKey];

return {
label: levelKey,
baseFeePerGas: eip1559.baseFeePerGas,
tomasklim marked this conversation as resolved.
Show resolved Hide resolved
maxFeePerGas: level.maxFeePerGas,
maxPriorityFeePerGas: level.maxPriorityFeePerGas,
minWaitTimeEstimate: level.minWaitTimeEstimate / 1000, // Infura provides wait time in miliseconds
maxWaitTimeEstimate: level.maxWaitTimeEstimate / 1000,
feePerUnit: '0',
feeLimit: undefined,
ethFeeType: 'eip1559' as const,
blocks: -1,
};
});

this.levels = [...eip1559Levels];
} else {
//misc coins should have only one FeeLevel (normal), for ethereum depends on availability of eip1559
this.levels[0] = {
...this.levels[0],
...response,
// validate `feePerUnit` from the backend
// should be lower than `coinInfo.maxFee` and higher than `coinInfo.minFee`
// xrp sends values from 1 to very high number occasionally
// see: https://github.com/trezor/trezor-suite/blob/develop/packages/blockchain-link/src/workers/ripple/index.ts#L316
feePerUnit: Math.min(
this.coinInfo.maxFee,
Math.max(this.coinInfo.minFee, parseInt(response.feePerUnit, 10)),
).toString(),
};
}
} catch {
// silent
}
Expand Down
15 changes: 15 additions & 0 deletions packages/connect/src/types/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export const FeeInfo = Type.Object({
dustLimit: Type.Number(),
});

export type PriorityFeeEstimationDetails = Static<typeof PriorityFeeEstimationDetails>;
export const PriorityFeeEstimationDetails = Type.Object({
maxFeePerGas: Type.String(),
maxPriorityFeePerGas: Type.String(),
maxWaitTimeEstimate: Type.Number(),
minWaitTimeEstimate: Type.Number(),
});

export type FeeLevel = Static<typeof FeeLevel>;
export const FeeLevel = Type.Object({
label: Type.Union([
Expand All @@ -16,11 +24,18 @@ export const FeeLevel = Type.Object({
Type.Literal('economy'),
Type.Literal('low'),
Type.Literal('custom'),
Type.Literal('medium'),
]),
ethFeeType: Type.Optional(Type.Union([Type.Literal('legacy'), Type.Literal('eip1559')])),
feePerUnit: Type.String(),
blocks: Type.Number(),
feeLimit: Type.Optional(Type.String()), // eth gas limit
feePerTx: Type.Optional(Type.String()), // fee for BlockchainEstimateFeeParams.request.specific
baseFeePerGas: Type.Optional(Type.String()),
maxFeePerGas: Type.Optional(Type.String()),
maxPriorityFeePerGas: Type.Optional(Type.String()),
maxWaitTimeEstimate: Type.Optional(Type.Number()),
minWaitTimeEstimate: Type.Optional(Type.Number()),
});

export type SelectFeeLevel = Static<typeof SelectFeeLevel>;
Expand Down
71 changes: 66 additions & 5 deletions packages/suite/src/components/wallet/Fees/CustomFee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import {
import { NetworkType } from '@suite-common/wallet-config';
import { FeeInfo, FormState } from '@suite-common/wallet-types';
import { getFeeUnits, getInputState, isInteger } from '@suite-common/wallet-utils';
import { Banner, Column, Grid, Note, Text, useMediaQuery, variables } from '@trezor/components';
import {
Banner,
Column,
Grid,
Icon,
Note,
Row,
Text,
useMediaQuery,
variables,
} from '@trezor/components';
import { FeeLevel } from '@trezor/connect';
import { NumberInput } from '@trezor/product-components';
import { spacings } from '@trezor/theme';
import { HELP_CENTER_TRANSACTION_FEES_URL } from '@trezor/urls';
Expand All @@ -36,16 +47,16 @@ interface CustomFeeProps<TFieldValues extends FormState> {
control: Control;
setValue: UseFormSetValue<TFieldValues>;
getValues: UseFormGetValues<TFieldValues>;
changeFeeLimit?: (value: string) => void;
composedFeePerByte: string;
}

const getCurrentFee = (levels: FeeLevel[]) => `${levels[levels.length > 2 ? 1 : 0].feePerUnit}`;

export const CustomFee = <TFieldValues extends FormState>({
networkType,
feeInfo,
register,
control,
changeFeeLimit,
composedFeePerByte,
...props
}: CustomFeeProps<TFieldValues>) => {
Expand Down Expand Up @@ -146,7 +157,7 @@ export const CustomFee = <TFieldValues extends FormState>({
feeLimitError?.type === 'feeLimit' ? feeLimitValidationProps : undefined;

return (
<Column gap={spacings.xs}>
<Column gap={spacings.md} margin={{ bottom: spacings.md }}>
<Banner
icon
variant="warning"
Expand All @@ -160,7 +171,58 @@ export const CustomFee = <TFieldValues extends FormState>({
>
<Translation id="TR_CUSTOM_FEE_WARNING" />
</Banner>
<Row justifyContent="space-between">
<Text variant="tertiary">
<Translation id="TR_CURRENT_FEE_CUSTOM_FEES" />
</Text>
<Text variant="tertiary">
<Row alignItems="center" gap={spacings.xxs}>
<Text>
{getCurrentFee(feeInfo.levels)} {getFeeUnits(networkType)}
</Text>
<Icon
name={networkType === 'ethereum' ? 'gasPump' : 'receipt'}
size="mediumLarge"
/>
</Row>
</Text>
</Row>
<Grid gap={spacings.xs} columns={useFeeLimit && !isBelowLaptop ? 2 : 1}>
{/*
{shouldUsePriorityFees && feeInfo.levels[0].ethFeeType === 'eip1559' ? (
<>
<NumberInput
label={<Translation id="TR_MAX_PRIORITY_FEE_PER_GAS" />}
locale={locale}
control={control}
inputState={getInputState(feePerUnitError)}
innerAddon={
<Text variant="tertiary" typographyStyle="label">
{feeUnits}
</Text>
}
name={MAX_PRIORITY_FEE_PER_GAS}
data-testid={MAX_PRIORITY_FEE_PER_GAS}
rules={feeRules}
bottomText={feePerUnitError?.message || null}
/>
<NumberInput
label={<Translation id="TR_MAX_FEE_PER_GAS" />}
locale={locale}
control={control}
inputState={getInputState(feePerUnitError)}
innerAddon={
<Text variant="tertiary" typographyStyle="label">
{feeUnits}
</Text>
}
name={MAX_FEE_PER_GAS}
data-testid={MAX_FEE_PER_GAS}
rules={feeRules}
bottomText={feePerUnitError?.message || null}
/>
</>
) : ( */}
{useFeeLimit ? (
<NumberInput
label={<Translation id="TR_GAS_LIMIT" />}
Expand All @@ -169,7 +231,6 @@ export const CustomFee = <TFieldValues extends FormState>({
inputState={getInputState(feeLimitError)}
name={FEE_LIMIT}
data-testid={FEE_LIMIT}
onChange={changeFeeLimit}
bottomText={
feeLimitError?.message ? (
<InputError
Expand Down
Loading
Loading