Skip to content

Index out of bound #14

@kzmnbrs

Description

@kzmnbrs

Hello there and thank you for your work.

I faced a bunch of index out of bound exceptions using jsonvector.

panic: runtime error: index out of range [3520] with length 3481

goroutine 1 [running]:
github.com/koykov/vector.(*Node).Each(0xc0002e0438, 0x7580f8)
	/Users/senpai/go/pkg/mod/github.com/koykov/vector@v1.2.6/node.go:239 +0xee
main.main()
	/Users/senpai/dev/personal/ton_watch/main.go:62 +0x487
panic: runtime error: index out of range [3520] with length 3481

goroutine 1 [running]:
github.com/koykov/vector.(*Node).Each(0xc0002e0438, 0x7580f8)
	/Users/senpai/go/pkg/mod/github.com/koykov/vector@v1.2.6/node.go:239 +0xee
main.main()
	/Users/senpai/dev/personal/ton_watch/main.go:62 +0x487

I did not manage to reproduce it on static data, but it's quite easy from the following code sample:

package main

import (
	"context"
	"flag"
	"fmt"
	"log/slog"
	"math"
	"os/signal"
	"syscall"
	"time"

	"github.com/koykov/jsonvector"
	"github.com/koykov/vector"
	"github.com/valyala/fasthttp"
)

const (
	marketCapApiLatest = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest"
	tickInterval       = time.Minute * 5
)

var pivotPrice = flag.Float64("p", 8, "pivot price")

func main() {
	flag.Parse()

	sigint, _ := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)

	ticker := time.NewTicker(tickInterval)
	defer ticker.Stop()

	var (
		recv fasthttp.Request
		resp fasthttp.Response
		vec  jsonvector.Vector
	)

	recv.SetRequestURI(marketCapApiLatest)
	recv.Header.SetMethod(fasthttp.MethodGet)
	recv.Header.Set("Accepts", "application/json")
	recv.Header.Set("X-CMC_PRO_API_KEY", marketCapApiKey)

	for {
		resp.Reset()
		if err := fasthttp.Do(&recv, &resp); err != nil {
			slog.Warn("network", "error", err)
			continue
		}

		body := resp.Body()

		vec.Reset()

		if err := vec.Parse(body); err != nil {
			slog.Warn("parse", "error", err)
			continue
		}

		vec.DotArray("data").Each(func(i int, node *vector.Node) {
			if node.DotString("symbol") != "TON" {
				return
			}

			price, err := node.DotFloat("quote.USD.price")
			if err != nil {
				slog.Warn("get price", "error", err)
				return
			}

			if devp := math.Abs(1 - price / *pivotPrice) * 100; devp >= 10 {
				slog.Info("price change", "devp", devp)
				switch {
				case price > *pivotPrice:
					// 
				case price < *pivotPrice:
					//
				}
				if err != nil {
					slog.Warn("send alert", "error", err)
					return
				}
			}
		})

		select {
		case <-sigint.Done():
			return
		case <-ticker.C:
		}
	}
}

Don't suppose you could look into it? A free token from coinmarketcap might be required. Thank you in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions