-
Notifications
You must be signed in to change notification settings - Fork 870
Update DEX TWAP endpoint #19
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| syntax = "proto3"; | ||
| package seiprotocol.seichain.dex; | ||
|
|
||
| import "gogoproto/gogo.proto"; | ||
| import "dex/pair.proto"; | ||
|
|
||
| option go_package = "github.com/sei-protocol/sei-chain/x/dex/types"; | ||
|
|
||
| message Price { | ||
|
|
||
| uint64 snapshotTimestampInSeconds = 1; | ||
| string price = 2 [ | ||
| (gogoproto.moretags) = "yaml:\"price\"", | ||
| (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", | ||
| (gogoproto.nullable) = false | ||
| ]; | ||
| Pair pair = 3; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| syntax = "proto3"; | ||
| package seiprotocol.seichain.dex; | ||
|
|
||
| import "gogoproto/gogo.proto"; | ||
| import "dex/pair.proto"; | ||
|
|
||
| option go_package = "github.com/sei-protocol/sei-chain/x/dex/types"; | ||
|
|
||
|
|
||
| message Twap { | ||
|
|
||
| uint64 lastEpoch = 1; | ||
| repeated uint64 prices = 2; | ||
| uint64 twapPrice = 3; | ||
| string priceDenom = 4; | ||
| string assetDenom = 5; | ||
|
|
||
| Pair pair = 1; | ||
| string twap = 2 [ | ||
| (gogoproto.moretags) = "yaml:\"twap\"", | ||
| (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", | ||
| (gogoproto.nullable) = false | ||
| ]; | ||
| uint64 lookbackSeconds = 3; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/client" | ||
| "github.com/cosmos/cosmos-sdk/client/flags" | ||
| "github.com/sei-protocol/sei-chain/x/dex/types" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var _ = strconv.Itoa(0) | ||
|
|
||
| func CmdGetPrice() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "get-prices [contract-address] [price-denom] [asset-denom]", | ||
| Short: "Query getPrices", | ||
| Args: cobra.ExactArgs(3), | ||
| RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
| reqContractAddr := args[0] | ||
| reqPriceDenom, _, err := types.GetDenomFromStr(args[1]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| reqAssetDenom, _, err := types.GetDenomFromStr(args[2]) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| clientCtx, err := client.GetClientTxContext(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| queryClient := types.NewQueryClient(clientCtx) | ||
|
|
||
| params := &types.QueryGetPricesRequest{ | ||
| ContractAddr: reqContractAddr, | ||
| PriceDenom: reqPriceDenom, | ||
| AssetDenom: reqAssetDenom, | ||
| } | ||
|
|
||
| res, err := queryClient.GetPrices(cmd.Context(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return clientCtx.PrintProto(res) | ||
| }, | ||
| } | ||
|
|
||
| flags.AddQueryFlagsToCmd(cmd) | ||
|
|
||
| return cmd | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/client" | ||
| "github.com/cosmos/cosmos-sdk/client/flags" | ||
| "github.com/sei-protocol/sei-chain/x/dex/types" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var _ = strconv.Itoa(0) | ||
|
|
||
| func CmdGetTwaps() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "get-twaps [contract-address] [lookback]", | ||
| Short: "Query getPrice", | ||
| Args: cobra.ExactArgs(2), | ||
| RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
| reqContractAddr := args[0] | ||
| reqLookback, err := strconv.ParseUint(args[1], 10, 64) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| clientCtx, err := client.GetClientTxContext(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| queryClient := types.NewQueryClient(clientCtx) | ||
|
|
||
| params := &types.QueryGetTwapsRequest{ | ||
| ContractAddr: reqContractAddr, | ||
| LookbackSeconds: reqLookback, | ||
| } | ||
|
|
||
| res, err := queryClient.GetTwaps(cmd.Context(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return clientCtx.PrintProto(res) | ||
| }, | ||
| } | ||
|
|
||
| flags.AddQueryFlagsToCmd(cmd) | ||
|
|
||
| return cmd | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package keeper | ||
|
|
||
| import ( | ||
| "context" | ||
| "sort" | ||
|
|
||
| sdk "github.com/cosmos/cosmos-sdk/types" | ||
| "github.com/sei-protocol/sei-chain/x/dex/types" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| func (k Keeper) GetTwaps(goCtx context.Context, req *types.QueryGetTwapsRequest) (*types.QueryGetTwapsResponse, error) { | ||
| if req == nil { | ||
| return nil, status.Error(codes.InvalidArgument, "invalid request") | ||
| } | ||
|
|
||
| ctx := sdk.UnwrapSDKContext(goCtx) | ||
| allRegisteredPairs := k.GetAllRegisteredPairs(ctx, req.ContractAddr) | ||
| twaps := []*types.Twap{} | ||
| for _, pair := range allRegisteredPairs { | ||
| prices := k.GetAllPrices(ctx, req.ContractAddr, pair) | ||
| twaps = append(twaps, &types.Twap{ | ||
| Pair: &pair, | ||
| Twap: calculateTwap(ctx, prices, req.LookbackSeconds), | ||
| LookbackSeconds: req.LookbackSeconds, | ||
| }) | ||
| } | ||
|
|
||
| return &types.QueryGetTwapsResponse{ | ||
| Twaps: twaps, | ||
| }, nil | ||
| } | ||
|
|
||
| func calculateTwap(ctx sdk.Context, prices []*types.Price, lookback uint64) sdk.Dec { | ||
| // sort prices in descending order to start iteration from the latest | ||
| sort.Slice(prices, func(p1, p2 int) bool { | ||
| return prices[p1].SnapshotTimestampInSeconds > prices[p2].SnapshotTimestampInSeconds | ||
| }) | ||
| var timeTraversed uint64 = 0 | ||
| var weightedPriceSum sdk.Dec = sdk.ZeroDec() | ||
| for _, price := range prices { | ||
| newTimeTraversed := uint64(ctx.BlockTime().Unix()) - price.SnapshotTimestampInSeconds | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just double check ctx.BlockTime() is static (i.e. block start time) right?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah it's set during BeginBlock based on block proposal (so consistent across all nodes) |
||
| if newTimeTraversed > lookback { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in this case, wouldn't we exclude any data from the first time interval? Lets say for example we had data at T-61 and then T-59 ... T-0, when calculating the TWAP, it would technically only represent the weighted price sum for T-59 - T-0, right? because we would break on T-61 and never include data for the first interval?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's true. We should align on the behavior in such case between dex twap and oracle twap. Still factoring in T-61 but only assign a time weight of
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that makes sense to me, currently my implementation also has the missing first interval issue, but I can modify it to keep the first out of bounds data point for calculating to the start of the TWAP window
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will approve once this change is in
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pushed the fix as well as a unit test for this case |
||
| weightedPriceSum = weightedPriceSum.Add( | ||
| price.Price.MulInt64(int64(lookback - timeTraversed)), | ||
| ) | ||
| timeTraversed = lookback | ||
| break | ||
| } | ||
| weightedPriceSum = weightedPriceSum.Add( | ||
| price.Price.MulInt64(int64(newTimeTraversed - timeTraversed)), | ||
| ) | ||
| timeTraversed = newTimeTraversed | ||
| } | ||
| if timeTraversed == 0 { | ||
| return sdk.ZeroDec() | ||
| } else { | ||
| return weightedPriceSum.QuoInt64(int64(timeTraversed)) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the ordering won't change, we could consider using a priority queue to reduce number of sorts . If number of prices is pretty small then probably doesn't matter