Account Management
Account Management
useAccount
component of this SDK handles the account management tasks within the INTU Vault ecosystem, such as creating a new account, fetching existing account details, and performing airdrops for both the user's wallet and the master account. Additionally, it manages the state of the current account and whether an account is being created.
First, import the Account module onto your page.
import { useAccount } from "@intuweb3/steven-web-kit";
Get current Account
This function allows you to retrieve the currently active account associated with the user.
const { currentAccount } = useAccount();
useEffect(() => {
if (currentAccount) {
console.log("Current Account:", currentAccount);
}
}, [currentAccount]);
Returns: Account Object: contains all relevant details about the account fetch either from local storage or from the user’s accounts. Outputs the current account details or null if no account is set.
Create a new Account
Creates a new account for the user. This function generates a vault, initiates registration, completes the registration process, and stores the account details in local storage. Returns: <Account | Error> Account: Details of the user's current account Error: An error message if fetching the account details fails.
const handleCreateVaultClick = async () => {
setIsLoading(true);
try {
// proceed only if the user has no accounts
if (account !== null) {
return;
}
await airdrop();
const newAccount = await createAccount();
handleGetVaultsClick();
} catch (error) {
console.error("Failed to create an account:", error);
}
setIsLoading(false);
};
Get Account
Fetches the details of the current account from local storage or the user's accounts. This function is useful when you need to retrieve account details after the app has been reloaded or initialized. Returns: <Account | Error> Account: The details of the user's current account. Error: An error message if fetching the account details fails.
const handleGetVaultsClick = async () => {
setIsLoading(true);
try {
const vaultsData = await getAccount();
vaultsData ? setAccount(vaultsData) : setAccount(null);
} catch (error) {
console.error("Failed to fetch accounts:", error);
}
setIsLoading(false);
};
Airdrop
Initiates an airdrop to the user's wallet. This is typically used to fund the wallet with tokens. These functions can be useful for testing purposes too.
Returns: <void | Error>
Error: An error message if the airdrop process fails
const { airdrop } = useAccount();
const airdropHandllder = async () => {
try {
await airdrop();
} catch (error) {
console.log("Error", error);
}
};
AirDrop Master Account
Initiates an airdrop to the user's master account. This is used to fund the master account with tokens. Returns: <void | Error> Error: An error message if the airdrop process fails
const { airDropMasterAccount } = useAccount();
const airdropMasterAccHandler = async () => {
try {
await airDropMasterAccount();
toast.success("Success", {
position: "bottom-right",
});
} catch (error) {
console.log("Error", error);
}
};
Account creation status
isCreatingAccount
is a boolean value that helps to manage the UI state during the account creation process. This component can be used to disable buttons, show loading indicators, or prevent duplicate submissions.
Returns: boolean
const { isCreatingAccount } = useAccount();
if (isCreatingAccount) {
console.log("Account creation in progress...");
}