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

Fix chain widget switch wallet #280

Merged
merged 6 commits into from
Sep 19, 2024
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
6 changes: 6 additions & 0 deletions .changeset/many-bears-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@reservoir0x/relay-sdk': patch
'@reservoir0x/relay-kit-ui': patch
---

Fix wallet switching in ChainWidget, fix sdk convert viem chain to RelayChain helper
7 changes: 5 additions & 2 deletions packages/sdk/src/utils/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Chain } from 'viem'
import { zeroAddress, type Chain } from 'viem'
import type { RelayChain, paths } from '../types/index.js'
import * as viemChains from 'viem/chains'
import { ASSETS_RELAY_API } from '../constants/servers.js'
Expand Down Expand Up @@ -93,7 +93,10 @@ export const convertViemChainToRelayChain = (
squaredDark: `${ASSETS_RELAY_API}/icons/square/${chain.id}/dark.png`,
squaredLight: `${ASSETS_RELAY_API}/icons/square/${chain.id}/light.png`
},
currency: chain.nativeCurrency,
currency: {
address: zeroAddress,
...chain.nativeCurrency
},
explorerUrl: chain.blockExplorers?.default.url ?? '',
vmType: 'evm',
depositEnabled: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/ui/src/components/common/MultiWalletDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ export const MultiWalletDropdown: FC<MultiWalletDropdownProps> = ({
}
contentProps={{
sideOffset: 12,
align: 'end',
alignOffset: -16,
align: 'start',
css: { maxWidth: 248, p: 0 }
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
useContext,
useCallback
} from 'react'
import { parseUnits, type Address } from 'viem'
import type {
AdaptedWallet,
Execute,
ExecuteStep,
ExecuteStepItem,
RelayChain
import { parseUnits, type Address, type WalletClient } from 'viem'
import {
type AdaptedWallet,
type Execute,
type ExecuteStep,
type ExecuteStepItem,
type RelayChain
} from '@reservoir0x/relay-sdk'
import {
calculateExecutionTime,
Expand All @@ -31,6 +31,7 @@ import { EventNames } from '../../../constants/events.js'
import { ProviderOptionsContext } from '../../../providers/RelayKitProvider.js'
import { useAccount, useWalletClient } from 'wagmi'
import { extractQuoteId, parseFees } from '../../../utils/quote.js'
import { adaptViemWallet } from '@reservoir0x/relay-sdk'

export enum TransactionProgressStep {
ReviewQuote,
Expand Down Expand Up @@ -236,11 +237,18 @@ export const TransactionModalRenderer: FC<Props> = ({
throw 'Missing a quote'
}

const activeWalletChainId = await wallet?.getChainId()
if (!wallet && !walletClient.data) {
throw 'Missing a wallet'
}

const _wallet =
wallet ?? adaptViemWallet(walletClient.data as WalletClient)

const activeWalletChainId = await _wallet?.getChainId()

if (fromToken && fromToken?.chainId !== activeWalletChainId) {
onAnalyticEvent?.(EventNames.SWAP_SWITCH_NETWORK)
await wallet?.switchChain(fromToken.chainId)
await _wallet?.switchChain(fromToken.chainId)
}

setProgressStep(TransactionProgressStep.WalletConfirmation)
Expand Down Expand Up @@ -333,6 +341,7 @@ export const TransactionModalRenderer: FC<Props> = ({
address,
connector,
wallet,
walletClient,
fromToken,
toToken,
customToAddress,
Expand Down