syntax = "proto3";
package osmosis.twap.v1beta1;

import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "cosmos_proto/cosmos.proto";
import "cosmos/base/v1beta1/coin.proto";
import "google/protobuf/timestamp.proto";

option go_package = "github.com/osmosis-labs/osmosis/v13/x/twap/types";

// A TWAP record should be indexed in state by pool_id, (asset pair), timestamp
// The asset pair assets should be lexicographically sorted.
// Technically (pool_id, asset_0_denom, asset_1_denom, height) do not need to
// appear in the struct however we view this as the wrong performance tradeoff
// given SDK today. Would rather we optimize for readability and correctness,
// than an optimal state storage format. The system bottleneck is elsewhere for
// now.
message TwapRecord {
  uint64 pool_id = 1;
  // Lexicographically smaller denom of the pair
  string asset0_denom = 2;
  // Lexicographically larger denom of the pair
  string asset1_denom = 3;
  // height this record corresponds to, for debugging purposes
  int64 height = 4 [
    (gogoproto.moretags) = "yaml:\"record_height\"",
    (gogoproto.jsontag) = "record_height"
  ];
  // This field should only exist until we have a global registry in the state
  // machine, mapping prior block heights within {TIME RANGE} to times.
  google.protobuf.Timestamp time = 5 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true,
    (gogoproto.moretags) = "yaml:\"record_time\""
  ];

  // We store the last spot prices in the struct, so that we can interpolate
  // accumulator values for times between when accumulator records are stored.
  string p0_last_spot_price = 6 [
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable) = false
  ];
  string p1_last_spot_price = 7 [
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable) = false
  ];

  string p0_arithmetic_twap_accumulator = 8 [
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable) = false
  ];
  string p1_arithmetic_twap_accumulator = 9 [
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable) = false
  ];

  string geometric_twap_accumulator = 10 [
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
    (gogoproto.nullable) = false
  ];

  // This field contains the time in which the last spot price error occured.
  // It is used to alert the caller if they are getting a potentially erroneous
  // TWAP, due to an unforeseen underlying error.
  google.protobuf.Timestamp last_error_time = 11 [
    (gogoproto.nullable) = false,
    (gogoproto.stdtime) = true,
    (gogoproto.moretags) = "yaml:\"last_error_time\""
  ];
}
