-
Notifications
You must be signed in to change notification settings - Fork 28
/
invariants.go
49 lines (41 loc) · 1.29 KB
/
invariants.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package keeper
import (
"fmt"
"github.com/OmniFlix/omniflixhub/v2/x/onft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// RegisterInvariants registers all supply invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "supply", SupplyInvariant(k))
}
// SupplyInvariant checks that the total amount of NFTs on collections matches the total amount owned by addresses
func SupplyInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
ownersCollectionsSupply := make(map[string]uint64)
var msg string
count := 0
collections, err := k.GetCollections(ctx)
if err != nil {
panic(err)
}
for _, collection := range collections {
ownersCollectionsSupply[collection.Denom.Id] = uint64(len(collection.ONFTs))
}
for denom, supply := range ownersCollectionsSupply {
if supply != k.GetTotalSupply(ctx, denom) {
count++
msg += fmt.Sprintf(
"total %s NFTs supply invariance:\n"+
"\ttotal %s NFTs supply: %d\n"+
"\tsum of %s NFTs by owner: %d\n",
denom, denom, supply, denom, ownersCollectionsSupply[denom],
)
}
}
broken := count != 0
return sdk.FormatInvariant(
types.ModuleName, "supply",
fmt.Sprintf("%d NFT supply invariants found\n%s", count, msg),
), broken
}
}