github.com/Finschia/finschia-sdk@v0.48.1/x/token/keeper/alias.go (about) 1 package keeper 2 3 import ( 4 sdk "github.com/Finschia/finschia-sdk/types" 5 "github.com/Finschia/finschia-sdk/x/token" 6 ) 7 8 // iterate through the balances of a contract and perform the provided function 9 func (k Keeper) iterateContractBalances(ctx sdk.Context, contractID string, fn func(balance token.Balance) (stop bool)) { 10 k.iterateBalancesImpl(ctx, balanceKeyPrefixByContractID(contractID), func(_ string, balance token.Balance) (stop bool) { 11 return fn(balance) 12 }) 13 } 14 15 func (k Keeper) iterateBalancesImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, balance token.Balance) (stop bool)) { 16 store := ctx.KVStore(k.storeKey) 17 18 iterator := sdk.KVStorePrefixIterator(store, prefix) 19 defer iterator.Close() 20 21 for ; iterator.Valid(); iterator.Next() { 22 contractID, addr := splitBalanceKey(iterator.Key()) 23 24 var amount sdk.Int 25 if err := amount.Unmarshal(iterator.Value()); err != nil { 26 panic(err) 27 } 28 balance := token.Balance{ 29 Address: addr.String(), 30 Amount: amount, 31 } 32 33 stop := fn(contractID, balance) 34 if stop { 35 break 36 } 37 } 38 } 39 40 // iterate through the classes and perform the provided function 41 func (k Keeper) iterateClasses(ctx sdk.Context, fn func(class token.Contract) (stop bool)) { 42 store := ctx.KVStore(k.storeKey) 43 44 iterator := sdk.KVStorePrefixIterator(store, classKeyPrefix) 45 defer iterator.Close() 46 47 for ; iterator.Valid(); iterator.Next() { 48 var class token.Contract 49 k.cdc.MustUnmarshal(iterator.Value(), &class) 50 51 stop := fn(class) 52 if stop { 53 break 54 } 55 } 56 } 57 58 func (k Keeper) iterateContractGrants(ctx sdk.Context, contractID string, fn func(grant token.Grant) (stop bool)) { 59 k.iterateGrantsImpl(ctx, grantKeyPrefixByContractID(contractID), func(_ string, grant token.Grant) (stop bool) { 60 return fn(grant) 61 }) 62 } 63 64 func (k Keeper) iterateGrantsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, grant token.Grant) (stop bool)) { 65 store := ctx.KVStore(k.storeKey) 66 67 iterator := sdk.KVStorePrefixIterator(store, prefix) 68 defer iterator.Close() 69 70 for ; iterator.Valid(); iterator.Next() { 71 contractID, grantee, permission := splitGrantKey(iterator.Key()) 72 grant := token.Grant{ 73 Grantee: grantee.String(), 74 Permission: permission, 75 } 76 77 stop := fn(contractID, grant) 78 if stop { 79 break 80 } 81 } 82 } 83 84 func (k Keeper) iterateContractAuthorizations(ctx sdk.Context, contractID string, fn func(authorization token.Authorization) (stop bool)) { 85 k.iterateAuthorizationsImpl(ctx, authorizationKeyPrefixByContractID(contractID), func(_ string, authorization token.Authorization) (stop bool) { 86 return fn(authorization) 87 }) 88 } 89 90 func (k Keeper) iterateAuthorizationsImpl(ctx sdk.Context, prefix []byte, fn func(contractID string, authorization token.Authorization) (stop bool)) { 91 store := ctx.KVStore(k.storeKey) 92 93 iterator := sdk.KVStorePrefixIterator(store, prefix) 94 defer iterator.Close() 95 96 for ; iterator.Valid(); iterator.Next() { 97 contractID, operator, holder := splitAuthorizationKey(iterator.Key()) 98 authorization := token.Authorization{ 99 Holder: holder.String(), 100 Operator: operator.String(), 101 } 102 103 stop := fn(contractID, authorization) 104 if stop { 105 break 106 } 107 } 108 } 109 110 func (k Keeper) iterateStatistics(ctx sdk.Context, prefix []byte, fn func(contractID string, amount sdk.Int) (stop bool)) { 111 store := ctx.KVStore(k.storeKey) 112 113 iterator := sdk.KVStorePrefixIterator(store, prefix) 114 defer iterator.Close() 115 116 for ; iterator.Valid(); iterator.Next() { 117 var amount sdk.Int 118 if err := amount.Unmarshal(iterator.Value()); err != nil { 119 panic(err) 120 } 121 122 contractID := splitStatisticsKey(iterator.Key(), prefix) 123 124 stop := fn(contractID, amount) 125 if stop { 126 break 127 } 128 } 129 } 130 131 func (k Keeper) iterateSupplies(ctx sdk.Context, fn func(contractID string, amount sdk.Int) (stop bool)) { 132 k.iterateStatistics(ctx, supplyKeyPrefix, fn) 133 } 134 135 func (k Keeper) iterateMinteds(ctx sdk.Context, fn func(contractID string, amount sdk.Int) (stop bool)) { 136 k.iterateStatistics(ctx, mintKeyPrefix, fn) 137 } 138 139 func (k Keeper) iterateBurnts(ctx sdk.Context, fn func(contractID string, amount sdk.Int) (stop bool)) { 140 k.iterateStatistics(ctx, burnKeyPrefix, fn) 141 }