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

Incorrect rate value after adding a new asset #6

Open
jomagalo opened this issue Feb 22, 2024 · 1 comment
Open

Incorrect rate value after adding a new asset #6

jomagalo opened this issue Feb 22, 2024 · 1 comment

Comments

@jomagalo
Copy link

When adding a new asset, the rate value is the last asset added to the array. It is always the same value regardless of the asset you choose.

@jomagalo jomagalo changed the title error in rate adding new asset Incorrect rate value after adding a new asset Feb 22, 2024
@jomagalo
Copy link
Author

for example:

currencyutil.go

package main

type Asset struct {
	AssetHash string
	Precision int
	Ticker    string
	Name      string
}

var currencyToAsset = map[string]Asset{
	"USDT":  {"f3d1ec678811398cd2ae277cbe3849c6f6dbd72c74bc542f7c4b11ff0e820958", 8, "USDT", "Tether USD"},
        "EUR":  {"0671737d2f6e83afaeb2667bc6298e69788681caad303e751479802e439a5f7f", 8, "EUR", "Vulpem EUR"},
	"L-BTC": {"144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49", 8, "L-BTC", "Liquid Bitcoin"},
}

var assetToCurrency = map[string]string{   
	"f3d1ec678811398cd2ae277cbe3849c6f6dbd72c74bc542f7c4b11ff0e820958": "USDT",
        "0671737d2f6e83afaeb2667bc6298e69788681caad303e751479802e439a5f7f": "EUR",
	"144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49": "L-BTC",
}

app.go

func GetMarkets() []*Market {
	return []*Market{
		{
			BaseAsset:         "L-BTC",
			QuoteAsset:        "L-BTC",
			BuyPercentageFee:  0.1,
			SellPercentageFee: 0.1,
			BuyLimit:          0,
			SellLimit:         0,
		},
		{
			BaseAsset:         "L-BTC",
			QuoteAsset:        "USDT",
			BuyPercentageFee:  0.1,
			SellPercentageFee: 0.75,
			BuyLimit:          0,
			SellLimit:         0,
		},
                {
			BaseAsset:         "L-BTC",
			QuoteAsset:        "EUR",
			BuyPercentageFee:  0.1,
			SellPercentageFee: 0.75,
			BuyLimit:          0,
			SellLimit:         0,
		},
		// Add more markets here if needed
	}
}

rates.go

func (kc *KrakenClient) Subscribe() error {
	// Initialize the map
	kc.PriceStreams = make(map[string]chan float64)

	// Create channels to stream the market prices
	kc.PriceStreams["L-BTC/USDT"] = make(chan float64)
        kc.PriceStreams["L-BTC/EUR"] = make(chan float64)
	kc.PriceStreams["L-BTC/L-BTC"] = make(chan float64)

	// Start a goroutine for each market pair to read from the WebSocket and update the last price
	for marketPair, priceStream := range kc.PriceStreams {
		go func(marketPair string, priceStream chan float64) {
			for price := range priceStream {
				kc.LastPrices[marketPair] = price
			}
		}(marketPair, priceStream)
	}

	// XBT/USDT
	// Subscribe to ticker information for the trading pair
	if err := kc.ws.SubscribeTicker([]string{ws.BTCUSDT}); err != nil {
		return fmt.Errorf("SubscribeTicker error: %s", err.Error())
	}

	go func() {
		defer close(kc.PriceStreams["L-BTC/USDT"])

		for update := range kc.ws.Listen() {
			switch data := update.Data.(type) {
			case ws.TickerUpdate:
				price, err := data.Ask.Price.Float64()
				if err != nil {
					log.Println("Error parsing price:", err)
					continue
				}
				kc.PriceStreams["L-BTC/USDT"] <- price
			default:
				return
			}
		}
	}()

       // XBT/EUR
	// Subscribe to ticker information for the trading pair
	if err := kc.ws.SubscribeTicker([]string{ws.BTCEUR}); err != nil {
		return fmt.Errorf("SubscribeTicker error: %s", err.Error())
	}

	go func() {
		defer close(kc.PriceStreams["L-BTC/EUR"])

		for update := range kc.ws.Listen() {
			switch data := update.Data.(type) {
			case ws.TickerUpdate:
				price, err := data.Ask.Price.Float64()
				if err != nil {
					log.Println("Error parsing price:", err)
					continue
				}
				kc.PriceStreams["L-BTC/EUR"] <- price
			default:
				return
			}
		}
	}()


	// L-BTC/L-BTC
	go func() {
		for {
			kc.PriceStreams["L-BTC/L-BTC"] <- 1.00
			time.Sleep(1 * time.Second)
		}
	}()
	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant