diff --git a/cypress/e2e/flows.test.ts b/cypress/e2e/flows.test.ts index c8826067..d6bc8184 100644 --- a/cypress/e2e/flows.test.ts +++ b/cypress/e2e/flows.test.ts @@ -133,19 +133,12 @@ describe('Select tokens & input amount', () => { describe('Input & output amount validation', () => { it('should type an input amount & wait for output amount', () => { cy.visit('/swap'); - //Select input asset -/* cy.get('[data-testid="swap__input__panel"]').within(() => { - cy.get('[data-testid="swap__token__select"]').click(); - }); - cy.get('[data-testid="currency__list__XLM"]').click(); - */ //Select output asset cy.get('[data-testid="swap__output__panel"]').within(() => { cy.get('[data-testid="swap__token__select"]').click(); }); - cy.get('[data-testid="token-search-input"]').type('ngnt'); - cy.get('[data-testid="currency__list__NGNT"]').click(); - + cy.get('[data-testid="token-search-input"]').type('usdc'); + cy.get('[data-testid="currency__list__USDC"]').click(); //Input amount cy.get('[data-testid="swap__input__panel"]').within(() => { @@ -153,7 +146,6 @@ describe('Input & output amount validation', () => { }); //await for calcs cy.wait(5000); - cy.screenshot() //Get the output amount cy.get('[data-testid="swap-output-input-panel"]').invoke('val').as('outputAmount'); //Get the input amount @@ -168,8 +160,8 @@ describe('Input & output amount validation', () => { cy.get('[data-testid="swap__input__panel"]').within(() => { cy.get('[data-testid="swap__token__select"]').click(); }); - cy.get('[data-testid="token-search-input"]').type('ngnt'); - cy.get('[data-testid="currency__list__NGNT"]').click(); + cy.get('[data-testid="token-search-input"]').type('usdc'); + cy.get('[data-testid="currency__list__USDC"]').click(); //Input amount cy.get('[data-testid="swap__output__panel"]').within(() => { cy.get('.token-amount-input').type('{backspace}'); @@ -177,16 +169,63 @@ describe('Input & output amount validation', () => { cy.get('.token-amount-input').type('1'); }); cy.wait(2500); - cy.screenshot() cy.get('[data-testid="swap-input-input-panel"]').invoke('val').then((inputAmount: any)=>{ - const belowOutput = Math.floor(parseFloat(outputAmount) * 0.9); - const aboveOutput = Math.ceil(parseFloat(outputAmount) * 1.1); + const belowOutput = Math.floor(parseFloat(outputAmount) * 0.5); + const aboveOutput = Math.ceil(parseFloat(outputAmount) * 1.5); expect(parseFloat(inputAmount)).within(belowOutput, aboveOutput) }) }) }) }); + +describe('Slippage tolerance config', ()=>{ + it('should change slippage tolerance', ()=>{ + cy.visit('/swap'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').click(); + cy.get('[data-testid="slippage-input"]').type('1.05'); + cy.get('[data-testid="max-slippage-settings"]').contains('1.05%'); + }) + it('Show display an alert if slippage is too high', ()=>{ + cy.visit('/swap'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').click(); + cy.get('[data-testid="slippage-input"]').type('10'); + cy.get('[data-testid="max-slippage-settings"]').contains('10%'); + cy.get('[data-testid="slippage-alert"]').should('exist'); + cy.get('[data-testid="slippage-alert-too-high"]').should('exist'); + }) + it('Show display an alert if slippage is too low', ()=>{ + cy.visit('/swap'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').click(); + cy.get('[data-testid="slippage-input"]').type('0.01'); + cy.get('[data-testid="max-slippage-settings"]').contains('0.01%'); + cy.get('[data-testid="slippage-alert"]').should('exist'); + cy.get('[data-testid="slippage-alert-too-low"]').should('exist'); + }) + it('should keep the slippage tolerance after closing the settings', ()=>{ + cy.visit('/swap'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').click(); + cy.get('[data-testid="slippage-input"]').type('1.05'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').should('not.exist'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').should('exist') + cy.get('[data-testid="max-slippage-settings"]').contains('1.05%'); + }) + it('Should set the slippage tolerance to auto when pressing "auto" button', ()=>{ + cy.visit('/swap'); + cy.get('[data-testid="open-settings-dialog-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').click(); + cy.get('[data-testid="slippage-input"]').type('1.05'); + cy.get('[data-testid="max-slippage-settings"]').contains('1.05%'); + cy.get('[data-testid="slippage-auto-button"]').click(); + cy.get('[data-testid="max-slippage-settings"]').contains('Auto'); + }) +}) // Navigation flow describe('Navigation flow', () => { it('should render the navbar', () => { diff --git a/package.json b/package.json index b13f17c9..946eb447 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "@scio-labs/use-inkathon": "^0.8.1", "@soroban-react/chains": "9.1.13", "@soroban-react/connect-button": "9.1.13", - "@soroban-react/contracts": "9.1.13", + "@soroban-react/contracts": "9.2.2", "@soroban-react/core": "9.1.13", "@soroban-react/events": "9.1.13", "@soroban-react/freighter": "9.1.13", diff --git a/src/components/Settings/MaxSlippageSettings/index.tsx b/src/components/Settings/MaxSlippageSettings/index.tsx index 603199cd..77facf7f 100644 --- a/src/components/Settings/MaxSlippageSettings/index.tsx +++ b/src/components/Settings/MaxSlippageSettings/index.tsx @@ -103,15 +103,16 @@ export default function MaxSlippageSettings({ autoSlippage }: { autoSlippage: nu function setCustomSlippage() { // When switching to custom slippage, use `auto` value as a default. - setUserSlippageTolerance(autoSlippage); selectSlippageInput(); } - const removeTrailingZeroes = (num: number) => { - return num + const removeTrailingZeroes = (num: number | SlippageTolerance.Auto) => { + if (num === SlippageTolerance.Auto) return; + const parsedNum = num .toFixed(2) .toString() .replace(/0{1,}$/, ''); + return parsedNum[parsedNum.length - 1] === '.' ? parsedNum.slice(0, -1) : parsedNum; }; useEffect(function () { @@ -139,8 +140,8 @@ export default function MaxSlippageSettings({ autoSlippage }: { autoSlippage: nu } button={ - - {isAuto ? <>Auto : `${removeTrailingZeroes(userSlippageTolerance)}%`} + + {isAuto ? 'Auto' : `${removeTrailingZeroes(userSlippageTolerance)}%`} } > @@ -154,7 +155,7 @@ export default function MaxSlippageSettings({ autoSlippage }: { autoSlippage: nu }} isActive={isAuto} > - + Auto @@ -173,11 +174,11 @@ export default function MaxSlippageSettings({ autoSlippage }: { autoSlippage: nu onChange={(e) => parseSlippageInput(e.target.value)} onBlur={() => { // When the input field is blurred, reset the input field to the default value - setSlippageInput(DEFAULT_SLIPPAGE_INPUT_VALUE); - setSlippageError(false); + //setSlippageInput(DEFAULT_SLIPPAGE_INPUT_VALUE); + //setSlippageError(false); }} onFocus={setCustomSlippage} - type="number" + type="text" step="0.1" /> % @@ -185,14 +186,14 @@ export default function MaxSlippageSettings({ autoSlippage }: { autoSlippage: nu {tooLow || tooHigh ? ( - + {tooLow ? ( -
+
Slippage below {MINIMUM_RECOMMENDED_SLIPPAGE.toFixed(2)}% may result in a failed transaction
) : ( -
Your transaction may be frontrun and result in an unfavorable trade.
+
Your transaction may be frontrun and result in an unfavorable trade.
)} diff --git a/src/components/Swap/AdvancedSwapDetails.tsx b/src/components/Swap/AdvancedSwapDetails.tsx index 6268655f..546388d1 100644 --- a/src/components/Swap/AdvancedSwapDetails.tsx +++ b/src/components/Swap/AdvancedSwapDetails.tsx @@ -1,7 +1,6 @@ -import { BodySmall } from 'components/Text'; +import React from 'react'; +import { BodySmall, } from 'components/Text'; import { Box, styled } from 'soroswap-ui'; -import { ChevronRight } from '@mui/icons-material'; -import { useSorobanReact } from '@soroban-react/core'; import Column from 'components/Column'; import { LoadingRows } from 'components/Loader/styled'; import CurrencyLogo from 'components/Logo/CurrencyLogo'; @@ -10,19 +9,11 @@ import { Separator } from 'components/SearchModal/styleds'; import { MouseoverTooltip } from 'components/Tooltip'; import { formatTokenAmount } from 'helpers/format'; import { useAllTokens } from 'hooks/tokens/useAllTokens'; -import { findToken } from 'hooks/tokens/useToken'; -import React, { useEffect, useState } from 'react'; import { Percent } from 'soroswap-router-sdk'; -import { InterfaceTrade, PlatformType } from 'state/routing/types'; +import SwapPathComponent from './SwapPathComponent'; +import { InterfaceTrade } from 'state/routing/types'; -export const PathBox = styled(Box)` - display: flex; - align-items: center; - justify-content: center; - flex-direction: row; -`; - -interface AdvancedSwapDetailsProps { +export interface AdvancedSwapDetailsProps { trade: InterfaceTrade | undefined; allowedSlippage: number; syncing?: boolean; @@ -63,6 +54,13 @@ export const formattedPriceImpact = (priceImpact: Percent | Number | undefined) return `~${priceImpact?.toFixed(2)}%`; }; +export const FormattedProtocolName = (protocol: string) => { + return protocol.charAt(0).toUpperCase() + protocol.slice(1); +} +export const calculatePercentage = (parts: number, totalParts: number) => { + return (parts / totalParts) * 100; +} + export function AdvancedSwapDetails({ trade, allowedSlippage, @@ -72,42 +70,8 @@ export function AdvancedSwapDetails({ // const { chainId } = useWeb3React() // const nativeCurrency = useNativeCurrency(chainId) // const txCount = getTransactionCount(trade) - const sorobanContext = useSorobanReact(); const { tokensAsMap, isLoading } = useAllTokens(); - const [pathArray, setPathArray] = useState([]); - - const [pathTokensIsLoading, setPathTokensIsLoading] = useState(false); - - useEffect(() => { - (async () => { - if (!trade?.path || isLoading) return; - if (trade.platform == PlatformType.ROUTER) { - setPathTokensIsLoading(true); - const promises = trade.path.map(async (contract) => { - const asset = await findToken(contract, tokensAsMap, sorobanContext); - const code = asset?.code == 'native' ? 'XLM' : asset?.code; - return code; - }); - const results = await Promise.allSettled(promises); - - const fulfilledValues = results - .filter((result) => result.status === 'fulfilled' && result.value) - .map((result) => (result.status === 'fulfilled' && result.value ? result.value : '')); - setPathArray(fulfilledValues); - setPathTokensIsLoading(false); - } else if (trade.platform == PlatformType.STELLAR_CLASSIC) { - setPathTokensIsLoading(true); - const codes = trade.path.map((address) => { - if (address == 'native') return 'XLM'; - return address.split(':')[0]; - }); - setPathArray(codes); - setPathTokensIsLoading(false); - } - })(); - }, [trade?.path, isLoading, sorobanContext]); - return ( @@ -160,29 +124,7 @@ export function AdvancedSwapDetails({ - { - - - - Path - - - - - {pathArray?.map((contract, index) => ( - - {contract} - {index !== pathArray.length - 1 && } - - ))} - - - - } + {trade?.platform && ( diff --git a/src/components/Swap/SwapModalFooter.tsx b/src/components/Swap/SwapModalFooter.tsx index 737c6ed6..0818ea14 100644 --- a/src/components/Swap/SwapModalFooter.tsx +++ b/src/components/Swap/SwapModalFooter.tsx @@ -1,25 +1,20 @@ -import { CircularProgress, styled, useTheme } from 'soroswap-ui'; -import { useSorobanReact } from '@soroban-react/core'; -import BigNumber from 'bignumber.js'; +import { styled, useTheme } from 'soroswap-ui'; import { ButtonError, SmallButtonPrimary } from 'components/Buttons/Button'; import Column from 'components/Column'; import CurrencyLogo from 'components/Logo/CurrencyLogo'; import Row, { AutoRow, RowBetween, RowFixed } from 'components/Row'; import { BodySmall, HeadlineSmall, SubHeaderSmall } from 'components/Text'; import { MouseoverTooltip } from 'components/Tooltip'; -import { getPriceImpactNew2 } from 'functions/getPriceImpact'; -import { formatTokenAmount, twoDecimalsPercentage } from 'helpers/format'; -import { useAllTokens } from 'hooks/tokens/useAllTokens'; -import { findToken } from 'hooks/tokens/useToken'; -import useGetReservesByPair from 'hooks/useGetReservesByPair'; +import { formatTokenAmount } from 'helpers/format'; import { getSwapAmounts } from 'hooks/useSwapCallback'; -import React, { ReactNode, useEffect, useState } from 'react'; -import { AlertTriangle, ChevronRight } from 'react-feather'; -import { InterfaceTrade, PlatformType, TradeType } from 'state/routing/types'; -import { PathBox, TextWithLoadingPlaceholder, formattedPriceImpact } from './AdvancedSwapDetails'; +import React, { ReactNode } from 'react'; +import { AlertTriangle } from 'react-feather'; +import { InterfaceTrade, TradeType } from 'state/routing/types'; +import { formattedPriceImpact } from './AdvancedSwapDetails'; import { Label } from './SwapModalHeaderAmount'; import { getExpectedAmountOfOne } from './TradePrice'; import { SwapCallbackError, SwapShowAcceptChanges } from './styleds'; +import SwapPathComponent from './SwapPathComponent'; const DetailsContainer = styled(Column)` padding: 0 8px; @@ -77,13 +72,10 @@ export default function SwapModalFooter({ // const routes = isClassicTrade(trade) ? getRoutingDiagramEntries(trade) : undefined // const { chainId } = useWeb3React() // const nativeCurrency = useNativeCurrency(chainId) - const { tokensAsMap, isLoading } = useAllTokens(); const label = `${trade?.inputAmount?.currency.code}`; const labelInverted = `${trade?.outputAmount?.currency.code}`; - const sorobanContext = useSorobanReact(); - const getSwapValues = () => { if (!trade || !trade?.tradeType) return { formattedAmount0: '0', formattedAmount1: '0' }; @@ -100,39 +92,6 @@ export default function SwapModalFooter({ return { formattedAmount0, formattedAmount1 }; }; - const [pathArray, setPathArray] = useState([]); - - const [pathTokensIsLoading, setPathTokensIsLoading] = useState(false); - - useEffect(() => { - (async () => { - if (!trade?.path || isLoading) return; - if (trade.platform == PlatformType.ROUTER) { - setPathTokensIsLoading(true); - const promises = trade.path.map(async (contract) => { - const asset = await findToken(contract, tokensAsMap, sorobanContext); - const code = asset?.code == 'native' ? 'XLM' : asset?.code; - return code; - }); - const results = await Promise.allSettled(promises); - - const fulfilledValues = results - .filter((result) => result.status === 'fulfilled' && result.value) - .map((result) => (result.status === 'fulfilled' && result.value ? result.value : '')); - setPathArray(fulfilledValues); - setPathTokensIsLoading(false); - } else if (trade.platform == PlatformType.STELLAR_CLASSIC) { - setPathTokensIsLoading(true); - const codes = trade.path.map((address) => { - if (address == "native") return "XLM" - return address.split(":")[0] - }) - setPathArray(codes); - setPathTokensIsLoading(false); - } - })(); - }, [trade?.path, isLoading, sorobanContext]); - return ( <> @@ -205,27 +164,7 @@ export default function SwapModalFooter({ - - - - - - - - - {pathArray?.map((contract, index) => ( - - {contract} - {index !== pathArray.length - 1 && } - - ))} - - - + {trade?.platform && ( diff --git a/src/components/Swap/SwapPathComponent.tsx b/src/components/Swap/SwapPathComponent.tsx new file mode 100644 index 00000000..d9e41bb9 --- /dev/null +++ b/src/components/Swap/SwapPathComponent.tsx @@ -0,0 +1,165 @@ +import React, { useEffect, useState } from 'react' +import { ChevronRight } from 'react-feather' +import { Box, styled } from 'soroswap-ui' +import Row, { RowBetween, RowFixed } from 'components/Row' +import { BodySmall, LabelSmall } from 'components/Text' +import { InterfaceTrade, PlatformType } from 'state/routing/types' +import { useSorobanReact } from '@soroban-react/core' +import { useAllTokens } from 'hooks/tokens/useAllTokens' +import { findToken } from 'hooks/tokens/useToken' +import { MouseoverTooltip } from 'components/Tooltip' +import { LoadingRows } from 'components/Loader/styled' + +export const PathBox = styled(Box)` + display: flex; + align-items: end; + justify-content: center; + flex-direction: row; +`; + +export const AggregatorPathBox = styled(Box)` + display: flex; + align-items: end; + justify-content: center; + flex-direction: column; +`; + +export function PathLoadingPlaceholder({ + syncing, + width, + children, +}: { + syncing: boolean; + width: number; + children: JSX.Element; +}) { + return syncing ? ( + +
+ + ) : ( + children + ); +} + +export interface distributionData { + parts: number; + path: string[]; + protocol: string; +} +export const formattedProtocolName = (protocol: string) => { + return protocol.charAt(0).toUpperCase() + protocol.slice(1); +} +export const calculatePercentage = (parts: number, totalParts: number) => { + return (parts / totalParts) * 100; +} + +function SwapPathComponent({ trade }: { trade: InterfaceTrade | undefined }) { + const sorobanContext = useSorobanReact(); + const { tokensAsMap, isLoading } = useAllTokens(); + + + const [pathArray, setPathArray] = useState([]); + const [distributionArray, setDistributionArray] = useState([]); + const [totalParts, setTotalParts] = useState(0); + const [pathTokensIsLoading, setPathTokensIsLoading] = useState(false); + + useEffect(() => { + (async () => { + if (!trade?.path || isLoading) return; + if (trade.platform == PlatformType.ROUTER) { + setPathTokensIsLoading(true); + const promises = trade.path.map(async (contract) => { + const asset = await findToken(contract, tokensAsMap, sorobanContext); + const code = asset?.code == 'native' ? 'XLM' : asset?.code; + return code; + }); + const results = await Promise.allSettled(promises); + + const fulfilledValues = results + .filter((result) => result.status === 'fulfilled' && result.value) + .map((result) => (result.status === 'fulfilled' && result.value ? result.value : '')); + setPathArray(fulfilledValues); + setPathTokensIsLoading(false); + } else if (trade.platform == PlatformType.STELLAR_CLASSIC) { + setPathTokensIsLoading(true); + const codes = trade.path.map((address) => { + if (address == 'native') return 'XLM'; + return address.split(':')[0]; + }); + setPathArray(codes); + setPathTokensIsLoading(false); + } else if (trade.platform == PlatformType.AGGREGATOR) { + if (!trade?.distribution) return; + let tempDistributionArray: distributionData[] = []; + setPathTokensIsLoading(!pathTokensIsLoading); + for (let distribution of trade?.distribution) { + const promises = distribution.path.map(async (contract) => { + const asset = await findToken(contract, tokensAsMap, sorobanContext); + const code = asset?.code == 'native' ? 'XLM' : asset?.code; + return code; + }); + const results = await Promise.allSettled(promises); + const fulfilledValues = results + .filter((result) => result.status === 'fulfilled' && result.value) + .map((result) => (result.status === 'fulfilled' && result.value ? result.value : '')); + tempDistributionArray.push({ path: fulfilledValues, parts: distribution.parts, protocol: distribution.protocol_id }); + setDistributionArray(tempDistributionArray); + setTotalParts(tempDistributionArray.reduce((acc, curr) => acc + curr.parts, 0)); + setPathTokensIsLoading(false); + } + } + })(); + }, [trade?.path, isLoading, sorobanContext]); + + return ( + + + + {trade?.platform == PlatformType.AGGREGATOR && distributionArray.length > 1 ? 'Paths:' : 'Path'} + + + + {(trade?.platform == PlatformType.ROUTER || trade?.platform == PlatformType.STELLAR_CLASSIC) && ( + + + {pathArray?.map((contract, index) => ( + + {contract} + {index !== pathArray.length - 1 && } + + ))} + + + )} + {(trade?.platform == PlatformType.AGGREGATOR) && ( + + + {distributionArray.map((distribution, index) => ( + + + {formattedProtocolName(distribution.protocol) + ':'} + {distribution.path.map((symbol, index) => ( + + {symbol} + {index !== distribution.path.length - 1 && ( + + )} + + ))} + ({calculatePercentage(distribution.parts, totalParts)}%) + + + ))} + + + )} + + ) +} + +export default SwapPathComponent diff --git a/src/hooks/useBestTrade.ts b/src/hooks/useBestTrade.ts index 210b57ee..d59d71f3 100644 --- a/src/hooks/useBestTrade.ts +++ b/src/hooks/useBestTrade.ts @@ -130,6 +130,7 @@ export function useBestTrade( tradeType, rawRoute: data, path: data?.trade.path, + distribution: data?.trade.distribution, priceImpact: data?.priceImpact, platform: data?.platform, };