Skip to content

Commit 860d859

Browse files
committed
add oracle extension
1 parent 612fb22 commit 860d859

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

x/oracle/extenstion.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package oracle
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"time"
8+
9+
"cosmossdk.io/math"
10+
abci "github.com/cometbft/cometbft/abci/types"
11+
sdk "github.com/cosmos/cosmos-sdk/types"
12+
)
13+
14+
type OracleVoteExtension struct {
15+
Height int64
16+
Prices map[string]math.LegacyDec
17+
}
18+
19+
type VoteExtHandler struct {
20+
logger log.Logger
21+
currentBlock int64 // current block height
22+
lastPriceSyncTS time.Time // last time we synced prices
23+
providerTimeout time.Duration // timeout for fetching prices from providers
24+
// providers map[string]Provider // mapping of provider name to provider (e.g. Binance -> BinanceProvider)
25+
// providerPairs map[string][]keeper.CurrencyPair // mapping of provider name to supported pairs (e.g. Binance -> [ATOM/USD])
26+
27+
// Keeper keeper.Keeper // keeper of our oracle module
28+
}
29+
30+
func (h *VoteExtHandler) ExtendVoteHandler() sdk.ExtendVoteHandler {
31+
return func(ctx sdk.Context, req *abci.RequestExtendVote) (*abci.ResponseExtendVote, error) {
32+
// here we'd have a helper function that gets all the prices and does a weighted average using the volume of each market
33+
// prices := h.getAllVolumeWeightedPrices()
34+
35+
// voteExt := OracleVoteExtension{
36+
// Height: req.Height,
37+
// Prices: prices,
38+
// }
39+
40+
bz := []byte{}
41+
// bz, err := json.Marshal(voteExt)
42+
// if err != nil {
43+
// return nil, fmt.Errorf("failed to marshal vote extension: %w", err)
44+
// }
45+
46+
return &abci.ResponseExtendVote{VoteExtension: bz}, nil
47+
}
48+
}
49+
50+
func (h *VoteExtHandler) VerifyVoteExtensionHandler() sdk.VerifyVoteExtensionHandler {
51+
return func(ctx sdk.Context, req *abci.RequestVerifyVoteExtension) (*abci.ResponseVerifyVoteExtension, error) {
52+
var voteExt OracleVoteExtension
53+
err := json.Unmarshal(req.VoteExtension, &voteExt)
54+
if err != nil {
55+
return nil, fmt.Errorf("failed to unmarshal vote extension: %w", err)
56+
}
57+
58+
if voteExt.Height != req.Height {
59+
return nil, fmt.Errorf("vote extension height does not match request height; expected: %d, got: %d", req.Height, voteExt.Height)
60+
}
61+
62+
// Verify incoming prices from a validator are valid. Note, verification during
63+
// VerifyVoteExtensionHandler MUST be deterministic. For brevity and demo
64+
// purposes, we omit implementation.
65+
// if err := h.verifyOraclePrices(ctx, voteExt.Prices); err != nil {
66+
// return nil, fmt.Errorf("failed to verify oracle prices from validator %X: %w", req.ValidatorAddress, err)
67+
// }
68+
69+
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_ACCEPT}, nil
70+
}
71+
}
72+
73+
type ProposalHandler struct {
74+
logger log.Logger
75+
// keeper keeper.Keeper // our oracle module keeper
76+
// valStore baseapp.ValidatorStore // to get the current validators' pubkeys
77+
}
78+
type StakeWeightedPrices struct {
79+
StakeWeightedPrices map[string]math.LegacyDec
80+
ExtendedCommitInfo abci.ExtendedCommitInfo
81+
}
82+
83+
func (h *ProposalHandler) PrepareProposal() sdk.PrepareProposalHandler {
84+
return func(ctx sdk.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
85+
// err := baseapp.ValidateVoteExtensions(ctx, h.valStore, req.Height, ctx.ChainID(), req.LocalLastCommit)
86+
// if err != nil {
87+
// return nil, err
88+
// }
89+
return nil, nil
90+
}
91+
}
92+
93+
func (h *ProposalHandler) ProcessProposal() sdk.ProcessProposalHandler {
94+
return func(ctx sdk.Context, req *abci.RequestProcessProposal) (*abci.ResponseProcessProposal, error) {
95+
return nil, nil
96+
}
97+
}
98+
99+
func (h *ProposalHandler) PreBlocker(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
100+
return nil, nil
101+
}

0 commit comments

Comments
 (0)