Cursor functionality with GoLang

Anyone have luck getting the cursor functionality to work with GoLang? Iā€™m close but am also stuck

how are you stuck, what did you make it work by now?

you can see here how to post code on forum

1 Like
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"time"
)

type Response struct {
	Trades []Trade `json:"trades"`
	Cursor string  `json:"cursor"`
}

type Token_IDS struct {
	Token_Ids int64 `json:"token_ids"`
}

type Result_Info struct {
	TransactionHash    string `json:"transaction_hash"`
	TransactionIndex   int64  `json:"transaction_index"`
	TokenIDS           Token_IDS
	SellerAddress      string  `json:"seller_address"`
	BuyerAddress       string  `json:"buyer_address"`
	TokenAddress       string  `json:"token_address"`
	MarketplaceAddress string  `json:"marketplace_address"`
	Price              float64 `json:"price"`
	PriceTokenAddress  string  `json:"price_token_address"`
	BlockTimestamp     string  `json:"block_timestamp"`
	BlockNumber        int64   `json:"block_number"`
	BlockHash          string  `json:"block_hash"`
}

type Trade struct {
	Total    int64  `json:"total"`
	Page     int64  `json:"page"`
	PageSize int64  `json:"page_size"`
	Cursor   string `json:"cursor"`
	Result   Result_Info
}

func main() {
	address := "0xbe9371326F91345777b04394448c23E2BFEaa826"
	chain := "eth"
	fromBlock := 1
	toBlock := 9999999999999
	marketplace := "opensea"
	limit := 100
	disableTotal := true

	// Create query parameters
	queryParams := url.Values{}
	cursor := ""

	for {
		queryParams.Set("cursor", cursor)
		queryParams.Set("chain", chain)
		queryParams.Set("from_block", fmt.Sprintf("%d", fromBlock))
		queryParams.Set("to_block", fmt.Sprintf("%d", toBlock))
		queryParams.Set("marketplace", marketplace)
		queryParams.Set("limit", fmt.Sprintf("%d", limit))
		queryParams.Set("disable_total", fmt.Sprintf("%t", disableTotal))

		// Create URL
		url := fmt.Sprintf("https://deep-index.moralis.io/api/v2/nft/%s/trades?%s", address, queryParams.Encode())

		// Create request
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			fmt.Println("Error creating request:", err)
			return
		}

		// Add headers
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("X-API-Key", "APIKey")

		// Make request
		client := &http.Client{Timeout: 10 * time.Second}
		res, err := client.Do(req)
		if err != nil {
			fmt.Println("Error making request:", err)
			return
		}
		defer res.Body.Close()

		// Parse response
		if res.StatusCode != http.StatusOK {
			fmt.Println("Request failed with status:", res.StatusCode)
			return
		}

		var response Response
		err = json.NewDecoder(res.Body).Decode(&response)
		if err != nil {
			fmt.Println("Error parsing response:", err)
			return
		}

		// Print trades
		for _, trade := range response.Trades {
			fmt.Printf("Total: %d, Page: %d, PageSize: %d, Cursor: %s, TransactionHash: %s, TransactionIndex %d, TokenID: %d, SellerAddress: %s, BuyerAddress: %s, TokenAddress: %s, MarketplaceAddress: %s, Price: %f, PriceTokenAddress: %s, BlockTimestamp: %s, BlockNumber: %d, BlockHash: %s\n",
				trade.Total, trade.Page, trade.PageSize, trade.Cursor, trade.Result.TransactionHash, trade.Result.TransactionIndex, trade.Result.TokenIDS.Token_Ids, trade.Result.SellerAddress, trade.Result.BuyerAddress, trade.Result.TokenAddress, trade.Result.MarketplaceAddress, trade.Result.Price, trade.Result.PriceTokenAddress, trade.Result.BlockTimestamp, trade.Result.BlockNumber, trade.Result.BlockHash)
		}

		// Check if there is a cursor
		if response.Cursor != "" {
			cursor = response.Cursor
			fmt.Println("Next cursor:", cursor)
		} else {
			break
		}

	}
}

The above is the approach I used to fix the payload issue, but the cursor functionality isnā€™t working yet and Iā€™m not sure why

does this print anything?

Yes, it prints what I think is the correct next cursor

ok, and you are using that cursor in the next request?

can you log this url to see if it has the cursor?

It does have that cursor. If I run the try it api on the Moralis web site, it shows the same cursor. Thereā€™s something more basic Iā€™m missing I think

Can you paste that url to see how it looks? The one with the cursor