Skip to content

Instantly share code, notes, and snippets.

@jasonpaulos
Created October 20, 2021 15:49
Show Gist options
  • Save jasonpaulos/79b04f7b911483bfe3de5b8fa75e18f3 to your computer and use it in GitHub Desktop.
Save jasonpaulos/79b04f7b911483bfe3de5b8fa75e18f3 to your computer and use it in GitHub Desktop.
Calculate Algorand account minimum balance
const MIN_BALANCE_PER_ACCOUNT = BigInt(100000);
const MIN_BALANCE_PER_ASSET = BigInt(100000);
const MIN_BALANCE_PER_APP = BigInt(100000);
const MIN_BALANCE_PER_APP_BYTESLICE = BigInt(25000+25000);
const MIN_BALANCE_PER_APP_UINT = BigInt(25000+3500);
const MIN_BALANCE_PER_APP_EXTRA_PAGE = BigInt(100000);
/**
* Get the minimum balance of an account.
* @param client - An Algodv2 client.
* @param addr - The address of the account.
* @returns A promise that resolves to the minimum balance of the account, as a bigint.
*/
export async function getMinBalanceForAccount(client: algosdk.Algodv2, addr: string): Promise<bigint> {
// This code is based on https://github.com/algorand/go-algorand/blob/1e5603c2691d7c0978d9688e5c89b0b0332528ec/data/basics/userBalance.go#L464
const accountInfo = await client
.accountInformation(addr)
.setIntDecoding(algosdk.IntDecoding.BIGINT)
.do();
const totalSchema = accountInfo['apps-total-schema'];
let totalByteSlices = BigInt(0);
let totalUints = BigInt(0);
if (totalSchema) {
if (totalSchema['num-byte-slice']) {
totalByteSlices = totalSchema['num-byte-slice'];
}
if (totalSchema['num-uint']) {
totalUints = totalSchema['num-uint'];
}
}
const totalExtraPages = accountInfo['apps-total-extra-pages'] || BigInt(0);
const localApps = accountInfo['apps-local-state'] || [];
const createdApps = accountInfo['created-apps'] || [];
const assets = accountInfo['assets'] || [];
return MIN_BALANCE_PER_ACCOUNT +
MIN_BALANCE_PER_ASSET * BigInt(assets.length) +
MIN_BALANCE_PER_APP * BigInt(createdApps.length + localApps.length) +
MIN_BALANCE_PER_APP_UINT * totalUints +
MIN_BALANCE_PER_APP_BYTESLICE * totalByteSlices +
MIN_BALANCE_PER_APP_EXTRA_PAGE * totalExtraPages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment