Skip to content

Commit

Permalink
just use int
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jan 9, 2024
1 parent fd1a39d commit a582fa1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 8 additions & 2 deletions osmoutils/store_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osmoutils
import (
"errors"
"fmt"
"strings"

db "github.com/cometbft/cometbft-db"
"github.com/cosmos/cosmos-sdk/store/prefix"
Expand Down Expand Up @@ -237,7 +238,12 @@ func GetCoinArrayFromPrefix(ctx sdk.Context, storeKey storetypes.StoreKey, store
bz := iterator.Value()
sdkInt := osmomath.Int{}
if err := sdkInt.Unmarshal(bz); err == nil {
coinArray = append(coinArray, sdk.NewCoin(string(iterator.Key()), sdkInt))
// Split the key into prefix and denom
keyParts := strings.Split(string(iterator.Key()), "|")
if len(keyParts) > 1 {
denom := keyParts[1]
coinArray = append(coinArray, sdk.NewCoin(denom, sdkInt))
}
}
}

Expand Down Expand Up @@ -266,7 +272,7 @@ func GetCoinByDenomFromPrefix(ctx sdk.Context, storeKey storetypes.StoreKey, sto
// IncreaseCoinByDenomFromPrefix increases the coin from the store that has the given prefix and denom by the specified amount.
func IncreaseCoinByDenomFromPrefix(ctx sdk.Context, storeKey storetypes.StoreKey, storePrefix []byte, denom string, increasedAmt osmomath.Int) error {
store := prefix.NewStore(ctx.KVStore(storeKey), storePrefix)
key := append(storePrefix, []byte(denom)...)
key := []byte(fmt.Sprintf("%s%s%s", storePrefix, "|", []byte(denom)))

coin, err := GetCoinByDenomFromPrefix(ctx, storeKey, storePrefix, denom)
if err != nil {
Expand Down
16 changes: 15 additions & 1 deletion x/protorev/keeper/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,21 @@ func (k Keeper) IncrementNumberOfTrades(ctx sdk.Context) error {

// GetAllProfits returns all of the profits made by the ProtoRev module.
func (k Keeper) GetAllProfits(ctx sdk.Context) []sdk.Coin {
return osmoutils.GetCoinArrayFromPrefix(ctx, k.storeKey, types.KeyPrefixProfitByDenom)
profits := make([]sdk.Coin, 0)

store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixProfitByDenom)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
bz := iterator.Value()
profit := sdk.Coin{}
if err := profit.Unmarshal(bz); err == nil {
profits = append(profits, profit)
}
}

return profits
}

// GetProfitsByDenom returns the profits made by the ProtoRev module for the given denom.
Expand Down

0 comments on commit a582fa1

Please sign in to comment.