/
account.ts
39 lines (32 loc) · 1.59 KB
/
account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import algosdk from 'algosdk'
import { Config, getDispenserAccount } from '../'
import { transferAlgos } from '../transfer/transfer-algos'
import { GetTestAccountParams } from '../types/testing'
import Account = algosdk.Account
import Algodv2 = algosdk.Algodv2
import Kmd = algosdk.Kmd
/**
* Creates an ephemeral Algorand account for the purposes of testing.
* Returns a newly created random test account that is funded from the dispenser
* DO NOT USE THIS TO CREATE A MAINNET ACCOUNT!
* Note: By default this will log the mnemonic of the account.
* @param param0 The config for the test account to generate
* @param algod An algod client
* @param kmd A KMD client, if not specified then a default KMD client will be loaded from environment variables
* @returns The account, with private key loaded
*/
export async function getTestAccount(
{ suppressLog, initialFunds, accountGetter }: GetTestAccountParams,
algod: Algodv2,
kmd?: Kmd,
): Promise<Account> {
const account = accountGetter ? await accountGetter(algod, kmd) : algosdk.generateAccount()
Config.getLogger(suppressLog).info(
`New test account created with address '${account.addr}' and mnemonic '${algosdk.secretKeyToMnemonic(account.sk)}'.`,
)
const dispenser = await getDispenserAccount(algod, kmd)
await transferAlgos({ from: dispenser, to: account.addr, amount: initialFunds, note: 'Funding test account', suppressLog }, algod)
const accountInfo = await algod.accountInformation(account.addr).do()
Config.getLogger(suppressLog).info('Test account funded; account balance: %d µAlgos', accountInfo.amount)
return account
}