Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn authored and ValarDragon committed Feb 16, 2023
1 parent ccc84b3 commit 41a18f3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
20 changes: 14 additions & 6 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,24 +581,32 @@ func (s *IntegrationTestSuite) TestArithmeticTWAP() {
// start time (timeBeforeSwap) is not equal to the block time.
chainA.WaitForNumHeights(2)

// Wait for a bit longer to have a large difference between start time and end time.
chainA.WaitForNumHeights(2)

timeBeforeSwapEnd := chainANode.QueryLatestBlockTime()

// timeBeforeSwapStartTwo = right in betweeen timeBeforeSwap and timeBeforeSwapEnd
timeBeforeSwapStartTwo := timeBeforeSwap.Add(timeBeforeSwapEnd.Sub(timeBeforeSwap) / 2)

s.T().Log("querying for the first TWAP to now before swap")
twapFromBeforeSwapToBeforeSwapOneAB, err := chainANode.QueryArithmeticTwapToNow(poolId, denomA, denomB, timeBeforeSwap)
twapFromBeforeSwapToBeforeSwapOneAB, err := chainANode.QueryArithmeticTwap(poolId, denomA, denomB, timeBeforeSwap, timeBeforeSwapEnd)
s.Require().NoError(err)
twapFromBeforeSwapToBeforeSwapOneBC, err := chainANode.QueryArithmeticTwapToNow(poolId, denomB, denomC, timeBeforeSwap)
twapFromBeforeSwapToBeforeSwapOneBC, err := chainANode.QueryArithmeticTwap(poolId, denomB, denomC, timeBeforeSwap, timeBeforeSwapEnd)
s.Require().NoError(err)
twapFromBeforeSwapToBeforeSwapOneCA, err := chainANode.QueryArithmeticTwapToNow(poolId, denomC, denomA, timeBeforeSwap)
twapFromBeforeSwapToBeforeSwapOneCA, err := chainANode.QueryArithmeticTwap(poolId, denomC, denomA, timeBeforeSwap, timeBeforeSwapEnd)
s.Require().NoError(err)

chainANode.BankSend(coinAIn, chainA.NodeConfigs[0].PublicAddress, swapWalletAddr)
chainANode.BankSend(coinBIn, chainA.NodeConfigs[0].PublicAddress, swapWalletAddr)
chainANode.BankSend(coinCIn, chainA.NodeConfigs[0].PublicAddress, swapWalletAddr)

s.T().Log("querying for the second TWAP to now before swap, must equal to first")
twapFromBeforeSwapToBeforeSwapTwoAB, err := chainANode.QueryArithmeticTwapToNow(poolId, denomA, denomB, timeBeforeSwap.Add(50*time.Millisecond))
twapFromBeforeSwapToBeforeSwapTwoAB, err := chainANode.QueryArithmeticTwap(poolId, denomA, denomB, timeBeforeSwapStartTwo, timeBeforeSwapEnd)
s.Require().NoError(err)
twapFromBeforeSwapToBeforeSwapTwoBC, err := chainANode.QueryArithmeticTwapToNow(poolId, denomB, denomC, timeBeforeSwap.Add(50*time.Millisecond))
twapFromBeforeSwapToBeforeSwapTwoBC, err := chainANode.QueryArithmeticTwap(poolId, denomB, denomC, timeBeforeSwapStartTwo, timeBeforeSwapEnd)
s.Require().NoError(err)
twapFromBeforeSwapToBeforeSwapTwoCA, err := chainANode.QueryArithmeticTwapToNow(poolId, denomC, denomA, timeBeforeSwap.Add(50*time.Millisecond))
twapFromBeforeSwapToBeforeSwapTwoCA, err := chainANode.QueryArithmeticTwap(poolId, denomC, denomA, timeBeforeSwapStartTwo, timeBeforeSwapEnd)
s.Require().NoError(err)

// Since there were no swaps between the two queries, the TWAPs should be the same.
Expand Down
50 changes: 50 additions & 0 deletions x/twap/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,56 @@ Example:
return cmd
}

// GetQueryArithmeticCommand returns an arithmetic twap query command.
func GetTwapRecordAtOrBeforeTime() *cobra.Command {
cmd := &cobra.Command{
Use: "[start time] [end time]",
Short: "Query arithmetic twap",
Aliases: []string{"twap"},
Long: osmocli.FormatLongDescDirect(`Query arithmetic twap for pool. Start time must be unix time. End time can be unix time or duration.
Example:
{{.CommandPrefix}} arithmetic 1 uosmo stake 1667088000 24h
{{.CommandPrefix}} arithmetic 1 uosmo stake 1667088000 1667174400
`, types.ModuleName),
Args: cobra.ExactArgs(5),
RunE: func(cmd *cobra.Command, args []string) error {
// boilerplate parse fields
poolId, baseDenom, quoteDenom, startTime, endTime, err := twapQueryParseArgs(args)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
// quoteDenom, err := getQuoteDenomFromLiquidity(cmd.Context(), clientCtx, poolId, baseDenom)
// if err != nil {
// return err
// }

queryClient := queryproto.NewQueryClient(clientCtx)
res, err := queryClient.ArithmeticTwap(cmd.Context(), &queryproto.ArithmeticTwapRequest{
PoolId: poolId,
BaseAsset: baseDenom,
QuoteAsset: quoteDenom,
StartTime: startTime,
EndTime: &endTime,
})

if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

// getQuoteDenomFromLiquidity gets the quote liquidity denom from the pool. In addition, validates that base denom
// exists in the pool. Fails if not.
func getQuoteDenomFromLiquidity(ctx context.Context, clientCtx client.Context, poolId uint64, baseDenom string) (string, error) {
Expand Down

0 comments on commit 41a18f3

Please sign in to comment.