Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Historical prices query #64

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion proto/kujira/oracle/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated ExchangeRateTuple exchange_rates = 2
[(gogoproto.castrepeated) = "ExchangeRateTuples", (gogoproto.nullable) = false];
repeated MissCounter miss_counters = 3 [(gogoproto.nullable) = false];
repeated MissCounter miss_counters = 3 [(gogoproto.nullable) = false];
repeated HistoricalExchangeRate historical_exchange_rates = 4 [(gogoproto.nullable) = false];
}

// FeederDelegation is the address for where oracle feeder authority are
Expand Down
28 changes: 28 additions & 0 deletions proto/kujira/oracle/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ message Params {
(gogoproto.castrepeated) = "DenomList",
(gogoproto.nullable) = false
];
repeated PriceIntervalParam exchange_rate_snap_epochs = 10
[ (gogoproto.nullable) = false ];
}

// Denom - the object to hold configurations of each denom
Expand All @@ -71,3 +73,29 @@ message ExchangeRateTuple {
(gogoproto.nullable) = false
];
}

message VoteExtension {
int64 height = 1;
repeated ExchangeRateTuple prices = 2 [
(gogoproto.nullable) = false
];
}

message PriceIntervalParam {
option (gogoproto.equal) = true;

string epoch = 1; // e.g. day
int64 duration = 2; // e.g. 86400
int64 max_count = 3; // e.g. 30
}

message HistoricalExchangeRate {
string epoch = 1;
int64 timestamp = 2;
string denom = 3 [ (gogoproto.moretags) = "yaml:\"denom\"" ];
string exchange_rate = 4 [
(gogoproto.moretags) = "yaml:\"exchange_rate\"",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}
15 changes: 14 additions & 1 deletion proto/kujira/oracle/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ service Query {
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/oracle/params";
}

rpc HistoricalExchangeRates(QueryHistoricalExchangeRatesRequest) returns (QueryHistoricalExchangeRatesResponse) {
option (google.api.http).get = "/oracle/historical_exchange_rates";
}
}

// QueryExchangeRateRequest is the request type for the Query/ExchangeRate RPC method.
Expand Down Expand Up @@ -109,4 +113,13 @@ message QueryParamsRequest {}
message QueryParamsResponse {
// params defines the parameters of the module.
Params params = 1 [(gogoproto.nullable) = false];
}
}

message QueryHistoricalExchangeRatesRequest {
string epoch = 1;
string denom = 2;
}

message QueryHistoricalExchangeRatesResponse {
repeated HistoricalExchangeRate historical_rates = 1 [ (gogoproto.nullable) = false ];
}
8 changes: 5 additions & 3 deletions x/oracle/abci/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"fmt"
"sort"

"cosmossdk.io/core/appmodule"

Check failure on line 9 in x/oracle/abci/proposal.go

View workflow job for this annotation

GitHub Actions / lint

"cosmossdk.io/core/appmodule" imported and not used (typecheck)
"cosmossdk.io/log"
"cosmossdk.io/math"
"github.com/Team-Kujira/core/x/oracle/keeper"
Expand Down Expand Up @@ -248,13 +248,15 @@
if ok {
power := claim.Power

var voteExt OracleVoteExtension
if err := json.Unmarshal(v.VoteExtension, &voteExt); err != nil {
var voteExt types.VoteExtension
if err := voteExt.Unmarshal(v.VoteExtension); err != nil {
h.logger.Error("failed to decode vote extension", "err", err, "validator", fmt.Sprintf("%x", v.Validator.Address))
return votes
}

for base, price := range voteExt.Prices {
for _, tuple := range voteExt.Prices {
base := tuple.Denom
price := tuple.ExchangeRate
tmpPower := power
if !price.IsPositive() {
// Make the power of abstain vote zero
Expand Down
160 changes: 110 additions & 50 deletions x/oracle/abci/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,35 @@ func TestGetBallotByDenom(t *testing.T) {
power := int64(100)

// organize votes by denom
voteExt1 := abci.OracleVoteExtension{
voteExt1 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25000),
"ETH": math.LegacyNewDec(2200),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25000),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2200),
},
},
}
voteExt2 := abci.OracleVoteExtension{
voteExt2 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25030),
"ETH": math.LegacyNewDec(2180),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25030),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2180),
},
},
}
voteExt1Bytes, err := json.Marshal(voteExt1)
voteExt1Bytes, err := voteExt1.Marshal()
require.NoError(t, err)
voteExt2Bytes, err := json.Marshal(voteExt2)
voteExt2Bytes, err := voteExt2.Marshal()
require.NoError(t, err)

