Skip to content

Commit

Permalink
feat: Create and test AddOracleProposal. (#615)
Browse files Browse the repository at this point in the history
* feat(pricefeed): Create and test AddOracleProposal

* test(common): TestAssetPairs_Contains

* refactor: filter for unique oracles in NewGenesisState

* Make proposal work for multiple oracles

* test(pricefeed): TestMarshalAddOracleProposal

* small import fix

* refactor: Improve query variable names and remove unnecessary token0, token1 attributes on market
  • Loading branch information
Unique-Divine authored Jun 26, 2022
1 parent 4b68e2c commit 98a2316
Show file tree
Hide file tree
Showing 23 changed files with 1,413 additions and 410 deletions.
7 changes: 6 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"

pricefeedcli "github.com/NibiruChain/nibiru/x/pricefeed/client/cli"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
Expand Down Expand Up @@ -151,6 +153,8 @@ var (
distrclient.ProposalHandler,
upgradeclient.ProposalHandler,
upgradeclient.CancelProposalHandler,
pricefeedcli.AddOracleProposalHandler,
// pricefeedcli.RemoveOracleProposalHandler, // TODO
ibcclientclient.UpdateClientProposalHandler,
ibcclientclient.UpgradeProposalHandler,
),
Expand Down Expand Up @@ -476,7 +480,8 @@ func NewNibiruApp(
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper))
AddRoute(ibcclienttypes.RouterKey, ibcclient.NewClientProposalHandler(app.IBCKeeper.ClientKeeper)).
AddRoute(pricefeedtypes.RouterKey, pricefeed.NewPricefeedProposalHandler(app.PricefeedKeeper))

app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/NibiruChain/nibiru

go 1.18
go 1.17

require (
github.com/cosmos/cosmos-sdk v0.45.5
Expand Down
74 changes: 74 additions & 0 deletions go.sum

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions proto/pricefeed/gov.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";
package nibiru.pricefeed.v1;

import "gogoproto/gogo.proto";

option go_package = "github.com/NibiruChain/nibiru/x/pricefeed/types";

message AddOracleProposal {
string title = 1;
string description = 2;
repeated string oracles = 3 [(gogoproto.moretags) = "yaml:\"oracles\""];
repeated string pairs = 4 [(gogoproto.moretags) = "yaml:\"pairs\""];
}
29 changes: 14 additions & 15 deletions proto/pricefeed/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ service Query {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/oracles/{pair_id}";
}

// QueryPairs queries all pairs
rpc QueryPairs(QueryPairsRequest) returns (QueryPairsResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/pairs";
// QueryMarkets queries all markets
rpc QueryMarkets(QueryMarketsRequest) returns (QueryMarketsResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/markets";
}
}

Expand Down Expand Up @@ -112,15 +112,16 @@ message QueryOraclesResponse {
repeated string oracles = 1;
}

// QueryPairsRequest is the request type for the Query/Pairs RPC method.
message QueryPairsRequest {}
// QueryMarketsRequest is the request type for the Query/Pairs RPC method.
message QueryMarketsRequest {}

// QueryPairsResponse is the response type for the Query/Pairs RPC method.
message QueryPairsResponse {
// QueryMarketsResponse is the response type for the Query/Pairs RPC method.
message QueryMarketsResponse {
option (gogoproto.goproto_getters) = false;

// List of 'PairResponse'
repeated PairResponse pairs = 1 [(gogoproto.castrepeated) = "PairResponses", (gogoproto.nullable) = false];
repeated Market markets = 1 [
(gogoproto.castrepeated) = "Markets",
(gogoproto.nullable) = false];
}

// PostedPriceResponse defines a price for 'PairID' posted by a specific oracle.
Expand All @@ -138,11 +139,9 @@ message CurrentPriceResponse {
string price = 2 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
}

// PairResponse defines an asset in the pricefeed.
message PairResponse {
// Market defines an asset in the pricefeed.
message Market {
string pair_id = 1 [(gogoproto.customname) = "PairID"];
string token0 = 2;
string token1 = 3;
repeated string oracles = 4;
bool active = 5;
repeated string oracles = 2;
bool active = 3;
}
15 changes: 7 additions & 8 deletions x/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,10 @@ func NewAssetPairs(pairStrings ...string) (pairs AssetPairs) {
return pairs
}

// Contains checks if a token pair is contained within 'Pairs'
func (pairs AssetPairs) Contains(pair AssetPair) bool {
for _, element := range pairs {
if (element.Token0 == pair.Token0) && (element.Token1 == pair.Token1) {
return true
}
}
return false
isContained, _ := pairs.ContainsAtIndex(pair)
return isContained
}

func (pairs AssetPairs) Strings() []string {
Expand All @@ -170,7 +167,7 @@ func (pairs AssetPairs) Strings() []string {
func (pairs AssetPairs) Validate() error {
seenPairs := make(map[string]bool)
for _, pair := range pairs {
pairID := SortedPairNameFromDenoms([]string{pair.Token0, pair.Token1})
pairID := pair.String()
if seenPairs[pairID] {
return fmt.Errorf("duplicate pair %s", pairID)
}
Expand All @@ -182,7 +179,9 @@ func (pairs AssetPairs) Validate() error {
return nil
}

// Contains checks if a token pair is contained within 'Pairs'
// ContainsAtIndex checks if a token pair is contained within 'Pairs' and
// a boolean for this condition alongside the corresponding index of 'pair' in
// the slice of pairs.
func (pairs AssetPairs) ContainsAtIndex(pair AssetPair) (bool, int) {
for idx, element := range pairs {
if (element.Token0 == pair.Token0) && (element.Token1 == pair.Token1) {
Expand Down
19 changes: 19 additions & 0 deletions x/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/x/common"
Expand Down Expand Up @@ -180,3 +181,21 @@ func TestAssetPair_Marshaling(t *testing.T) {
})
}
}

func TestAssetPairs_Contains(t *testing.T) {
pairs := common.AssetPairs{
common.PairBTCStable, common.PairETHStable,
}

pair := common.PairGovStable
isContained, atIdx := pairs.ContainsAtIndex(pair)
assert.False(t, isContained)
assert.Equal(t, -1, atIdx)
assert.False(t, pairs.Contains(pair))

pair = pairs[0]
isContained, atIdx = pairs.ContainsAtIndex(pair)
assert.True(t, isContained)
assert.Equal(t, 0, atIdx)
assert.True(t, pairs.Contains(pair))
}
Loading

0 comments on commit 98a2316

Please sign in to comment.