github.com/InjectiveLabs/sdk-go@v1.53.0/client/metadata/fetch_metadata.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/InjectiveLabs/sdk-go/client/common"
     9  	exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange"
    10  	derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb"
    11  
    12  	// derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb"
    13  	"math"
    14  	"strconv"
    15  
    16  	spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb"
    17  )
    18  
    19  var metadataTemplate = `[%s]
    20  description = '%s %s %s'
    21  base = %d
    22  quote = %d
    23  min_price_tick_size = %s
    24  min_display_price_tick_size = %s
    25  min_quantity_tick_size = %s
    26  min_display_quantity_tick_size = %s
    27  
    28  `
    29  var symbolTemplate = `[%s]
    30  peggy_denom = %s
    31  decimals = %s
    32  
    33  `
    34  
    35  func FetchDenom(network common.Network) {
    36  	metadataOutput := ""
    37  	symbols := make(map[string][]string)
    38  	exchangeClient, err := exchangeclient.NewExchangeClient(network)
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	// fetch spot markets
    44  	spotMarketsReq := spotExchangePB.MarketsRequest{MarketStatus: "active"}
    45  	ctx := context.Background()
    46  	spotRes, err := exchangeClient.GetSpotMarkets(ctx, &spotMarketsReq)
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	for _, m := range spotRes.Markets {
    51  		// skip markets that don't have enough metadata
    52  		if m.BaseTokenMeta == nil || m.QuoteTokenMeta == nil {
    53  			continue
    54  		}
    55  		// append symbols to map
    56  		symbols[m.BaseTokenMeta.Symbol] = []string{m.BaseDenom, fmt.Sprintf("%d", m.BaseTokenMeta.Decimals)}
    57  		symbols[m.QuoteTokenMeta.Symbol] = []string{m.BaseDenom, fmt.Sprintf("%d", m.QuoteTokenMeta.Decimals)}
    58  
    59  		// format market metadata into ini entry
    60  		minPriceTickSize, err := strconv.ParseFloat(m.MinPriceTickSize, 64)
    61  		if err != nil {
    62  			panic(err)
    63  		}
    64  		minQuantityTickSize, err := strconv.ParseFloat(m.MinQuantityTickSize, 64)
    65  		if err != nil {
    66  			panic(err)
    67  		}
    68  		minDisplayPriceTickSize := minPriceTickSize / math.Pow(10, float64(m.QuoteTokenMeta.Decimals-m.BaseTokenMeta.Decimals))
    69  		minDisplayQuantityTickSize := minQuantityTickSize / math.Pow(10, float64(m.BaseTokenMeta.Decimals))
    70  		config := fmt.Sprintf(
    71  			metadataTemplate,
    72  			m.MarketId,
    73  			network.Name, "Spot", m.Ticker,
    74  			m.BaseTokenMeta.Decimals,
    75  			m.QuoteTokenMeta.Decimals,
    76  			strconv.FormatFloat(minPriceTickSize, 'f', -1, 64),
    77  			strconv.FormatFloat(minDisplayPriceTickSize, 'f', -1, 64),
    78  			strconv.FormatFloat(minQuantityTickSize, 'f', -1, 64),
    79  			strconv.FormatFloat(minDisplayQuantityTickSize, 'f', -1, 64),
    80  		)
    81  		metadataOutput += config
    82  	}
    83  
    84  	// fetch derivative markets
    85  	derivativeMarketsReq := derivativeExchangePB.MarketsRequest{MarketStatus: "active"}
    86  	derivativeRes, err := exchangeClient.GetDerivativeMarkets(ctx, &derivativeMarketsReq)
    87  	if err != nil {
    88  		panic(err)
    89  	}
    90  	for _, m := range derivativeRes.Markets {
    91  		// skip markets that don't have quote metadata
    92  		if m.QuoteTokenMeta == nil {
    93  			continue
    94  		}
    95  		// append symbols to map
    96  		symbols[m.QuoteTokenMeta.Symbol] = []string{m.QuoteDenom, string(m.QuoteTokenMeta.Decimals)}
    97  		// format market metadata into ini entry
    98  		minPriceTickSize, err := strconv.ParseFloat(m.MinPriceTickSize, 64)
    99  		if err != nil {
   100  			panic(err)
   101  		}
   102  		minQuantityTickSize, err := strconv.ParseFloat(m.MinQuantityTickSize, 64)
   103  		if err != nil {
   104  			panic(err)
   105  		}
   106  		minDisplayPriceTickSize := minPriceTickSize / math.Pow(10, float64(m.QuoteTokenMeta.Decimals))
   107  		config := fmt.Sprintf(
   108  			metadataTemplate,
   109  			m.MarketId,
   110  			network.Name, "Derivative", m.Ticker,
   111  			0,
   112  			m.QuoteTokenMeta.Decimals,
   113  			strconv.FormatFloat(minPriceTickSize, 'f', -1, 64),
   114  			strconv.FormatFloat(minDisplayPriceTickSize, 'f', -1, 64),
   115  			strconv.FormatFloat(minQuantityTickSize, 'f', -1, 64),
   116  			strconv.FormatFloat(minQuantityTickSize, 'f', -1, 64),
   117  		)
   118  		metadataOutput += config
   119  	}
   120  
   121  	// format into ini entry
   122  	for k, v := range symbols {
   123  		symbol := fmt.Sprintf(
   124  			symbolTemplate,
   125  			k, v[0], v[1],
   126  		)
   127  		metadataOutput += symbol
   128  	}
   129  
   130  	fileName := fmt.Sprintf("client/metadata/assets/%s.ini", network.Name)
   131  	err = os.WriteFile(fileName, []byte(metadataOutput), 0600) // nolint:gocritic // 0600 is the correct permission
   132  	if err != nil {
   133  		panic(err)
   134  	}
   135  }
   136  
   137  func main() {
   138  	devnet := common.LoadNetwork("devnet", "")
   139  	testnet := common.LoadNetwork("testnet", "lb")
   140  	mainnet := common.LoadNetwork("mainnet", "lb")
   141  	FetchDenom(devnet)
   142  	FetchDenom(testnet)
   143  	FetchDenom(mainnet)
   144  }