marketmap

package
v2.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 21 Imported by: 22

README

x/marketmap

Contents

Concepts

The x/marketmap module encapsulates a system for creating and updating a unified configuration that is stored on-chain and consumed by a set of oracle service providers (Connect oracle, etc.).

The core goal of the system is to collect off-chain market updates and to post them on chain, informing oracle service providers to fetch prices for new markets.

The data is stored in a MarketMap data structure which can be queried and consumed by oracle services.

Integration

When integrating x/marketmap into your Cosmos SDK application, some considerations must be made:

Module Hooks

Integrating modules can use the hooks exposed by x/marketmap to update their state whenever changes are made to the marketmap.

An example of this can be seen in x/oracle's implementation of the AfterMarketCreated hook. This hook triggers the creation of a CurrencyPairState that corresponds to the new Ticker that was created in the marketmap. This allows for a unified flow where updates to the market map prepare the x/oracle module for new price feeds.

Genesis Order

Any modules that integrate with x/marketmap must set their InitGenesis to occur before the x/marketmap module's InitGenesis. This is so that logic any consuming modules may want to implement in AfterMarketGenesis will be run properly.

State

MarketMap

The market map data is as follows:

// Market encapsulates a Ticker and its provider-specific configuration.
message Market {
  option (gogoproto.goproto_stringer) = false;
  option (gogoproto.stringer) = false;

  // Ticker represents a price feed for a given asset pair i.e. BTC/USD. The
  // price feed is scaled to a number of decimal places and has a minimum number
  // of providers required to consider the ticker valid.
  Ticker ticker = 1 [ (gogoproto.nullable) = false ];

  // ProviderConfigs is the list of provider-specific configs for this Market.
  repeated ProviderConfig provider_configs = 2 [ (gogoproto.nullable) = false ];
}

// Ticker represents a price feed for a given asset pair i.e. BTC/USD. The price
// feed is scaled to a number of decimal places and has a minimum number of
// providers required to consider the ticker valid.
message Ticker {
  option (gogoproto.goproto_stringer) = false;
  option (gogoproto.stringer) = false;

  // CurrencyPair is the currency pair for this ticker.
  connect.types.v2.CurrencyPair currency_pair = 1 [ (gogoproto.nullable) = false ];

  // Decimals is the number of decimal places for the ticker. The number of
  // decimal places is used to convert the price to a human-readable format.
  uint64 decimals = 2;

  // MinProviderCount is the minimum number of providers required to consider
  // the ticker valid.
  uint64 min_provider_count = 3;

  // Enabled is the flag that denotes if the Ticker is enabled for price
  // fetching by an oracle.
  bool enabled = 14;

  // MetadataJSON is a string of JSON that encodes any extra configuration
  // for the given ticker.
  string metadata_JSON = 15;
}

message ProviderConfig {
  // Name corresponds to the name of the provider for which the configuration is
  // being set.
  string name = 1;

  // OffChainTicker is the off-chain representation of the ticker i.e. BTC/USD.
  // The off-chain ticker is unique to a given provider and is used to fetch the
  // price of the ticker from the provider.
  string off_chain_ticker = 2;

  // NormalizeByPair is the currency pair for this ticker to be normalized by.
  // For example, if the desired Ticker is BTC/USD, this market could be reached
  // using: OffChainTicker = BTC/USDT NormalizeByPair = USDT/USD This field is
  // optional and nullable.
  connect.types.v2.CurrencyPair normalize_by_pair = 3;

  // Invert is a boolean indicating if the BASE and QUOTE of the market should
  // be inverted. i.e. BASE -> QUOTE, QUOTE -> BASE
  bool invert = 4;

  // MetadataJSON is a string of JSON that encodes any extra configuration
  // for the given provider config.
  string metadata_JSON = 15;
}

// MarketMap maps ticker strings to their Markets.
message MarketMap {
  option (gogoproto.goproto_stringer) = false;
  option (gogoproto.stringer) = false;

  // Markets is the full list of tickers and their associated configurations
  // to be stored on-chain.
  map<string, Market> markets = 1 [ (gogoproto.nullable) = false ];
}

The MarketMap message itself is not stored in state. Rather, ticker strings are used as key prefixes so that the data can be stored in a map-like structure, while retaining determinism.

