Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit d98f859
Author: Eric <eric.warehime@gmail.com>
Date:   Mon Mar 11 12:46:53 2024 -0700

    Update sample pregenesis

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 7f178fe
Author: Mohammed Affan <affanmd@nyu.edu>
Date:   Mon Mar 11 13:46:08 2024 -0400

    [OTE-209] Emit metrics gated through execution mode (dydxprotocol#1157)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 47e365d
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Mon Mar 11 13:43:16 2024 -0400

    add aggregate comlink response code stats (dydxprotocol#1162)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 7774ad9
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Fri Mar 8 17:30:49 2024 -0500

    [TRA-70] Add state migrations for isolated markets (dydxprotocol#1155)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 89c7405
Author: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Date:   Thu Mar 7 17:28:06 2024 -0500

    [CT-517] E2E tests batch cancel (dydxprotocol#1149)

    * more end to end test

    * extraprint

    * more e2e test

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 41a3a41
Author: Teddy Ding <teddy@dydx.exchange>
Date:   Thu Mar 7 15:42:30 2024 -0500

    [OTE-200] OIMF protos (dydxprotocol#1125)

    * OIMF protos

    * add default genesis value, modify methods interface

    * fix genesis file

    * fix integration test

    * lint

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 2a062b1
Author: Teddy Ding <teddy@dydx.exchange>
Date:   Thu Mar 7 15:24:15 2024 -0500

    Add `v5` upgrade handler and set up container upgrade test (dydxprotocol#1153)

    * wip

    * update preupgrade_genesis

    * fix preupgrade_genesis.json

    * nit

    * setupUpgradeStoreLoaders for v5.0.0

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b7942b3
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Thu Mar 7 13:43:48 2024 -0500

    [CT-647] construct the initial orderbook snapshot (dydxprotocol#1147)

    * [CT-647] construct the initial orderbook snapshot

    * [CT-647] initialize new streams and send orderbook snapshot (dydxprotocol#1152)

    * [CT-647] initialize new streams and send orderbook snapshot

    * use sync once

    * comments

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit c67a3c6
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Thu Mar 7 12:40:37 2024 -0500

    [TRA-84] Move SA module address transfers to use perpetual based SA accounts (dydxprotocol#1146)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit dba23e0
Author: Mohammed Affan <affanmd@nyu.edu>
Date:   Thu Mar 7 10:34:11 2024 -0500

    update readme link to point to the right page (dydxprotocol#1151)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b5870d5
Author: Tian <tian@dydx.exchange>
Date:   Wed Mar 6 16:43:04 2024 -0500

    [TRA-86] scaffold x/vault (dydxprotocol#1148)

    * scaffold x/vault

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 0eca041
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Wed Mar 6 10:48:42 2024 -0500

    [CT-652] add command line flag for full node streaming (dydxprotocol#1145)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b319cb8
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Tue Mar 5 21:58:35 2024 -0500

    [CT-646] stream offchain updates through stream manager (dydxprotocol#1138)

    * [CT-646] stream offchain updates through stream manager

    * comments

    * fix lint

    * get rid of finished

    * comments

    * comments

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 1c54620
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Tue Mar 5 16:34:19 2024 -0500

    [TRA-78] Add function to retrieve collateral pool addr for a subaccount (dydxprotocol#1142)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit b8c1d62
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Tue Mar 5 15:03:28 2024 -0500

    [OTE-141] implement post /compliance/geoblock (dydxprotocol#1129)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit ab8c570
Author: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Date:   Tue Mar 5 11:19:53 2024 -0500

    Fix mock-gen dydxprotocol#1140

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 12506a1
Author: shrenujb <98204323+shrenujb@users.noreply.github.com>
Date:   Mon Mar 4 21:33:28 2024 -0500

    [TRA-64] Use market specific insurance fund for cross or isolated markets (dydxprotocol#1132)

    Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 929f09e
Author: Jonathan Fung <121899091+jonfung-dydx@users.noreply.github.com>
Date:   Mon Mar 4 13:48:04 2024 -0800

    [CT-514] Clob `MsgBatchCancel` functionality (dydxprotocol#1110)

    * wip implementation

    * use new cometbft

    * Revert "use new cometbft"

    This reverts commit e5b8a03.

    * go mod tidy

    * basic e2e test

    * more msgBatchCancels in code

    * repeated fixed32 -> uint32

    * remove debug prints

    * update cometbft replace go.mod sha

    * one more debug print

    * typo

    * regen indexer protos

    * update comment on proto

    * proto comment changes

    * extract stateful validation into own fn

    * pr format comments

    * clean up test file

    * new return type with success and failure

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 41de83e
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Mon Mar 4 12:22:16 2024 -0500

    add index to address read replica lag (dydxprotocol#1137)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 735d9a8
Author: dydxwill <119354122+dydxwill@users.noreply.github.com>
Date:   Mon Mar 4 11:56:59 2024 -0500

    rename (dydxprotocol#1136)

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 86617dd
Author: jayy04 <103467857+jayy04@users.noreply.github.com>
Date:   Mon Mar 4 10:43:31 2024 -0500

    [CT-644] instantiate grpc stream manager (dydxprotocol#1134)

    * [CT-644] instantiate grpc stream manager

    * update type

    * update channel type

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit 32afd64
Author: Eric <eric.warehime@gmail.com>
Date:   Mon Mar 11 12:41:06 2024 -0700

    Update go version in Dockerfile

    Signed-off-by: Eric <eric.warehime@gmail.com>

commit ba27204
Author: Eric <eric.warehime@gmail.com>
Date:   Fri Mar 8 09:44:04 2024 -0800

    Add slinky utils, use that to convert between market and currency pair

commit 667a804
Author: Eric <eric.warehime@gmail.com>
Date:   Wed Mar 6 20:43:40 2024 -0800

    Update error messages

commit d53292c
Author: Eric <eric.warehime@gmail.com>
Date:   Wed Mar 6 20:16:01 2024 -0800

    Update docstrings, rename OracleClient

commit daad125
Author: Eric <eric.warehime@gmail.com>
Date:   Mon Mar 4 10:51:23 2024 -0800

    VoteExtension slinky logic
  • Loading branch information
Eric-Warehime committed Mar 12, 2024
1 parent 525bb6f commit c50ed6e
Show file tree
Hide file tree
Showing 20 changed files with 1,235 additions and 229 deletions.
15 changes: 12 additions & 3 deletions protocol/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NB: This is a digest for a multi-arch manifest list, you will want to get this by running
# `docker buildx imagetools inspect golang:1.21-alpine`
ARG GOLANG_1_21_ALPINE_DIGEST="926f7f7e1ab8509b4e91d5ec6d5916ebb45155b0c8920291ba9f361d65385806"
ARG GOLANG_1_22_ALPINE_DIGEST="8e96e6cff6a388c2f70f5f662b64120941fcd7d4b89d62fec87520323a316bd9"

# This Dockerfile is a stateless build of the `dydxprotocold` binary as a Docker container.
# It does not include any configuration, state, or genesis information.
Expand All @@ -9,7 +9,7 @@ ARG GOLANG_1_21_ALPINE_DIGEST="926f7f7e1ab8509b4e91d5ec6d5916ebb45155b0c8920291b
# Builder
# --------------------------------------------------------

FROM golang@sha256:${GOLANG_1_21_ALPINE_DIGEST} as builder
FROM golang@sha256:${GOLANG_1_22_ALPINE_DIGEST} as builder
ARG VERSION
ARG COMMIT

Expand Down Expand Up @@ -41,15 +41,24 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
-o /dydxprotocol/build/ \
./...

# Build the oracle binary
WORKDIR /
RUN git clone https://github.com/skip-mev/slinky.git
WORKDIR /slinky
RUN make build

# --------------------------------------------------------
# Runner
# --------------------------------------------------------

FROM golang@sha256:${GOLANG_1_21_ALPINE_DIGEST}
FROM golang@sha256:${GOLANG_1_22_ALPINE_DIGEST}

RUN apk add --no-cache bash

COPY --from=builder /dydxprotocol/build/dydxprotocold /bin/dydxprotocold
COPY --from=builder /slinky/build/oracle /bin/slinky
COPY --from=builder /slinky/config/local/oracle.json /etc/oracle.json
COPY --from=builder /slinky/config/local/market.json /etc/market.json

ENV HOME /dydxprotocol
WORKDIR $HOME
Expand Down
28 changes: 28 additions & 0 deletions protocol/app/process/market_price_decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package process

import (
sdk "github.com/cosmos/cosmos-sdk/types"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

// MarketPriceDecoder is an interface for decoding market price transactions, This interface is responsible
// for distinguishing between logic for unmarshalling MarketPriceUpdates, between MarketPriceUpdates
// determined by the proposer's price-cache, and from VoteExtensions.
type UpdateMarketPriceTxDecoder interface {
// DecodeUpdateMarketPricesTx decodes the tx-bytes from the RequestProcessProposal and returns a MarketPriceUpdateTx.
DecodeUpdateMarketPricesTx(ctx sdk.Context, txs [][]byte) (*UpdateMarketPricesTx, error)

// GetTxOffset returns the offset that other injected txs should be placed with respect to their normally
// expected indices. This method is used to account for injected vote-extensions, or any other injected
// txs from dependencies.
GetTxOffset(ctx sdk.Context) int
}

func NewUpdateMarketPricesTx(
ctx sdk.Context, pk ProcessPricesKeeper, msg *pricestypes.MsgUpdateMarketPrices) *UpdateMarketPricesTx {
return &UpdateMarketPricesTx{
ctx: ctx,
pricesKeeper: pk,
msg: msg,
}
}
18 changes: 18 additions & 0 deletions protocol/app/vote_extensions/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package vote_extensions

import (
sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/skip-mev/slinky/pkg/types"

pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

// PricesKeeper is the expected interface for the x/price keeper used by the vote extension handlers
type PricesKeeper interface {
GetCurrencyPairFromID(ctx sdk.Context, id uint64) (cp oracletypes.CurrencyPair, found bool)
GetValidMarketPriceUpdates(ctx sdk.Context) *pricestypes.MsgUpdateMarketPrices
UpdateMarketPrices(
ctx sdk.Context,
updates []*pricestypes.MsgUpdateMarketPrices_MarketPrice,
) (err error)
}
62 changes: 62 additions & 0 deletions protocol/app/vote_extensions/extend_vote_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package vote_extensions

import (
"fmt"

cometabci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dydxprotocol/v4-chain/protocol/app/process"
prices "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

// ExtendVoteHandler is a wrapper around the Slinky ExtendVoteHandler. This wrapper is responsible for
// applying the newest MarketPriceUpdates in a block so that the prices to be submitted in a vote extension are
// determined on the latest available information.
type ExtendVoteHandler struct {
SlinkyExtendVoteHandler sdk.ExtendVoteHandler
PricesTxDecoder process.UpdateMarketPriceTxDecoder
PricesKeeper PricesKeeper
}

// ExtendVoteHandler returns a sdk.ExtendVoteHandler, responsible for the following:
// 1. Decoding the x/prices MsgUpdateMarketPrices in the current block - fail on errors
// 2. Validating the proposed MsgUpdateMarketPrices in accordance with the ProcessProposal check
// 3. Updating the market prices in the PricesKeeper so that the GetValidMarketPriceUpdates function returns the
// latest available market prices
// 4. Calling the Slinky ExtendVoteHandler to handle the rest of ExtendVote
//
// See https://github.com/skip-mev/slinky/blob/a5b1d3d3a2723e4746b5d588c512d7cc052dc0ff/abci/ve/vote_extension.go#L77
// for the Slinky ExtendVoteHandler logic.
func (e *ExtendVoteHandler) ExtendVoteHandler() sdk.ExtendVoteHandler {
return func(ctx sdk.Context, req *cometabci.RequestExtendVote) (resp *cometabci.ResponseExtendVote, err error) {
// Decode the x/prices txn in the current block
updatePricesTx, err := e.PricesTxDecoder.DecodeUpdateMarketPricesTx(ctx, req.Txs)
if err != nil {
return nil, fmt.Errorf("DecodeMarketPricesTx failure %w", err)
}

// ensure that the proposed MsgUpdateMarketPrices is valid in accordance w/ stateful information
// this check is equivalent to the check in ProcessProposal (indexPriceCache has not been updated)
err = updatePricesTx.Validate()
if err != nil {
return nil, fmt.Errorf("updatePricesTx.Validate failure %w", err)
}

// Update the market prices in the PricesKeeper, so that the GetValidMarketPriceUpdates
// function returns the latest available market prices.
updateMarketPricesMsg, ok := updatePricesTx.GetMsg().(*prices.MsgUpdateMarketPrices)
if !ok {
return nil, fmt.Errorf("expected %s, got %T", "MsgUpdateMarketPrices", updateMarketPricesMsg)
}

// Update the market prices in the PricesKeeper
err = e.PricesKeeper.UpdateMarketPrices(ctx, updateMarketPricesMsg.MarketPriceUpdates)
if err != nil {
return nil, fmt.Errorf("failed to update market prices in extend vote handler pre-slinky invocation %w", err)
}

// Call the Slinky ExtendVoteHandler
return e.SlinkyExtendVoteHandler(ctx, req)
}
}
133 changes: 133 additions & 0 deletions protocol/app/vote_extensions/extend_vote_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package vote_extensions

import (
"fmt"
"testing"

cometabci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/dydxprotocol/v4-chain/protocol/app/process"
"github.com/dydxprotocol/v4-chain/protocol/mocks"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
)

func TestExtendVoteHandlerDeecodeMarketPricesFailure(t *testing.T) {
slinkyEvh := mocks.NewExtendVoteHandler(t)
pricesTxDecoder := mocks.NewUpdateMarketPriceTxDecoder(t)
pricesKeeper := mocks.NewPricesKeeper(t)
evh := ExtendVoteHandler{
SlinkyExtendVoteHandler: slinkyEvh.Execute,
PricesTxDecoder: pricesTxDecoder,
PricesKeeper: pricesKeeper,
}

pricesTxDecoder.On("DecodeUpdateMarketPricesTx", mock.Anything, mock.Anything).Return(
nil, fmt.Errorf("foobar"))
_, err := evh.ExtendVoteHandler()(sdk.Context{}, &cometabci.RequestExtendVote{Txs: make([][]byte, 0)})

require.ErrorContains(t, err, "DecodeMarketPricesTx failure foobar")
pricesTxDecoder.AssertExpectations(t)
pricesKeeper.AssertExpectations(t)
slinkyEvh.AssertExpectations(t)
}

func TestExtendVoteHandlerUpdatePricesTxValidateFailure(t *testing.T) {
slinkyEvh := mocks.NewExtendVoteHandler(t)
pricesTxDecoder := mocks.NewUpdateMarketPriceTxDecoder(t)
pricesKeeper := mocks.NewPricesKeeper(t)
evh := ExtendVoteHandler{
SlinkyExtendVoteHandler: slinkyEvh.Execute,
PricesTxDecoder: pricesTxDecoder,
PricesKeeper: pricesKeeper,
}

pricesTxDecoder.On("DecodeUpdateMarketPricesTx", mock.Anything, mock.Anything).Return(
process.NewUpdateMarketPricesTx(sdk.Context{}, pricesKeeper, constants.InvalidMsgUpdateMarketPricesStateless),
nil)
_, err := evh.ExtendVoteHandler()(sdk.Context{}, &cometabci.RequestExtendVote{Txs: make([][]byte, 0)})

require.ErrorContains(t, err, "updatePricesTx.Validate failure")
pricesTxDecoder.AssertExpectations(t)
pricesKeeper.AssertExpectations(t)
slinkyEvh.AssertExpectations(t)
}

func TestExtendVoteHandlerUpdateMarketPricesError(t *testing.T) {
slinkyEvh := mocks.NewExtendVoteHandler(t)
pricesTxDecoder := mocks.NewUpdateMarketPriceTxDecoder(t)
pricesKeeper := mocks.NewPricesKeeper(t)
evh := ExtendVoteHandler{
SlinkyExtendVoteHandler: slinkyEvh.Execute,
PricesTxDecoder: pricesTxDecoder,
PricesKeeper: pricesKeeper,
}

pricesTxDecoder.On("DecodeUpdateMarketPricesTx", mock.Anything, mock.Anything).Return(
process.NewUpdateMarketPricesTx(sdk.Context{}, pricesKeeper, constants.EmptyMsgUpdateMarketPrices),
nil)
pricesKeeper.On("PerformStatefulPriceUpdateValidation", mock.Anything, mock.Anything, mock.Anything).
Return(nil)
pricesKeeper.On("UpdateMarketPrices", mock.Anything, mock.Anything).
Return(fmt.Errorf(""))
_, err := evh.ExtendVoteHandler()(sdk.Context{}, &cometabci.RequestExtendVote{Txs: make([][]byte, 0)})

require.ErrorContains(t, err, "failed to update market prices in extend vote handler pre-slinky invocation")
pricesTxDecoder.AssertExpectations(t)
pricesKeeper.AssertExpectations(t)
slinkyEvh.AssertExpectations(t)
}

func TestExtendVoteHandlerSlinkyFailure(t *testing.T) {
slinkyEvh := mocks.NewExtendVoteHandler(t)
pricesTxDecoder := mocks.NewUpdateMarketPriceTxDecoder(t)
pricesKeeper := mocks.NewPricesKeeper(t)
evh := ExtendVoteHandler{
SlinkyExtendVoteHandler: slinkyEvh.Execute,
PricesTxDecoder: pricesTxDecoder,
PricesKeeper: pricesKeeper,
}

pricesTxDecoder.On("DecodeUpdateMarketPricesTx", mock.Anything, mock.Anything).Return(
process.NewUpdateMarketPricesTx(sdk.Context{}, pricesKeeper, constants.EmptyMsgUpdateMarketPrices),
nil)
pricesKeeper.On("PerformStatefulPriceUpdateValidation", mock.Anything, mock.Anything, mock.Anything).
Return(nil)
pricesKeeper.On("UpdateMarketPrices", mock.Anything, mock.Anything).Return(nil)
slinkyEvh.On("Execute", mock.Anything, mock.Anything).
Return(&cometabci.ResponseExtendVote{}, fmt.Errorf("slinky failure"))
_, err := evh.ExtendVoteHandler()(sdk.Context{}, &cometabci.RequestExtendVote{Txs: make([][]byte, 0)})

require.ErrorContains(t, err, "slinky failure")
pricesTxDecoder.AssertExpectations(t)
pricesKeeper.AssertExpectations(t)
slinkyEvh.AssertExpectations(t)
}

func TestExtendVoteHandlerSlinkySuccess(t *testing.T) {
slinkyEvh := mocks.NewExtendVoteHandler(t)
pricesTxDecoder := mocks.NewUpdateMarketPriceTxDecoder(t)
pricesKeeper := mocks.NewPricesKeeper(t)
evh := ExtendVoteHandler{
SlinkyExtendVoteHandler: slinkyEvh.Execute,
PricesTxDecoder: pricesTxDecoder,
PricesKeeper: pricesKeeper,
}

pricesTxDecoder.On("DecodeUpdateMarketPricesTx", mock.Anything, mock.Anything).Return(
process.NewUpdateMarketPricesTx(sdk.Context{}, pricesKeeper, constants.EmptyMsgUpdateMarketPrices),
nil)
pricesKeeper.On("PerformStatefulPriceUpdateValidation", mock.Anything, mock.Anything, mock.Anything).
Return(nil)
pricesKeeper.On("UpdateMarketPrices", mock.Anything, mock.Anything).Return(nil)
slinkyEvh.On("Execute", mock.Anything, mock.Anything).
Return(&cometabci.ResponseExtendVote{}, nil)
_, err := evh.ExtendVoteHandler()(sdk.Context{}, &cometabci.RequestExtendVote{Txs: make([][]byte, 0)})

require.NoError(t, err)
pricesTxDecoder.AssertExpectations(t)
pricesKeeper.AssertExpectations(t)
slinkyEvh.AssertExpectations(t)
}
77 changes: 77 additions & 0 deletions protocol/app/vote_extensions/oracle_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package vote_extensions

import (
"context"
"fmt"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
oracleservicetypes "github.com/skip-mev/slinky/service/servers/oracle/types"
"google.golang.org/grpc"
)

// OraclePrices is an implementation of the Slinky OracleClient interface.
// This object is responsible for requesting prices from the x/prices module, and injecting those prices into the
// vote-extension.
// The
type OraclePrices struct {
PricesKeeper PricesKeeper
}

// NewOraclePrices returns a new OracleClient object.
func NewOraclePrices(pricesKeeper PricesKeeper) *OraclePrices {
return &OraclePrices{
PricesKeeper: pricesKeeper,
}
}

// Start is a no-op.
func (o *OraclePrices) Start(_ context.Context) error {
return nil
}

// Stop is a no-op.
func (o *OraclePrices) Stop() error {
return nil
}

// Prices is called in ExtendVoteHandler to determine which Prices are put into the extended commit.
// This method is responsible for doing the following:
// 1. Get the latest prices from the x/prices module's indexPriceCache via GetValidMarketPriceUpdates
// 2. Translate the response from x/prices into a QueryPricesResponse, and return it.
//
// This method fails if:
// - The passed in context is not an sdk.Context
func (o *OraclePrices) Prices(ctx context.Context,
_ *oracleservicetypes.QueryPricesRequest,
_ ...grpc.CallOption) (*oracleservicetypes.QueryPricesResponse, error) {
sdkCtx, ok := ctx.(sdk.Context)
if !ok {
return nil, fmt.Errorf("OraclePrices was passed on non-sdk context object")
}

// get the final prices to include in the vote-extension from the x/prices module
validUpdates := o.PricesKeeper.GetValidMarketPriceUpdates(sdkCtx)
if validUpdates == nil {
sdkCtx.Logger().Info("prices keeper returned no valid market price updates")
return nil, nil
}
sdkCtx.Logger().Info("prices keeper returned valid updates", "length", len(validUpdates.MarketPriceUpdates))

// translate price updates into oracle response
var outputResponse = &oracleservicetypes.QueryPricesResponse{
Prices: make(map[string]string),
Timestamp: time.Now(),
}
for _, update := range validUpdates.MarketPriceUpdates {
mappedPair, found := o.PricesKeeper.GetCurrencyPairFromID(sdkCtx, uint64(update.GetMarketId()))
if found {
sdkCtx.Logger().Info("added currency pair", "pair", mappedPair.String())
outputResponse.Prices[mappedPair.String()] = strconv.FormatUint(update.Price, 10)
} else {
sdkCtx.Logger().Info("failed to add currency pair", "pair", mappedPair.String())
}
}
return outputResponse, nil
}
Loading

0 comments on commit c50ed6e

Please sign in to comment.