Skip to content

fix: make algorand client persistent for test fixture #356

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

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 40 additions & 19 deletions src/testing/fixtures/algorand-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig): Algorand
export function algorandFixture(fixtureConfig: AlgorandFixtureConfig | undefined, config: AlgoConfig): AlgorandFixture

export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?: AlgoConfig): AlgorandFixture {
Config.configure({ debug: true })

fixtureConfig = { ...fixtureConfig, ...config }
if (!fixtureConfig.algod || !fixtureConfig.indexer || !fixtureConfig.kmd) {
fixtureConfig = { ...ClientManager.getConfigFromEnvironmentOrLocalNet(), ...fixtureConfig }
Expand All @@ -83,36 +85,55 @@ export function algorandFixture(fixtureConfig?: AlgorandFixtureConfig, config?:
const algod = fixtureConfig.algod ?? ClientManager.getAlgodClient(fixtureConfig.algodConfig!)
const indexer = fixtureConfig.indexer ?? ClientManager.getIndexerClient(fixtureConfig.indexerConfig!)
const kmd = fixtureConfig.kmd ?? ClientManager.getKmdClient(fixtureConfig.kmdConfig!)
const transactionLogger = new TransactionLogger()
const transactionLoggerAlgod = transactionLogger.capture(algod)

const algorand = AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd })

const generateAccount = async (params: GetTestAccountParams) => {
const account = await getTestAccount(params, algorand)
algorand.setSignerFromAccount(account)
return account
}

// TODO: Figure out why waitForIndexer is not working
// The tests pass if we just wait for 15 seconds:
// const waitForIndexer = () => new Promise((resolve) => setTimeout(resolve, 15_000))
// DO NOT MERGE UNTIL WE FIX THIS
const waitForIndexer = () => transactionLogger.waitForIndexer(indexer)
const waitForIndexerTransaction = (transactionId: string) =>
runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(transactionId).do())

// If running against LocalNet we are likely in dev mode and we need to set a much higher validity window
// otherwise we are more likely to get invalid transactions.
const algorandClientSetup = new Promise<void>((resolve) => {
algorand.client.isLocalNet().then((isLocalNet) => {
if (isLocalNet) {
algorand.setDefaultValidityWindow(1000)
algorand.setSuggestedParamsCacheTimeout(0)
resolve()
}
})
})

let context: AlgorandTestAutomationContext
let algorand: AlgorandClient

const beforeEach = async () => {
Config.configure({ debug: true })
const transactionLogger = new TransactionLogger()
const transactionLoggerAlgod = transactionLogger.capture(algod)
algorand = AlgorandClient.fromClients({ algod: transactionLoggerAlgod, indexer, kmd })
const testAccount = await getTestAccount({ initialFunds: fixtureConfig?.testAccountFunding ?? algos(10), suppressLog: true }, algorand)
algorand.setSignerFromAccount(testAccount).setSuggestedParamsCacheTimeout(0)
// If running against LocalNet we are likely in dev mode and we need to set a much higher validity window
// otherwise we are more likely to get invalid transactions.
if (await algorand.client.isLocalNet()) {
algorand.setDefaultValidityWindow(1000)
}
algorand.setSignerFromAccount(testAccount)

await algorandClientSetup
algorand.account.setSignerFromAccount(testAccount)
context = {
algorand,
algod: transactionLoggerAlgod,
indexer: indexer,
kmd: kmd,
testAccount,
generateAccount: async (params: GetTestAccountParams) => {
const account = await getTestAccount(params, algorand)
algorand.setSignerFromAccount(account)
return account
},
transactionLogger: transactionLogger,
waitForIndexer: () => transactionLogger.waitForIndexer(indexer),
waitForIndexerTransaction: (transactionId: string) => runWhenIndexerCaughtUp(() => indexer.lookupTransactionByID(transactionId).do()),
generateAccount,
transactionLogger,
waitForIndexer,
waitForIndexerTransaction,
}
}

Expand Down
Loading