Skip to content

Commit

Permalink
feat: ugov module (#2050)
Browse files Browse the repository at this point in the history
* feat: LegacyMsg support

* ugov module implementation

* lint
  • Loading branch information
robert-zaremba committed May 17, 2023
1 parent 50f2ded commit 6a6cb90
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 54 deletions.
3 changes: 1 addition & 2 deletions proto/umee/ugov/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ option (gogoproto.goproto_getters_all) = false;

// GenesisState of the ugov module.
message GenesisState {
repeated cosmos.base.v1beta1.Coin min_tx_fees = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];
cosmos.base.v1beta1.DecCoin min_gas_price = 1 [(gogoproto.nullable) = false];
}
42 changes: 42 additions & 0 deletions x/ugov/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ugov

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
"github.com/gogo/protobuf/proto"
)

// Amino codecs
// Note, the ModuleCdc should ONLY be used in certain instances of tests and for JSON
// encoding as Amino is still used for that purpose.
var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)
RegisterLegacyAminoCodec(amino)

amino.Seal()
}

// RegisterLegacyAminoCodec registers the necessary x/uibc interfaces and
// concrete types on the provided LegacyAmino codec. These types are used for
// Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgGovUpdateMinGasPrice{}, proto.MessageName(&MsgGovUpdateMinGasPrice{}), nil)
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations(
(*sdk.Msg)(nil),
&MsgGovUpdateMinGasPrice{},
)

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}
16 changes: 16 additions & 0 deletions x/ugov/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ugov

import (
"github.com/umee-network/umee/v4/util/coin"
)

// DefaultGenesis creates a default genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
MinGasPrice: coin.UmeeDec("0.1"),
}
}

func (gs *GenesisState) Validate() error {
return gs.MinGasPrice.Validate()
}
70 changes: 29 additions & 41 deletions x/ugov/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions x/ugov/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ugov

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/umee-network/umee/v4/util/coin"
)

func TestGenesis(t *testing.T) {
require := require.New(t)
gs := DefaultGenesis()

require.NoError(gs.Validate(), "default genesis must be correct")

gs.MinGasPrice = coin.UtokenDec(coin.Dollar, "0.1")
require.NoError(gs.Validate(), "min_gas_price = 0.1dollar is correct")

gs.MinGasPrice.Amount = sdk.MustNewDecFromStr("-1")
require.Error(gs.Validate(), "negative min_gas_price is NOT correct")
}
10 changes: 5 additions & 5 deletions x/ugov/keeper/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"github.com/umee-network/umee/v4/util/store"
)

func (k Keeper) SetMinGasPrice(p *sdk.DecCoin) error {
return store.SetValue(k.store, keyMinGasPrice, p, "gas_price")
func (k Keeper) SetMinGasPrice(p sdk.DecCoin) error {
return store.SetValue(k.store, keyMinGasPrice, &p, "gas_price")
}

func (k Keeper) MinGasPrice() *sdk.DecCoin {
func (k Keeper) MinGasPrice() sdk.DecCoin {
gp := store.GetValue[*sdk.DecCoin](k.store, keyMinGasPrice, "gas_price")
if gp == nil {
return &coin.Umee0dec
return coin.Umee0dec
}
return gp
return *gp
}
7 changes: 4 additions & 3 deletions x/ugov/keeper/fees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/umee-network/umee/v4/util/coin"
)

Expand All @@ -13,9 +14,9 @@ func TestGasPrice(t *testing.T) {
k := initKeeper(t)

gpOut := k.MinGasPrice()
require.Equal(*gpOut, coin.UmeeDec("0"), "when nothing is set, 0uumee should be returned")
require.Equal(gpOut, coin.UmeeDec("0"), "when nothing is set, 0uumee should be returned")

gp := coin.Atom1_25dec
k.SetMinGasPrice(&gp)
require.Equal(k.MinGasPrice(), &gp)
k.SetMinGasPrice(gp)
require.Equal(k.MinGasPrice(), gp)
}
13 changes: 13 additions & 0 deletions x/ugov/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package keeper

import "github.com/umee-network/umee/v4/x/ugov"

func (k Keeper) ExportGenesis() *ugov.GenesisState {
return &ugov.GenesisState{
MinGasPrice: k.MinGasPrice(),
}
}

func (k Keeper) InitGenesis(gs *ugov.GenesisState) error {
return k.SetMinGasPrice(gs.MinGasPrice)
}
22 changes: 22 additions & 0 deletions x/ugov/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/umee-network/umee/v4/x/ugov"
)

func TestGenesis(t *testing.T) {
t.Parallel()
require := require.New(t)
k := initKeeper(t)

gs := ugov.DefaultGenesis()
err := k.InitGenesis(gs)
require.NoError(err)

gsOut := k.ExportGenesis()
require.Equal(gs, gsOut)
}
2 changes: 1 addition & 1 deletion x/ugov/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (m msgServer) GovUpdateMinGasPrice(ctx context.Context, msg *ugov.MsgGovUpd
}

k := m.kb.Keeper(&sdkCtx)
if err := k.SetMinGasPrice(&msg.MinGasPrice); err != nil {
if err := k.SetMinGasPrice(msg.MinGasPrice); err != nil {
return nil, err
}
sdkutil.Emit(&sdkCtx, &ugov.EventMinTxFees{
Expand Down
8 changes: 6 additions & 2 deletions x/ugov/keeper/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ type Querier struct {
Builder
}

func NewQuerier(kb Builder) Querier {
return Querier{kb}
}

// MinTxFees returns minimum transaction fees.
func (q Querier) MinGasPrice(ctx context.Context, _ *ugov.QueryMinGasPrice) (*ugov.QueryMinGasPriceResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
p := q.Keeper(&sdkCtx).MinGasPrice()
return &ugov.QueryMinGasPriceResponse{MinGasPrice: *p}, nil
return &ugov.QueryMinGasPriceResponse{MinGasPrice: q.Keeper(&sdkCtx).MinGasPrice()},
nil
}
Loading

0 comments on commit 6a6cb90

Please sign in to comment.