Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Parse out CMC markets from MMU and convert to CMC only market map #603

Merged
merged 7 commits into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading