Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

🌈 rainbowkit & wagmi integration #1156

Merged
merged 35 commits into from
Jun 9, 2022
Merged

Conversation

enesozturk
Copy link
Contributor

Why

We want to use Rainbow Kit for login on the web instead of Magic.

How

After some discussion with the Rainbow team and some fixes, it's now working as expected. For this transition, I needed to totally refactor useWalletLogin because with the wagmi.sh, we get connection data with different hooks and that trigger different effects to handle login, log out, network change, etc.

Additionally, I needed to add the wrong network button to the header because if the user doesn't realize they logged in with mainnet or something else, they won't be able to Mint, Buy, etc. When they press the Wrong Network button, they will need to select Poly gon and resign again, the button will disappear.

Test Plan

These changes should be well tested and I would appreciate if you help me and test these scenarios:

  • Login with the following wallets with Polygon Network

    • Rainbow, MetaMask, Coinbase
  • Try Minting

  • Login with the following wallets with Ethereum Network

    • Rainbow, MetaMask, Coinbase
  • You'll see the Wrong Network button on the header. Press it, Select Polygon

  • If you are logged with Rainbow or Coinbase, you'll get a notification on the mobile app. Confirm it.

Questions

  • On mobile, due to restrictions, we cannot use the buying feature. That is why I didn't add a check for mobile in useBuyNft hook when I refactor it with Wagmi hooks. Should I consider it? @intergalacticspacehighway

Additional Notes

Currently login business logic looks pretty messy including this one. It's hard to follow data, handle effects, etc. My previous PR was including some refactor. I'll handle them later to keep this PR smaller.

@linear
Copy link

linear bot commented Jun 3, 2022

SHOW2-770 RainbowKit

We are experiencing a lot of issues with bare WalletConnect, especially on mobile web (even if this is working very well sometimes)

Let's try RainbowKit

@vercel
Copy link

vercel bot commented Jun 3, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
showtime ✅ Ready (Inspect) Visit Preview Jun 9, 2022 at 7:43AM (UTC)

Copy link
Contributor

@axeldelafosse axeldelafosse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not deploying because the yarn.lock file is not in a good state. Not sure what's happening -- can't try the PR previews

@@ -0,0 +1,96 @@
import { ConnectButton as RainbotConnectButton } from "@rainbow-me/rainbowkit";

import { Button, View } from "design-system";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if this is working I think it's best to import individual packages from @showtime-xyz/universal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅ Realized that we don't use universal for buttons (@showtime-xyz/universal.button) in the project. Does it have any special case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just fetched new commits, thats ok 👍🏽


