Skip to content

Commit

Permalink
feat: add rewards bucket keeper logic + tests (#3426)
Browse files Browse the repository at this point in the history
* feat: add rewards bucket keeper logic + tests

* fix: remove unecessary comments

* fix: add missing rest endpoints

* test: fix linter
  • Loading branch information
snobbee committed Nov 7, 2023
1 parent 4244795 commit 40bb2c3
Show file tree
Hide file tree
Showing 30 changed files with 2,690 additions and 197 deletions.
30 changes: 17 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ run:
linters:
enable:
- bodyclose
- deadcode
- depguard
# WARN [runner] The linter 'deadcode' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused.
# - deadcode
# x/tokenregistry/client/cli/query.go:9:2: import 'github.com/cosmos/cosmos-sdk/client' is not allowed from list 'Main' (depguard)
# - depguard
- dogsled
- goconst
# - gocritic
# - gofmt
# - goimports
# - golint
- revive
# - gocritic
# - gofmt
# - goimports
# - golint
# x/clp/client/cli/query.go:49:17: unused-parameter: parameter 'queryRoute' seems to be unused, consider removing or renaming it as _ (revive)
# - revive
- gosec
- govet
- ineffassign
# - interfacer
# this is deprecated/obsolete
# - maligned
# - interfacer
# this is deprecated/obsolete
# - maligned
- misspell
- nakedret
- prealloc
# - scopelint
# - scopelint
- exportloopref
- staticcheck
- structcheck
# WARN [runner] The linter 'structcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused.
# - structcheck
- stylecheck
- typecheck
- unconvert
Expand Down Expand Up @@ -63,7 +67,7 @@ issues:
- ineffassign
- prealloc
- staticcheck
# Not a real issue here, so ignore
# Not a real issue here, so ignore
- path: x/clp/keeper/pureCalculation_test.go
text: "G113:"
linters:
Expand Down
8 changes: 4 additions & 4 deletions integrationtest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,10 @@ func TestIntegration(t *testing.T) {
return genesisState
})

app.ClpKeeper.SetRewardParams(ctx, &tc.Setup.RewardsParams)
app.ClpKeeper.SetLiquidityProtectionParams(ctx, &tc.Setup.ProtectionParams)
app.ClpKeeper.SetPmtpParams(ctx, &tc.Setup.ShiftingParams)
app.ClpKeeper.SetProviderDistributionParams(ctx, &tc.Setup.ProviderParams)
app.ClpKeeper.SetRewardParams(ctx, &tc.Setup.RewardsParams) //nolint
app.ClpKeeper.SetLiquidityProtectionParams(ctx, &tc.Setup.ProtectionParams) //nolint
app.ClpKeeper.SetPmtpParams(ctx, &tc.Setup.ShiftingParams) //nolint
app.ClpKeeper.SetProviderDistributionParams(ctx, &tc.Setup.ProviderParams) //nolint

clpSrv := clpkeeper.NewMsgServerImpl(app.ClpKeeper)
marginSrv := marginkeeper.NewMsgServerImpl(app.MarginKeeper)
Expand Down
2 changes: 2 additions & 0 deletions proto/sifnode/clp/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ option go_package = "github.com/Sifchain/sifnode/x/clp/types";

import "sifnode/clp/v1/params.proto";
import "sifnode/clp/v1/types.proto";
import "sifnode/clp/v1/rewards_bucket.proto";

// GenesisState - all clp state that must be provided at genesis
// TODO: Add parameters to Genesis state ,such as minimum liquidity required to
Expand All @@ -16,4 +17,5 @@ message GenesisState {
repeated string address_whitelist = 2;
repeated sifnode.clp.v1.Pool pool_list = 3;
repeated sifnode.clp.v1.LiquidityProvider liquidity_providers = 4;
repeated RewardsBucket rewards_bucket_list = 5 [(gogoproto.nullable) = false];
}
30 changes: 29 additions & 1 deletion proto/sifnode/clp/v1/querier.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "sifnode/clp/v1/types.proto";
import "sifnode/clp/v1/params.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "google/api/annotations.proto";
import "sifnode/clp/v1/rewards_bucket.proto";

option go_package = "github.com/Sifchain/sifnode/x/clp/types";

Expand Down Expand Up @@ -59,6 +60,15 @@ service Query {
rpc GetPoolShareEstimate(PoolShareEstimateReq) returns (PoolShareEstimateRes) {
option (google.api.http).get = "/sifchain/clp/v1/pool_share_estimate";
};
// Queries a list of RewardsBucket items.
rpc GetRewardsBucket (RewardsBucketReq) returns (RewardsBucketRes) {
option (google.api.http).get = "/sifchain/clp/v1/rewards_bucket/{denom}";

}
rpc GetRewardsBucketAll (AllRewardsBucketReq) returns (AllRewardsBucketRes) {
option (google.api.http).get = "/sifchain/clp/v1/clp/rewards_bucket";

}
}

message PoolReq {
Expand Down Expand Up @@ -262,4 +272,22 @@ message SwapInfo {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
}
}

message RewardsBucketReq {
string denom = 1;
}

