Skip to content

Commit

Permalink
refactor((x/participationrewards/keeper): combine GetProtocolData+Unm…
Browse files Browse the repository at this point in the history
…arshalProtocolData

This change combines GetProtocolData and types.UnmarshalProtocolData
into a generic function that unifies the functionality and the pattern.

Fixes quicksilver-zone#1631
  • Loading branch information
odeke-em committed Jun 29, 2024
1 parent 154b78d commit a3376df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 70 deletions.
84 changes: 14 additions & 70 deletions x/participationrewards/keeper/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,12 @@ func OsmosisPoolUpdateCallback(ctx sdk.Context, k *Keeper, response []byte, quer
}

poolID := sdk.BigEndianToUint64(query.Request[1:])
data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeOsmosisPool, fmt.Sprintf("%d", poolID))
if !ok {
return fmt.Errorf("unable to find protocol data for osmosispools/%d", poolID)
}
ipool, err := types.UnmarshalProtocolData(types.ProtocolDataTypeOsmosisPool, data.Data)
key := fmt.Sprintf("%d", poolID)
data, pool, ok := GetAndUnmarshalProtocolData[*types.OsmosisPoolProtocolData](ctx, key, types.ProtocolDataTypeOsmosisPool)
if err != nil {
return err
}
pool, ok := ipool.(*types.OsmosisPoolProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for osmosispools/%d", poolID)
}

pool.PoolData, err = json.Marshal(pd)
if err != nil {
return err
Expand Down Expand Up @@ -185,18 +179,11 @@ func OsmosisClPoolUpdateCallback(ctx sdk.Context, k *Keeper, response []byte, qu
return err
}

data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeOsmosisCLPool, fmt.Sprintf("%d", poolID))
if !ok {
return fmt.Errorf("unable to find protocol data for osmosisclpools/%d", poolID)
}
ipool, err := types.UnmarshalProtocolData(types.ProtocolDataTypeOsmosisCLPool, data.Data)
data, pool, err := GetAndUnmarshalProtocolData[*types.OsmosisClPoolProtocolData](ctx, k, fmt.Sprintf("%d", poolID), types.ProtocolDataTypeOsmosisCLPool)
if err != nil {
return err
}
pool, ok := ipool.(*types.OsmosisClPoolProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for osmosisclpools/%d", poolID)
}

pool.PoolData, err = json.Marshal(pd)
if err != nil {
return err
Expand All @@ -222,18 +209,11 @@ func UmeeReservesUpdateCallback(ctx sdk.Context, k *Keeper, response []byte, que
}

denom := umeetypes.DenomFromKey(query.Request, umeetypes.KeyPrefixReserveAmount)
data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeUmeeReserves, denom)
if !ok {
return fmt.Errorf("unable to find protocol data for umeereserves/%s", denom)
}
ireserves, err := types.UnmarshalProtocolData(types.ProtocolDataTypeUmeeReserves, data.Data)
data, reserves, err := GetAndUnmarshalProtocolData[*types.UmeeReservesProtocolData](ctx, k, denom, types.ProtocolDataTypeUmeeReserves)
if err != nil {
return err
}
reserves, ok := ireserves.(*types.UmeeReservesProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for umeereserves/%s", denom)
}

reserves.Data, err = json.Marshal(reserveAmount)
if err != nil {
return err
Expand All @@ -259,18 +239,11 @@ func UmeeTotalBorrowsUpdateCallback(ctx sdk.Context, k *Keeper, response []byte,
}

denom := umeetypes.DenomFromKey(query.Request, umeetypes.KeyPrefixAdjustedTotalBorrow)
data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeUmeeTotalBorrows, denom)
if !ok {
return fmt.Errorf("unable to find protocol data for umee-types total borrows/%s", denom)
}
iborrows, err := types.UnmarshalProtocolData(types.ProtocolDataTypeUmeeTotalBorrows, data.Data)
data, borrows, err := GetAndUnmarshalProtocolData[*types.UmeeTotalBorrowsProtocolData](ctx, k, denom, types.ProtocolDataTypeUmeeTotalBorrows)
if err != nil {
return err
}
borrows, ok := iborrows.(*types.UmeeTotalBorrowsProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for umee-types total borrows/%s", denom)
}

borrows.Data, err = json.Marshal(totalBorrows)
if err != nil {
return err
Expand All @@ -296,18 +269,11 @@ func UmeeInterestScalarUpdateCallback(ctx sdk.Context, k *Keeper, response []byt
}

denom := umeetypes.DenomFromKey(query.Request, umeetypes.KeyPrefixInterestScalar)
data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeUmeeInterestScalar, denom)
if !ok {
return fmt.Errorf("unable to find protocol data for interestscalar/%s", denom)
}
iinterest, err := types.UnmarshalProtocolData(types.ProtocolDataTypeUmeeInterestScalar, data.Data)
data, interest, err := GetAndUnmarshalProtocolData[*types.UmeeInterestScalarProtocolData](ctx, k, denom, types.ProtocolDataTypeUmeeInterestScalar)
if err != nil {
return err
}
interest, ok := iinterest.(*types.UmeeInterestScalarProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for interestscalar/%s", denom)
}