Params

The x/marketmap module stores its params in the keeper state. The params can be updated with governance or the keeper authority address.

The x/marketmap module contains the following parameters:

| Key | Type | Example | | MarketAuthorities | []string | "cosmos1vq93x443c0fznuf6...q4jd28ke6r46p999s0" |

MarketAuthority

A MarketAuthority is the bech32 address that is permitted to submit market updates to the chain.

Events

The marketmap module emits the following events:

CreateMarket
Attribute Key Attribute Value
currency_pair {CurrencyPair}
decimals {uint64}
min_provider_count {uint64}
metadata {json string}

Hooks

Other modules can register routines to execute after a certain event has occurred in x/marketmap. The following hooks can be registered:

AfterMarketCreated
  • AfterMarketCreated(ctx sdk.Context, ticker marketmaptypes.Market) error
    • Called after a new market is created in CreateMarket message server.
AfterMarketUpdated
  • AfterMarketUpdated(ctx sdk.Context, ticker marketmaptypes.Market) error
    • Called after a new market is updated in UpdateMarket message server.
AfterMarketGenesis
  • AfterMarketGenesis(ctx sdk.Context, tickers map[string]marketmaptypes.Market) error
    • Called at the end of InitGenesis for the x/marketmap keeper.

Client

gRPC

A user can query the marketmap module using gRPC endpoints.

MarketMap

The MarketMap endpoint queries the full state of the market map as well as associated information such as LastUpdated.

Example:

grpcurl -plaintext localhost:9090 connect.marketmap.v2.Query/MarketMap

Example response:

{
  "marketMap": {
    "tickers": {
      "BITCOIN/USD": {
        "currencyPair": {
          "Base": "BITCOIN",
          "Quote": "USD"
        },
        "decimals": "8",
        "minProviderCount": "3"
      }
    },
    "paths": {
      "BITCOIN/USD": {
        "paths": [
          {
            "operations": [
              {
                "currencyPair": {
                  "Base": "BITCOIN",
                  "Quote": "USD"
                }
              }
            ]
          }
        ]
      }
    },
    "providers": {
      "BITCOIN/USD": {
        "providers": [
          {
            "name": "kucoin",
            "offChainTicker": "btc_usd"
          },
          {
            "name": "mexc",
            "offChainTicker": "btc-usd"
          },
          {
            "name": "binance",
            "offChainTicker": "BTCUSD"
          }
        ]
      }
    }
  },
  "lastUpdated": "1"
}
LastUpdated

The LastUpdated endpoint queries the last block height that the market map was updated. This can be consumed by oracle service providers to recognize when their local configurations must be updated using the heavier MarketMap query.

Example:

grpcurl -plaintext localhost:9090 connect.marketmap.v2.Query/LastUpdated

Example response:

{
  "lastUpdated": "1"
}
Params

The params query allows users to query values set as marketmap parameters.

Example:

grpcurl -plaintext localhost:9090 connect.marketmap.v2.Query/Params

Example response:

{
  "params": {
    "marketAuthorities": "[cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn]"
  }
}
CLI

A user can query the marketmap module using the CLI.

MarketMap

The MarketMap endpoint queries the full state of the market map as well as associated information such as LastUpdated and Version.

Example:

  connectd q marketmap market-map
LastUpdated

The LastUpdated query queries the last block height that the market map was updated. This can be consumed by oracle service providers to recognize when their local configurations must be updated using the heavier MarketMap query.

Example:

  connectd q marketmap last-updated
Params

The params query allows users to query values set as marketmap parameters.

Example:

  connectd q marketmap params

Documentation

Index

Constants

View Source
const ConsensusVersion = 1

ConsensusVersion is the x/marketmap module's current version, as modules integrate and updates are made, this value determines what version of the module is being run by the chain.

Variables

This section is empty.

Functions

func InvokeSetMarketMapHooks

func InvokeSetMarketMapHooks(
	config *marketmapmodulev1.Module,
	keeper *keeper.Keeper,
	hooks map[string]types.MarketMapHooksWrapper,
) error

InvokeSetMarketMapHooks uses the module config to set the hooks on the module.

Types

type AppModule

