/
invariants.go
64 lines (52 loc) · 1.84 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2016-2021 Shanghai Bianjie AI Technology Inc. (licensed under the Apache License, Version 2.0)
// Modifications Copyright (c) 2021, CRO Protocol Labs ("Crypto.org") (licensed under the Apache License, Version 2.0)
package keeper
// DONTCOVER
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-org-chain/chain-main/v2/x/nft/types"
)
// RegisterInvariants registers all supply invariants
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "supply", SupplyInvariant(k))
}
// AllInvariants runs all invariants of the NFT module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
return SupplyInvariant(k)(ctx)
}
}
// 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
owners, err := k.GetOwners(ctx)
if err != nil {
return sdk.FormatInvariant(types.ModuleName, "supply", "unable to fetch owners"), true
}
for _, owner := range owners {
for _, idCollection := range owner.IDCollections {
ownersCollectionsSupply[idCollection.DenomId] += uint64(idCollection.Supply())
}
}
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
}
}