-
Notifications
You must be signed in to change notification settings - Fork 0
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
Migrate from useDapp -> wagmi #42
Conversation
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/0xstation/web/9wigfPRGS3Nf3oXEWbGcAG26NytF |
@@ -1,5 +0,0 @@ | |||
interface Account { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saw this type already exists here, so deleting this one
// Chains for connectors to support | ||
const chains = defaultChains | ||
|
||
const provider = ({ chainId }) => new providers.AlchemyProvider(4, process.env.RINKEBY_API_KEY) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rinkeby provider is required to interact with the contracts
const { ethereum } = window | ||
|
||
// @ts-ignore | ||
if (!ethereum?.providers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm actually not sure where providers
is coming from 😐 - metamask doesn't define providers
on window.ethereum
's type . It appears when I have both the metamask and coinbase extension though. Using @ts-ignore for now, lmk if there's a better solution
|
||
const Map = () => { | ||
const activeUser: Account | null = useStore((state) => state.activeUser) | ||
const [contributorBoolean, setContributorBoolean] = useState(false) | ||
const [page, setPage] = useState(0) | ||
const [terminals] = useQuery(getTerminals, { suspense: false }) | ||
const [terminals] = useQuery(getTerminals, {}, { suspense: false }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason adding the second argument solved the suspense issue I was experiencing
Error: ReactDOMServer does not yet support Suspense.
at ReactDOMServerRenderer.read (/Users/kristencheung/projects/station-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:3704:25)
at renderToString (/Users/kristencheung/projects/station-web/node_modules/react-dom/cjs/react-dom-server.node.development.js:4298:27)
at Object.renderPage (/Users/kristencheung/projects/station-web/node_modules/next/dist/server/render.js:653:31)
at Function.getInitialProps (webpack-internal:///./node_modules/next/dist/pages/_document.js:193:19)
at Object.loadGetInitialProps (/Users/kristencheung/projects/station-web/node_modules/next/dist/shared/lib/utils.js:69:29)
at Object.renderToHTML (/Users/kristencheung/projects/station-web/node_modules/next/dist/server/render.js:676:40)
at async doRender (/Users/kristencheung/projects/station-web/node_modules/next/dist/server/next-server.js:1133:38)
at async /Users/kristencheung/projects/station-web/node_modules/next/dist/server/next-server.js:1227:28
at async /Users/kristencheung/projects/station-web/node_modules/next/dist/server/response-cache.js:60:36
```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, I don't really know what the react code is doing but the other stuff checks out. thanks for being so thorough!!
import { useEthers } from "@usedapp/core" | ||
import { useState, useEffect, useMemo } from "react" | ||
import { useAccount, useBalance } from "wagmi" | ||
import { BigNumberish, utils } from "ethers" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lmao "ish" in BigNumberish
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know.. crypto naming man. wagmi.. BigNumberish...
const [{ data: accountData }] = useAccount({ | ||
fetchEns: true, | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe a comment describing what happens to accountData
if there is an ENS found for the address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's on the type so I'm gonna leave it out for now.. I still need to figure out how to fetch the ens name from rinkeby too
{
data?: {
address: string
connector: Connector
ens?: {
avatar?: string
name: string
}
}
error?: Error
loading?: boolean
}
const { read: getAllowance } = useEndorsementTokenRead({ | ||
methodName: "allowance", | ||
}) | ||
|
||
const { write: approveAllowance } = useEndorsementTokenWrite({ methodName: "approve" }) | ||
|
||
const { loading, write: endorse } = useEndorsementGraphWrite({ methodName: "endorse" }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this work? is this creating functions that we can apply later? maybe a comment to describe what's going on bc it looks not-normal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's actually a learning I had from wagmi:
the hook can call a contract on mount, but it can also return a function to call the contract manually later on. An optional skip argument can be passed if you don't want the data to be called on mount, and a watch option can be passed if you want the hook to monitor each block that's hashed.
It's a core characteristic on every wagmi hook so Ima leave it out, but can add if anyone has trouble using it.
I can also add the wagmi docs in the README so people know where to look for more info.
Description
Spent the past couple of days reading, prototyping, and implementing wagmi code and ethers. Code is far from perfect, but the chunkiest and messiest part of this pr is used for the
EndorseContributorModal
which will mostly be scrapped anyways for the actual endorse flow that reflects the designs.Overall took away a couple of learnings on how to use the library - I'm mainly comparing against
useDapp
since I haven't tried other libraries likeweb3-react
. I'll try to point out some of the learnings via github comments too.How to connect to a wallet without coinbase and metamask opening up:
Multiple wallets opening up is technically a feature✨ to allow a user to choose which provider they would like to use. Since we're presenting the user with options on the UI this doesn't make sense for our use case. To prevent this, we need to select the provider prior to the user connecting their wallet.
Ref: Uniswap/web3-react#300
AutoConnect functionality
autoConnect
is an option that can be specified in the wagmi provider to determine whether we want to auto-connect the user every time they come to station. This keeps the user logged in every time they refresh the page or when they leave and come back. I'm pretty sure I've seen some sites use this, but I don't recommend using it. The users's wallet connector is stored in localStorage and is automatically used to read the user's address even if their metamask wallet isn't connected to the site anymore. Also, when the user disconnects and refreshes the page, it will look like they've connected again in the nav bar.After playing around with the mirror site, it seems like they're also not using the
autoConnect
option. I'm not connected when I leave the site and come back after a while but upon refreshing, my account is still connected, so I believe they're storing the connection in state somehow.Ref: https://wagmi-xyz.vercel.app/docs/provider#autoconnect-optional
Disconnect functionality
I was under the impression the disconnect function removes the connection to the site which would also be reflected via metamask settings, but that's not the case. Instead "disconnect" makes it so that "the provider will not accept any new requests until the connection to the chain has been re-restablished, which requires reloading the page" (ref: metamask docs). I'm a little confused by what the docs mean when they say "reloading the page" since I can connect my wallet again without refreshing the page if I click "enter station".
Also this is an insightful comment about connecting / disconnecting from metamask:
It makes me think that the "disconnect" option shouldn't be featured in the nav bar unless we add a signing challenge.
Wagmi pros / cons
Pros:
useBalance
, the data provides a formatted response of the uint256 according to the number of decimals you'd like the uint256 to be formatted to.skip
argument can be passed if you don't want the data to be called on mount, and awatch
option can be passed if you want the hook to monitor each block that's hashed.Cons:
Documentation is decent / an improvement from
useDapp
. The documentation isn't a replacement for reviewingethers
andmetamask
though, I've also had to review the source code multiple times to figure out how everything's working. On a good note, the only library/documentation that you do have to worry about isethers
/metamask
rather than researching multiple library dependencies' documentation.TODO: add rinkeby api key to vercel