getAccount
Usage
connect()
must be called and a connection 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(); setAccount(response.account); } 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
enum Network { AleoTestnet = 'AleoTestnet', AleoMainnet = 'AleoMainnet'}
type PuzzleAccount = { address: string; shortenedAddress: string; network: Network;};
type GetSelectedAccountResponse = { account: PuzzleAccount; };