Skip to content

Commit

Permalink
Tweaks to AlgorandClient:
Browse files Browse the repository at this point in the history
* Rename AlgoKitClient to AlgorandClient and added static construction methods
* Added fluent with* methods to AlgorandClient to allow for fluent configuration
* Extracted client related stuff to ClientManager and account stuff to AccountManager and added extra functionality to both
* Add transaction and confirmation to the result of sending a single transaction for better ergonomics
* Added ability to specify suggested params and create a copy of suggested params when providing it
* Moved classes to types directory
* Added getAccountInformation to make tests terser (this method should have been added ages ago)
* Incorporated client into testing fixture
* Changed all possible bigint | numbers' to number (where it's impractical for them to hit 2^53)
* Incorporated TransactionSignerAccount with TransactionSigner to allow for terser code
* Rename ID to Id for consistency with rest of algokit utils
  • Loading branch information
robdmoore committed Mar 16, 2024
1 parent 790fb8d commit c4a7d20
Show file tree
Hide file tree
Showing 12 changed files with 826 additions and 236 deletions.
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) }
},
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

0 comments on commit c4a7d20

Please sign in to comment.