message RewardsBucketRes {
RewardsBucket rewards_bucket = 1 [(gogoproto.nullable) = false];
}

message AllRewardsBucketReq {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message AllRewardsBucketRes {
repeated RewardsBucket rewards_bucket = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

17 changes: 17 additions & 0 deletions proto/sifnode/clp/v1/rewards_bucket.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";
package sifnode.clp.v1;

import "gogoproto/gogo.proto";

option go_package = "github.com/Sifchain/sifnode/x/clp/types";

message RewardsBucket {
string denom = 1;
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"amount\""
];

}

59 changes: 59 additions & 0 deletions testutil/keeper/clp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package keeper

import (
"testing"

"github.com/Sifchain/sifnode/x/clp/keeper"
"github.com/Sifchain/sifnode/x/clp/types"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

func ClpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

paramsSubspace := typesparams.NewSubspace(cdc,
types.Amino,
storeKey,
memStoreKey,
"ClpParams",
)
k := keeper.NewKeeper(
cdc,
storeKey,
nil,
nil,
nil,
nil,
mintkeeper.Keeper{},
nil,
paramsSubspace,
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

// Initialize params
k.SetParams(ctx, types.DefaultParams())

return &k, ctx
}
90 changes: 90 additions & 0 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package network

import (
"fmt"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
tmdb "github.com/tendermint/tm-db"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/require"
tmrand "github.com/tendermint/tendermint/libs/rand"

"github.com/Sifchain/sifnode/app"
)

type (
Network = network.Network
Config = network.Config
)

// New creates instance with fully configured cosmos network.
// Accepts optional config, that will be used in place of the DefaultConfig() if provided.
func New(t *testing.T, configs ...Config) *Network {
if len(configs) > 1 {
panic("at most one config should be provided")
}
var cfg network.Config
if len(configs) == 0 {
cfg = DefaultConfig()
} else {
cfg = configs[0]
}
net := network.New(t, cfg)
_, err := net.WaitForHeight(1)
require.NoError(t, err)
t.Cleanup(net.Cleanup)
return net
}

// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig
func DefaultConfig() network.Config {
var (
encoding = app.MakeTestEncodingConfig()
chainID = "chain-" + tmrand.NewRand().Str(6)
)
return network.Config{
Codec: encoding.Marshaler,
TxConfig: encoding.TxConfig,
LegacyAmino: encoding.Amino,
InterfaceRegistry: encoding.InterfaceRegistry,
AccountRetriever: authtypes.AccountRetriever{},
AppConstructor: func(val network.Validator) servertypes.Application {
return app.NewSifApp(
val.Ctx.Logger,
tmdb.NewMemDB(),
nil,
true,
map[int64]bool{},
val.Ctx.Config.RootDir,
0,
encoding,
app.EmptyAppOptions{},
baseapp.SetPruning(storetypes.NewPruningOptionsFromString(val.AppConfig.Pruning)),
baseapp.SetMinGasPrices(val.AppConfig.MinGasPrices),
)
},
GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler),
TimeoutCommit: 2 * time.Second,
ChainID: chainID,
NumValidators: 1,
BondDenom: sdk.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
PruningStrategy: storetypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
KeyringOptions: []keyring.Option{},
}
}
57 changes: 57 additions & 0 deletions testutil/nullify/nullify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package nullify provides methods to init nil values structs for test assertion.
package nullify

import (
"reflect"
"unsafe"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
coinType = reflect.TypeOf(sdk.Coin{})
coinsType = reflect.TypeOf(sdk.Coins{})
)

// Fill analyze all struct fields and slices with
// reflection and initialize the nil and empty slices,
// structs, and pointers.
func Fill(x interface{}) interface{} {
v := reflect.Indirect(reflect.ValueOf(x))
switch v.Kind() {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
obj := v.Index(i)
objPt := reflect.NewAt(obj.Type(), unsafe.Pointer(obj.UnsafeAddr())).Interface()
objPt = Fill(objPt)
obj.Set(reflect.ValueOf(objPt))
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := reflect.Indirect(v.Field(i))
if !f.CanSet() {
continue
}
switch f.Kind() {
case reflect.Slice:
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
case reflect.Struct:
switch f.Type() {
case coinType:
coin := reflect.New(coinType).Interface()
s := reflect.ValueOf(coin).Elem()
f.Set(s)
case coinsType:
coins := reflect.New(coinsType).Interface()
s := reflect.ValueOf(coins).Elem()
f.Set(s)
default:
objPt := reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Interface()
s := Fill(objPt)
f.Set(reflect.ValueOf(s))
}
}
}
}
return reflect.Indirect(v).Interface()
}
2 changes: 2 additions & 0 deletions x/clp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
GetCmdProviderDistributionParams(queryRoute),
GetCmdSwapFeeParams(queryRoute),
GetCmdPoolShareEstimate(queryRoute),
GetCmdListRewardsBucket(),
GetCmdShowRewardsBucket(),
)
return clpQueryCmd
}
Expand Down
Loading

0 comments on commit 40bb2c3

Please sign in to comment.