export const ConnectButton = ({ handleSubmitWallet }: ConnectButtonProps) => {
return (
<RainbotConnectButton.Custom>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo RainbotConnectButton

name: "Coinbase",
description: "Use the Coinbase Wallet app on your mobile device",
},
coinbasewallet: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need web3 modal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I removed it ✅

@@ -2,12 +2,13 @@ import { useEffect, useMemo, useState } from "react";
import { Platform } from "react-native";

import { Web3Provider as EthersWeb3Provider } from "@ethersproject/providers";
import "@rainbow-me/rainbowkit/styles.css";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should keep this import in _app.tsx, or might crash on native, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed ✅

const provider = await web3Modal.connect();
const ethersProvider = new EthersWeb3Provider(provider);
setWeb3(ethersProvider);
if (Platform.OS !== "web") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think magic wallet was on web and native both, right? Do we need this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I needed to refactor all providers for web and mobile usage because on mobile we use wallet connect and on the web we use Wagmi. This comment might be invalid.

Here is what I did; To use wagmi on same providers, I created provider wrappers for the web (web3-provider.web.tsx, auth-provider.web.tsx). And passed some props to the Web3Provider and AuthProvider. Because I didn't want to add the wagmi and rainbow packages to the mobile repo and wrap the mobile app with their providers.

Can you check them and give some feedback guys? @intergalacticspacehighway @axeldelafosse @alantoa

const useWallet = () => {
const address = useMemo(async () => {
const web3Modal = await getWeb3Modal();
const web3 = new ethers.providers.Web3Provider(await web3Modal.connect());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think here we should use useWeb3 hook as we already have the web3 provider so no need to recreate it I guess. We can delete the getWeb3modal from native as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But on mobile, don't we use getWeb3Modal to get this info as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, actually it was not being used before. I think we added that file so import won't break on native. We always called getWeb3Modal under Platform.OS web check (exception is buying NFT which was not implemented on native)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, there is web3-modal.native.ts file and I though that method uses this on native. I tried not to break something, I might use unnecessary implementations, thanks to mention 👍🏽

@intergalacticspacehighway
Copy link
Collaborator

if we're on dev then we can switch to polygon testnet instead of polygon. There's an env NEXT_PUBLIC_CHAIN_ID which we can use.

@intergalacticspacehighway
Copy link
Collaborator

intergalacticspacehighway commented Jun 6, 2022

i think we might have to discuss UI regarding this!

Oh yes, exactly! I was about to share it but forgot then. I'll share on the design channel how to continue on it. @intergalacticspacehighway

address: wagmiData?.address,
connected,
signed,
loggedIn,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason we need loggedIn and connected both? Logged in might be a bit confusing as if it's a showtime user login or a wallet login.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connected is just used the indicate that if the wallet connection is done and the loggedIn is used to indicate if the user logged in and have storage data (apart from wallet connection). I needed to use both separately to handle set web3 object and handle logins log outs while configure the effects in use-wallet-login.ts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, but loggedIn is derived from connected so they'll always be same, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I get it now, yes, here is the thing; loggedIn is triggered when the connected change, I needed to catch that to trigger something at once. This is because I used that way. Yeah that doesn't looks good approach, I try to refactor it.

@intergalacticspacehighway
Copy link
Collaborator

Great work @enesozturk 👏

We might need to refactor or move some stuff for a better abstraction.
e.g. there are 4 hooks useWallet, useSignerAndProvider, useSignTypedData and useWeb3 and there's some duplication in useSignedTypedData and useWallet. useSignerAndProvider handles biconomy but is poorly named (my fault!).

I think we can have a single hook useWallet abstracted on top of pure wallet connect on native and wagmi on web. This hook can export APIs to handle signing, network change, user address, connected etc.

We can bring this up after merging this PR. I have some ideas on this. Will open a discussion or just do a PR ❇️

@enesozturk
Copy link
Contributor Author

enesozturk commented Jun 6, 2022

@intergalacticspacehighway yeah looks like there are more hooks than needed. You also added useSignTypedData with your last work, I didn't realize that tbh. We would better to get together and refactor some of them I think, that will fix all. Thanks for review!

@enesozturk
Copy link
Contributor Author

enesozturk commented Jun 7, 2022

Today we got an issue with @intergalacticspacehighway; while switching the network on MetaMask, Wagmi disconnects us. We thought it was related to wagmi.sh but looks like it's not.
Here is the issue I opened and the response: wevm/wagmi#563 (comment)

Additionally, I tested drop NFT on both staging and production environments and here is the results;

Rainbow Drop NFT Tests - Production

  • Rainbow Wallet
    • Switch Network ✅
      • If I’m in the wrong network, after pressing Switch Chain on the alert message, I see the confirmation dialog on the Rainbow app, then when I retry, it’s ok ✅
      • Additional note here; Rainbow app should be open state on mobile
    • Drop NFT ✅
  • Coinbase Wallet
    • Switch Network ✅
      • If I’m in the wrong network, after pressing Switch Network on the alert message, I see the confirmation dialog on the Coinbase app, then retry, and it’s ok. ✅
      • Additional note here; Coinbase app should be opened state on mobile
    • Drop NFT ✅
  • MetaMask
    • Getting this error when sign with mainnet => message: "from_address 0xf5B035287c1465F29C7e08FbB5c3b8a4975Bf831' does not belong to the authenticated user
    • Did these changes affected in prod? @fontanierh

Rainbow Drop NFT Tests - Staging (Test APIs)

  • Rainbow Wallet
    • Switch Network 💥
      • I connected with Polygon (not Mumbai), when I try to drop NFT, the switch chain alert opened, pressed but on the Rainbow app, no confirmation modal showed, probably the reason is Polygon Mumbai credentials don't exist on the Rainbow app, and no way to do it as we do on MetaMask. I think this will not be a problem for production.
  • Coinbase Wallet
    • Switch Network ✅
    • Drop NFT ✅
  • MetaMask
    • Switch Network ✅
    • Drop NFT ✅

Also needed to update env keys because they were not consistent, there were some keys which are not included in all of them.

@intergalacticspacehighway
Copy link
Collaborator

@enesozturk LGTM ❇️ . can you look why CI stuff is failing?

@enesozturk
Copy link
Contributor Author

@enesozturk LGTM ❇️ . can you look why CI stuff is failing?

@intergalacticspacehighway Yeah I'm not sure, taking a look the logs now 👍🏽

@github-actions
Copy link

github-actions bot commented Jun 8, 2022

PR Preview - Storybook

This pull request preview deployment is now available.

✅ Preview: exp+io.showtime.storybook://expo-development-client/?url=https://u.expo.dev/e77d5b68-bb27-45da-aa5c-96c1fdbf6706&channel-name=pr-1156

Comment ID: 1149588261

@enesozturk enesozturk merged commit 51cd6c7 into staging Jun 9, 2022
@enesozturk enesozturk deleted the feature/show2-770-rainbowkit branch June 9, 2022 08:09
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants