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

SwapWidget not accepting ethers providers #216

Open
rowanryan opened this issue Sep 21, 2022 · 10 comments
Open

SwapWidget not accepting ethers providers #216

rowanryan opened this issue Sep 21, 2022 · 10 comments
Labels
bug Something isn't working

Comments

@rowanryan
Copy link

rowanryan commented Sep 21, 2022

Bug Description
Ethers.js FallbackProvider not working with SwapWidget. The FallbackProvider is what is returned by ethers.getDefaultPovider(). When using an editor like VSCode, the editor tells you that the passed in provider is missing properties of the jsonRpcProvider. This is weird, because the documentation says I can also pass in any ethers provider or any EIP-1193 provider.

Steps to Reproduce

  1. Import the SwapWidget on a page.
  2. Create a provider using ethers.getDefaultProvider()
  3. Pass the ethers provider into the provider props of the SwapWidget.
  4. You will get an error saying that the SwapWidget doesn't accept this provider.

Expected Behavior
I expect the widget to accept any ethers provider, like the documentation says.

Additional Context
I am using version 2.8.1 of the "@uniswap/widgets package".
Version 5.7.0 of the "ethers" package.

Image of the error:
image

@rowanryan rowanryan added the bug Something isn't working label Sep 21, 2022
@rowanryan rowanryan changed the title window is not defined in NextJS app SwapWidget not accepting ethers providers Sep 21, 2022
@0x2me
Copy link

0x2me commented Oct 7, 2022

Yeah i have the same issue, using the most recent version of wagmi, I cant use the provider in the Uni wap widget. Seems like it must be an older version of the provider needed, not sure exactly which version tho,

@0x2me
Copy link

0x2me commented Oct 7, 2022

I think it's just a typescript error so if you cast your provider to a Web3Provider it will work.
provider={provider as Web3Provider}

@rowanryan
Copy link
Author

I think it's just a typescript error so if you cast your provider to a Web3Provider it will work. provider={provider as Web3Provider}

I tried this now. It doesn't give the typescript error anymore, but it also doesn't work. I still have to manually connect my wallet through the widget. It doesn't detect the provider.

I also use wagmi, by the way.

@0x2me
Copy link

0x2me commented Oct 7, 2022

https://wagmi.sh/docs/hooks/useWebSocketProvider use the websocket provider. It worked for me. You actually don't need the cast then. You have to pass in the chain Id through

@rowanryan
Copy link
Author

https://wagmi.sh/docs/hooks/useWebSocketProvider use the websocket provider. It worked for me. You actually don't need the cast then. You have to pass in the chain Id through

So, I tested it, and it does work for fetching the price, but unfortunately it is still not picking up on the actual connected wallet.
I connected my wallet through WalletConnect using wagmi, but the widget was still connected to my metamask wallet that is installed in my browser (google chrome). I think we'll just have to wait for Uniswap to update the widget.

Thank you for these tips, though.

@ilmpc
Copy link

ilmpc commented Nov 3, 2022

I figured out, that we should get provider from wallet connector.
So I done something like:

  • useAccount to fetch connector
  • connector.getProvider() returns Promise, so either use react-query, either use effect and state hooks
  • then cast provider to any to skip ts errors
  • also we need react on isConnected, to remove provider from state. If user has disconnected their wallet

@zyrm
Copy link

zyrm commented Jan 17, 2023

@rowanryan did anything change regarding this after the latest updates to the widget?

@fdarian
Copy link

fdarian commented Feb 27, 2023

This is what I found working with the latest wagmi and @uniswap/widgets

import { Web3Provider } from '@ethersproject/providers'
import { useAccount } from 'wagmi'
import { SwapWidget } from '@uniswap/widgets'

// some code...

const [provider, setProvider] = useState<Web3Provider | undefined>()
const { connector } = useAccount()
useEffect(() => {
  if (!connector) {
    return () => setProvider(undefined)
  }

  connector.getProvider().then((provider) => {
    setProvider(new Web3Provider(provider))
  })
}, [connector])

// some code...

        <SwapWidget
          provider={provider} />

@paintoshi
Copy link

@farreldarian Nice solution! I spent hours on this today and came up with a slightly different approach.

export function walletClientToProvider(walletClient: WalletClient) {
  const { chain, transport } = walletClient
  const network = {
    chainId: chain.id,
    name: chain.name,
    ensAddress: chain.contracts?.ensRegistry?.address,
  }
  const provider = new providers.Web3Provider(transport as any, network)
  return provider
}

/** Hook to convert a viem Wallet Client to an ethers.js Web3Provider. */
export function useEthersWeb3Provider({ chainId }: { chainId?: number } = {}) {
  const { data: walletClient } = useWalletClient({ chainId })
  return React.useMemo(
      () => (walletClient ? walletClientToProvider(walletClient) : undefined),
      [walletClient],
  )
}

Then just get the provider for the widget:

const { chain } = useNetwork()
const provider = useEthersWeb3Provider({chain?.id})

@ZiggStardust
Copy link

@paintoshi the widget keep refreshing and loosing state using this method - any ideas ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants