Skip to content

Commit

Permalink
Feat 0.1.4 (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
codingki authored Jan 22, 2024
2 parents 72a8c42 + 797cac5 commit d5613ae
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 13 deletions.
7 changes: 7 additions & 0 deletions docs/docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ sidebar_position: 8

# Changelog

## Version 0.1.4

- Improve `useChainInfos` and `useChainInfo` to return `ChainInfo` object from `GrazProvider` with given `chainId`
- Added Actions `getChainInfos` and `getChainInfo` to retrieve `ChainInfo` object from `GrazProvider` with given `chainId`
- Improve wallet connect `getKey`
- Improve Metamask Snap if okxwallet inject ethereum object to window

## Version 0.1.3

- ✅ Added `signArbitrary` to `Wallet` type
Expand Down
25 changes: 25 additions & 0 deletions docs/docs/hooks/useChainInfo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# useActiveChains

hook to retrieve `ChainInfo` object from `GrazProvider` with given `chainId`

#### Usage

```tsx
import { useChainInfo } from "graz";

function App() {
const chainInfo = useChainInfo({ chainId: "cosmoshub-4" });

return (
<div>
<span>{chainInfo.chainName}</span>
</div>
);
}
```

#### Return Value

```tsx
ChainInfo | undefined; // @keplr-wallet/types
```
19 changes: 19 additions & 0 deletions docs/docs/hooks/useChainInfos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# useActiveChains

hook to retrieve `ChainInfo` objects from `GrazProvider` with given `chainId`

#### Usage

```tsx
import { useChainInfos } from "graz";

function App() {
const chainInfos = useChainInfos({ chainId: ["cosmoshub-4", "osmosis-1"] });
}
```

#### Return Value

```tsx
ChainInfo[] | undefined; // @keplr-wallet/types
```
3 changes: 1 addition & 2 deletions example/starter/src/ui/modal/connect-wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
Button,
Heading,
HStack,
Image,
Modal,
ModalBody,
ModalContent,
Expand Down Expand Up @@ -53,7 +52,7 @@ const WalletModal = ({
p={4}
spacing={4}
>
<Image alt={wallet.name} boxSize="40px" src={wallet.imgSrc} />
{/* <Image alt={wallet.name} boxSize="40px" src={wallet.imgSrc} /> */}
<Text fontWeight="bold">{wallet.name}</Text>
</HStack>
))}
Expand Down
2 changes: 1 addition & 1 deletion packages/graz/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graz",
"description": "React hooks for Cosmos",
"version": "0.1.3",
"version": "0.1.4",
"author": "Griko Nibras <[email protected]>",
"repository": "https://github.com/graz-sh/graz.git",
"homepage": "https://github.com/graz-sh/graz",
Expand Down
8 changes: 8 additions & 0 deletions packages/graz/src/actions/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export const getRecentChains = (): ChainInfo[] | null => {
return recentChains?.map((chainId) => chains!.find((x) => x.chainId === chainId)!) ?? null;
};

export const getChainInfo = ({ chainId }: { chainId?: string }): ChainInfo | undefined => {
return useGrazInternalStore.getState().chains?.find((x) => x.chainId === chainId);
};

export const getChainInfos = ({ chainId }: { chainId?: string[] }): ChainInfo[] | undefined => {
return useGrazInternalStore.getState().chains?.filter((x) => chainId?.includes(x.chainId));
};

export interface SuggestChainArgs {
chainInfo: ChainInfo;
walletType: WalletType;
Expand Down
9 changes: 9 additions & 0 deletions packages/graz/src/actions/wallet/metamask-snap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ export const getMetamaskSnap = (params?: GetMetamaskSnap): Wallet => {
});

const isMetamask = (clientVersion as string).includes("MetaMask");

if (!isMetamask) throw new Error("Metamask is not installed");

if (typeof window.okxwallet !== "undefined") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (window.okxwallet.isOkxWallet) {
throw new Error("You have OKX Wallet installed. Please disable and reload the page to use Metamask Snap.");
}
}
const version = (clientVersion as string).split("MetaMask/v")[1]?.split(".")[0];
const isSupportMMSnap = Number(version) >= 11;
if (!isSupportMMSnap) throw new Error("Metamask Snap is not supported in this version");
Expand Down
19 changes: 9 additions & 10 deletions packages/graz/src/actions/wallet/wallet-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,16 @@ export const getWalletConnect = (params?: GetWalletConnectParams): Wallet => {
};
};

// eslint-disable-next-line @typescript-eslint/require-await
const getKey = async (chainId: string): Promise<Key> => {
const { address, algo, pubkey } = await getAccount(chainId);
return {
address: fromBech32(address).data,
algo,
bech32Address: address,
name: "",
pubKey: pubkey,
isKeystone: false,
isNanoLedger: false,
};
const session = getSession([chainId]);
if (!session?.topic) throw new Error("No wallet connect session");

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const key: Key | undefined = session.sessionProperties && JSON.parse(String(session.sessionProperties.keys))[0];
if (!key) throw new Error("No wallet connect key");

return key;
};

const wcSignDirect = async (...args: SignDirectParams) => {
Expand Down
30 changes: 30 additions & 0 deletions packages/graz/src/hooks/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ export const useActiveChains = (): ChainInfo[] | undefined => {
.filter(Boolean) as ChainInfo[] | undefined;
};

/**
* graz hook to retrieve ChainInfo object from GrazProvider with given chainId
*
* @param chainId - chainId to search
*
* @example
* ```ts
* import { useChain } from "graz";
* const chainInfo = useChainInfo({chainId: "cosmoshub-4"});
* ```
*/
export const useChainInfo = ({ chainId }: { chainId?: string }) => {
return useGrazInternalStore().chains?.find((x) => x.chainId === chainId);
};

/**
* graz hook to retrieve ChainInfo objects from GrazProvider with given chainId
*
* @param chainId - chainId to search
*
* @example
* ```ts
* import { useChainInfos } from "graz";
* const chainInfos = useChainInfos({chainId: ["cosmoshub-4", "juno-1"]});
* ```
*/
export const useChainInfos = ({ chainId }: { chainId?: string[] }) => {
return useGrazInternalStore().chains?.filter((x) => chainId?.includes(x.chainId));
};

/**
* graz hook to retrieve specific connected chains currency
*
Expand Down
1 change: 1 addition & 0 deletions packages/graz/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ declare global {
};
};
ethereum?: import("@metamask/providers").MetaMaskInpageProvider;
okxwallet?: import("@metamask/providers").BaseProvider;
station?: Station;
xfi?: {
keplr: KeplrWindow["keplr"];
Expand Down

0 comments on commit d5613ae

Please sign in to comment.