Skip to content

Commit

Permalink
Add useAbstraxionClient hook to expose a non-signing CosmWasm client
Browse files Browse the repository at this point in the history
This will unblock apps which wish to make rpc calls before login
  • Loading branch information
justinbarry committed Oct 30, 2024
1 parent 6a1a037 commit dc060e4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-terms-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@burnt-labs/abstraxion": minor
---

Add a `useAbstraxionClient` hook to expose non-signing client.
1 change: 1 addition & 0 deletions packages/abstraxion/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { useAbstraxionAccount } from "./useAbstraxionAccount";
export { useAbstraxionSigningClient } from "./useAbstraxionSigningClient";
export { useAbstraxionClient } from "./useAbstraxionClient";
export { useModal } from "./useModal";
35 changes: 35 additions & 0 deletions packages/abstraxion/src/hooks/useAbstraxionClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useContext, useEffect, useState } from "react";
import { testnetChainInfo } from "@burnt-labs/constants";
import { AbstraxionContext } from "@/src/components/AbstraxionContext";
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";

export const useAbstraxionClient = (): {
readonly client: CosmWasmClient | undefined;
} => {
const { rpcUrl } = useContext(AbstraxionContext);

const [abstractClient, setAbstractClient] = useState<
CosmWasmClient | undefined
>(undefined);

useEffect(() => {
async function getClient() {
try {
const client = await CosmWasmClient.connect(
// Should be set in the context but defaulting here just in case
rpcUrl || testnetChainInfo.rpc,
);

setAbstractClient(client);
} catch (error) {
setAbstractClient(undefined);
}
}

getClient();
}, [rpcUrl]);

return {
client: abstractClient,
} as const;
};
1 change: 1 addition & 0 deletions packages/abstraxion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { Abstraxion, AbstraxionProvider } from "./components/Abstraxion";
export {
useAbstraxionAccount,
useAbstraxionSigningClient,
useAbstraxionClient,
useModal,
} from "./hooks";

Expand Down

0 comments on commit dc060e4

Please sign in to comment.