github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/keeper/fee_splits.go (about)

     1  package keeper
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/store/prefix"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    10  	"github.com/fibonacci-chain/fbc/x/feesplit/types"
    11  )
    12  
    13  // GetFeeSplits returns all registered FeeSplits.
    14  func (k Keeper) GetFeeSplits(ctx sdk.Context) []types.FeeSplit {
    15  	feeSplits := []types.FeeSplit{}
    16  
    17  	store := ctx.KVStore(k.storeKey)
    18  	iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixFeeSplit)
    19  	defer iterator.Close()
    20  
    21  	for ; iterator.Valid(); iterator.Next() {
    22  		var feeSplit types.FeeSplit
    23  		k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &feeSplit)
    24  
    25  		feeSplits = append(feeSplits, feeSplit)
    26  	}
    27  
    28  	return feeSplits
    29  }
    30  
    31  // IterateFeeSplits iterates over all registered contracts and performs a
    32  // callback with the corresponding FeeSplit.
    33  func (k Keeper) IterateFeeSplits(
    34  	ctx sdk.Context,
    35  	handlerFn func(fee types.FeeSplit) (stop bool),
    36  ) {
    37  	store := ctx.KVStore(k.storeKey)
    38  	iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixFeeSplit)
    39  	defer iterator.Close()
    40  
    41  	for ; iterator.Valid(); iterator.Next() {
    42  		var feeSplit types.FeeSplit
    43  		k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &feeSplit)
    44  
    45  		if handlerFn(feeSplit) {
    46  			break
    47  		}
    48  	}
    49  }
    50  
    51  // GetFeeSplitWithCache returns the FeeSplit for a registered contract from cache
    52  func (k Keeper) GetFeeSplitWithCache(
    53  	ctx sdk.Context,
    54  	contract common.Address,
    55  ) (feeSplit types.FeeSplit, found bool) {
    56  	if ctx.UseParamCache() && !tmtypes.DownloadDelta {
    57  		if feeSplit, found = types.GetParamsCache().GetFeeSplit(contract); !found {
    58  			if feeSplit, found = k.GetFeeSplit(ctx, contract); found {
    59  				types.GetParamsCache().UpdateFeeSplit(feeSplit.ContractAddress, feeSplit, ctx.IsCheckTx())
    60  			}
    61  		}
    62  	} else {
    63  		feeSplit, found = k.GetFeeSplit(ctx, contract)
    64  	}
    65  
    66  	return
    67  }
    68  
    69  // GetFeeSplit returns the FeeSplit for a registered contract
    70  func (k Keeper) GetFeeSplit(
    71  	ctx sdk.Context,
    72  	contract common.Address,
    73  ) (types.FeeSplit, bool) {
    74  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeSplit)
    75  	bz := store.Get(contract.Bytes())
    76  	if len(bz) == 0 {
    77  		return types.FeeSplit{}, false
    78  	}
    79  
    80  	var feeSplit types.FeeSplit
    81  	k.cdc.MustUnmarshalBinaryBare(bz, &feeSplit)
    82  	return feeSplit, true
    83  }
    84  
    85  // SetFeeSplit stores the FeeSplit for a registered contract.
    86  func (k Keeper) SetFeeSplit(ctx sdk.Context, feeSplit types.FeeSplit) {
    87  	if feeSplit.WithdrawerAddress.Empty() {
    88  		feeSplit.WithdrawerAddress = feeSplit.DeployerAddress
    89  	}
    90  
    91  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeSplit)
    92  	key := feeSplit.ContractAddress
    93  	bz := k.cdc.MustMarshalBinaryBare(feeSplit)
    94  	store.Set(key.Bytes(), bz)
    95  
    96  	// update cache
    97  	if ctx.IsDeliver() || ctx.ParaMsg() != nil {
    98  		types.GetParamsCache().UpdateFeeSplit(feeSplit.ContractAddress, feeSplit, ctx.IsCheckTx())
    99  	}
   100  }
   101  
   102  // DeleteFeeSplit deletes a FeeSplit of a registered contract.
   103  func (k Keeper) DeleteFeeSplit(ctx sdk.Context, feeSplit types.FeeSplit) {
   104  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeSplit)
   105  	key := feeSplit.ContractAddress
   106  	store.Delete(key.Bytes())
   107  
   108  	// update cache
   109  	if ctx.IsDeliver() || ctx.ParaMsg() != nil {
   110  		types.GetParamsCache().DeleteFeeSplit(feeSplit.ContractAddress, ctx.IsCheckTx())
   111  	}
   112  }
   113  
   114  // SetDeployerMap stores a contract-by-deployer mapping
   115  func (k Keeper) SetDeployerMap(
   116  	ctx sdk.Context,
   117  	deployer sdk.AccAddress,
   118  	contract common.Address,
   119  ) {
   120  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeployer)
   121  	key := append(deployer.Bytes(), contract.Bytes()...)
   122  	store.Set(key, []byte{1})
   123  }
   124  
   125  // DeleteDeployerMap deletes a contract-by-deployer mapping
   126  func (k Keeper) DeleteDeployerMap(
   127  	ctx sdk.Context,
   128  	deployer sdk.AccAddress,
   129  	contract common.Address,
   130  ) {
   131  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeployer)
   132  	key := append(deployer.Bytes(), contract.Bytes()...)
   133  	store.Delete(key)
   134  }
   135  
   136  // SetWithdrawerMap stores a contract-by-withdrawer mapping
   137  func (k Keeper) SetWithdrawerMap(
   138  	ctx sdk.Context,
   139  	withdrawer sdk.AccAddress,
   140  	contract common.Address,
   141  ) {
   142  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWithdrawer)
   143  	key := append(withdrawer.Bytes(), contract.Bytes()...)
   144  	store.Set(key, []byte{1})
   145  }
   146  
   147  // DeleteWithdrawerMap deletes a contract-by-withdrawer mapping
   148  func (k Keeper) DeleteWithdrawerMap(
   149  	ctx sdk.Context,
   150  	withdrawer sdk.AccAddress,
   151  	contract common.Address,
   152  ) {
   153  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWithdrawer)
   154  	key := append(withdrawer.Bytes(), contract.Bytes()...)
   155  	store.Delete(key)
   156  }
   157  
   158  // IsFeeSplitRegistered checks if a contract was registered for receiving transaction fees
   159  func (k Keeper) IsFeeSplitRegistered(
   160  	ctx sdk.Context,
   161  	contract common.Address,
   162  ) bool {
   163  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeSplit)
   164  	return store.Has(contract.Bytes())
   165  }
   166  
   167  // IsDeployerMapSet checks if a given contract-by-withdrawer mapping is set in store
   168  func (k Keeper) IsDeployerMapSet(
   169  	ctx sdk.Context,
   170  	deployer sdk.AccAddress,
   171  	contract common.Address,
   172  ) bool {
   173  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeployer)
   174  	key := append(deployer.Bytes(), contract.Bytes()...)
   175  	return store.Has(key)
   176  }
   177  
   178  // IsWithdrawerMapSet checks if a giveb contract-by-withdrawer mapping is set in store
   179  func (k Keeper) IsWithdrawerMapSet(
   180  	ctx sdk.Context,
   181  	withdrawer sdk.AccAddress,
   182  	contract common.Address,
   183  ) bool {
   184  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWithdrawer)
   185  	key := append(withdrawer.Bytes(), contract.Bytes()...)
   186  	return store.Has(key)
   187  }
   188  
   189  // SetContractShare stores the share for a registered contract.
   190  func (k Keeper) SetContractShare(
   191  	ctx sdk.Context,
   192  	contract common.Address,
   193  	share sdk.Dec,
   194  ) {
   195  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixContractShare)
   196  	store.Set(contract.Bytes(), share.Bytes())
   197  
   198  	// update cache
   199  	if ctx.IsDeliver() || ctx.ParaMsg() != nil {
   200  		types.GetParamsCache().UpdateShare(contract, share, ctx.IsCheckTx())
   201  	}
   202  }
   203  
   204  // GetContractShare returns the share for a registered contract
   205  func (k Keeper) GetContractShare(
   206  	ctx sdk.Context,
   207  	contract common.Address,
   208  ) (sdk.Dec, bool) {
   209  	store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixContractShare)
   210  	bz := store.Get(contract.Bytes())
   211  	// if share=0, the 'bz' is []byte{}, so can not use "len(bz)"
   212  	if bz == nil {
   213  		return sdk.ZeroDec(), false
   214  	}
   215  
   216  	return sdk.NewDecFromBigIntWithPrec(new(big.Int).SetBytes(bz), sdk.Precision), true
   217  }
   218  
   219  // GetContractShareWithCache  returns the share for a registered contract from cache
   220  func (k Keeper) GetContractShareWithCache(
   221  	ctx sdk.Context,
   222  	contract common.Address,
   223  ) (share sdk.Dec, found bool) {
   224  	if ctx.UseParamCache() && !tmtypes.DownloadDelta {
   225  		if share, found = types.GetParamsCache().GetShare(contract); !found {
   226  			if share, found = k.GetContractShare(ctx, contract); found {
   227  				types.GetParamsCache().UpdateShare(contract, share, ctx.IsCheckTx())
   228  			}
   229  		}
   230  	} else {
   231  		share, found = k.GetContractShare(ctx, contract)
   232  	}
   233  
   234  	return
   235  }