-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Matth26/burner-remove-options
Burner remove options
- Loading branch information
Showing
13 changed files
with
248 additions
and
86 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
|
@@ -40,3 +40,11 @@ | |
.read-the-docs { | ||
color: #888; | ||
} | ||
|
||
.error { | ||
color: red; | ||
} | ||
|
||
.success { | ||
color: green; | ||
} |
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,65 +1,95 @@ | ||
import { createContext, ReactNode, useContext, useMemo } from "react"; | ||
import { SetupResult } from "./dojo/setup"; | ||
import { useBurner } from "@dojoengine/create-burner"; | ||
import { BurnerProvider, useBurner } from "@dojoengine/create-burner"; | ||
import { ReactNode, createContext, useContext, useMemo } from "react"; | ||
import { Account, RpcProvider } from "starknet"; | ||
import { SetupResult } from "./dojo/setup"; | ||
|
||
interface DojoContextType extends SetupResult { | ||
masterAccount: Account; | ||
} | ||
|
||
const DojoContext = createContext<SetupResult | null>(null); | ||
const DojoContext = createContext<DojoContextType | null>(null); | ||
|
||
type Props = { | ||
const requiredEnvs = [ | ||
"VITE_PUBLIC_MASTER_ADDRESS", | ||
"VITE_PUBLIC_MASTER_PRIVATE_KEY", | ||
"VITE_PUBLIC_ACCOUNT_CLASS_HASH", | ||
]; | ||
|
||
for (const env of requiredEnvs) { | ||
if (!import.meta.env[env]) { | ||
throw new Error(`Environment variable ${env} is not set!`); | ||
} | ||
} | ||
|
||
type DojoProviderProps = { | ||
children: ReactNode; | ||
value: SetupResult; | ||
}; | ||
|
||
export const DojoProvider = ({ children, value }: Props) => { | ||
export const DojoProvider = ({ children, value }: DojoProviderProps) => { | ||
const currentValue = useContext(DojoContext); | ||
if (currentValue) throw new Error("DojoProvider can only be used once"); | ||
return ( | ||
<DojoContext.Provider value={value}>{children}</DojoContext.Provider> | ||
); | ||
}; | ||
|
||
export const useDojo = () => { | ||
const value = useContext(DojoContext); | ||
|
||
if (!value) | ||
throw new Error( | ||
"The `useDojo` hook must be used within a `DojoProvider`" | ||
); | ||
|
||
const provider = useMemo( | ||
const rpcProvider = useMemo( | ||
() => | ||
new RpcProvider({ | ||
nodeUrl: import.meta.env.VITE_PUBLIC_NODE_URL!, | ||
nodeUrl: | ||
import.meta.env.VITE_PUBLIC_NODE_URL || | ||
"http://localhost:5050", | ||
}), | ||
[] | ||
); | ||
|
||
// | ||
// this can be substituted with a wallet provider | ||
// | ||
const masterAddress = import.meta.env.VITE_PUBLIC_MASTER_ADDRESS!; | ||
const privateKey = import.meta.env.VITE_PUBLIC_MASTER_PRIVATE_KEY!; | ||
const masterAddress = import.meta.env.VITE_PUBLIC_MASTER_ADDRESS; | ||
const privateKey = import.meta.env.VITE_PUBLIC_MASTER_PRIVATE_KEY; | ||
const accountClassHash = import.meta.env.VITE_PUBLIC_ACCOUNT_CLASS_HASH; | ||
const masterAccount = useMemo( | ||
() => new Account(provider, masterAddress, privateKey), | ||
[provider, masterAddress, privateKey] | ||
() => new Account(rpcProvider, masterAddress, privateKey), | ||
[rpcProvider, masterAddress, privateKey] | ||
); | ||
|
||
return ( | ||
<BurnerProvider | ||
initOptions={{ masterAccount, accountClassHash, rpcProvider }} | ||
> | ||
<DojoContext.Provider value={{ ...value, masterAccount }}> | ||
{children} | ||
</DojoContext.Provider> | ||
</BurnerProvider> | ||
); | ||
}; | ||
|
||
export const useDojo = () => { | ||
const contextValue = useContext(DojoContext); | ||
if (!contextValue) | ||
throw new Error( | ||
"The `useDojo` hook must be used within a `DojoProvider`" | ||
); | ||
|
||
const { create, list, get, account, select, isDeploying, clear } = | ||
useBurner({ | ||
masterAccount: masterAccount, | ||
accountClassHash: import.meta.env.VITE_PUBLIC_ACCOUNT_CLASS_HASH!, | ||
}); | ||
const { | ||
create, | ||
list, | ||
get, | ||
account, | ||
select, | ||
isDeploying, | ||
clear, | ||
copyToClipboard, | ||
applyFromClipboard, | ||
} = useBurner(); | ||
|
||
return { | ||
setup: value, | ||
setup: contextValue, | ||
account: { | ||
create, | ||
list, | ||
get, | ||
select, | ||
clear, | ||
account: account ? account : masterAccount, | ||
account: account ?? contextValue.masterAccount, | ||
isDeploying, | ||
copyToClipboard, | ||
applyFromClipboard, | ||
}, | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ReactNode, createContext } from "react"; | ||
import { BurnerManagerOptions } from "../types"; | ||
|
||
export const BurnerContext = createContext<BurnerManagerOptions | null>(null); | ||
|
||
interface BurnerProviderProps { | ||
children: ReactNode; | ||
initOptions: BurnerManagerOptions; | ||
} | ||
|
||
export const BurnerProvider = ({ | ||
children, | ||
initOptions, | ||
}: BurnerProviderProps): JSX.Element => { | ||
return ( | ||
<BurnerContext.Provider value={initOptions}> | ||
{children} | ||
</BurnerContext.Provider> | ||
); | ||
}; |
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 @@ | ||
export * from "./burnerProvider"; |
Oops, something went wrong.