Skip to content

Commit

Permalink
Fix funding for cucumber tests by rotating sender accounts (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos authored May 8, 2024
1 parent 3a7c4ec commit dde38d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
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

0 comments on commit dde38d8

Please sign in to comment.