Skip to content

getAccount

Usage

connect() must be called and WalletConnect session must be initiated before account information will be returned.

The getAccount() function returns the currently selected account at the time of connection. When your account change in the wallet, the account change will not be reflected in the dApp. If you want the account state to automatically be reflected in the dApp, you should use the useAccount hook.

import { getAccount } from '@puzzlehq/sdk-core';
import { useState } from 'react';
export const Page = () => {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | undefined>();
const [account, setAccount] = useState<PuzzleAccount | undefined>();
const onClick = async () => {
setLoading(true);
setError(undefined);
try {
const response: GetSelectedAccountResponse = await getAccount()
if (response.account) setAccount(response.account);
else if (response.error) setError(response.error);
} catch (e) {
setError((e as Error).message);
} finally {
setLoading(false);
}
}
return (
<div>
<button
onClick={ onClick }
disabled={ loading }
>
get selected account
</button>
{ account && <p>account: {account.address}</p> }
{ error && <p>error getting account: {error}</p> }
</div>
)
}

Types

type PuzzleAccount = {
filter?: RecordsFilter,
page?: number, // Page offset for records fetching. Records offset is page * 50 records. Default is 0.
};
type GetSelectedAccountResponse = {
account?: PuzzleAccount;
error?: string;
};