getAccountInfo
How to call the API from an app
Hedera Wallet Snap connects to your currently connected Metamask account by default. To learn how apps can connect to Hedera Wallet Snap using a non-metamask(external) account, refer to this documentation.
Then, depending on whether you're trying to connect to a metamask account or a non-metamask account, you can call the snap API in the following way:
const snapId = `npm:@hashgraph/hedera-wallet-snap`
const getAccountInfoAPI = async () => {
const externalAccountParams = {
externalAccount: {
accountIdOrEvmAddress: '0.0.12345',
curve: 'ED25519'
}
}
await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId,
request: {
method: 'getAccountInfo',
params: {
network: 'testnet',
mirrorNodeUrl: 'https://testnet.mirrornode.hedera.com'
// Pass 'accountId' is useful if you want to retrieve account info
// for someone else rather than yourself
accountId: '0.0.67890',
/*
Uncomment the below line if you want to connect
to a non-metamask account
*/
// ...externalAccountParams
}
}
}
})
}
Note that you can also call this API to retrieve account info for another account Id which you do not own. Think of this as fetching the account info of an arbitrary hedera account Id.
To do that, you would just pass in accountId
like this:
const snapId = `npm:@hashgraph/hedera-wallet-snap`
await window.ethereum.request({
method: 'wallet_invokeSnap',
params: {
snapId,
request: {
method: 'getAccountInfo',
params: {
network: 'testnet',
mirrorNodeUrl: 'https://testnet.mirrornode.hedera.com'
accountId: '0.0.1',
}
}
}
})
}
This would retrieve the account info of the account 0.0.1
from the Hedera Mirror Nodes.
What the API does
Retrieves the currently connected account the user has selected on Metamask. If it's the first time, a new snap account is created and the account info is saved in snap state.
Calls the Mirror Node Account Info REST API to get account info from the Hedera Mirror Node if
mirrorNodeUrl
is passed. If not, it calls the Get Account Info API of the Hedera SDKReturns the result.
Some example responses:
For a hedera account id 0.0.4235873
:
{
"currentAccount": {
"hederaAccountId": "0.0.4235873",
"hederaEvmAddress": "0x3ba201df50314e4702d4d92b52d304ee63bfca23",
"balance": {
"hbars": 0.75108133,
"timestamp": "Wed, 24 Jan 2024 21:58:32 GMT",
"tokens": {}
},
"network": "mainnet"
},
"accountInfo": {
"accountId": "0.0.4235873",
"alias": "HORADX2QGFHEOAWU3EVVFUYE5ZR37SRD",
"createdTime": "Mon, 11 Dec 2023 05:42:32 GMT",
"expirationTime": "Sun, 10 Mar 2024 05:42:32 GMT",
"memo": "lazy-created account",
"evmAddress": "0x3ba201df50314e4702d4d92b52d304ee63bfca23",
"key": {
"type": "ECDSA_SECP256K1",
"key": "0380f14bf0a7c8c8c0bbae90e4f6d18b8a016fa945ec646943e8d56a1f7e4dd02a"
},
"balance": {
"hbars": 0.75108133,
"timestamp": "Wed, 24 Jan 2024 21:58:32 GMT",
"tokens": {}
},
"autoRenewPeriod": "7776000",
"ethereumNonce": "0",
"isDeleted": false,
"stakingInfo": {
"declineStakingReward": false,
"stakePeriodStart": "",
"pendingReward": "0",
"stakedToMe": "0",
"stakedAccountId": "",
"stakedNodeId": ""
}
}
}
Live Demo on CodePen
Last updated
Was this helpful?