From bf496529c29a120fdb6f9557a66dc28d492eadb6 Mon Sep 17 00:00:00 2001 From: fanismichalakis Date: Sat, 29 Oct 2022 10:53:48 +0200 Subject: [PATCH 1/4] feat: use minSendable and maxSendable amounts for input suggestions --- src/app/components/SatButtons/index.tsx | 35 ++++++++++++++++++------- src/app/screens/LNURLPay/index.tsx | 11 ++++++-- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/app/components/SatButtons/index.tsx b/src/app/components/SatButtons/index.tsx index 5a7b33b3a7..b3b71c7199 100644 --- a/src/app/components/SatButtons/index.tsx +++ b/src/app/components/SatButtons/index.tsx @@ -5,36 +5,53 @@ import Button from "../Button"; type Props = { onClick: (amount: string) => void; disabled?: boolean; + min?: number; + max?: number; }; -function SatButtons({ onClick, disabled }: Props) { +function roundThousands(number: number) { + return number < 1000 ? number : Math.floor(number / 1000) * 1000; +} + +function stringifyThousands(number: number) { + return number < 1000 ? `${number}` : `${Math.floor(number / 1000)}K`; +} + +function amountsArray(min: number, max: number) { + const distance = Math.min(max - min, 10000); + const safeMin = max < 100 ? min : Math.max(min, 100); + return [safeMin, min + distance / 10, min + distance / 2, min + distance]; +} + +function SatButtons({ onClick, disabled, min = 100, max = 10000 }: Props) { + const amounts = amountsArray(min, max); return (
)} @@ -441,7 +443,12 @@ function LNURLPay() { isFocused={false} label={tCommon("actions.confirm")} loading={loadingConfirm} - disabled={loadingConfirm || !valueSat} + disabled={ + loadingConfirm || + !valueSat || + +valueSat < +details.minSendable / 1000 || + +valueSat > +details.maxSendable / 1000 + } onCancel={reject} /> From d1daf68b6f9a173af511d0d56c6004c07c07f8b9 Mon Sep 17 00:00:00 2001 From: Fanis Michalakis Date: Tue, 1 Nov 2022 14:42:02 +0100 Subject: [PATCH 2/4] feat: round sub-1k numbers and dedup suggestions when range is tight --- src/app/components/SatButtons/index.tsx | 50 +++++++++---------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/app/components/SatButtons/index.tsx b/src/app/components/SatButtons/index.tsx index b3b71c7199..de3605d7fc 100644 --- a/src/app/components/SatButtons/index.tsx +++ b/src/app/components/SatButtons/index.tsx @@ -10,51 +10,37 @@ type Props = { }; function roundThousands(number: number) { - return number < 1000 ? number : Math.floor(number / 1000) * 1000; + return number < 1000 ? Math.round(number) : Math.floor(number / 1000) * 1000; } function stringifyThousands(number: number) { - return number < 1000 ? `${number}` : `${Math.floor(number / 1000)}K`; + return number < 1000 + ? `${Math.round(number)}` + : `${Math.floor(number / 1000)}K`; } function amountsArray(min: number, max: number) { const distance = Math.min(max - min, 10000); const safeMin = max < 100 ? min : Math.max(min, 100); - return [safeMin, min + distance / 10, min + distance / 2, min + distance]; + return [safeMin, min + distance / 10, min + distance / 2, min + distance].map( + (amount) => roundThousands(amount) + ); } function SatButtons({ onClick, disabled, min = 100, max = 10000 }: Props) { - const amounts = amountsArray(min, max); + const amounts = [...new Set(amountsArray(min, max))]; //we use Set to dedup the array, useful of the min-max range is tight return (
-
); } From 9884913da40fc9a1e97775c1d1612599d84e1fc4 Mon Sep 17 00:00:00 2001 From: fanismichalakis Date: Wed, 2 Nov 2022 09:50:58 +0100 Subject: [PATCH 3/4] fix: typo in comment --- src/app/components/SatButtons/index.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/components/SatButtons/index.tsx b/src/app/components/SatButtons/index.tsx index de3605d7fc..73a830cf29 100644 --- a/src/app/components/SatButtons/index.tsx +++ b/src/app/components/SatButtons/index.tsx @@ -28,7 +28,8 @@ function amountsArray(min: number, max: number) { } function SatButtons({ onClick, disabled, min = 100, max = 10000 }: Props) { - const amounts = [...new Set(amountsArray(min, max))]; //we use Set to dedup the array, useful of the min-max range is tight + // we use Set to dedup the array, useful if the min-max range is tight + const amounts = [...new Set(amountsArray(min, max))]; return (
{amounts.map((amount, index) => ( From 9efa98d474491e0624fb0fbdb7ca900b50dcecdc Mon Sep 17 00:00:00 2001 From: fanismichalakis Date: Mon, 19 Dec 2022 18:55:48 +0100 Subject: [PATCH 4/4] fix: better rounding and amount suggestion --- src/app/components/SatButtons/index.tsx | 46 +++++++++++++++++++------ 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/app/components/SatButtons/index.tsx b/src/app/components/SatButtons/index.tsx index 73a830cf29..1a5d72f0ab 100644 --- a/src/app/components/SatButtons/index.tsx +++ b/src/app/components/SatButtons/index.tsx @@ -9,22 +9,48 @@ type Props = { max?: number; }; -function roundThousands(number: number) { - return number < 1000 ? Math.round(number) : Math.floor(number / 1000) * 1000; -} - function stringifyThousands(number: number) { return number < 1000 ? `${Math.round(number)}` - : `${Math.floor(number / 1000)}K`; + : number < 1_000_000 + ? `${Math.floor(number / 1000)}K` + : `${Math.floor(number / 1_000_000)}M`; +} + +function tensRounder(number: number, index: number) { + const rounded = Math.round(number); + const tens = Math.round(Math.log(rounded) * Math.LOG10E) - 1; + const integralFunction = index === 0 ? Math.ceil : Math.trunc; + return integralFunction(rounded / Math.pow(10, tens)) * Math.pow(10, tens); } function amountsArray(min: number, max: number) { - const distance = Math.min(max - min, 10000); - const safeMin = max < 100 ? min : Math.max(min, 100); - return [safeMin, min + distance / 10, min + distance / 2, min + distance].map( - (amount) => roundThousands(amount) - ); + let safeMin = Math.max(Math.ceil(min), 100); + const safeMax = Math.min(Math.floor(max), 100_000_000); + if (safeMin >= safeMax) { + safeMin = Math.ceil(min); + } + const distance = Math.round( + Math.log(Math.round(safeMax / safeMin)) * Math.LOG10E + ); // we define the distance as the number of tens of the ratio + if (distance >= 3) { + const factor = Math.pow(10, distance - 3); + return distance % 2 === 0 + ? [safeMin, factor * 50 * safeMin, factor * 100 * safeMin, safeMax].map( + (x, index) => tensRounder(x, index) + ) + : [safeMin, factor * 10 * safeMin, factor * 100 * safeMin, safeMax].map( + (x, index) => tensRounder(x, index) + ); + } else if (distance === 2) { + return [safeMin, 5 * safeMin, 10 * safeMin, safeMax].map((x, index) => + tensRounder(x, index) + ); + } else { + return [safeMin, (safeMin + safeMax) / 2, safeMax].map((x, index) => + tensRounder(x, index) + ); + } } function SatButtons({ onClick, disabled, min = 100, max = 10000 }: Props) {