Skip to content

Commit 7196a88

Browse files
committed
fix: add low fee caps
1 parent b844fbe commit 7196a88

File tree

9 files changed

+105
-40
lines changed

9 files changed

+105
-40
lines changed

config/wallet-config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"okcoin": { "name": "okcoin", "enabled": true }
88
},
99
"feeEstimations": {
10+
"maxValues": [500000, 750000, 2000000],
1011
"maxValuesEnabled": false,
11-
"maxValues": [500000, 750000, 2000000]
12+
"minValues": [2500, 3000, 3500],
13+
"minValuesEnabled": true
1214
}
1315
}

config/wallet-config.schema.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@
7979
"type": "number",
8080
"description": "Fee estimation max value"
8181
}
82+
},
83+
"minValuesEnabled": {
84+
"type": "boolean",
85+
"description": "Whether or not the minimum values are enabled"
86+
},
87+
"minValues": {
88+
"type": "array",
89+
"description": "Low, middle and high min values for fee estimations",
90+
"minItems": 3,
91+
"maxItems": 3,
92+
"items": {
93+
"type": "number",
94+
"description": "Fee estimation min value"
95+
}
8296
}
8397
}
8498
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {
2+
useConfigFeeEstimationsMaxEnabled,
3+
useConfigFeeEstimationsMaxValues,
4+
useConfigFeeEstimationsMinEnabled,
5+
useConfigFeeEstimationsMinValues,
6+
} from '@app/query/hiro-config/hiro-config.query';
7+
8+
const defaultFeeEstimationsMaxValues = [500000, 750000, 2000000];
9+
const defaultFeeEstimationsMinValues = [2500, 3000, 3500];
10+
11+
export function useFeeEstimationsMaxValues() {
12+
// Get it first from the config
13+
const configFeeEstimationsMaxEnabled = useConfigFeeEstimationsMaxEnabled();
14+
const configFeeEstimationsMaxValues = useConfigFeeEstimationsMaxValues();
15+
// Only when the remote config file explicitly sets the maxValuesEnabled as false, we return no max cap for fees
16+
if (configFeeEstimationsMaxEnabled === false) return;
17+
return configFeeEstimationsMaxValues || defaultFeeEstimationsMaxValues;
18+
}
19+
20+
export function useFeeEstimationsMinValues() {
21+
// Get it first from the config
22+
const configFeeEstimationsMinEnabled = useConfigFeeEstimationsMinEnabled();
23+
const configFeeEstimationsMinValues = useConfigFeeEstimationsMinValues();
24+
// Only when the remote config file explicitly sets the minValuesEnabled as false, we return no min cap for fees
25+
if (configFeeEstimationsMinEnabled === false) return;
26+
return configFeeEstimationsMinValues || defaultFeeEstimationsMinValues;
27+
}

