Skip to content

Commit

Permalink
Merge pull request #446 from reservoirprotocol/ted/-add-slippage-conf…
Browse files Browse the repository at this point in the history
…iguration

Add slippageTolerance prop to SwapWidget
  • Loading branch information
ted-palmer authored Jan 24, 2025
2 parents 378d5fd + f0c558e commit a5dada7
Show file tree
Hide file tree
Showing 8 changed files with 10,044 additions and 7,989 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-tigers-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-kit-ui': patch
---

Add slippageTolerance prop to swap widget
21 changes: 21 additions & 0 deletions demo/pages/ui/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ const SwapWidgetPage: NextPage = () => {
}
| undefined
>()
const [slippageTolerance, setSlippageTolerance] = useState<
string | undefined
>(undefined)

const linkedWallets = useMemo(() => {
const _wallets = userWallets.reduce((linkedWallets, wallet) => {
Expand Down Expand Up @@ -262,6 +265,7 @@ const SwapWidgetPage: NextPage = () => {
onSwapSuccess={(data) => {
console.log('onSwapSuccess Triggered', data)
}}
slippageTolerance={slippageTolerance}
/>
</div>
<div
Expand All @@ -280,6 +284,23 @@ const SwapWidgetPage: NextPage = () => {
onChange={(e) => setSingleChainMode(e.target.checked)}
/>
</div>
<div style={{ marginTop: '20px' }}>
<label>
Slippage Tolerance (basis points - 50 for 0.5% slippage):{' '}
</label>
<input
type="number"
min="0"
max="10000"
value={slippageTolerance ?? ''}
onChange={(e) =>
setSlippageTolerance(
e.target.value === '' ? undefined : e.target.value
)
}
placeholder="auto"
/>
</div>
</div>
</Layout>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type SwapModalProps = {
customToAddress?: Address | string
tradeType: TradeType
useExternalLiquidity: boolean
slippageTolerance?: string
wallet?: AdaptedWallet
linkedWallets?: LinkedWallet[]
multiWalletSupportEnabled?: boolean
Expand All @@ -62,6 +63,7 @@ export const SwapModal: FC<SwapModalProps> = (swapModalProps) => {
amountInputValue,
amountOutputValue,
useExternalLiquidity,
slippageTolerance,
timeEstimate,
isCanonical,
wallet,
Expand All @@ -82,6 +84,7 @@ export const SwapModal: FC<SwapModalProps> = (swapModalProps) => {
debouncedOutputAmountValue={debouncedOutputAmountValue}
tradeType={tradeType}
useExternalLiquidity={useExternalLiquidity}
slippageTolerance={slippageTolerance}
address={address}
recipient={recipient}
wallet={wallet}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Props = {
customToAddress?: Address
tradeType: TradeType
useExternalLiquidity: boolean
slippageTolerance?: string
wallet?: AdaptedWallet
invalidateBalanceQueries: () => void
children: (props: ChildrenProps) => ReactNode
Expand All @@ -130,6 +131,7 @@ export const TransactionModalRenderer: FC<Props> = ({
customToAddress,
tradeType,
useExternalLiquidity,
slippageTolerance,
wallet,
invalidateBalanceQueries,
children,
Expand Down Expand Up @@ -182,6 +184,7 @@ export const TransactionModalRenderer: FC<Props> = ({
toToken.decimals
).toString(),
referrer: relayClient?.source ?? undefined,
slippageTolerance: slippageTolerance,
useExternalLiquidity
}
: undefined
Expand Down
5 changes: 5 additions & 0 deletions packages/ui/src/components/widgets/SwapWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type BaseSwapWidgetProps = {
defaultToAddress?: Address
defaultAmount?: string
defaultTradeType?: 'EXACT_INPUT' | 'EXPECTED_OUTPUT'
slippageTolerance?: string
lockToToken?: boolean
lockFromToken?: boolean
lockChainId?: number
Expand Down Expand Up @@ -86,6 +87,7 @@ const SwapWidget: FC<SwapWidgetProps> = ({
defaultToAddress,
defaultAmount,
defaultTradeType,
slippageTolerance,
lockToToken = false,
lockFromToken = false,
lockChainId,
Expand Down Expand Up @@ -133,6 +135,7 @@ const SwapWidget: FC<SwapWidgetProps> = ({
defaultTradeType={defaultTradeType}
defaultFromToken={initialFromToken}
defaultToToken={defaultToToken}
slippageTolerance={slippageTolerance}
wallet={wallet}
linkedWallets={linkedWallets}
multiWalletSupportEnabled={multiWalletSupportEnabled}
Expand Down Expand Up @@ -187,6 +190,7 @@ const SwapWidget: FC<SwapWidgetProps> = ({
isValidToAddress,
supportsExternalLiquidity,
useExternalLiquidity,
slippageTolerance,
canonicalTimeEstimate,
fromChainWalletVMSupported,
toChainWalletVMSupported,
Expand Down Expand Up @@ -318,6 +322,7 @@ const SwapWidget: FC<SwapWidgetProps> = ({
}
}}
useExternalLiquidity={useExternalLiquidity}
slippageTolerance={slippageTolerance}
onSwapSuccess={onSwapSuccess}
onSwapValidating={onSwapValidating}
onAnalyticEvent={onAnalyticEvent}
Expand Down
15 changes: 14 additions & 1 deletion packages/ui/src/components/widgets/SwapWidgetRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type SwapWidgetRendererProps = {
defaultToAddress?: Address
defaultAmount?: string
defaultTradeType?: TradeType
slippageTolerance?: string
context: 'Swap' | 'Deposit' | 'Withdraw'
wallet?: AdaptedWallet
linkedWallets?: LinkedWallet[]
Expand Down Expand Up @@ -115,6 +116,7 @@ export type ChildrenProps = {
ctaCopy: string
isFromNative: boolean
useExternalLiquidity: boolean
slippageTolerance?: string
supportsExternalLiquidity: boolean
timeEstimate?: { time: number; formattedTime: string }
canonicalTimeEstimate?: { time: number; formattedTime: string }
Expand All @@ -141,6 +143,7 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
defaultToAddress,
defaultAmount,
defaultTradeType,
slippageTolerance,
context,
wallet,
multiWalletSupportEnabled = false,
Expand Down Expand Up @@ -404,6 +407,14 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
? true
: false

const [currentSlippageTolerance, setCurrentSlippageTolerance] = useState<
string | undefined
>(slippageTolerance)

useEffect(() => {
setCurrentSlippageTolerance(slippageTolerance)
}, [slippageTolerance])

const quoteParameters =
fromToken && toToken
? {
Expand All @@ -427,7 +438,8 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
).toString(),
referrer: relayClient?.source ?? undefined,
useExternalLiquidity,
useDepositAddress: !fromChainWalletVMSupported
useDepositAddress: !fromChainWalletVMSupported,
slippageTolerance: slippageTolerance
}
: undefined

Expand Down Expand Up @@ -691,6 +703,7 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
ctaCopy,
isFromNative,
useExternalLiquidity,
slippageTolerance: currentSlippageTolerance,
supportsExternalLiquidity,
timeEstimate,
canonicalTimeEstimate,
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/components/widgets/WidgetContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type WidgetContainerProps = {
| 'setCustomToAddress'
| 'useExternalLiquidity'
| 'timeEstimate'
| 'slippageTolerance'
>

const WidgetContainer: FC<WidgetContainerProps> = ({
Expand All @@ -63,6 +64,7 @@ const WidgetContainer: FC<WidgetContainerProps> = ({
customToAddress,
address,
useExternalLiquidity,
slippageTolerance,
timeEstimate,
recipient,
toChain,
Expand Down Expand Up @@ -99,6 +101,7 @@ const WidgetContainer: FC<WidgetContainerProps> = ({
debouncedOutputAmountValue={debouncedOutputAmountValue}
tradeType={tradeType}
useExternalLiquidity={useExternalLiquidity}
slippageTolerance={slippageTolerance}
address={address}
recipient={recipient}
isCanonical={useExternalLiquidity}
Expand Down
Loading

0 comments on commit a5dada7

Please sign in to comment.