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

Tests: Fix funding for cucumber tests by rotating sender accounts #630

Merged
merged 1 commit into from
May 8, 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
16 changes: 13 additions & 3 deletions test/applications_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ func iCreateANewTransientAccountAndFundItWithMicroalgos(microalgos int) error {
}

params.Fee = types.MicroAlgos(fee)
ltxn, err := transaction.MakePaymentTxn(accounts[1], transientAccount.Address.String(), uint64(microalgos), note, close, params)

funder, err := fundingAccount(algodV2client, uint64(microalgos))
if err != nil {
return err
}

ltxn, err := transaction.MakePaymentTxn(funder, transientAccount.Address.String(), uint64(microalgos), note, close, params)
if err != nil {
return err
}
lsk, err := kcl.ExportKey(handle, walletPswd, accounts[1])
lsk, err := kcl.ExportKey(handle, walletPswd, funder)
if err != nil {
return err
}
Expand All @@ -91,7 +97,11 @@ func iFundTheCurrentApplicationsAddress(microalgos int) error {
return err
}

txn, err := transaction.MakePaymentTxn(accounts[0], address.String(), uint64(microalgos), nil, "", params)
funder, err := fundingAccount(algodV2client, uint64(microalgos))
if err != nil {
return err
}
txn, err := transaction.MakePaymentTxn(funder, address.String(), uint64(microalgos), nil, "", params)
if err != nil {
return err
}
Expand Down
28 changes: 27 additions & 1 deletion test/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"encoding/json"
"flag"
"fmt"
"math/rand"
"os"
"path"
"reflect"
Expand Down Expand Up @@ -148,13 +149,38 @@ func waitForAlgodInDevMode() {
time.Sleep(500 * time.Millisecond)
}

// fundingAccount finds an account with enough funds to fund the given amount
func fundingAccount(client *algodV2.Client, amount uint64) (string, error) {
// Random shuffle to spread the load
shuffledAccounts := make([]string, len(accounts))
copy(shuffledAccounts, accounts)
rand.Shuffle(len(shuffledAccounts), func(i, j int) {
shuffledAccounts[i], shuffledAccounts[j] = shuffledAccounts[j], shuffledAccounts[i]
})
for _, accountAddress := range shuffledAccounts {
res, err := client.AccountInformation(accountAddress).Do(context.Background())
if err != nil {
return "", err
}
if res.Amount < amount+100_000 { // 100,000 microalgos is default account min balance
continue
}
return accountAddress, nil
}
return "", fmt.Errorf("no account has enough to fund %d microalgos", amount)
}

func initializeAccount(accountAddress string) error {
params, err := aclv2.SuggestedParams().Do(context.Background())
if err != nil {
return err
}

txn, err = transaction.MakePaymentTxn(accounts[0], accountAddress, devModeInitialAmount, []byte{}, "", params)
funder, err := fundingAccount(aclv2, devModeInitialAmount)
if err != nil {
return err
}
txn, err = transaction.MakePaymentTxn(funder, accountAddress, devModeInitialAmount, []byte{}, "", params)
if err != nil {
return err
}
Expand Down
Loading