src/app/common/transactions/use-fee-estimations-max-values.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/app/pages/send-tokens/components/send-form-inner.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ import {
3131
import { SendFormMemoWarning } from './memo-warning';
3232
import {
3333
getDefaultSimulatedFeeEstimations,
34-
getFeeEstimationsWithMaxValues,
34+
getFeeEstimationsWithCappedValues,
3535
} from '@shared/transactions/fee-estimations';
36-
import { useFeeEstimationsMaxValues } from '@app/common/transactions/use-fee-estimations-max-values';
36+
import {
37+
useFeeEstimationsMaxValues,
38+
useFeeEstimationsMinValues,
39+
} from '@app/common/transactions/use-fee-estimations-capped-values';
3740

3841
interface SendFormInnerProps {
3942
assetError: string | undefined;
@@ -52,6 +55,7 @@ export function SendFormInner(props: SendFormInnerProps) {
5255

5356
const [, setFeeEstimations] = useFeeEstimationsState();
5457
const feeEstimationsMaxValues = useFeeEstimationsMaxValues();
58+
const feeEstimationsMinValues = useFeeEstimationsMinValues();
5559
const { selectedAsset } = useSelectedAsset();
5660
const assets = useTransferableAssets();
5761
const analytics = useAnalytics();
@@ -69,13 +73,14 @@ export function SendFormInner(props: SendFormInnerProps) {
6973
void analytics.track('use_fee_estimation_default_simulated');
7074
}
7175
if (feeEstimationsResp.estimations && feeEstimationsResp.estimations.length) {
72-
const feeEstimationsWithMaxValues = getFeeEstimationsWithMaxValues(
76+
const feeEstimationsWithCappedValues = getFeeEstimationsWithCappedValues(
7377
feeEstimationsResp.estimations,
74-
feeEstimationsMaxValues
78+
feeEstimationsMaxValues,
79+
feeEstimationsMinValues
7580
);
76-
setFeeEstimations(feeEstimationsWithMaxValues);
81+
setFeeEstimations(feeEstimationsWithCappedValues);
7782
void analytics.track('use_fee_estimation', {
78-
maxValues: feeEstimationsWithMaxValues,
83+
cappedValues: feeEstimationsWithCappedValues,
7984
estimations: feeEstimationsResp.estimations,
8085
});
8186
}

src/app/pages/transaction-request/components/fee-form.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import { useFeeEstimationsState } from '@app/store/transactions/fees.hooks';
1515
import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
1616
import {
1717
getDefaultSimulatedFeeEstimations,
18-
getFeeEstimationsWithMaxValues,
18+
getFeeEstimationsWithCappedValues,
1919
} from '@shared/transactions/fee-estimations';
20-
import { useFeeEstimationsMaxValues } from '@app/common/transactions/use-fee-estimations-max-values';
20+
import {
21+
useFeeEstimationsMaxValues,
22+
useFeeEstimationsMinValues,
23+
} from '@app/common/transactions/use-fee-estimations-capped-values';
2124

2225
export function FeeForm(): JSX.Element | null {
2326
const analytics = useAnalytics();
@@ -34,6 +37,7 @@ export function FeeForm(): JSX.Element | null {
3437

3538
const [, setFeeEstimations] = useFeeEstimationsState();
3639
const feeEstimationsMaxValues = useFeeEstimationsMaxValues();
40+
const feeEstimationsMinValues = useFeeEstimationsMinValues();
3741

3842
useEffect(() => {
3943
if (feeEstimationsResp) {
@@ -45,13 +49,14 @@ export function FeeForm(): JSX.Element | null {
4549
void analytics.track('use_fee_estimation_default_simulated');
4650
}
4751
if (feeEstimationsResp.estimations && feeEstimationsResp.estimations.length) {
48-
const feeEstimationsWithMaxValues = getFeeEstimationsWithMaxValues(
52+
const feeEstimationsWithCappedValues = getFeeEstimationsWithCappedValues(
4953
feeEstimationsResp.estimations,
50-
feeEstimationsMaxValues
54+
feeEstimationsMaxValues,
55+
feeEstimationsMinValues
5156
);
52-
setFeeEstimations(feeEstimationsWithMaxValues);
57+
setFeeEstimations(feeEstimationsWithCappedValues);
5358
void analytics.track('use_fee_estimation', {
54-
maxValues: feeEstimationsWithMaxValues,
59+
cappedValues: feeEstimationsWithCappedValues,
5560
estimations: feeEstimationsResp.estimations,
5661
});
5762
}

src/app/query/hiro-config/hiro-config.query.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ interface ActiveFiatProviderType {
1919
}
2020

2121
interface FeeEstimationsConfig {
22-
maxValuesEnabled?: boolean;
2322
maxValues?: number[];
23+
maxValuesEnabled?: boolean;
24+
minValues?: number[];
25+
minValuesEnabled?: boolean;
2426
}
2527

2628
interface HiroConfig {
2729
messages: any;
2830
activeFiatProviders?: Record<string, ActiveFiatProviderType>;
29-
feeEstimations?: FeeEstimationsConfig;
31+
feeEstimationsMinMax?: FeeEstimationsConfig;
3032
}
3133

3234
const GITHUB_PRIMARY_BRANCH = 'main';
@@ -69,16 +71,30 @@ export function useHasFiatProviders() {
6971
);
7072
}
7173

72-
export function useConfigFeeEstimationsEnabled() {
74+
export function useConfigFeeEstimationsMaxEnabled() {
7375
const config = useRemoteHiroConfig();
74-
if (isUndefined(config) || isUndefined(config?.feeEstimations)) return;
75-
return config.feeEstimations.maxValuesEnabled;
76+
if (isUndefined(config) || isUndefined(config?.feeEstimationsMinMax)) return;
77+
return config.feeEstimationsMinMax.maxValuesEnabled;
7678
}
7779

7880
export function useConfigFeeEstimationsMaxValues() {
7981
const config = useRemoteHiroConfig();
80-
if (typeof config?.feeEstimations === 'undefined') return;
81-
if (!config.feeEstimations.maxValues) return;
82-
if (!Array.isArray(config.feeEstimations.maxValues)) return;
83-
return config.feeEstimations.maxValues;
82+
if (typeof config?.feeEstimationsMinMax === 'undefined') return;
83+
if (!config.feeEstimationsMinMax.maxValues) return;
84+
if (!Array.isArray(config.feeEstimationsMinMax.maxValues)) return;
85+
return config.feeEstimationsMinMax.maxValues;
86+
}
87+
88+
export function useConfigFeeEstimationsMinEnabled() {
89+
const config = useRemoteHiroConfig();
90+
if (isUndefined(config) || isUndefined(config?.feeEstimationsMinMax)) return;
91+
return config.feeEstimationsMinMax.minValuesEnabled;
92+
}
93+
94+
export function useConfigFeeEstimationsMinValues() {
95+
const config = useRemoteHiroConfig();
96+
if (typeof config?.feeEstimationsMinMax === 'undefined') return;
97+
if (!config.feeEstimationsMinMax.minValues) return;
98+
if (!Array.isArray(config.feeEstimationsMinMax.minValues)) return;
99+
return config.feeEstimationsMinMax.minValues;
84100
}

src/shared/transactions/fee-estimations.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1+
import { BigNumber } from 'bignumber.js';
2+
13
import { DEFAULT_FEE_RATE } from '@shared/constants';
24
import { FeeEstimation } from '@shared/models/fees-types';
3-
import { BigNumber } from 'bignumber.js';
45

5-
export function getFeeEstimationsWithMaxValues(
6+
export function getFeeEstimationsWithCappedValues(
67
feeEstimations: FeeEstimation[],
7-
feeEstimationsMaxValues: number[] | undefined
8+
feeEstimationsMaxValues: number[] | undefined,
9+
feeEstimationsMinValues: number[] | undefined
810
) {
911
return feeEstimations.map((feeEstimation, index) => {
1012
if (
1113
feeEstimationsMaxValues &&
1214
new BigNumber(feeEstimation.fee).isGreaterThan(feeEstimationsMaxValues[index])
1315
) {
1416
return { fee: feeEstimationsMaxValues[index], fee_rate: 0 };
17+
} else if (
18+
feeEstimationsMinValues &&
19+
new BigNumber(feeEstimation.fee).isLessThan(feeEstimationsMinValues[index])
20+
) {
21+
return { fee: feeEstimationsMinValues[index], fee_rate: 0 };
1522
} else {
1623
return feeEstimation;
1724
}

tests/integration/send-tokens/send-tokens.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { UserAreaSelectors } from '@tests/integration/user-area.selectors';
77
import { SendPage } from '../../page-objects/send-form.page';
88
import { WalletPage } from '../../page-objects/wallet.page';
99
import { BrowserDriver, createTestSelector, setupBrowser } from '../utils';
10+
import { SettingsSelectors } from '../settings.selectors';
1011

1112
jest.setTimeout(120_000);
1213
jest.retryTimes(process.env.CI ? 2 : 0);
@@ -42,6 +43,9 @@ describe(`Send tokens flow`, () => {
4243

4344
describe('Set max button', () => {
4445
it('does not set a fee below zero, when the account balance is 0 STX', async () => {
46+
await walletPage.clickSettingsButton();
47+
await walletPage.page.click(createTestSelector(SettingsSelectors.SwitchAccount));
48+
await walletPage.page.click(createTestSelector('switch-account-item-1'));
4549
await sendForm.clickSendMaxBtn();
4650
const amount = await sendForm.getAmountFieldValue();
4751
expect(amount).toEqual('');

0 commit comments

Comments
 (0)