type AppModule struct {
	AppModuleBasic
	// contains filtered or unexported fields
}

AppModule is the actual app module for x/marketmap.

func NewAppModule

func NewAppModule(cdc codec.Codec, k *keeper.Keeper) AppModule

NewAppModule constructs a new application module for the x/marketmap module.

func (AppModule) BeginBlock

func (am AppModule) BeginBlock(_ context.Context) error

BeginBlock is a no-op for x/marketmap.

func (AppModule) ConsensusVersion

func (AppModule) ConsensusVersion() uint64

ConsensusVersion implements AppModule/ConsensusVersion.

func (AppModule) EndBlock

func (am AppModule) EndBlock(_ context.Context) error

EndBlock is a no-op for x/marketmap.

func (AppModule) ExportGenesis

func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage

ExportGenesis returns the oracle module's exported genesis state as raw JSON bytes. This method panics on any error.

func (AppModule) InitGenesis

func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage)

InitGenesis performs the genesis initialization for the x/marketmap module. It determines the genesis state to initialize from via a json-encoded genesis-state. This method returns no validator set updates. This method panics on any errors.

func (AppModule) IsAppModule

func (am AppModule) IsAppModule()

IsAppModule implements the appmodule.AppModule interface. It is a no-op.

func (AppModule) IsOnePerModuleType

func (am AppModule) IsOnePerModuleType()

IsOnePerModuleType implements the depinject.OnePerModuleType interface.

func (AppModule) RegisterInvariants

func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry)

RegisterInvariants registers the invariants of the marketmap module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted). No invariants exist for the marketmap module.

func (AppModule) RegisterServices

func (am AppModule) RegisterServices(cfc module.Configurator)

RegisterServices registers the module's services with the app's module configurator.

type AppModuleBasic

type AppModuleBasic struct {
	// contains filtered or unexported fields
}

AppModuleBasic is the base struct for the x/marketmap module. It implements the module.AppModuleBasic interface.

func (AppModuleBasic) DefaultGenesis

func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage

DefaultGenesis returns default genesis state as raw bytes for the marketmap module.

func (AppModuleBasic) GetQueryCmd

func (amb AppModuleBasic) GetQueryCmd() *cobra.Command

GetQueryCmd returns the x/marketmap module base query cli-command.

func (AppModuleBasic) GetTxCmd

func (amb AppModuleBasic) GetTxCmd() *cobra.Command

GetTxCmd is a no-op, as no txs are registered for submission (apart from messages that can only be executed by governance).

func (AppModuleBasic) Name

func (amb AppModuleBasic) Name() string

Name returns the canonical name of the module.

func (AppModuleBasic) RegisterGRPCGatewayRoutes

func (amb AppModuleBasic) RegisterGRPCGatewayRoutes(cliCtx client.Context, mux *runtime.ServeMux)

RegisterGRPCGatewayRoutes registers the necessary REST routes for the GRPC-gateway to the x/marketmap module QueryService on mux. This method panics on failure.

func (AppModuleBasic) RegisterInterfaces

func (amb AppModuleBasic) RegisterInterfaces(ir codectypes.InterfaceRegistry)

RegisterInterfaces registers the necessary implementations / interfaces in the x/marketmap module w/ the interface-registry ir.

func (AppModuleBasic) RegisterLegacyAminoCodec

func (amb AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino)

RegisterLegacyAminoCodec registers the necessary types from the x/marketmap module for amino serialization.

func (AppModuleBasic) ValidateGenesis

ValidateGenesis performs genesis state validation for the marketmap module.

type Inputs

type Inputs struct {
	depinject.In

	// module dependencies
	Config       *marketmapmodulev1.Module
	Cdc          codec.Codec
	StoreService store.KVStoreService
}

Inputs contains the dependencies required for module construction.

type Outputs

type Outputs struct {
	depinject.Out

	MarketMapKeeper *keeper.Keeper
	Module          appmodule.AppModule
}

Outputs defines the constructor outputs for the module.

func ProvideModule

func ProvideModule(in Inputs) Outputs

ProvideModule is the depinject constructor for the module.

Directories

Path Synopsis
client
cli
Package types is a reverse proxy.
Package types is a reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