Skip to content

Commit

Permalink
Updating MsgFeedPrice and MsgFeedMultiplePrices (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
avkr003 authored Nov 22, 2024
1 parent 48b7b2c commit 99d34ab
Show file tree
Hide file tree
Showing 42 changed files with 1,318 additions and 692 deletions.
1,414 changes: 951 additions & 463 deletions api/elys/oracle/tx.pulsar.go

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions proto/elys/oracle/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ service Msg {
// this line is used by starport scaffolding # proto/tx/rpc
rpc CreateAssetInfo(MsgCreateAssetInfo) returns (MsgCreateAssetInfoResponse);
}
message MsgFeedPrice {
option (cosmos.msg.v1.signer) = "provider";
option (amino.name) = "oracle/MsgFeedPrice";

message FeedPrice {
string asset = 1;
string price = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string source = 3;
string provider = 4 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}
message MsgFeedPrice {
option (cosmos.msg.v1.signer) = "provider";
option (amino.name) = "oracle/MsgFeedPrice";
string provider = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
FeedPrice feed_price = 2 [ (gogoproto.nullable) = false ];
}

message MsgFeedPriceResponse {}
Expand All @@ -73,7 +77,7 @@ message MsgFeedMultiplePrices {
option (cosmos.msg.v1.signer) = "creator";
option (amino.name) = "oracle/MsgFeedMultiplePrices";
string creator = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
repeated Price prices = 2 [ (gogoproto.nullable) = false ];
repeated FeedPrice feed_prices = 2 [ (gogoproto.nullable) = false ];
}

message MsgFeedMultiplePricesResponse {}
Expand Down
1 change: 0 additions & 1 deletion x/accountedpool/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/accountedpool/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/amm/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/amm/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/assetprofile/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/assetprofile/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/burner/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/burner/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/commitment/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/commitment/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/epochs/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/estaking/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/estaking/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/leveragelp/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/leveragelp/types/tx.pb.go

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

1 change: 0 additions & 1 deletion x/masterchef/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/masterchef/types/tx.pb.go

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

14 changes: 9 additions & 5 deletions x/oracle/keeper/msg_server_feed_multiple_prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/oracle/types"
)
Expand All @@ -20,10 +19,15 @@ func (k msgServer) FeedMultiplePrices(goCtx context.Context, msg *types.MsgFeedM
return nil, types.ErrPriceFeederNotActive
}

for _, price := range msg.Prices {
price.Provider = msg.Creator
price.Timestamp = uint64(ctx.BlockTime().Unix())
price.BlockHeight = uint64(ctx.BlockHeight())
for _, feedPrice := range msg.FeedPrices {
price := types.Price{
Asset: feedPrice.Asset,
Price: feedPrice.Price,
Source: feedPrice.Source,
Provider: msg.Creator,
Timestamp: uint64(ctx.BlockTime().Unix()),
BlockHeight: uint64(ctx.BlockHeight()),
}
k.SetPrice(ctx, price)
}

Expand Down
6 changes: 3 additions & 3 deletions x/oracle/keeper/msg_server_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func (k msgServer) FeedPrice(goCtx context.Context, msg *types.MsgFeedPrice) (*t
}

price := types.Price{
Asset: msg.FeedPrice.Asset,
Price: msg.FeedPrice.Price,
Source: msg.FeedPrice.Source,
Provider: msg.Provider,
Asset: msg.Asset,
Price: msg.Price,
Source: msg.Source,
Timestamp: uint64(ctx.BlockTime().Unix()),
BlockHeight: uint64(ctx.BlockHeight()),
}
Expand Down
10 changes: 6 additions & 4 deletions x/oracle/keeper/msg_server_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ func (suite *KeeperTestSuite) TestPriceMsgServerCreate() {
for i := 0; i < 5; i++ {
expected := &types.MsgFeedPrice{
Provider: creator,
Asset: strconv.Itoa(i),
Source: "elys",
Price: math.LegacyOneDec(),
FeedPrice: types.FeedPrice{
Asset: strconv.Itoa(i),
Source: "elys",
Price: math.LegacyOneDec(),
},
}
_, err := srv.FeedPrice(ctx, expected)
suite.Require().NoError(err)
rst, found := k.GetPrice(ctx, expected.Asset, expected.Source, uint64(ctx.BlockTime().Unix()))
rst, found := k.GetPrice(ctx, expected.FeedPrice.Asset, expected.FeedPrice.Source, uint64(ctx.BlockTime().Unix()))
suite.Require().True(found)
suite.Require().Equal(expected.Provider, rst.Provider)
}
Expand Down
8 changes: 5 additions & 3 deletions x/oracle/simulation/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ func SimulateMsgFeedPrice(
i := r.Int()
msg := &types.MsgFeedPrice{
Provider: simAccount.Address.String(),
Asset: "asset" + strconv.Itoa(i),
Source: types.BAND,
FeedPrice: types.FeedPrice{
Asset: "asset" + strconv.Itoa(i),
Source: types.BAND,
},
}

_, found := k.GetPrice(ctx, msg.Asset, msg.Source, uint64(ctx.BlockTime().Unix()))
_, found := k.GetPrice(ctx, msg.FeedPrice.Asset, msg.FeedPrice.Source, uint64(ctx.BlockTime().Unix()))
if found {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(&types.MsgFeedPrice{}), "Price already exist"), nil, nil
}
Expand Down
32 changes: 4 additions & 28 deletions x/oracle/types/message_feed_multiple_prices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,13 @@ func (msg *MsgFeedMultiplePrices) ValidateBasic() error {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}

if len(msg.Prices) == 0 {
if len(msg.FeedPrices) == 0 {
return fmt.Errorf("no prices provided")
}

for _, price := range msg.Prices {
if price.Price.IsNil() {
return errorsmod.Wrapf(ErrInvalidPrice, "price is nil")
}

if price.Price.IsNegative() {
return errorsmod.Wrapf(ErrInvalidPrice, "price is negative")
}

if err = sdk.ValidateDenom(price.Asset); err != nil {
return err
}

if len(price.Source) == 0 {
return errorsmod.Wrapf(ErrInvalidPrice, "source is empty")
}

if len(price.Provider) == 0 {
return errorsmod.Wrapf(ErrInvalidPrice, "provider is empty")
}

if price.BlockHeight == 0 {
return errorsmod.Wrapf(ErrInvalidPrice, "block height is zero")
}

if price.Timestamp == 0 {
return errorsmod.Wrapf(ErrInvalidPrice, "timestamp is zero")
for _, price := range msg.FeedPrices {
if err = price.Validate(); err != nil {
return errorsmod.Wrapf(ErrInvalidPrice, "invalid price (%s)", err)
}
}
return nil
Expand Down
11 changes: 4 additions & 7 deletions x/oracle/types/message_feed_multiple_prices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ func TestMsgFeedMultiplePrices_ValidateBasic(t *testing.T) {
name: "valid address",
msg: types.MsgFeedMultiplePrices{
Creator: sample.AccAddress(),
Prices: []types.Price{
FeedPrices: []types.FeedPrice{
{
Asset: ptypes.ATOM,
Price: math.LegacyOneDec(),
Source: "source",
Provider: "provider",
Timestamp: 1,
BlockHeight: 1,
Asset: ptypes.ATOM,
Price: math.LegacyOneDec(),
Source: "source",
},
},
},
Expand Down
39 changes: 24 additions & 15 deletions x/oracle/types/messages_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package types
import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -18,32 +17,42 @@ func NewMsgFeedPrice(
) *MsgFeedPrice {
return &MsgFeedPrice{
Provider: creator,
Asset: asset,
Price: price,
Source: source,
FeedPrice: FeedPrice{
Asset: asset,
Price: price,
Source: source,
},
}
}

func (msg *MsgFeedPrice) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Provider)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid provider address (%s)", err)
}

if msg.Price.IsNil() {
func (price FeedPrice) Validate() error {
if price.Price.IsNil() {
return errorsmod.Wrapf(ErrInvalidPrice, "price is nil")
}

if msg.Price.IsNegative() {
if price.Price.IsNegative() {
return errorsmod.Wrapf(ErrInvalidPrice, "price is negative")
}

if err = sdk.ValidateDenom(msg.Asset); err != nil {
if err := sdk.ValidateDenom(price.Asset); err != nil {
return err
}

if len(msg.Source) == 0 {
return fmt.Errorf("source is empty")
if len(price.Source) == 0 {
return errorsmod.Wrapf(ErrInvalidPrice, "source is empty")
}

return nil
}

func (msg *MsgFeedPrice) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Provider)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid provider address (%s)", err)
}

if err = msg.FeedPrice.Validate(); err != nil {
return errorsmod.Wrapf(ErrInvalidPrice, "invalid price (%s)", err)
}

return nil
Expand Down
8 changes: 5 additions & 3 deletions x/oracle/types/messages_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func TestMsgFeedPrice_ValidateBasic(t *testing.T) {
name: "valid address",
msg: types.MsgFeedPrice{
Provider: sample.AccAddress(),
Price: sdkmath.LegacyMustNewDecFromStr("100"),
Asset: ptypes.ATOM,
Source: "source",
FeedPrice: types.FeedPrice{
Price: sdkmath.LegacyMustNewDecFromStr("100"),
Asset: ptypes.ATOM,
Source: "source",
},
},
},
}
Expand Down
1 change: 0 additions & 1 deletion x/oracle/types/query.pb.go

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

Loading

0 comments on commit 99d34ab

Please sign in to comment.