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(suite): allow lower gas prices for eth networks #16066

Merged
merged 5 commits into from
Jan 2, 2025
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
4 changes: 2 additions & 2 deletions packages/connect/src/api/ethereum/api/ethereumGetAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getEthereumNetwork, getUniqueNetworks } from '../../../data/coinInfo';
import { stripHexPrefix } from '../../../utils/formatUtils';
import { PROTO, ERRORS } from '../../../constants';
import { UI, createUiMessage } from '../../../events';
import type { EthereumNetworkInfo } from '../../../types';
import type { EthereumNetworkInfoDefinitionValues } from '../../../types';
import {
getEthereumDefinitions,
decodeEthereumDefinition,
Expand All @@ -21,7 +21,7 @@ import { GetAddress as GetAddressSchema } from '../../../types/api/getAddress';

type Params = PROTO.EthereumGetAddress & {
address?: string;
network?: EthereumNetworkInfo;
network?: EthereumNetworkInfoDefinitionValues;
encoded_network?: ArrayBuffer;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import {
} from '../ethereumDefinitions';
import type { EthereumTransaction, EthereumTransactionEIP1559 } from '../../../types/api/ethereum';
import {
EthereumNetworkInfo,
EthereumNetworkInfoDefinitionValues,
EthereumSignTransaction as EthereumSignTransactionSchema,
} from '../../../types';

type Params = {
path: number[];
network?: EthereumNetworkInfo;
network?: EthereumNetworkInfoDefinitionValues;
definitions?: MessagesSchema.EthereumDefinitions;
chunkify: boolean;
} & (
Expand Down
8 changes: 2 additions & 6 deletions packages/connect/src/api/ethereum/ethereumDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { trzd } from '@trezor/protocol';
import { Type, Static, Assert } from '@trezor/schema-utils';

import { DataManager } from '../../data/DataManager';
import { EthereumNetworkInfo } from '../../types';
import { EthereumNetworkInfoDefinitionValues } from '../../types';
import { ethereumNetworkInfoBase } from '../../data/coinInfo';

interface GetEthereumDefinitions {
Expand Down Expand Up @@ -135,14 +135,10 @@ export const decodeEthereumDefinition = (
return decoded;
};

/**
* Converts protobuf decoded eth definitions to EthereumNetworkInfo type
*/
export const ethereumNetworkInfoFromDefinition = (
definition: EthereumNetworkDefinitionDecoded,
): EthereumNetworkInfo => ({
Copy link
Contributor

Choose a reason for hiding this comment

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

on line 139, comment does not match anymore. nitpick

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I don't like the EthereumNetworkInfoDefinition type name. This utility is called ethereumNetworkInfoFromDefinition (as it would expect EthereumNetworkInfoDefinition as argument but it actually returns it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to EthereumNetworkInfoDefinitionValues.

): EthereumNetworkInfoDefinitionValues => ({
...ethereumNetworkInfoBase,

chainId: definition.chain_id,
label: definition.name,
name: definition.name,
Expand Down
90 changes: 90 additions & 0 deletions packages/connect/src/data/__tests__/defaultFeeLevels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { getEthereumFeeLevels } from '../defaultFeeLevels';

describe('getEthereumFeeLevels', () => {
const fixtures = {
eth: {
defaultGas: 15,
minFee: 1,
maxFee: 10000,
expected: {
blockTime: -1,
defaultFees: [
{
label: 'normal',
feePerUnit: '15000000000', // 15 Gwei * 1e9 = 15000000000 Wei
feeLimit: '21000',
blocks: -1,
},
],
minFee: 1,
maxFee: 10000,
dustLimit: -1,
},
},
pol: {
defaultGas: 200,
minFee: 1,
maxFee: 10000000,
expected: {
blockTime: -1,
defaultFees: [
{
label: 'normal',
feePerUnit: '200000000000', // 200 Gwei * 1e9 = 200000000000 Wei
feeLimit: '21000',
blocks: -1,
},
],
minFee: 1,
maxFee: 10000000,
dustLimit: -1,
},
},
base: {
defaultGas: 0.01,
minFee: 0.000000001,
maxFee: 100,
expected: {
blockTime: -1,
defaultFees: [
{
label: 'normal',
feePerUnit: '10000000', // 0.01 Gwei * 1e9 = 10000000 Wei
feeLimit: '21000',
blocks: -1,
},
],
minFee: 0.0000001,
maxFee: 1000,
dustLimit: -1,
},
},
unknown: {
defaultGas: 5,
minFee: 0.000000001,
maxFee: 10000,
expected: {
blockTime: -1,
defaultFees: [
{
label: 'normal',
feePerUnit: '5000000000', // 5 Gwei * 1e9 = 5000000000 Wei
feeLimit: '21000',
blocks: -1,
},
],
minFee: 0.000000001,
maxFee: 10000,
dustLimit: -1,
},
},
};

Object.entries(fixtures).forEach(([chain, { expected }]) => {
it(`should return correct fee levels for ${chain}`, () => {
const result = getEthereumFeeLevels(chain);

expect(result).toEqual(expected);
});
});
});
9 changes: 4 additions & 5 deletions packages/connect/src/data/coinInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { getBitcoinFeeLevels, getEthereumFeeLevels, getMiscFeeLevels } from './d
import { ERRORS } from '../constants';
import { toHardened, fromHardened } from '../utils/pathUtils';
import type {
CoinInfo,
BitcoinNetworkInfo,
EthereumNetworkInfo,
MiscNetworkInfo,
Expand Down Expand Up @@ -249,8 +248,7 @@ const parseBitcoinNetworksJson = (json: any) => {

export const ethereumNetworkInfoBase = {
type: 'ethereum' as const,
decimals: 16,
tomasklim marked this conversation as resolved.
Show resolved Hide resolved
...getEthereumFeeLevels(),
decimals: 18,
};

const parseEthereumNetworksJson = (json: any) => {
Expand All @@ -259,6 +257,7 @@ const parseEthereumNetworksJson = (json: any) => {

ethereumNetworks.push({
...ethereumNetworkInfoBase,
...getEthereumFeeLevels(network.chain),
blockchainLink: network.blockchain_link,
chainId: network.chain_id,
label: network.label,
Expand Down Expand Up @@ -304,8 +303,8 @@ export const parseCoinsJson = (json: any) => {
});
};

export const getUniqueNetworks = (networks: (CoinInfo | undefined)[]) =>
networks.reduce((result: CoinInfo[], info?: CoinInfo) => {
export const getUniqueNetworks = <T extends { shortcut: string }>(networks: (T | undefined)[]) =>
networks.reduce((result: T[], info?: T) => {
if (!info || result.find(i => i.shortcut === info.shortcut)) return result;

return result.concat(info);
Expand Down
53 changes: 39 additions & 14 deletions packages/connect/src/data/defaultFeeLevels.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BigNumber } from '@trezor/utils';

import { FeeLevel, FeeInfo } from '../types';

// this is workaround for the lack of information from 'trezor-common'
Expand All @@ -18,6 +20,25 @@ const getDefaultBlocksForFeeLevel = (shortcut: string, label: string) =>
? BLOCKS_FOR_FEE_LEVEL[shortcut][label]
: -1; // -1 for unknown

const EVM_GAS_PRICE_PER_CHAIN_IN_GWEI: Record<
string,
{ min: number; max: number; defaultGas: number }
> = {
eth: { min: 1, max: 10000, defaultGas: 15 },
pol: { min: 1, max: 10000000, defaultGas: 200 },
bsc: { min: 1, max: 100000, defaultGas: 3 },
base: { min: 0.0000001, max: 1000, defaultGas: 0.01 },
arb: { min: 0.001, max: 1000, defaultGas: 0.01 },
op: { min: 0.000000001, max: 1000, defaultGas: 0.01 },
};

const getEvmChainGweiGasPrice = (chain: string) =>
EVM_GAS_PRICE_PER_CHAIN_IN_GWEI[chain] ?? {
min: 0.000000001,
max: 10000,
defaultGas: 5,
};

// partial data from coins.jon
interface CoinsJsonData {
shortcut: string; // uppercase shortcut
Expand Down Expand Up @@ -58,20 +79,24 @@ export const getBitcoinFeeLevels = (coin: CoinsJsonData): FeeInfoWithLevels => {
};
};

export const getEthereumFeeLevels = (): FeeInfoWithLevels => ({
blockTime: -1, // unknown
defaultFees: [
{
label: 'normal' as const,
feePerUnit: '5000000000',
feeLimit: '21000', // unlike the other networks ethereum have additional value "feeLimit" (Gas limit)
blocks: -1, // unknown
},
],
minFee: 1,
maxFee: 10000,
dustLimit: -1, // unknown/unused
});
export const getEthereumFeeLevels = (chain: string): FeeInfoWithLevels => {
const { min, max, defaultGas } = getEvmChainGweiGasPrice(chain);

return {
blockTime: -1, // unknown
defaultFees: [
{
label: 'normal' as const,
feePerUnit: new BigNumber(defaultGas).multipliedBy('1e+9').toString(), // defined in wei 1 Gwei = 10^9 Wei
feeLimit: '21000', // default transfer gas limit
blocks: -1, // unknown
},
],
minFee: min,
maxFee: max,
dustLimit: -1, // unknown/unused
};
};

const RIPPLE_FEE_INFO: FeeInfoWithLevels = {
blockTime: -1, // unknown
Expand Down
10 changes: 10 additions & 0 deletions packages/connect/src/types/coinInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ export const EthereumNetworkInfo = Type.Intersect([
}),
]);

export type EthereumNetworkInfoDefinitionValues = Static<
typeof EthereumNetworkInfoDefinitionValues
>;
export const EthereumNetworkInfoDefinitionValues = Type.Omit(EthereumNetworkInfo, [
'minFee',
'maxFee',
'defaultFees',
'blockTime',
]);

export type MiscNetworkInfo = Static<typeof MiscNetworkInfo>;
export const MiscNetworkInfo = Type.Intersect([
Common,
Expand Down
9 changes: 6 additions & 3 deletions packages/connect/src/utils/ethereumUtils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/utils/ethereumUtils.js

import type { CoinInfo } from '../types';

export const getNetworkLabel = (label: string, network?: CoinInfo) => {
export const getNetworkLabel = (
label: string,
network?: {
name: string;
},
) => {
if (network) {
const name = network.name.toLowerCase().indexOf('testnet') >= 0 ? 'Testnet' : network.name;

Expand Down
10 changes: 7 additions & 3 deletions packages/suite/src/components/wallet/Fees/CustomFee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ export const CustomFee = <TFieldValues extends FormState>({
except: networkType !== 'ethereum',
}),
range: (value: string) => {
const feeBig = new BigNumber(value);
if (feeBig.isGreaterThan(maxFee) || feeBig.isLessThan(minFee)) {
return translationString('CUSTOM_FEE_NOT_IN_RANGE', { minFee, maxFee });
const customFee = new BigNumber(value);

if (customFee.isGreaterThan(maxFee) || customFee.isLessThan(minFee)) {
return translationString('CUSTOM_FEE_NOT_IN_RANGE', {
minFee: new BigNumber(minFee).toString(),
maxFee: new BigNumber(maxFee).toString(),
});
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { UseFormReturn } from 'react-hook-form';
import { COMPOSE_ERROR_TYPES } from '@suite-common/wallet-constants';
import { selectAccounts, selectSelectedDevice } from '@suite-common/wallet-core';
import { AddressDisplayOptions } from '@suite-common/wallet-types';
import { getFeeLevels } from '@suite-common/wallet-utils';
import { getFeeInfo } from '@suite-common/wallet-utils';

import { saveComposedTransactionInfo } from 'src/actions/wallet/coinmarket/coinmarketCommonActions';
import { FORM_OUTPUT_ADDRESS, FORM_OUTPUT_AMOUNT } from 'src/constants/wallet/coinmarket/form';
Expand Down Expand Up @@ -41,9 +41,14 @@ export const useCoinmarketComposeTransaction = <T extends CoinmarketSellExchange
>;
const chunkify = addressDisplayType === AddressDisplayOptions.CHUNKED;
const { symbol, networkType } = account;
const coinFees = fees[symbol];
const levels = getFeeLevels(networkType, coinFees);
const feeInfo = useMemo(() => ({ ...coinFees, levels }), [coinFees, levels]);
const feeInfo = useMemo(
() =>
getFeeInfo({
networkType,
feeInfo: fees[symbol],
}),
[networkType, symbol, fees],
);
const initState = useMemo(() => ({ account, network, feeInfo }), [account, network, feeInfo]);
const outputAddress = values?.outputs?.[0].address;
const [state, setState] = useState<CoinmarketUseComposeTransactionStateProps>(initState);
Expand Down
15 changes: 9 additions & 6 deletions packages/suite/src/hooks/wallet/useClaimEthForm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, useCallback, useContext, useEffect, useMemo } from 'react';
import { useForm } from 'react-hook-form';

import { getFeeLevels } from '@suite-common/wallet-utils';
import { getFeeInfo } from '@suite-common/wallet-utils';
import { PrecomposedTransactionFinal } from '@suite-common/wallet-types';

import { useDispatch, useSelector } from 'src/hooks/suite';
Expand Down Expand Up @@ -40,8 +40,10 @@ export const useClaimEthForm = ({ selectedAccount }: UseStakeFormsProps): ClaimC
}, [account.symbol]);

const state = useMemo(() => {
const levels = getFeeLevels(account.networkType, symbolFees);
const feeInfo = { ...symbolFees, levels };
const feeInfo = getFeeInfo({
networkType: account.networkType,
feeInfo: symbolFees,
});

return {
account,
Expand Down Expand Up @@ -101,9 +103,10 @@ export const useClaimEthForm = ({ selectedAccount }: UseStakeFormsProps): ClaimC

// sub-hook, FeeLevels handler
const fees = useSelector(state => state.wallet.fees);
const coinFees = fees[account.symbol];
const levels = getFeeLevels(account.networkType, coinFees);
const feeInfo = { ...coinFees, levels };
const feeInfo = getFeeInfo({
networkType: account.networkType,
feeInfo: fees[account.symbol],
});
const { changeFeeLevel, selectedFee: _selectedFee } = useFees({
defaultValue: 'normal',
feeInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useCallback } from 'react';
import { notificationsActions } from '@suite-common/toast-notifications';
import { DEFAULT_VALUES, DEFAULT_PAYMENT } from '@suite-common/wallet-constants';
import { FormState } from '@suite-common/wallet-types';
import { getFeeLevels } from '@suite-common/wallet-utils';
import { getFeeInfo } from '@suite-common/wallet-utils';
import { networks } from '@suite-common/wallet-config';
import type { Account, FormOptions } from '@suite-common/wallet-types';
import { composeSendFormTransactionFeeLevelsThunk } from '@suite-common/wallet-core';
Expand Down Expand Up @@ -80,9 +80,10 @@ export const useCoinmarketRecomposeAndSign = () => {
};

// prepare form state for composeAction
const coinFees = fees[account.symbol];
const levels = getFeeLevels(account.networkType, coinFees);
const feeInfo = { ...coinFees, levels };
const feeInfo = getFeeInfo({
networkType: account.networkType,
feeInfo: fees[account.symbol],
});
const composeContext = { account, network, feeInfo };

// recalcCustomLimit is used in case of custom fee level, when we want to keep the feePerUnit defined by the user
Expand Down
Loading
Loading