From 9c3511e2f95b4d4b3587812b4668ad360b1e91db Mon Sep 17 00:00:00 2001 From: jorbuedo Date: Tue, 10 Oct 2023 13:05:48 +0200 Subject: [PATCH 1/2] Min received with slippage --- .../ConfirmTxScreen/TransactionSummary.tsx | 10 ++---- .../CreateOrder/EditPool/ShowPoolActions.tsx | 10 ++---- .../getMinAdaReceiveAfterSlippage.test.ts | 34 ------------------- .../amounts/getMinAdaReceiveAfterSlippage.ts | 30 ---------------- packages/swap/src/index.ts | 1 - 5 files changed, 6 insertions(+), 79 deletions(-) delete mode 100644 packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.test.ts delete mode 100644 packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.ts diff --git a/apps/wallet-mobile/src/features/Swap/useCases/ConfirmTxScreen/TransactionSummary.tsx b/apps/wallet-mobile/src/features/Swap/useCases/ConfirmTxScreen/TransactionSummary.tsx index 8200a426d9..04612bbb6f 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/ConfirmTxScreen/TransactionSummary.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/ConfirmTxScreen/TransactionSummary.tsx @@ -1,11 +1,10 @@ -import {getMinAdaReceiveAfterSlippage, useSwap} from '@yoroi/swap' +import {useSwap} from '@yoroi/swap' import React from 'react' import {StyleSheet, TouchableOpacity, View} from 'react-native' import {Icon, Spacer, Text} from '../../../../components' import {AmountItem} from '../../../../components/AmountItem/AmountItem' import {BottomSheetModal} from '../../../../components/BottomSheetModal' -import {useLanguage} from '../../../../i18n' import {useSelectedWallet} from '../../../../SelectedWallet' import {COLORS} from '../../../../theme' import {useTokenInfo} from '../../../../yoroi-wallets/hooks' @@ -20,7 +19,6 @@ export const TransactionSummary = () => { }) const strings = useStrings() const wallet = useSelectedWallet() - const {numberLocale} = useLanguage() const {orderData} = useSwap() const {amounts, selectedPoolCalculation} = orderData @@ -39,11 +37,9 @@ export const TransactionSummary = () => { }, { label: strings.swapMinReceivedTitle, - value: `${getMinAdaReceiveAfterSlippage( - amounts.buy.quantity, - orderData.slippage, + value: `${Quantities.format( + selectedPoolCalculation?.buyAmountWithSlippage?.quantity ?? Quantities.zero, buyTokenInfo.decimals ?? 0, - numberLocale, )} ${tokenToBuyName}`, info: strings.swapMinReceived, }, diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditPool/ShowPoolActions.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditPool/ShowPoolActions.tsx index 9a15564560..ddc9f35c62 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditPool/ShowPoolActions.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditPool/ShowPoolActions.tsx @@ -1,4 +1,4 @@ -import {getMinAdaReceiveAfterSlippage, useSwap} from '@yoroi/swap' +import {useSwap} from '@yoroi/swap' import {capitalize} from 'lodash' import React from 'react' import {StyleSheet, Text, TouchableOpacity, View} from 'react-native' @@ -10,7 +10,6 @@ import { HiddenInfoWrapper, Spacer, } from '../../../../../../components' -import {useLanguage} from '../../../../../../i18n' import {useSelectedWallet} from '../../../../../../SelectedWallet' import {COLORS} from '../../../../../../theme' import {useTokenInfo} from '../../../../../../yoroi-wallets/hooks' @@ -22,7 +21,6 @@ import {useSwapTouched} from '../../../../common/SwapFormProvider' export const ShowPoolActions = () => { const navigateTo = useNavigateTo() - const {numberLocale} = useLanguage() const {orderData} = useSwap() const strings = useStrings() const {isBuyTouched, isSellTouched, isPoolTouched} = useSwapTouched() @@ -90,11 +88,9 @@ export const ShowPoolActions = () => { selectedPoolCalculation.pool.batcherFee.quantity, Number(wallet.primaryTokenInfo.decimals), )} - minReceived={getMinAdaReceiveAfterSlippage( - amounts.buy.quantity, - orderData.slippage, + minReceived={Quantities.format( + selectedPoolCalculation.buyAmountWithSlippage.quantity, buyTokenInfo.decimals ?? 0, - numberLocale, )} minAda={Quantities.format( selectedPoolCalculation.pool.deposit.quantity, diff --git a/packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.test.ts b/packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.test.ts deleted file mode 100644 index d0ac842251..0000000000 --- a/packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import {mockNumberLocale} from '../../../adapters/intl/number-locale.mocks' -import {asQuantity} from '../../../utils/asQuantity' -import {getMinAdaReceiveAfterSlippage} from './getMinAdaReceiveAfterSlippage' - -describe('getMinAdaReceiveAfterSlippage', () => { - it('should calculate the correct minimum ADA received after applying slippage', () => { - expect( - getMinAdaReceiveAfterSlippage( - asQuantity(1000), - 0.03, - 2, - mockNumberLocale, - ), - ).toBe('9.9') - expect( - getMinAdaReceiveAfterSlippage(asQuantity(500), 1, 2, mockNumberLocale), - ).toBe('4.9') - expect( - getMinAdaReceiveAfterSlippage(asQuantity(1000), 2, 2, mockNumberLocale), - ).toBe('9.8') - }) - - it('should return the original amount when slippage is zero', () => { - expect( - getMinAdaReceiveAfterSlippage(asQuantity(1000), 0, 2, mockNumberLocale), - ).toBe('1') - }) - - it('should return zero when the output amount is zero', () => { - expect( - getMinAdaReceiveAfterSlippage(asQuantity(0), 5, 2, mockNumberLocale), - ).toBe('') - }) -}) diff --git a/packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.ts b/packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.ts deleted file mode 100644 index 828abc1b15..0000000000 --- a/packages/swap/src/helpers/orders/amounts/getMinAdaReceiveAfterSlippage.ts +++ /dev/null @@ -1,30 +0,0 @@ -import {Balance, Numbers} from '@yoroi/types' -import {Quantities} from '../../../utils/quantities' -import {asQuantity} from '../../../utils/asQuantity' - -/** - * Calculate the minimum amount of ADA received after accounting for slippage. - * - * @param {Balance.Quantity} outputAmount - The amount of ADA or output currency. - * @param {number} slippagePercentage - The slippage percentage as a decimal (e.g., 3% as 0.03). - * @param {number} decimals - The number of decimal places for formatting the result. - * @param {NumberLocale} numberLocale - The locale information for formatting numbers. - * - * @returns {string} The minimum ADA amount received after slippage as a string with specified decimals. - */ - -export const getMinAdaReceiveAfterSlippage = ( - outputAmount: Balance.Quantity, - slippagePercentage: number, - decimals: number, - numberLocale: Numbers.Locale, -): string => { - const slippageDecimal = slippagePercentage / 100 - const result = Number(outputAmount) / (1 + slippageDecimal) - const [quantities] = Quantities.parseFromText( - Quantities.denominated(asQuantity(result), decimals), - decimals, - numberLocale, - ) - return quantities.slice(0, -1) -} diff --git a/packages/swap/src/index.ts b/packages/swap/src/index.ts index 91d86d1005..c6cb65ddcd 100644 --- a/packages/swap/src/index.ts +++ b/packages/swap/src/index.ts @@ -10,7 +10,6 @@ export {apiMocks} from './adapters/openswap-api/api.mocks' // orders amounts export {getBuyAmount} from './helpers/orders/amounts/getBuyAmount' export {getSellAmount} from './helpers/orders/amounts/getSellAmount' -export {getMinAdaReceiveAfterSlippage} from './helpers/orders/amounts/getMinAdaReceiveAfterSlippage' export {getQuantityWithSlippage} from './helpers/orders/amounts/getQuantityWithSlippage' // orders factories export {makePossibleMarketOrder} from './helpers/orders/factories/makePossibleMarketOrder' From 0ecf95c097349b69d628671abf703003a9a0d18f Mon Sep 17 00:00:00 2001 From: Sorin Chis Date: Tue, 10 Oct 2023 14:22:02 +0300 Subject: [PATCH 2/2] fix: edit limit price (#2748) --- .../CreateOrder/EditLimitPrice.tsx | 10 +++-- .../ShowTokenActions/ClearQuantities.tsx | 3 +- .../ShowTokenActions/TopTokenActions.tsx | 3 +- .../Swap/common/AmountCard/AmountCard.json | 40 +++++++++---------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditLimitPrice.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditLimitPrice.tsx index 53d342cf13..74ae20f445 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditLimitPrice.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/EditLimitPrice.tsx @@ -19,6 +19,7 @@ export const EditLimitPrice = () => { const {numberLocale} = useLanguage() const [text, setText] = React.useState('') const wallet = useSelectedWallet() + const inputRef = React.useRef(null) const {orderData, limitPriceChanged} = useSwap() const sellTokenInfo = useTokenInfo({wallet, tokenId: orderData.amounts.sell.tokenId}) @@ -33,7 +34,8 @@ export const EditLimitPrice = () => { React.useEffect(() => { if (orderData.type === 'limit') { - setText(Quantities.format(orderData.limitPrice ?? Quantities.zero, denomination, PRECISION)) + !inputRef?.current?.isFocused() && + setText(Quantities.format(orderData.limitPrice ?? Quantities.zero, denomination, PRECISION)) } else { setText( Quantities.format(orderData.selectedPoolCalculation?.prices.market ?? Quantities.zero, denomination, PRECISION), @@ -54,7 +56,7 @@ export const EditLimitPrice = () => { {disabled ? strings.marketPrice : strings.limitPrice} - + @@ -70,8 +72,9 @@ type AmountInputProps = { value?: string onChange(value: string): void editable: boolean + inputRef?: React.RefObject } -const AmountInput = ({onChange, value, editable}: AmountInputProps) => { +const AmountInput = ({onChange, value, editable, inputRef}: AmountInputProps) => { return ( { style={styles.amountInput} underlineColorAndroid="transparent" editable={editable} + ref={inputRef} /> ) } diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/ClearQuantities.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/ClearQuantities.tsx index 28c49f79e6..a5873c84c6 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/ClearQuantities.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/ClearQuantities.tsx @@ -1,6 +1,6 @@ import {useSwap} from '@yoroi/swap' import React from 'react' -import {StyleSheet, Text} from 'react-native' +import {Keyboard, StyleSheet, Text} from 'react-native' import {TouchableOpacity} from 'react-native-gesture-handler' import {COLORS} from '../../../../../../theme' @@ -13,6 +13,7 @@ export const ClearQuantities = () => { const {poolDefaulted} = useSwapTouched() const handleReset = () => { + Keyboard.dismiss() resetQuantities() poolDefaulted() } diff --git a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/TopTokenActions.tsx b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/TopTokenActions.tsx index 561123cbc4..d60ef48368 100644 --- a/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/TopTokenActions.tsx +++ b/apps/wallet-mobile/src/features/Swap/useCases/StartSwapScreen/CreateOrder/ShowTokenActions/TopTokenActions.tsx @@ -1,6 +1,6 @@ import {useSwap, useSwapPoolsByPair} from '@yoroi/swap' import React from 'react' -import {StyleSheet, View} from 'react-native' +import {Keyboard, StyleSheet, View} from 'react-native' import {TouchableOpacity} from 'react-native-gesture-handler' import {Icon} from '../../../../../../components' @@ -40,6 +40,7 @@ export const TopTokenActions = () => { } const handleRefresh = () => { + Keyboard.dismiss() refetch() } diff --git a/apps/wallet-mobile/translations/messages/src/features/Swap/common/AmountCard/AmountCard.json b/apps/wallet-mobile/translations/messages/src/features/Swap/common/AmountCard/AmountCard.json index 7fa3933302..51c7130421 100644 --- a/apps/wallet-mobile/translations/messages/src/features/Swap/common/AmountCard/AmountCard.json +++ b/apps/wallet-mobile/translations/messages/src/features/Swap/common/AmountCard/AmountCard.json @@ -4,14 +4,14 @@ "defaultMessage": "!!!Select token", "file": "src/features/Swap/common/AmountCard/AmountCard.tsx", "start": { - "line": 134, + "line": 135, "column": 15, - "index": 4170 + "index": 4171 }, "end": { - "line": 137, + "line": 138, "column": 3, - "index": 4253 + "index": 4254 } }, { @@ -19,14 +19,14 @@ "defaultMessage": "!!!Current Balance", "file": "src/features/Swap/common/AmountCard/AmountCard.tsx", "start": { - "line": 138, + "line": 139, "column": 18, - "index": 4273 + "index": 4274 }, "end": { - "line": 141, + "line": 142, "column": 3, - "index": 4362 + "index": 4363 } }, { @@ -34,14 +34,14 @@ "defaultMessage": "!!!Not enough balance", "file": "src/features/Swap/common/AmountCard/AmountCard.tsx", "start": { - "line": 142, + "line": 143, "column": 20, - "index": 4384 + "index": 4385 }, "end": { - "line": 145, + "line": 146, "column": 3, - "index": 4478 + "index": 4479 } }, { @@ -49,14 +49,14 @@ "defaultMessage": "!!!Not enough supply in the pool", "file": "src/features/Swap/common/AmountCard/AmountCard.tsx", "start": { - "line": 146, + "line": 147, "column": 19, - "index": 4499 + "index": 4500 }, "end": { - "line": 149, + "line": 150, "column": 3, - "index": 4603 + "index": 4604 } }, { @@ -64,14 +64,14 @@ "defaultMessage": "!!! This pair is not available in any liquidity pool", "file": "src/features/Swap/common/AmountCard/AmountCard.tsx", "start": { - "line": 150, + "line": 151, "column": 10, - "index": 4615 + "index": 4616 }, "end": { - "line": 153, + "line": 154, "column": 3, - "index": 4730 + "index": 4731 } } ] \ No newline at end of file