Skip to content

Commit

Permalink
setup known keys map to aleviate multiple calls to metamask event bus… (
Browse files Browse the repository at this point in the history
  • Loading branch information
codingki authored Apr 22, 2024
2 parents 26dfb3b + beb3417 commit be71d4b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The following steps will get you up and running to contribute to `graz`:
- `pnpm graz build`: build `graz` package
- `pnpm install`: install all projects
- `pnpm dev`: compiles `graz` and start the development server of the example app
- `pnpm graz cli -g`: compiles `graz` chains, needed for running examples
- `pnpm project:docs dev`: start the documentation website
- `pnpm example dev`: start the example app

Expand Down
16 changes: 12 additions & 4 deletions packages/graz/src/actions/wallet/metamask-snap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export interface GetMetamaskSnap {
params?: Record<string, unknown>;
}

export interface KnownKeys {
[key: string]: Key
}

const knownKeysMap: KnownKeys = {};

/**
* Function to return {@link Wallet} object and throws and error if it does not exist on `window`.
*
Expand Down Expand Up @@ -155,7 +161,9 @@ export const getMetamaskSnap = (params?: GetMetamaskSnap): Wallet => {
};

// getKey from @leapwallet/cosmos-snap-provider return type is wrong
const getKey = async (chainId: string): Promise<Key> => {
const getKey = async (chainId: string): Promise<Key> => {
if (typeof knownKeysMap[chainId] !== 'undefined') return knownKeysMap[chainId] as Key;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const res: any = await ethereum.request({
method: "wallet_invokeSnap",
Expand All @@ -178,9 +186,9 @@ export const getMetamaskSnap = (params?: GetMetamaskSnap): Wallet => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
delete res.pubkey;

return {
...res,
} as Key;
// Cache the key for future use
knownKeysMap[chainId] = res;
return knownKeysMap[chainId] as Key;
};

const getAccount = async (chainId: string): Promise<AccountData> => {
Expand Down

0 comments on commit be71d4b

Please sign in to comment.