Skip to content

Commit

Permalink
feat: Parse out CMC markets from MMU and convert to CMC only market m…
Browse files Browse the repository at this point in the history
…ap (#603)
  • Loading branch information
davidterpay authored Jul 17, 2024
1 parent 4496b6c commit 3ff5fbe
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions scripts/genesis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package main

import (
"encoding/json"
"flag"
"fmt"

"github.com/skip-mev/slinky/cmd/constants/marketmaps"
"github.com/skip-mev/slinky/providers/apis/coinmarketcap"
mmtypes "github.com/skip-mev/slinky/x/marketmap/types"
"github.com/skip-mev/slinky/x/marketmap/types/tickermetadata"
)

var (
convertToCMC = flag.Bool("convert-to-cmc", false, "convert to coinmarketcap markets")
marketFile = flag.String("market-config-path", "", "market file to convert to coinmarketcap markets")
useCore = flag.Bool("use-core", false, "use core markets")
useRaydium = flag.Bool("use-raydium", false, "use raydium markets")
useUniswapV3Base = flag.Bool("use-uniswapv3-base", false, "use uniswapv3 base markets")
Expand All @@ -22,6 +27,29 @@ func main() {
// If the user specifies a different market.json, we use that instead.
flag.Parse()

if *convertToCMC {
if *marketFile == "" {
fmt.Fprintf(flag.CommandLine.Output(), "market map config path (market-cfg-path) cannot be empty\n")
panic("market map config path (market-cfg-path) cannot be empty")
}

marketMap, err := mmtypes.ReadMarketMapFromFile(*marketFile)
if err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "failed to read market map from file: %s\n", err)
panic(err)
}

marketMap = filterToOnlyCMCMarkets(marketMap)

// Write the market map back to the original file.
if err := mmtypes.WriteMarketMapToFile(marketMap, *marketFile); err != nil {
fmt.Fprintf(flag.CommandLine.Output(), "failed to write market map to file: %s\n", err)
panic(err)
}

return
}

marketMap := mmtypes.MarketMap{
Markets: make(map[string]mmtypes.Market),
}
Expand Down Expand Up @@ -101,3 +129,49 @@ func mergeMarketMaps(this, other mmtypes.MarketMap) mmtypes.MarketMap {
func providerConfigToKey(cfg mmtypes.ProviderConfig) string {
return cfg.Name + cfg.OffChainTicker
}

// filterToOnlyCMCMarkets is a helper function that filters out all markets that are not from CoinMarketCap. It
// mutates the marketmap to only include CoinMarketCap markets. Notably the CMC ID will be pricing in the base
// asset.
func filterToOnlyCMCMarkets(marketmap mmtypes.MarketMap) mmtypes.MarketMap {
res := mmtypes.MarketMap{
Markets: make(map[string]mmtypes.Market),
}

// Filter out all markets that are not from CoinMarketCap.
for _, market := range marketmap.Markets {
var meta tickermetadata.DyDx
if err := json.Unmarshal([]byte(market.Ticker.Metadata_JSON), &meta); err != nil {
continue
}

var id string
for _, aggregateID := range meta.AggregateIDs {
if aggregateID.Venue == "coinmarketcap" {
id = aggregateID.ID
break
}
}

if len(id) == 0 {
continue
}

resTicker := market.Ticker
resTicker.MinProviderCount = 1

providers := []mmtypes.ProviderConfig{
{
Name: coinmarketcap.Name,
OffChainTicker: id,
},
}

res.Markets[resTicker.CurrencyPair.String()] = mmtypes.Market{
Ticker: resTicker,
ProviderConfigs: providers,
}
}

return res
}

0 comments on commit 3ff5fbe

Please sign in to comment.