Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks to AlgorandClient: #248

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/account/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { Config } from '../config'
import { getOrCreateKmdWalletAccount } from '../localnet/get-or-create-kmd-wallet-account'
import { isLocalNet } from '../localnet/is-localnet'
import { getSenderAddress } from '../transaction/transaction'
import { MultisigAccount, SigningAccount, TransactionSignerAccount } from '../types/account'
import { AccountInformation, MultisigAccount, SigningAccount, TransactionSignerAccount } from '../types/account'
import { AlgoAmount } from '../types/amount'
import { SendTransactionFrom } from '../types/transaction'
import { getAccountConfigFromEnvironment } from './get-account-config-from-environment'
import { mnemonicAccount } from './mnemonic-account'
import AccountInformationModel = algosdk.modelsv2.Account
import Account = algosdk.Account
import Algodv2 = algosdk.Algodv2
import Kmd = algosdk.Kmd
Expand Down Expand Up @@ -127,3 +128,41 @@ export function getAccountAddressAsUint8Array(account: SendTransactionFrom | str
export function getAccountAddressAsString(addressEncodedInB64: string): string {
return algosdk.encodeAddress(Buffer.from(addressEncodedInB64, 'base64'))
}

/**
* Returns the given sender account's current status, balance and spendable amounts.
*
* @example
* ```typescript
* const address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA";
* const accountInfo = await account.getInformation(address);
* ```
*
* [Response data schema details](https://developer.algorand.org/docs/rest-apis/algod/#get-v2accountsaddress)
* @param sender The address of the sender/account to look up
* @returns The account information
*/
export async function getAccountInformation(sender: string | SendTransactionFrom, algod: Algodv2): Promise<AccountInformation> {
const account = AccountInformationModel.from_obj_for_encoding(
await algod.accountInformation(typeof sender === 'string' ? sender : getSenderAddress(sender)).do(),
)

return {
...account,
// None of these can practically overflow 2^53
amount: Number(account.amount),
amountWithoutPendingRewards: Number(account.amountWithoutPendingRewards),
minBalance: Number(account.minBalance),
pendingRewards: Number(account.pendingRewards),
rewards: Number(account.rewards),
round: Number(account.round),
totalAppsOptedIn: Number(account.totalAppsOptedIn),
totalAssetsOptedIn: Number(account.totalAssetsOptedIn),
totalCreatedApps: Number(account.totalCreatedApps),
totalCreatedAssets: Number(account.totalCreatedAssets),
appsTotalExtraPages: account.appsTotalExtraPages ? Number(account.appsTotalExtraPages) : undefined,
rewardBase: account.rewardBase ? Number(account.rewardBase) : undefined,
totalBoxBytes: account.totalBoxBytes ? Number(account.totalBoxBytes) : undefined,
totalBoxes: account.totalBoxes ? Number(account.totalBoxes) : undefined,
}
}
128 changes: 0 additions & 128 deletions src/client.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ export * from './app'
export * from './app-client'
export * from './app-deploy'
export * from './asset'
export * from './client'
export * from './composer'
export * from './config'
export * from './debugging'
export * from './dispenser-client'
Expand All @@ -14,3 +12,5 @@ export * from './localnet'
export * from './network-client'
export * from './transaction'
export * from './transfer'
export * from './types/algorand-client'
export * from './types/composer'
24 changes: 18 additions & 6 deletions src/testing/fixtures/algorand-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getConfigFromEnvOrDefaults,
lookupTransactionById,
} from '../../'
import AlgorandClient from '../../types/algorand-client'
import { AlgoConfig } from '../../types/network-client'
import { AlgorandFixture, AlgorandFixtureConfig, AlgorandTestAutomationContext, GetTestAccountParams } from '../../types/testing'
import { getTestAccount } from '../account'
Expand Down Expand Up @@ -92,21 +93,29 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?:
const indexer = fixtureConfig?.indexer ?? getAlgoIndexerClient(config.indexerConfig)
const kmd = fixtureConfig?.kmd ?? getAlgoKmdClient(config.kmdConfig)
let context: AlgorandTestAutomationContext
let algorandClient: AlgorandClient

const beforeEach = async () => {
Config.configure({ debug: true })
const transactionLogger = new TransactionLogger()
const transactionLoggerAlgod = transactionLogger.capture(algod)
const acc = await getTestAccount(
{ initialFunds: fixtureConfig?.testAccountFunding ?? algos(10), suppressLog: true },
transactionLoggerAlgod,
kmd,
)
algorandClient = AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd }).withAccount(acc)
const testAccount = { ...acc, signer: algorandClient.account.getSigner(acc.addr) }
context = {
algod: transactionLoggerAlgod,
indexer: indexer,
kmd: kmd,
testAccount: await getTestAccount(
{ initialFunds: fixtureConfig?.testAccountFunding ?? algos(10), suppressLog: true },
transactionLoggerAlgod,
kmd,
),
generateAccount: (params: GetTestAccountParams) => getTestAccount(params, transactionLoggerAlgod, kmd),
testAccount,
generateAccount: async (params: GetTestAccountParams) => {
const account = await getTestAccount(params, transactionLoggerAlgod, kmd)
algorandClient.withAccount(account)
return { ...account, signer: algorandClient.account.getSigner(account.addr) }
},
Comment on lines +102 to +118
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means generated accounts in testing will automatically be registered with the AlgorandClient that is now also available in the fixture.

transactionLogger: transactionLogger,
waitForIndexer: () => transactionLogger.waitForIndexer(indexer),
waitForIndexerTransaction: (transactionId: string) => runWhenIndexerCaughtUp(() => lookupTransactionById(transactionId, indexer)),
Expand All @@ -117,6 +126,9 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?:
get context() {
return context
},
get algorand() {
return algorandClient
},
beforeEach,
}
}
Loading