Skip to content

Commit

Permalink
A useAccount hook that throws if a wallet does not have an account …
Browse files Browse the repository at this point in the history
…matching a given address
  • Loading branch information
steveluscher committed Feb 7, 2024
1 parent 64498f6 commit 82daf8f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/errors/src/codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const SOLANA_ERROR__RPC_INTEGER_OVERFLOW = 3 as const;
export const SOLANA_ERROR__CHAIN_NOT_SUPPORTED = 4 as const;
export const SOLANA_ERROR__WALLET_HAS_NO_CONNECTED_ACCOUNTS = 5 as const;
export const SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN = 6 as const;
export const SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET = 9 as const;

/**
* A union of every Solana error code
Expand All @@ -34,4 +35,5 @@ export type SolanaErrorCode =
| typeof SOLANA_ERROR__RPC_INTEGER_OVERFLOW
| typeof SOLANA_ERROR__CHAIN_NOT_SUPPORTED
| typeof SOLANA_ERROR__WALLET_HAS_NO_CONNECTED_ACCOUNTS
| typeof SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN;
| typeof SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN
| typeof SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET;
5 changes: 5 additions & 0 deletions packages/errors/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
SOLANA_ERROR__CHAIN_NOT_SUPPORTED,
SOLANA_ERROR__RPC_INTEGER_OVERFLOW,
SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES,
SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET,
SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN,
SOLANA_ERROR__WALLET_HAS_NO_CONNECTED_ACCOUNTS,
SolanaErrorCode,
Expand Down Expand Up @@ -33,6 +34,10 @@ export type SolanaErrorContext = DefaultUnspecifiedErrorContextToUndefined<{
path?: string;
value: bigint;
};
[SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET]: {
accountAddress: string;
walletName: string;
};
[SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN]: {
chain: `${string}:${string}`;
walletName: string;
Expand Down
3 changes: 3 additions & 0 deletions packages/errors/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SOLANA_ERROR__RPC_INTEGER_OVERFLOW,
SOLANA_ERROR__TRANSACTION_MISSING_SIGNATURES,
SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE,
SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET,
SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN,
SOLANA_ERROR__WALLET_HAS_NO_CONNECTED_ACCOUNTS,
SolanaErrorCode,
Expand All @@ -29,6 +30,8 @@ export const SolanaErrorMessages: Readonly<{
[SOLANA_ERROR__TRANSACTION_SIGNATURE_NOT_COMPUTABLE]:
"Could not determine this transaction's signature. Make sure that the transaction has " +
'been signed by its fee payer.',
[SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET]:
'No account having address `$accountAddress` could be found in the wallet `$walletName}`',
[SOLANA_ERROR__WALLET_DOES_NOT_SUPPORT_CHAIN]:
"The wallet '$walletName' does not support connecting to the chain `$chain`",
[SOLANA_ERROR__WALLET_HAS_NO_CONNECTED_ACCOUNTS]:
Expand Down
55 changes: 55 additions & 0 deletions packages/react/src/__tests__/useWalletAccount-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { address } from '@solana/addresses';
import { SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, SolanaError } from '@solana/errors';
import { SOLANA_CHAINS } from '@solana/wallet-standard-chains';
import { Wallet } from '@wallet-standard/base';

import { renderHook } from '../test-renderer';
import { useWalletAccount } from '../useWalletAccount';

describe('useWalletAccount', () => {
let mockWallet: Wallet;
beforeEach(() => {
mockWallet = {
accounts: [
{
address: address('Httx5rAMNW3zA6NtXbgpnq22RdS9qK6rRBiNi8Msoc8a'),
chains: ['solana:devnet'],
features: ['solana:signMessage', 'solana:signAndSendTransaction'],
icon: 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAIBAAA=',
label: 'My Test Account',
publicKey: new Uint8Array([
251, 6, 90, 16, 167, 85, 10, 206, 169, 88, 60, 180, 238, 49, 109, 108, 152, 101, 243, 178, 93,
190, 195, 73, 206, 97, 76, 131, 200, 38, 175, 179,
]),
},
],
chains: SOLANA_CHAINS,
features: {},
icon: 'data:image/svg+xml;base64,ABC',
name: 'Mock Wallet',
version: '1.0.0',
};
// Suppresses console output when an `ErrorBoundary` is hit.
// See https://stackoverflow.com/a/72632884/802047
jest.spyOn(console, 'error').mockImplementation();
jest.spyOn(console, 'warn').mockImplementation();
});
it('returns the account matching the given address', () => {
const { result } = renderHook(() =>
useWalletAccount(mockWallet, address('Httx5rAMNW3zA6NtXbgpnq22RdS9qK6rRBiNi8Msoc8a')),
);
expect(result.current).toBe(mockWallet.accounts[0]);
});
it('fatals when a wallet has no account matching the given address', () => {
const { result } = renderHook(() =>
useWalletAccount(mockWallet, address('6bDQKLGyVpAUzhZa8jDvKbAPPs33ESMdTAjN4HX5PEVu')),
);
expect(result.__type).toBe('error');
expect(result.current).toEqual(
new SolanaError(SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, {
accountAddress: '6bDQKLGyVpAUzhZa8jDvKbAPPs33ESMdTAjN4HX5PEVu',
walletName: 'Mock Wallet',
}),
);
});
});
14 changes: 14 additions & 0 deletions packages/react/src/useWalletAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Address } from '@solana/addresses';
import { SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, SolanaError } from '@solana/errors';
import { Wallet, WalletAccount } from '@wallet-standard/base';

export function useWalletAccount(wallet: Wallet, address: Address): WalletAccount {
const account = wallet.accounts.find(account => account.address === address);
if (!account) {
throw new SolanaError(SOLANA_ERROR__WALLET_ACCOUNT_NOT_FOUND_IN_WALLET, {
accountAddress: address,
walletName: wallet.name,
});
}
return account;
}

0 comments on commit 82daf8f

Please sign in to comment.