Skip to content

Commit

Permalink
fix: match the previous getTestAccount behaviour when no kmd client i…
Browse files Browse the repository at this point in the history
…s supplied (#303)

* fix: match the previous getTestAccount behaviour when no kmd client is supplied
  • Loading branch information
neilcampbell authored Jul 22, 2024
1 parent 0e7fe7d commit 77bc079
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions docs/code/modules/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ ___
| :------ | :------ | :------ |
| `params` | [`GetTestAccountParams`](../interfaces/types_testing.GetTestAccountParams.md) | The config for the test account to generate |
| `algod` | `default` | An algod client |
| `kmd?` | `default` | A KMD client, if not specified then a default KMD client will be loaded from environment variables |
| `kmd?` | `default` | A KMD client, if not specified then a default KMD client will be loaded from environment variables and if not found fallback to the default LocalNet KMD client |

#### Returns

Expand All @@ -178,7 +178,7 @@ Note: By default this will log the mnemonic of the account.

#### Defined in

[src/testing/account.ts:20](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/account.ts#L20)
[src/testing/account.ts:21](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/account.ts#L21)

**getTestAccount**(`params`, `algorand`): `Promise`\<`Account`\>

Expand All @@ -202,7 +202,7 @@ The account, with private key loaded

#### Defined in

[src/testing/account.ts:30](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/account.ts#L30)
[src/testing/account.ts:31](https://github.com/algorandfoundation/algokit-utils-ts/blob/main/src/testing/account.ts#L31)

___

Expand Down
14 changes: 12 additions & 2 deletions src/testing/account.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import algosdk from 'algosdk'
import { AlgorandClient, Config } from '../'
import { ClientManager } from '../types/client-manager'
import { GetTestAccountParams } from '../types/testing'
import Account = algosdk.Account
import Algodv2 = algosdk.Algodv2
Expand All @@ -14,7 +15,7 @@ import Kmd = algosdk.Kmd
* Note: By default this will log the mnemonic of the account.
* @param params 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
* @param kmd A KMD client, if not specified then a default KMD client will be loaded from environment variables and if not found fallback to the default LocalNet KMD client
* @returns The account, with private key loaded
*/
export async function getTestAccount(params: GetTestAccountParams, algod: Algodv2, kmd?: Kmd): Promise<Account>
Expand All @@ -33,10 +34,19 @@ export async function getTestAccount(
algodOrAlgorandClient: Algodv2 | AlgorandClient,
kmd?: Kmd,
): Promise<Account> {
let kmdClient = kmd
if (!kmdClient) {
const kmdConfig = ClientManager.getConfigFromEnvironmentOrLocalNet().kmdConfig
kmdClient = kmdConfig ? ClientManager.getKmdClient(kmdConfig) : undefined
}

const algorand =
algodOrAlgorandClient instanceof AlgorandClient
? algodOrAlgorandClient
: AlgorandClient.fromClients({ algod: algodOrAlgorandClient, kmd })
: AlgorandClient.fromClients({
algod: algodOrAlgorandClient,
kmd: kmdClient,
})

const account = accountGetter ? await accountGetter(algorand) : algosdk.generateAccount()

Expand Down

0 comments on commit 77bc079

Please sign in to comment.