consAddrMap := map[string]sdk.ValAddress{
Expand Down Expand Up @@ -159,23 +171,35 @@ func TestComputeStakeWeightedPricesAndMissMap(t *testing.T) {
input, h := SetupTest(t)

// organize votes by denom
voteExt1 := abci.OracleVoteExtension{
voteExt1 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25000),
"ETH": math.LegacyNewDec(2200),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25000),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2200),
},
},
}
voteExt2 := abci.OracleVoteExtension{
voteExt2 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25030),
"ETH": math.LegacyNewDec(2180),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25030),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2180),
},
},
}
voteExt1Bytes, err := json.Marshal(voteExt1)
voteExt1Bytes, err := voteExt1.Marshal()
require.NoError(t, err)
voteExt2Bytes, err := json.Marshal(voteExt2)
voteExt2Bytes, err := voteExt2.Marshal()
require.NoError(t, err)

params := types.DefaultParams()
Expand Down Expand Up @@ -435,23 +459,35 @@ func TestPrepareProposal(t *testing.T) {
require.Error(t, err)

// Valid vote extension data
voteExt1 := abci.OracleVoteExtension{
voteExt1 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25000),
"ETH": math.LegacyNewDec(2200),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25000),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2200),
},
},
}
voteExt2 := abci.OracleVoteExtension{
voteExt2 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25030),
"ETH": math.LegacyNewDec(2180),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25030),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2180),
},
},
}
voteExt1Bytes, err := json.Marshal(voteExt1)
voteExt1Bytes, err := voteExt1.Marshal()
require.NoError(t, err)
voteExt2Bytes, err := json.Marshal(voteExt2)
voteExt2Bytes, err := voteExt2.Marshal()
require.NoError(t, err)
marshalDelimitedFn := func(msg proto.Message) ([]byte, error) {
var buf bytes.Buffer
Expand Down Expand Up @@ -576,23 +612,35 @@ func TestProcessProposal(t *testing.T) {
input.Ctx = input.Ctx.WithConsensusParams(consParams)

// Valid vote extension data
voteExt1 := abci.OracleVoteExtension{
voteExt1 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25000),
"ETH": math.LegacyNewDec(2200),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25000),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2200),
},
},
}
voteExt2 := abci.OracleVoteExtension{
voteExt2 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25030),
"ETH": math.LegacyNewDec(2180),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25030),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2180),
},
},
}
voteExt1Bytes, err := json.Marshal(voteExt1)
voteExt1Bytes, err := voteExt1.Marshal()
require.NoError(t, err)
voteExt2Bytes, err := json.Marshal(voteExt2)
voteExt2Bytes, err := voteExt2.Marshal()
require.NoError(t, err)
marshalDelimitedFn := func(msg proto.Message) ([]byte, error) {
var buf bytes.Buffer
Expand Down Expand Up @@ -779,23 +827,35 @@ func TestPreBlocker(t *testing.T) {
input.Ctx = input.Ctx.WithConsensusParams(consParams)

// Valid vote extension data
voteExt1 := abci.OracleVoteExtension{
voteExt1 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25000),
"ETH": math.LegacyNewDec(2200),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25000),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2200),
},
},
}
voteExt2 := abci.OracleVoteExtension{
voteExt2 := types.VoteExtension{
Height: 1,
Prices: map[string]math.LegacyDec{
"BTC": math.LegacyNewDec(25030),
"ETH": math.LegacyNewDec(2180),
Prices: []types.ExchangeRateTuple{
{
Denom: "BTC",
ExchangeRate: math.LegacyNewDec(25030),
},
{
Denom: "ETH",
ExchangeRate: math.LegacyNewDec(2180),
},
},
}
voteExt1Bytes, err := json.Marshal(voteExt1)
voteExt1Bytes, err := voteExt1.Marshal()
require.NoError(t, err)
voteExt2Bytes, err := json.Marshal(voteExt2)
voteExt2Bytes, err := voteExt2.Marshal()
require.NoError(t, err)
marshalDelimitedFn := func(msg proto.Message) ([]byte, error) {
var buf bytes.Buffer
Expand Down
Loading
Loading