-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4576d4
commit bc967a9
Showing
6 changed files
with
136 additions
and
66 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { QueryClient } from "@cosmjs/stargate"; | ||
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"; | ||
import { createContext, useContext, useEffect, useState } from "react"; | ||
|
||
import { BBN_RPC_URL } from "@/app/common/rpc"; | ||
|
||
interface BbnRpcContextType { | ||
queryClient: QueryClient | undefined; | ||
isLoading: boolean; | ||
error: Error | null; | ||
} | ||
|
||
const BbnRpcContext = createContext<BbnRpcContextType>({ | ||
queryClient: undefined, | ||
isLoading: true, | ||
error: null, | ||
}); | ||
|
||
export function BbnRpcProvider({ children }: { children: React.ReactNode }) { | ||
const [queryClient, setQueryClient] = useState<QueryClient>(); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState<Error | null>(null); | ||
|
||
useEffect(() => { | ||
let mounted = true; | ||
|
||
const init = async () => { | ||
try { | ||
const tmClient = await Tendermint34Client.connect(BBN_RPC_URL); | ||
const client = QueryClient.withExtensions(tmClient); | ||
|
||
if (mounted) { | ||
setQueryClient(client); | ||
setIsLoading(false); | ||
} | ||
} catch (err) { | ||
if (mounted) { | ||
setError(err instanceof Error ? err : new Error("Failed to connect")); | ||
setIsLoading(false); | ||
} | ||
} | ||
}; | ||
|
||
init(); | ||
|
||
return () => { | ||
mounted = false; | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<BbnRpcContext.Provider value={{ queryClient, isLoading, error }}> | ||
{children} | ||
</BbnRpcContext.Provider> | ||
); | ||
} | ||
|
||
export const useBbnRpc = () => useContext(BbnRpcContext); |
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