Skip to content

Commit dde38d8

Browse files
authored
Fix funding for cucumber tests by rotating sender accounts (#630)
1 parent 3a7c4ec commit dde38d8

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

test/applications_integration_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ func iCreateANewTransientAccountAndFundItWithMicroalgos(microalgos int) error {
6060
}
6161

6262
params.Fee = types.MicroAlgos(fee)
63-
ltxn, err := transaction.MakePaymentTxn(accounts[1], transientAccount.Address.String(), uint64(microalgos), note, close, params)
63+
64+
funder, err := fundingAccount(algodV2client, uint64(microalgos))
65+
if err != nil {
66+
return err
67+
}
68+
69+
ltxn, err := transaction.MakePaymentTxn(funder, transientAccount.Address.String(), uint64(microalgos), note, close, params)
6470
if err != nil {
6571
return err
6672
}
67-
lsk, err := kcl.ExportKey(handle, walletPswd, accounts[1])
73+
lsk, err := kcl.ExportKey(handle, walletPswd, funder)
6874
if err != nil {
6975
return err
7076
}
@@ -91,7 +97,11 @@ func iFundTheCurrentApplicationsAddress(microalgos int) error {
9197
return err
9298
}
9399

94-
txn, err := transaction.MakePaymentTxn(accounts[0], address.String(), uint64(microalgos), nil, "", params)
100+
funder, err := fundingAccount(algodV2client, uint64(microalgos))
101+
if err != nil {
102+
return err
103+
}
104+
txn, err := transaction.MakePaymentTxn(funder, address.String(), uint64(microalgos), nil, "", params)
95105
if err != nil {
96106
return err
97107
}

test/steps_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"flag"
1212
"fmt"
13+
"math/rand"
1314
"os"
1415
"path"
1516
"reflect"
@@ -148,13 +149,38 @@ func waitForAlgodInDevMode() {
148149
time.Sleep(500 * time.Millisecond)
149150
}
150151

152+
// fundingAccount finds an account with enough funds to fund the given amount
153+
func fundingAccount(client *algodV2.Client, amount uint64) (string, error) {
154+
// Random shuffle to spread the load
155+
shuffledAccounts := make([]string, len(accounts))
156+
copy(shuffledAccounts, accounts)
157+
rand.Shuffle(len(shuffledAccounts), func(i, j int) {
158+
shuffledAccounts[i], shuffledAccounts[j] = shuffledAccounts[j], shuffledAccounts[i]
159+
})
160+
for _, accountAddress := range shuffledAccounts {
161+
res, err := client.AccountInformation(accountAddress).Do(context.Background())
162+
if err != nil {
163+
return "", err
164+
}
165+
if res.Amount < amount+100_000 { // 100,000 microalgos is default account min balance
166+
continue
167+
}
168+
return accountAddress, nil
169+
}
170+
return "", fmt.Errorf("no account has enough to fund %d microalgos", amount)
171+
}
172+
151173
func initializeAccount(accountAddress string) error {
152174
params, err := aclv2.SuggestedParams().Do(context.Background())
153175
if err != nil {
154176
return err
155177
}
156178

157-
txn, err = transaction.MakePaymentTxn(accounts[0], accountAddress, devModeInitialAmount, []byte{}, "", params)
179+
funder, err := fundingAccount(aclv2, devModeInitialAmount)
180+
if err != nil {
181+
return err
182+
}
183+
txn, err = transaction.MakePaymentTxn(funder, accountAddress, devModeInitialAmount, []byte{}, "", params)
158184
if err != nil {
159185
return err
160186
}

0 commit comments

Comments
 (0)