interest.Data, err = json.Marshal(interestScalar)
if err != nil {
return err
Expand All @@ -333,18 +299,10 @@ func UmeeUTokenSupplyUpdateCallback(ctx sdk.Context, k *Keeper, response []byte,
}

denom := umeetypes.DenomFromKey(query.Request, umeetypes.KeyPrefixUtokenSupply)
data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeUmeeUTokenSupply, denom)
if !ok {
return fmt.Errorf("unable to find protocol data for umee-types utoken supply/%s", denom)
}
isupply, err := types.UnmarshalProtocolData(types.ProtocolDataTypeUmeeUTokenSupply, data.Data)
data, supply, err := GetAndUnmarshalProtocolData[*types.UmeeUTokenSupplyProtocolData](ctx, k, denom, types.ProtocolDataTypeUmeeUTokenSupply)
if err != nil {
return err
}
supply, ok := isupply.(*types.UmeeUTokenSupplyProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for umee-types utoken supply/%s", denom)
}
supply.Data, err = json.Marshal(supplyAmount)
if err != nil {
return err
Expand Down Expand Up @@ -377,18 +335,10 @@ func UmeeLeverageModuleBalanceUpdateCallback(ctx sdk.Context, k *Keeper, respons
}
balanceAmount := balanceCoin.Amount

data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeUmeeLeverageModuleBalance, denom)
if !ok {
return fmt.Errorf("unable to find protocol data for umee-types leverage module/%s", denom)
}
ibalance, err := types.UnmarshalProtocolData(types.ProtocolDataTypeUmeeLeverageModuleBalance, data.Data)
data, balance, err := GetAndUnmarshalProtocolData[*types.UmeeLeverageModuleBalanceProtocolData](ctx, k, denom, types.ProtocolDataTypeUmeeLeverageModuleBalance)
if err != nil {
return err
}
balance, ok := ibalance.(*types.UmeeLeverageModuleBalanceProtocolData)
if !ok {
return fmt.Errorf("unable to unmarshal protocol data for umee-types leverage module/%s", denom)
}
balance.Data, err = json.Marshal(balanceAmount)
if err != nil {
return err
Expand All @@ -405,14 +355,8 @@ func UmeeLeverageModuleBalanceUpdateCallback(ctx sdk.Context, k *Keeper, respons

// SetEpochBlockCallback records the block height of the registered zone at the epoch boundary.
func SetEpochBlockCallback(ctx sdk.Context, k *Keeper, args []byte, query icqtypes.Query) error {
data, ok := k.GetProtocolData(ctx, types.ProtocolDataTypeConnection, query.ChainId)
if !ok {
return fmt.Errorf("unable to find protocol data for connection/%s", query.ChainId)
}
k.Logger(ctx).Debug("epoch callback called")
iConnectionData, err := types.UnmarshalProtocolData(types.ProtocolDataTypeConnection, data.Data)
connectionData, _ := iConnectionData.(*types.ConnectionProtocolData)

data, connectionData, err := GetAndUnmarshalProtocolData[*types.ConnectionProtocolData](ctx, k, query.ChainId, types.ProtocolDataTypeConnection)
if err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions x/participationrewards/keeper/protocol_data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -38,6 +40,22 @@ func (k Keeper) SetProtocolData(ctx sdk.Context, key []byte, data *types.Protoco
store.Set(types.GetProtocolDataKey(types.ProtocolDataType(pdType), key), bz)
}

func GetAndUnmarshalProtocolData[T any](ctx sdk.Context, k *Keeper, key string, pdType types.ProtocolDataType) (dt types.ProtocolData, tt T, err error) {
data, ok := k.GetProtocolData(ctx, pdType, key)
if !ok {
return dt, tt, fmt.Errorf("unable to find protocol data for %q", key)
}
pd, err := types.UnmarshalProtocolData(pdType, data.Data)
if err != nil {
return dt, tt, err
}
asType, ok := pd.(T)
if !ok {
return dt, tt, fmt.Errorf("could not retrieve type of %T", (*T)(nil))
}
return data, asType, nil
}

// DeleteProtocolData deletes protocol data info.
func (k *Keeper) DeleteProtocolData(ctx sdk.Context, key []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixProtocolData)
Expand Down

0 comments on commit a3376df

Please sign in to comment.