-
Notifications
You must be signed in to change notification settings - Fork 194
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
draft: rainbow kit support #1548
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
258b10e
basic setup
y77cao 26312fa
create system calls after connect
y77cao d7bb9f5
delegationControl hook
y77cao c824cad
Merge branch 'y77cao/call-from' into y77cao/rainbow-kit
y77cao ef6ca42
buggy setupResult to context
y77cao b76d1eb
minor fixes
y77cao 59b2c3f
Merge branch 'y77cao/call-from' into y77cao/rainbow-kit
y77cao 9bd418e
use hooks for setup
y77cao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,42 @@ | ||
import { createContext, ReactNode, useContext } from "react"; | ||
import { SetupResult } from "./mud/setup"; | ||
import { createContext, ReactNode, useContext, useState, useEffect, useMemo } from "react"; | ||
import { setup, SetupResult } from "./mud/setup"; | ||
import { usePromise } from "@latticexyz/react"; | ||
|
||
const MUDContext = createContext<SetupResult | null>(null); | ||
|
||
type Props = { | ||
children: ReactNode; | ||
value: SetupResult; | ||
}; | ||
|
||
export const MUDProvider = ({ children, value }: Props) => { | ||
// THis is not used. Context doesn't play well with async data fetch so used hook instead | ||
export const MUDProvider = ({ children }: Props) => { | ||
const currentValue = useContext(MUDContext); | ||
if (currentValue) throw new Error("MUDProvider can only be used once"); | ||
return <MUDContext.Provider value={value}>{children}</MUDContext.Provider>; | ||
|
||
// Had weird re-rendering issue with the approach below | ||
// const setupResult = usePromise(setup()); | ||
|
||
// if (setupResult.status === "rejected") throw new Error("Ecountered error while setting up MUD"); | ||
|
||
// if (setupResult.status !== "fulfilled") return; | ||
const setupPromise = useMemo(() => { | ||
return setup(); | ||
}, []); | ||
|
||
const [setupResult, setSetupResult] = useState<Awaited<ReturnType<typeof setup>> | null>(null); | ||
|
||
useEffect(() => { | ||
setupPromise.then((result) => setSetupResult(result)); | ||
|
||
return () => { | ||
setupPromise.then((result) => result.network.world.dispose()); | ||
}; | ||
}, [setupPromise]); | ||
|
||
return <MUDContext.Provider value={setupResult}>{children}</MUDContext.Provider>; | ||
}; | ||
|
||
export const useMUD = () => { | ||
const value = useContext(MUDContext); | ||
if (!value) throw new Error("Must be used within a MUDProvider"); | ||
return value; | ||
}; |
41 changes: 41 additions & 0 deletions
41
templates/react/packages/client/src/hooks/useDelegationControl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ComponentValue, Type, getComponentValue } from "@latticexyz/recs"; | ||
import { useEffect, useState } from "react"; | ||
import { WalletClient, Transport, Chain, Account } from "viem"; | ||
import { encodeEntity } from "@latticexyz/store-sync/recs"; | ||
import { ClientComponents } from "../mud/createClientComponents"; | ||
|
||
export function useDelegationControl( | ||
delegator: WalletClient<Transport, Chain, Account> | null | undefined, | ||
delegatee: WalletClient<Transport, Chain, Account>, | ||
components: ClientComponents | ||
) { | ||
const [delegationControlId, setDelegationControlId] = useState< | ||
| ComponentValue< | ||
{ | ||
__staticData: Type.OptionalString; | ||
__encodedLengths: Type.OptionalString; | ||
__dynamicData: Type.OptionalString; | ||
} & { | ||
delegationControlId: Type.String; | ||
}, | ||
unknown | ||
> | ||
| undefined | ||
>(undefined); | ||
|
||
useEffect(() => { | ||
if (!delegator) return; | ||
|
||
const delegationsKeyEntity = encodeEntity(components.Delegations.metadata.keySchema, { | ||
delegator: delegator.account.address, | ||
delegatee: delegatee.account.address, | ||
}); | ||
const delegationControlId = getComponentValue(components.Delegations, delegationsKeyEntity); | ||
|
||
setDelegationControlId(delegationControlId); | ||
|
||
// TODO what's the cleanup fn? | ||
}, [delegator]); | ||
|
||
return delegationControlId; | ||
} |
24 changes: 24 additions & 0 deletions
24
templates/react/packages/client/src/hooks/usePromiseValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useEffect, useState, useRef } from "react"; | ||
|
||
export const usePromiseValue = <T>(promise: Promise<T> | null) => { | ||
const promiseRef = useRef<typeof promise>(promise); | ||
const [value, setValue] = useState<T | null>(null); | ||
useEffect(() => { | ||
if (!promise) return; | ||
let isMounted = true; | ||
promiseRef.current = promise; | ||
// TODO: do something with promise errors? | ||
promise.then((resolvedValue) => { | ||
// skip if unmounted (state changes will cause errors otherwise) | ||
if (!isMounted) return; | ||
// If our promise was replaced before it resolved, ignore the result | ||
if (promiseRef.current !== promise) return; | ||
|
||
setValue(resolvedValue); | ||
}); | ||
return () => { | ||
isMounted = false; | ||
}; | ||
}, [promise]); | ||
return value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useMemo } from "react"; | ||
import { usePromiseValue } from "./usePromiseValue"; | ||
import { setup } from "../mud/setup"; | ||
|
||
export const useSetup = () => { | ||
const setupPromise = useMemo(() => { | ||
return setup(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
return () => { | ||
setupPromise.then((result) => result.network.world.dispose()); | ||
}; | ||
}, [setupPromise]); | ||
|
||
return usePromiseValue(setupPromise); | ||
}; |
25 changes: 25 additions & 0 deletions
25
templates/react/packages/client/src/hooks/useSystemCalls.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect, useState } from "react"; | ||
import { WalletClient, Transport, Chain, Account } from "viem"; | ||
import { createSystemCalls } from "../mud/createSystemCalls"; | ||
import { SetupNetworkResult } from "../mud/setupNetwork"; | ||
import { ClientComponents } from "../mud/createClientComponents"; | ||
|
||
export function useSystemCalls( | ||
network: SetupNetworkResult, | ||
components: ClientComponents, | ||
client: WalletClient<Transport, Chain, Account> | null | undefined | ||
) { | ||
// TODO type systemCalls | ||
const [systemCalls, setSystemCalls] = useState<ReturnType<typeof createSystemCalls> | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
if (!client) return; | ||
|
||
const systemCalls = createSystemCalls(network, components, client); | ||
setSystemCalls(systemCalls); | ||
|
||
// TODO what's the cleanup fn? | ||
}, [client]); | ||
|
||
return systemCalls; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,45 @@ | ||
import ReactDOM from "react-dom/client"; | ||
import { App } from "./App"; | ||
import { setup } from "./mud/setup"; | ||
import { MUDProvider } from "./MUDContext"; | ||
import { RainbowKitProvider } from "@rainbow-me/rainbowkit"; | ||
import { WagmiConfig } from "wagmi"; | ||
import { share } from "rxjs"; | ||
import mudConfig from "contracts/mud.config"; | ||
import IWorldAbi from "contracts/out/IWorld.sol/IWorld.abi.json"; | ||
import { App } from "./App"; | ||
import { MUDProvider, useMUD } from "./MUDContext"; | ||
import { useSetup } from "./hooks/useSetup"; | ||
|
||
const rootElement = document.getElementById("react-root"); | ||
if (!rootElement) throw new Error("React root not found"); | ||
const root = ReactDOM.createRoot(rootElement); | ||
|
||
// TODO: figure out if we actually want this to be async or if we should render something else in the meantime | ||
setup().then(async (result) => { | ||
root.render( | ||
<MUDProvider value={result}> | ||
<App /> | ||
</MUDProvider> | ||
); | ||
const AppWithWalletContext = () => { | ||
const setup = useSetup(); | ||
|
||
// https://vitejs.dev/guide/env-and-mode.html | ||
if (import.meta.env.DEV) { | ||
const { mount: mountDevTools } = await import("@latticexyz/dev-tools"); | ||
mountDevTools({ | ||
config: mudConfig, | ||
publicClient: result.network.publicClient, | ||
walletClient: result.network.walletClient, | ||
latestBlock$: result.network.latestBlock$, | ||
storedBlockLogs$: result.network.storedBlockLogs$, | ||
worldAddress: result.network.worldContract.address, | ||
worldAbi: result.network.worldContract.abi, | ||
write$: result.network.write$, | ||
recsWorld: result.network.world, | ||
}); | ||
if (import.meta.env.DEV && setup) { | ||
import("@latticexyz/dev-tools").then(({ mount: mountDevTools }) => | ||
mountDevTools({ | ||
config: mudConfig, | ||
publicClient: setup.network.publicClient, | ||
walletClient: setup.network.walletClient, | ||
latestBlock$: setup.network.latestBlock$, | ||
storedBlockLogs$: setup.network.storedBlockLogs$, | ||
worldAddress: setup.network.worldAddress, | ||
worldAbi: IWorldAbi, | ||
write$: setup.network.write$.asObservable().pipe(share()), | ||
recsWorld: setup.network.world, | ||
}) | ||
); | ||
} | ||
}); | ||
|
||
if (!setup) return <>Loading...</>; | ||
return ( | ||
<WagmiConfig config={setup.network.wagmiConfig}> | ||
<RainbowKitProvider chains={[setup.network.chain]}> | ||
<App setup={setup} /> | ||
</RainbowKitProvider> | ||
</WagmiConfig> | ||
); | ||
}; | ||
|
||
root.render(<AppWithWalletContext />); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { encodePacked, toHex } from "viem"; | ||
|
||
const ROOT_NAMESPACE = 0; | ||
|
||
const encodedRootSpace = toHex(ROOT_NAMESPACE, { size: 16 }); | ||
const encodedDelegationId = toHex("unlimited.d", { size: 16 }); | ||
export const UNLIMITED_DELEGATION = encodePacked(["bytes16", "bytes16"], [encodedRootSpace, encodedDelegationId]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did you encounter serialization errors with this? I think @yonadaaa saw a similar thing.
If you have the error handy, we can bubble this up to the wagmi repo/team to get it fixed.
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.
yea commented out to make things work for now. The error is: