Skip to content

Commit

Permalink
refactor: asset pair refactor and tests for testutil/testapp (#653)
Browse files Browse the repository at this point in the history
* feat: governance proposal type for adding oracles

* govclient handler and cli command #wip

* govclient handler and cli command #wip

* test(pricefeed): TestAddOracleProposalFromJson

* linter + cleanup

* register codec for govtype

* cli(pricefeed): proposal initialization with cli command #wip

* debug(cli_test.go) #wip

* debug cli #wip

* add flag from

* test(pricefeed): TestWhitelistOraclesForPairs

* (pricefeed): proto and types changes

* (common): Make AssetPair a proto type; write Validate methods and new utilities

* refactor: update whitelist functions. (2) Create KV store for oracles and active pairs #wip

* fix(pricefeed) #wip

* fix,feat(pricefeed): SetMany. (2) Fix keeper_test.go

* refactor: Group and refactor grpc_query price files

* more fixes and quality improvements

* fix: expected keepers interfaces for pricefeed

* fix: get compilation and linter passing. Tests are #wip

* fix,tests(pricefeed): keeper_test, params_test passing; coverage increased to 75% #wip

* test(pricefeed/keeper): all keeper tests passing after refactor

* fix: corrected stablecoin and pricefeed following asset pair changes

* fix(common)

* test(pricefeed): Short tests passing; stablecoin and perp are #wip

* fix(stablecoin): abci_test.go fixed

* fix(perp,vpool): refactor functions to use the proto AssetPair

* fix: Change references to AssetPair.String to use AsString

* fix(ibc_test.go): Use asset pair proto

* test: all integration tests passing

* refactor: improve variable names for codec.ProtoMarshalers and add comments

* refactor: Clean up AssetPair and AssetPairs to function like sdk.Coin and sdk.Coins

* refactor: move http and rest logic from gov_client.go to rest.go

* finish leftover TODO items; add comments

* refactor: AsString -> String using option (gogoproto.goproto_stringer_all) = false

* refactor: Make common pairs for ETH and BTC. Replace UST with USDC.

* feat(pricefeed): Replace GetAuthorizedAddresses with GetOraclesForPairs

* (app):register proposal handler in govRouter

* (cli_test.go): Verify AddOracle txResp validity and query proposal status

* prepend rpc query methods with 'Query'

* (cli_test.go): Move proposal to vote status by meeting min deposit

* refactor: remove AssetPair.Name function

* linter

* refactor: use common.AssetPair proto type in the pricefeed params

* refactor: Return oraclesMap from GetOraclesForPairs

* add pricefeedcli.AddOracleProposalHandler to gov app.ModuleBasics

* refactor: Remove AddOracleProposalWithDeposit and use govcli flag instead. 2. Use AssetPair for PostPrice

* fix #wip resolve wiring and cli test flags inconsistency

* feat(testutil/testapp): Utility for initializing network with configurable genesis

* refactor: standardize testapp imports

* fix(pricefeed): Tests passing with accepted governance vote  20 sec vote time

* test(pricefeed/.../cli_test): Finish query TODOs in CmdAddOracleProposal

* fix(rest.go): Proposer field was not populated during rest handler

* fix: Remove CmdAddOracleProposal from pricefeed tx command

* refactor: strip away governance proposal logic and wiring

* refactor(testutil/testapp): Add comments and cleanup unused code

* removed: remainder of add oracle proposal wiring

* changes to address PR #653 comments

Co-authored-by: Agent Smith <[email protected]>
  • Loading branch information
Unique-Divine and AgentSmithMatrix authored Jun 25, 2022
1 parent 28e50de commit 4b68e2c
Show file tree
Hide file tree
Showing 143 changed files with 3,674 additions and 2,817 deletions.
26 changes: 16 additions & 10 deletions app/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
"github.com/stretchr/testify/suite"

"github.com/NibiruChain/nibiru/app"
"github.com/NibiruChain/nibiru/x/common"
pricefeedtypes "github.com/NibiruChain/nibiru/x/pricefeed/types"
testutilapp "github.com/NibiruChain/nibiru/x/testutil/app"
"github.com/NibiruChain/nibiru/x/testutil/sample"
"github.com/NibiruChain/nibiru/x/testutil/testapp"
)

/* SetupTestingApp returns the TestingApp and default genesis state used to
Expand All @@ -29,23 +30,28 @@ func SetupNibiruTestingApp() (
defaultGenesis map[string]json.RawMessage,
) {
// create testing app
nibiruApp, ctx := testutilapp.NewNibiruApp(true)
token0, token1 := "uatom", "unibi"
nibiruApp, ctx := testapp.NewNibiruAppAndContext(true)

// Whitelist a pair and oracle
pair, err := common.NewAssetPair("uatom:unibi")
if err != nil {
return nil, defaultGenesis
}
oracle := sample.AccAddress()
nibiruApp.PricefeedKeeper.SetParams(ctx, pricefeedtypes.Params{
Pairs: []pricefeedtypes.Pair{
{Token0: token0, Token1: token1,
Oracles: []sdk.AccAddress{oracle}, Active: true},
},
Pairs: common.AssetPairs{pair},
})
_, err := nibiruApp.PricefeedKeeper.SetPrice(
ctx, oracle, token0, token1, sdk.OneDec(),
nibiruApp.PricefeedKeeper.WhitelistOracles(ctx, []sdk.AccAddress{oracle})

_, err = nibiruApp.PricefeedKeeper.SetPrice(
ctx, oracle, pair.String(), sdk.OneDec(),
ctx.BlockTime().Add(time.Hour),
)
if err != nil {
return nil, defaultGenesis
}
err = nibiruApp.PricefeedKeeper.SetCurrentPrices(ctx, token0, token1)

err = nibiruApp.PricefeedKeeper.SetCurrentPrices(ctx, pair.Token0, pair.Token1)
if err != nil {
return nil, defaultGenesis
}
Expand Down
17 changes: 17 additions & 0 deletions proto/common/common.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package nibiru.common;

import "gogoproto/gogo.proto";

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

option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;
option (gogoproto.goproto_stringer_all) = false;

message AssetPair {
option (gogoproto.goproto_getters) = false;
string token0 = 1;
string token1 = 2;
}
8 changes: 5 additions & 3 deletions proto/pricefeed/genesis.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package NibiruChain.pricefeed.v1;
package nibiru.pricefeed.v1;

import "gogoproto/gogo.proto";
import "pricefeed/params.proto";
Expand All @@ -10,6 +10,8 @@ option go_package = "github.com/NibiruChain/nibiru/x/pricefeed/types";
message GenesisState {
// params defines all the paramaters of the module.
Params params = 1 [(gogoproto.nullable) = false];

repeated PostedPrice posted_prices = 2 [(gogoproto.castrepeated) = "PostedPrices", (gogoproto.nullable) = false];
repeated PostedPrice posted_prices = 2 [
(gogoproto.castrepeated) = "PostedPrices",
(gogoproto.nullable) = false];
repeated string genesis_oracles = 3;
}
38 changes: 23 additions & 15 deletions proto/pricefeed/params.proto
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
syntax = "proto3";
package NibiruChain.pricefeed.v1;
package nibiru.pricefeed.v1;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";
import "common/common.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/NibiruChain/nibiru/x/pricefeed/types";
option (gogoproto.equal_all) = true;
option (gogoproto.verbose_equal_all) = true;

// Params defines the parameters for the module.
// Params defines the parameters for the x/pricefeed module.
message Params {
repeated Pair pairs = 1 [(gogoproto.castrepeated) = "Pairs", (gogoproto.nullable) = false];
// Pairs is the list of valid trading pairs for the module.
// Add new trading pairs
repeated common.AssetPair pairs = 1 [(gogoproto.nullable) = false];
}

// Pair defines an asset in the pricefeed.
message Pair {
string token0 = 1;
string token1 = 2;
repeated bytes oracles = 3 [
// OraclesMarshaler is a codec.ProtoMarshaler for an oracles array in the
// OraclesState sdk.KVStore.
message OraclesMarshaler {
repeated bytes oracles = 1 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"
];
bool active = 4;
}

// ActivePairMarshaler is a codec.ProtoMarshaler for the "IsActive" status of
// a pair in the ActivePairState sdk.KVStore.
message ActivePairMarshaler {
bool is_active = 1;
}

// PostedPrice defines a price for an asset pair posted by a specific oracle.
message PostedPrice {
string pair_id = 1 [(gogoproto.customname) = "PairID"];
bytes oracle_address = 2 [
(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"
];
string price = 3 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false];
google.protobuf.Timestamp expiry = 4 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false];
string oracle = 2;
string price = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];
google.protobuf.Timestamp expiry = 4 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false];
}

// CurrentPrice defines the current price for an asset pair in the pricefeed
Expand Down
26 changes: 13 additions & 13 deletions proto/pricefeed/query.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package NibiruChain.pricefeed.v1;
package nibiru.pricefeed.v1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
Expand All @@ -13,33 +13,33 @@ option (gogoproto.verbose_equal_all) = true;

// Query defines the gRPC querier service for pricefeed module
service Query {
// Params queries all parameters of the pricefeed module.
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
// QueryParams queries all parameters of the pricefeed module.
rpc QueryParams(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/params";
}

// Price queries price details for a pair
rpc Price(QueryPriceRequest) returns (QueryPriceResponse) {
// QueryPrice queries price details for a pair
rpc QueryPrice(QueryPriceRequest) returns (QueryPriceResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/prices/{pair_id}";
}

// Prices queries all prices
rpc Prices(QueryPricesRequest) returns (QueryPricesResponse) {
// QueryPrices queries all prices
rpc QueryPrices(QueryPricesRequest) returns (QueryPricesResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/prices";
}

// RawPrices queries all raw prices for an asset pair
rpc RawPrices(QueryRawPricesRequest) returns (QueryRawPricesResponse) {
// QueryRawPrices queries all raw prices for an asset pair
rpc QueryRawPrices(QueryRawPricesRequest) returns (QueryRawPricesResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/rawprices/{pair_id}";
}

// Oracles queries all oracles for an asset pair
rpc Oracles(QueryOraclesRequest) returns (QueryOraclesResponse) {
// QueryOracles queries all oracles for an asset pair
rpc QueryOracles(QueryOraclesRequest) returns (QueryOraclesResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/oracles/{pair_id}";
}

// Pairs queries all pairs
rpc Pairs(QueryPairsRequest) returns (QueryPairsResponse) {
// QueryPairs queries all pairs
rpc QueryPairs(QueryPairsRequest) returns (QueryPairsResponse) {
option (google.api.http).get = "/nibiru/pricefeed/v1beta1/pairs";
}
}
Expand Down
2 changes: 1 addition & 1 deletion proto/pricefeed/tx.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package NibiruChain.pricefeed.v1;
package nibiru.pricefeed.v1;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
Expand Down
4 changes: 2 additions & 2 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
simulationtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"

testutilapp "github.com/NibiruChain/nibiru/x/testutil/app"
"github.com/NibiruChain/nibiru/x/testutil/testapp"
)

// Profile with:
Expand Down Expand Up @@ -52,7 +52,7 @@ func fullAppSimulation(tb testing.TB, is_testing bool) {
}
}()

nibiru := testutilapp.NewTestApp( /*shouldUseDefaultGenesis*/ true)
nibiru := testapp.NewNibiruApp( /*shouldUseDefaultGenesis*/ true)

// Run randomized simulation:
_, simParams, simErr := simulation.SimulateFromSeed(
Expand Down
22 changes: 22 additions & 0 deletions x/common/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package common

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

func AddrsToStrings(addrs ...sdk.AccAddress) []string {
var addrStrings []string
for _, addr := range addrs {
addrStrings = append(addrStrings, addr.String())
}
return addrStrings
}

func StringsToAddrs(strs ...string) []sdk.AccAddress {
var addrs []sdk.AccAddress
for _, str := range strs {
addr := sdk.MustAccAddressFromBech32(str)
addrs = append(addrs, addr)
}
return addrs
}
Loading

0 comments on commit 4b68e2c

Please sign in to comment.