# State

# PriceState

PriceState is common type to manage cumulative price and latest price along with timestamp for all oracle types.

Copy type PriceState struct { Price sdk.Dec CumulativePrice sdk.Dec Timestamp int64 }

where

  • Price represents the normalized decimal price
  • CumulativePrice represents the cumulative price for a given oracle price feed since the start of the oracle price feed's creation.
  • Timestamp represents the time at which the blocktime at which the price state was relayed.

Note that the CumulativePrice value follows the convention set by the Uniswap V2 Oracle and is used to allows modules to calculate Time-Weighted Average Price (TWAP) between 2 arbitrary block time intervals (t1, t2).

TWAP=CumulativePrice2CumulativePrice1Timestamp2Timestamp1\mathrm{TWAP = \frac{CumulativePrice_2 - CumulativePrice_1}{Timestamp_2 - Timestamp_1}}

# Band

Band price data for a given symbol are represented and stored as follows:

  • BandPriceState: 0x01 | []byte(symbol) -> ProtocolBuffer(BandPriceState)
Copy type BandPriceState struct { Symbol string Rate sdk.Int ResolveTime uint64 Request_ID uint64 PriceState PriceState }

Note that the Rate is the raw USD rate for the Symbol obtained from the Band chain which has is scaled by 1e9 (e.g. a price of 1.42 is 1420000000) while the PriceState has the normalized decimal price (e.g. 1.42).

Band relayers are stored by their address as follows.

  • BandRelayer: 0x02 | RelayerAddr -> []byte{}

# Band IBC

This section describes all the state management to maintain the price by connecting to Band chain via IBC.

  • LatestClientID is maintained to manage unique clientID for band IBC packets. It is increased by 1 when sending price request packet into bandchain.
  • LatestClientID: 0x32 -> Formated(LatestClientID)
  • LatestRequestID is maintained to manage unique BandIBCOracleRequests. Incremented by 1 when creating a new BandIBCOracleRequest.
  • LatestRequestID: 0x36 -> Formated(LatestRequestID)
  • Band IBC price data for a given symbol is stored as follows:
  • BandPriceState: 0x31 | []byte(symbol) -> ProtocolBuffer(BandPriceState)
Copy type BandPriceState struct { Symbol string Rate sdk.Int ResolveTime uint64 Request_ID uint64 PriceState PriceState }
  • BandIBCCallDataRecord is stored as follows when sending price request packet into bandchain:
  • CalldataRecord: 0x33 | []byte(ClientId) -> ProtocolBuffer(CalldataRecord)
Copy type CalldataRecord struct { ClientId uint64 Calldata []byte }
  • BandIBCOracleRequest is stored as follows when the governance configure oracle requests to send:
  • BandOracleRequest: 0x34 | []byte(RequestId) -> ProtocolBuffer(BandOracleRequest)
Copy type BandOracleRequest struct { RequestId uint64 OracleScriptId int64 Symbols []string AskCount uint64 MinCount uint64 FeeLimit sdk.Coins PrepareGas uint64 ExecuteGas uint64 }
  • BandIBCParams is stored as follows and configured by governance:
  • BandIBCParams: 0x35 -> ProtocolBuffer(BandIBCParams)

BandIBCParams contains the information for IBC connection with band chain.

Copy type BandIBCParams struct { BandIbcEnabled bool IbcRequestInterval int64 IbcSourceChannel string IbcVersion string IbcPortId string }

Note:

  1. BandIbcEnabled describes the status of band ibc connection
  2. IbcSourceChannel, IbcVersion, IbcPortId are common parameters required for IBC connection.
  3. IbcRequestInterval describes the automatic price fetch request interval that is automatically triggered on injective chain on beginblocker.

# Coinbase

Coinbase price data for a given symbol ("key") are represented and stored as follows:

  • CoinbasePriceState: 0x21 | []byte(key) -> CoinbasePriceState
Copy type CoinbasePriceState struct { // kind should always be "prices" Kind string // timestamp of the when the price was signed by coinbase Timestamp uint64 // the symbol of the price, e.g. BTC Key string // the value of the price scaled by 1e6 Value uint64 // the price state PriceState PriceState }

More details about the Coinbase price oracle can be found in the Coinbase API docs as well as this explanatory blog post.

Note that the Value is the raw USD price data obtained from Coinbase which has is scaled by 1e6 (e.g. a price of 1.42 is 1420000) while the PriceState has the normalized decimal price (e.g. 1.42).

# Pricefeed

Pricefeed price data for a given base quote pair are represented and stored as follows:

  • PriceFeedInfo: 0x11 + Keccak256Hash(base + quote) -> PriceFeedInfo
Copy type PriceFeedInfo struct { Base string Quote string }
  • PriceFeedPriceState: 0x12 + Keccak256Hash(base + quote) -> PriceFeedPriceState
Copy type PriceFeedState struct { Base string Quote string PriceState *PriceState Relayers []string }
  • PriceFeedRelayer: 0x13 + Keccak256Hash(base + quote) + relayerAddr -> relayerAddr