github.com/Finschia/finschia-sdk@v0.48.1/x/collection/keeper/alias.go (about)

     1  package keeper
     2  
     3  import (
     4  	gogotypes "github.com/gogo/protobuf/types"
     5  
     6  	sdk "github.com/Finschia/finschia-sdk/types"
     7  	"github.com/Finschia/finschia-sdk/x/collection"
     8  )
     9  
    10  // iterate through the balances of a contract and perform the provided function
    11  func (k Keeper) iterateContractBalances(ctx sdk.Context, contractID string, fn func(address sdk.AccAddress, balance collection.Coin) (stop bool)) {
    12  	k.iterateBalancesImpl(ctx, balanceKeyPrefixByContractID(contractID), func(_ string, address sdk.AccAddress, balance collection.Coin) (stop bool) {
    13  		return fn(address, balance)
    14  	})
    15  }
    16  
    17  func (k Keeper) iterateBalancesImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, address sdk.AccAddress, balance collection.Coin) (stop bool)) {
    18  	store := ctx.KVStore(k.storeKey)
    19  
    20  	iterator := sdk.KVStorePrefixIterator(store, prefix)
    21  	defer iterator.Close()
    22  
    23  	for ; iterator.Valid(); iterator.Next() {
    24  		contractID, address, tokenID := splitBalanceKey(iterator.Key())
    25  
    26  		var amount sdk.Int
    27  		if err := amount.Unmarshal(iterator.Value()); err != nil {
    28  			panic(err)
    29  		}
    30  		balance := collection.NewCoin(tokenID, amount)
    31  
    32  		stop := fn(contractID, address, balance)
    33  		if stop {
    34  			break
    35  		}
    36  	}
    37  }
    38  
    39  func (k Keeper) iterateContracts(ctx sdk.Context, fn func(contract collection.Contract) (stop bool)) {
    40  	store := ctx.KVStore(k.storeKey)
    41  
    42  	iterator := sdk.KVStorePrefixIterator(store, contractKeyPrefix)
    43  	defer iterator.Close()
    44  
    45  	for ; iterator.Valid(); iterator.Next() {
    46  		var contract collection.Contract
    47  		k.cdc.MustUnmarshal(iterator.Value(), &contract)
    48  
    49  		stop := fn(contract)
    50  		if stop {
    51  			break
    52  		}
    53  	}
    54  }
    55  
    56  func (k Keeper) iterateContractClasses(ctx sdk.Context, contractID string, fn func(class collection.TokenClass) (stop bool)) {
    57  	k.iterateClassesImpl(ctx, classKeyPrefixByContractID(contractID), fn)
    58  }
    59  
    60  // iterate through the classes and perform the provided function
    61  func (k Keeper) iterateClassesImpl(ctx sdk.Context, prefix []byte, fn func(class collection.TokenClass) (stop bool)) {
    62  	store := ctx.KVStore(k.storeKey)
    63  
    64  	iterator := sdk.KVStorePrefixIterator(store, prefix)
    65  	defer iterator.Close()
    66  
    67  	for ; iterator.Valid(); iterator.Next() {
    68  		var class collection.TokenClass
    69  		if err := k.cdc.UnmarshalInterface(iterator.Value(), &class); err != nil {
    70  			panic(err)
    71  		}
    72  
    73  		stop := fn(class)
    74  		if stop {
    75  			break
    76  		}
    77  	}
    78  }
    79  
    80  func (k Keeper) iterateContractGrants(ctx sdk.Context, contractID string, fn func(grant collection.Grant) (stop bool)) {
    81  	k.iterateGrantsImpl(ctx, grantKeyPrefixByContractID(contractID), func(_ string, grant collection.Grant) (stop bool) {
    82  		return fn(grant)
    83  	})
    84  }
    85  
    86  func (k Keeper) iterateGrantsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, grant collection.Grant) (stop bool)) {
    87  	store := ctx.KVStore(k.storeKey)
    88  
    89  	iterator := sdk.KVStorePrefixIterator(store, prefix)
    90  	defer iterator.Close()
    91  
    92  	for ; iterator.Valid(); iterator.Next() {
    93  		contractID, grantee, permission := splitGrantKey(iterator.Key())
    94  		grant := collection.Grant{
    95  			Grantee:    grantee.String(),
    96  			Permission: permission,
    97  		}
    98  
    99  		stop := fn(contractID, grant)
   100  		if stop {
   101  			break
   102  		}
   103  	}
   104  }
   105  
   106  func (k Keeper) iterateContractAuthorizations(ctx sdk.Context, contractID string, fn func(authorization collection.Authorization) (stop bool)) {
   107  	k.iterateAuthorizationsImpl(ctx, authorizationKeyPrefixByContractID(contractID), func(_ string, authorization collection.Authorization) (stop bool) {
   108  		return fn(authorization)
   109  	})
   110  }
   111  
   112  func (k Keeper) iterateAuthorizationsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, authorization collection.Authorization) (stop bool)) {
   113  	store := ctx.KVStore(k.storeKey)
   114  
   115  	iterator := sdk.KVStorePrefixIterator(store, prefix)
   116  	defer iterator.Close()
   117  
   118  	for ; iterator.Valid(); iterator.Next() {
   119  		contractID, operator, holder := splitAuthorizationKey(iterator.Key())
   120  		authorization := collection.Authorization{
   121  			Holder:   holder.String(),
   122  			Operator: operator.String(),
   123  		}
   124  
   125  		stop := fn(contractID, authorization)
   126  		if stop {
   127  			break
   128  		}
   129  	}
   130  }
   131  
   132  func (k Keeper) iterateContractNFTs(ctx sdk.Context, contractID string, fn func(nft collection.NFT) (stop bool)) {
   133  	k.iterateNFTsImpl(ctx, nftKeyPrefixByContractID(contractID), func(_ string, nft collection.NFT) (stop bool) {
   134  		return fn(nft)
   135  	})
   136  }
   137  
   138  func (k Keeper) iterateNFTsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, NFT collection.NFT) (stop bool)) {
   139  	store := ctx.KVStore(k.storeKey)
   140  	iter := sdk.KVStorePrefixIterator(store, prefix)
   141  
   142  	defer iter.Close()
   143  	for ; iter.Valid(); iter.Next() {
   144  		contractID, _ := splitNFTKey(iter.Key())
   145  
   146  		var nft collection.NFT
   147  		k.cdc.MustUnmarshal(iter.Value(), &nft)
   148  
   149  		if fn(contractID, nft) {
   150  			break
   151  		}
   152  	}
   153  }
   154  
   155  func (k Keeper) iterateContractParents(ctx sdk.Context, contractID string, fn func(tokenID, parentID string) (stop bool)) {
   156  	k.iterateParentsImpl(ctx, parentKeyPrefixByContractID(contractID), func(_ string, tokenID, parentID string) (stop bool) {
   157  		return fn(tokenID, parentID)
   158  	})
   159  }
   160  
   161  func (k Keeper) iterateParentsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, tokenID, parentID string) (stop bool)) {
   162  	store := ctx.KVStore(k.storeKey)
   163  	iter := sdk.KVStorePrefixIterator(store, prefix)
   164  
   165  	defer iter.Close()
   166  	for ; iter.Valid(); iter.Next() {
   167  		contractID, tokenID := splitParentKey(iter.Key())
   168  
   169  		var parentID gogotypes.StringValue
   170  		k.cdc.MustUnmarshal(iter.Value(), &parentID)
   171  
   172  		if fn(contractID, tokenID, parentID.Value) {
   173  			break
   174  		}
   175  	}
   176  }
   177  
   178  func (k Keeper) iterateChildrenImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, tokenID, childID string) (stop bool)) {
   179  	store := ctx.KVStore(k.storeKey)
   180  	iter := sdk.KVStorePrefixIterator(store, prefix)
   181  
   182  	defer iter.Close()
   183  	for ; iter.Valid(); iter.Next() {
   184  		contractID, tokenID, childID := splitChildKey(iter.Key())
   185  		if fn(contractID, tokenID, childID) {
   186  			break
   187  		}
   188  	}
   189  }
   190  
   191  func (k Keeper) iterateStatisticsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, classID string, amount sdk.Int) (stop bool)) {
   192  	store := ctx.KVStore(k.storeKey)
   193  
   194  	iterator := sdk.KVStorePrefixIterator(store, prefix)
   195  	defer iterator.Close()
   196  
   197  	for ; iterator.Valid(); iterator.Next() {
   198  		var amount sdk.Int
   199  		if err := amount.Unmarshal(iterator.Value()); err != nil {
   200  			panic(err)
   201  		}
   202  
   203  		keyPrefix := prefix[:1]
   204  		contractID, classID := splitStatisticKey(keyPrefix, iterator.Key())
   205  
   206  		stop := fn(contractID, classID, amount)
   207  		if stop {
   208  			break
   209  		}
   210  	}
   211  }
   212  
   213  func (k Keeper) iterateContractSupplies(ctx sdk.Context, contractID string, fn func(classID string, amount sdk.Int) (stop bool)) {
   214  	k.iterateStatisticsImpl(ctx, statisticKeyPrefixByContractID(supplyKeyPrefix, contractID), func(_ string, classID string, amount sdk.Int) (stop bool) {
   215  		return fn(classID, amount)
   216  	})
   217  }
   218  
   219  func (k Keeper) iterateContractBurnts(ctx sdk.Context, contractID string, fn func(classID string, amount sdk.Int) (stop bool)) {
   220  	k.iterateStatisticsImpl(ctx, statisticKeyPrefixByContractID(burntKeyPrefix, contractID), func(_ string, classID string, amount sdk.Int) (stop bool) {
   221  		return fn(classID, amount)
   222  	})
   223  }
   224  
   225  // iterate through the next token class ids and perform the provided function
   226  func (k Keeper) iterateNextTokenClassIDs(ctx sdk.Context, fn func(class collection.NextClassIDs) (stop bool)) {
   227  	store := ctx.KVStore(k.storeKey)
   228  
   229  	iterator := sdk.KVStorePrefixIterator(store, nextClassIDKeyPrefix)
   230  	defer iterator.Close()
   231  
   232  	for ; iterator.Valid(); iterator.Next() {
   233  		var class collection.NextClassIDs
   234  		k.cdc.MustUnmarshal(iterator.Value(), &class)
   235  
   236  		stop := fn(class)
   237  		if stop {
   238  			break
   239  		}
   240  	}
   241  }
   242  
   243  func (k Keeper) iterateContractNextTokenIDs(ctx sdk.Context, contractID string, fn func(nextID collection.NextTokenID) (stop bool)) {
   244  	k.iterateNextTokenIDsImpl(ctx, nextTokenIDKeyPrefixByContractID(contractID), func(_ string, nextID collection.NextTokenID) (stop bool) {
   245  		return fn(nextID)
   246  	})
   247  }
   248  
   249  // iterate through the next (non-fungible) token ids and perform the provided function
   250  func (k Keeper) iterateNextTokenIDsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, nextID collection.NextTokenID) (stop bool)) {
   251  	store := ctx.KVStore(k.storeKey)
   252  
   253  	iterator := sdk.KVStorePrefixIterator(store, prefix)
   254  	defer iterator.Close()
   255  
   256  	for ; iterator.Valid(); iterator.Next() {
   257  		contractID, classID := splitNextTokenIDKey(iterator.Key())
   258  
   259  		var id sdk.Uint
   260  		if err := id.Unmarshal(iterator.Value()); err != nil {
   261  			panic(err)
   262  		}
   263  
   264  		nextID := collection.NextTokenID{
   265  			ClassId: classID,
   266  			Id:      id,
   267  		}
   268  
   269  		stop := fn(contractID, nextID)
   270  		if stop {
   271  			break
   272  		}
   273  	}
   274  }