@@ -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+
151173func 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