github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/sdk/bank/invariants.go (about) 1 package bank 2 3 import ( 4 "fmt" 5 6 "github.com/gnolang/gno/tm2/pkg/sdk" 7 "github.com/gnolang/gno/tm2/pkg/sdk/auth" 8 ) 9 10 // RegisterInvariants registers the bank module invariants 11 func RegisterInvariants(ir sdk.InvariantRegistry, acck auth.AccountKeeper) { 12 ir.RegisterRoute(ModuleName, "nonnegative-outstanding", 13 NonnegativeBalanceInvariant(acck)) 14 } 15 16 // NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances 17 func NonnegativeBalanceInvariant(acck auth.AccountKeeper) sdk.Invariant { 18 return func(ctx sdk.Context) (string, bool) { 19 var msg string 20 var count int 21 22 accts := acck.GetAllAccounts(ctx) 23 for _, acc := range accts { 24 coins := acc.GetCoins() 25 if coins.IsAnyNegative() { 26 count++ 27 msg += fmt.Sprintf("\t%s has a negative denomination of %s\n", 28 acc.GetAddress().String(), 29 coins.String()) 30 } 31 } 32 broken := count != 0 33 34 return sdk.FormatInvariant(ModuleName, "nonnegative-outstanding", 35 fmt.Sprintf("amount of negative accounts found %d\n%s", count, msg)), broken 36 } 37 }