github.com/KiraCore/sekai@v0.3.43/x/basket/keeper/basket_action_history.go (about)

     1  package keeper
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/KiraCore/sekai/x/basket/types"
     7  	"github.com/cosmos/cosmos-sdk/store/prefix"
     8  	sdk "github.com/cosmos/cosmos-sdk/types"
     9  )
    10  
    11  func (k Keeper) RegisterMintAction(ctx sdk.Context, basketId uint64, amount sdk.Int) {
    12  	mintedAmount := k.GetMintAmount(ctx, basketId, ctx.BlockTime())
    13  	mintedAmount = mintedAmount.Add(amount)
    14  	k.SetMintAmount(ctx, ctx.BlockTime(), basketId, mintedAmount)
    15  }
    16  
    17  func (k Keeper) RegisterBurnAction(ctx sdk.Context, basketId uint64, amount sdk.Int) {
    18  	burnedAmount := k.GetBurnAmount(ctx, basketId, ctx.BlockTime())
    19  	burnedAmount = burnedAmount.Add(amount)
    20  	k.SetBurnAmount(ctx, ctx.BlockTime(), basketId, burnedAmount)
    21  }
    22  
    23  func (k Keeper) RegisterSwapAction(ctx sdk.Context, basketId uint64, amount sdk.Int) {
    24  	swapedAmount := k.GetSwapAmount(ctx, basketId, ctx.BlockTime())
    25  	swapedAmount = swapedAmount.Add(amount)
    26  	k.SetSwapAmount(ctx, ctx.BlockTime(), basketId, swapedAmount)
    27  }
    28  
    29  func (k Keeper) SetMintAmount(ctx sdk.Context, time time.Time, basketId uint64, amount sdk.Int) {
    30  	store := ctx.KVStore(k.storeKey)
    31  	amountByTime := types.AmountAtTime{
    32  		BasketId: basketId,
    33  		Amount:   amount,
    34  		Time:     uint64(time.Unix()),
    35  	}
    36  	bz := k.cdc.MustMarshal(&amountByTime)
    37  	store.Set(types.BasketMintByTimeKey(basketId, time), bz)
    38  }
    39  
    40  func (k Keeper) SetBurnAmount(ctx sdk.Context, time time.Time, basketId uint64, amount sdk.Int) {
    41  	store := ctx.KVStore(k.storeKey)
    42  	amountByTime := types.AmountAtTime{
    43  		BasketId: basketId,
    44  		Amount:   amount,
    45  		Time:     uint64(time.Unix()),
    46  	}
    47  	bz := k.cdc.MustMarshal(&amountByTime)
    48  	store.Set(types.BasketBurnByTimeKey(basketId, time), bz)
    49  }
    50  
    51  func (k Keeper) SetSwapAmount(ctx sdk.Context, time time.Time, basketId uint64, amount sdk.Int) {
    52  	store := ctx.KVStore(k.storeKey)
    53  	amountByTime := types.AmountAtTime{
    54  		BasketId: basketId,
    55  		Amount:   amount,
    56  		Time:     uint64(time.Unix()),
    57  	}
    58  	bz := k.cdc.MustMarshal(&amountByTime)
    59  	store.Set(types.BasketSwapByTimeKey(basketId, time), bz)
    60  }
    61  
    62  func (k Keeper) GetMintAmount(ctx sdk.Context, basketId uint64, time time.Time) sdk.Int {
    63  	store := ctx.KVStore(k.storeKey)
    64  	bz := store.Get(types.BasketMintByTimeKey(basketId, time))
    65  	if bz == nil {
    66  		return sdk.ZeroInt()
    67  	}
    68  	amountByTime := types.AmountAtTime{}
    69  	k.cdc.MustUnmarshal(bz, &amountByTime)
    70  	return amountByTime.Amount
    71  }
    72  
    73  func (k Keeper) GetBurnAmount(ctx sdk.Context, basketId uint64, time time.Time) sdk.Int {
    74  	store := ctx.KVStore(k.storeKey)
    75  	bz := store.Get(types.BasketBurnByTimeKey(basketId, time))
    76  	if bz == nil {
    77  		return sdk.ZeroInt()
    78  	}
    79  	amountByTime := types.AmountAtTime{}
    80  	k.cdc.MustUnmarshal(bz, &amountByTime)
    81  	return amountByTime.Amount
    82  }
    83  
    84  func (k Keeper) GetSwapAmount(ctx sdk.Context, basketId uint64, time time.Time) sdk.Int {
    85  	store := ctx.KVStore(k.storeKey)
    86  	bz := store.Get(types.BasketSwapByTimeKey(basketId, time))
    87  	if bz == nil {
    88  		return sdk.ZeroInt()
    89  	}
    90  	amountByTime := types.AmountAtTime{}
    91  	k.cdc.MustUnmarshal(bz, &amountByTime)
    92  	return amountByTime.Amount
    93  }
    94  
    95  func (k Keeper) GetLimitsPeriodMintAmount(ctx sdk.Context, basketId uint64, limitsPeriod uint64) sdk.Int {
    96  	store := ctx.KVStore(k.storeKey)
    97  	startTime := ctx.BlockTime().Add(-time.Second * time.Duration(limitsPeriod))
    98  	iterator := store.Iterator(
    99  		types.BasketMintByTimeKey(basketId, startTime),
   100  		sdk.PrefixEndBytes(append(types.PrefixBasketMintByTime, sdk.Uint64ToBigEndian(basketId)...)),
   101  	)
   102  
   103  	defer iterator.Close()
   104  
   105  	totalAmount := sdk.ZeroInt()
   106  	for ; iterator.Valid(); iterator.Next() {
   107  		amountByTime := types.AmountAtTime{}
   108  		k.cdc.MustUnmarshal(iterator.Value(), &amountByTime)
   109  		totalAmount = totalAmount.Add(amountByTime.Amount)
   110  	}
   111  	return totalAmount
   112  }
   113  
   114  func (k Keeper) GetLimitsPeriodBurnAmount(ctx sdk.Context, basketId uint64, limitsPeriod uint64) sdk.Int {
   115  	store := ctx.KVStore(k.storeKey)
   116  	startTime := ctx.BlockTime().Add(-time.Second * time.Duration(limitsPeriod))
   117  	iterator := store.Iterator(
   118  		types.BasketBurnByTimeKey(basketId, startTime),
   119  		sdk.PrefixEndBytes(append(types.PrefixBasketBurnByTime, sdk.Uint64ToBigEndian(basketId)...)),
   120  	)
   121  
   122  	defer iterator.Close()
   123  
   124  	totalAmount := sdk.ZeroInt()
   125  	for ; iterator.Valid(); iterator.Next() {
   126  		amountByTime := types.AmountAtTime{}
   127  		k.cdc.MustUnmarshal(iterator.Value(), &amountByTime)
   128  		totalAmount = totalAmount.Add(amountByTime.Amount)
   129  	}
   130  	return totalAmount
   131  }
   132  
   133  func (k Keeper) GetLimitsPeriodSwapAmount(ctx sdk.Context, basketId uint64, limitsPeriod uint64) sdk.Int {
   134  	store := ctx.KVStore(k.storeKey)
   135  	startTime := ctx.BlockTime().Add(-time.Second * time.Duration(limitsPeriod))
   136  	iterator := store.Iterator(
   137  		types.BasketSwapByTimeKey(basketId, startTime),
   138  		sdk.PrefixEndBytes(append(types.PrefixBasketSwapByTime, sdk.Uint64ToBigEndian(basketId)...)),
   139  	)
   140  
   141  	defer iterator.Close()
   142  
   143  	totalAmount := sdk.ZeroInt()
   144  	for ; iterator.Valid(); iterator.Next() {
   145  		amountByTime := types.AmountAtTime{}
   146  		k.cdc.MustUnmarshal(iterator.Value(), &amountByTime)
   147  		totalAmount = totalAmount.Add(amountByTime.Amount)
   148  	}
   149  	return totalAmount
   150  }
   151  
   152  func (k Keeper) ClearOldMintAmounts(ctx sdk.Context, basketId uint64, limitsPeriod uint64) {
   153  	store := ctx.KVStore(k.storeKey)
   154  	startTime := ctx.BlockTime().Add(-time.Second * time.Duration(limitsPeriod))
   155  	iterator := store.Iterator(
   156  		append(types.PrefixBasketMintByTime, sdk.Uint64ToBigEndian(basketId)...),
   157  		types.BasketMintByTimeKey(basketId, startTime))
   158  
   159  	defer iterator.Close()
   160  
   161  	for ; iterator.Valid(); iterator.Next() {
   162  		store.Delete(iterator.Key())
   163  	}
   164  }
   165  
   166  func (k Keeper) ClearOldBurnAmounts(ctx sdk.Context, basketId uint64, limitsPeriod uint64) {
   167  	store := ctx.KVStore(k.storeKey)
   168  	startTime := ctx.BlockTime().Add(-time.Second * time.Duration(limitsPeriod))
   169  	iterator := store.Iterator(
   170  		append(types.PrefixBasketBurnByTime, sdk.Uint64ToBigEndian(basketId)...),
   171  		types.BasketBurnByTimeKey(basketId, startTime))
   172  
   173  	defer iterator.Close()
   174  
   175  	for ; iterator.Valid(); iterator.Next() {
   176  		store.Delete(iterator.Key())
   177  	}
   178  }
   179  
   180  func (k Keeper) ClearOldSwapAmounts(ctx sdk.Context, basketId uint64, limitsPeriod uint64) {
   181  	store := ctx.KVStore(k.storeKey)
   182  	startTime := ctx.BlockTime().Add(-time.Second * time.Duration(limitsPeriod))
   183  	iterator := store.Iterator(
   184  		append(types.PrefixBasketSwapByTime, sdk.Uint64ToBigEndian(basketId)...),
   185  		types.BasketSwapByTimeKey(basketId, startTime))
   186  
   187  	defer iterator.Close()
   188  
   189  	for ; iterator.Valid(); iterator.Next() {
   190  		store.Delete(iterator.Key())
   191  	}
   192  }
   193  
   194  func (k Keeper) GetAllMintAmounts(ctx sdk.Context) []types.AmountAtTime {
   195  	prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixBasketMintByTime)
   196  	iterator := prefixStore.Iterator(nil, nil)
   197  
   198  	defer iterator.Close()
   199  
   200  	historicalMints := []types.AmountAtTime{}
   201  	for ; iterator.Valid(); iterator.Next() {
   202  		amount := types.AmountAtTime{}
   203  		k.cdc.MustUnmarshal(iterator.Value(), &amount)
   204  		historicalMints = append(historicalMints, amount)
   205  	}
   206  	return historicalMints
   207  }
   208  
   209  func (k Keeper) GetAllBurnAmounts(ctx sdk.Context) []types.AmountAtTime {
   210  	prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixBasketBurnByTime)
   211  	iterator := prefixStore.Iterator(nil, nil)
   212  
   213  	defer iterator.Close()
   214  
   215  	historicalBurns := []types.AmountAtTime{}
   216  	for ; iterator.Valid(); iterator.Next() {
   217  		amount := types.AmountAtTime{}
   218  		k.cdc.MustUnmarshal(iterator.Value(), &amount)
   219  		historicalBurns = append(historicalBurns, amount)
   220  	}
   221  	return historicalBurns
   222  }
   223  
   224  func (k Keeper) GetAllSwapAmounts(ctx sdk.Context) []types.AmountAtTime {
   225  	prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixBasketSwapByTime)
   226  	iterator := prefixStore.Iterator(nil, nil)
   227  
   228  	defer iterator.Close()
   229  
   230  	historicalSwaps := []types.AmountAtTime{}
   231  	for ; iterator.Valid(); iterator.Next() {
   232  		amount := types.AmountAtTime{}
   233  		k.cdc.MustUnmarshal(iterator.Value(), &amount)
   234  		historicalSwaps = append(historicalSwaps, amount)
   235  	}
   236  	return historicalSwaps
   237  }