2

I'm working on an inflation pool, using the go SDK. However the client/horizon package doesn't expose all the horizon endpoints (the Client Interface only has Root, HomeDomainForAccount, LoadAccount, LoadAccountOffers, LoadMemo, LoadOrderBook, StreamLedgers, StreamPayment, StreamTransaction, and SubmitTransaction).

For example, if I want to detect the last inflation operation validated, using JavaScript I could follow the suggestions from here: How can I determine when the last inflation was distributed?

But using the Go SDK doesn't seem to be that simple. I even tried importing the services/horizon/internal/resourceadapter/operations package, since it already has the struct definitions for the operations returned by the Horizon Endpoint. But Go doesn't allow that, with the error "use of internal package not allowed".

Is this a design decision of the Go SDK? Is JavaScript the best way to access these Horizon Endpoints?

Thanks!

1 Answer 1

2

The horizon internal package is not the right ones to use. It implements the server side. Like the js-stellar-sdk you will need to do an api call to horizon:

curl "https://horizon-testnet.stellar.org/operations?limit=200&order=desc"

The clients/horizon package is the client library for Go with helper methods.

Edit

There is no support for the operations method in the horizon client (yet). But it's not complicated to do. See my example below:

Last time inflation distributed

package main

import (
    "encoding/json"
    "log"
    "net/http"
    "os"
    "time"

    "github.com/stellar/go/clients/horizon"
)

type Operations struct {
    Links struct {
        Self horizon.Link `json:"self"`
        Next horizon.Link `json:"next"`
        Prev horizon.Link `json:"prev"`
    } `json:"_links"`
    Embedded struct {
        Records []Operation `json:"records"`
    } `json:"_embedded"`
}

type Operation struct {
    ID          string    `json:"id"`
    Type        string    `json:"type"`
    PagingToken string    `json:"paging_token"`
    CreatedAt   time.Time `json:"created_at"`
}

func main() {
    operationsURL := "https://horizon-testnet.stellar.org/operations?limit=200&order=desc"
    for operationsURL != "" {
        log.Print("calling: ", operationsURL)
        resp, err := http.Get(operationsURL)
        if err != nil {
            log.Fatalf("failed to fetch operations from horizon: %s", err)
        }
        var operations Operations
        if err := json.NewDecoder(resp.Body).Decode(&operations); err != nil {
            log.Fatalf("failed to decode operations: %s", err)
        }
        for _, o := range operations.Embedded.Records {
            if o.Type == "inflation" {
                log.Printf("Last inflation distributed at %s (%s})", o.CreatedAt, o.ID)
                os.Exit(0)
            }
        }
        operationsURL = operations.Links.Next.Href
    }
    log.Fatalf("not found")
}
2
  • Thanks, but the clients/horizon package implements some of the endpoints (and defines some of the responses structs), but not all of them, hence my question if this is a design decision. For example, you can just call client.LoadAccount(id) instead of curl "https://horizon-testnet.stellar.org/accounts/<id>". Jan 29, 2018 at 15:16
  • The internal package was introduced when horizon was moved to the monorepo: see github.com/stellar/go/issues/126
    – alpe1
    Jan 29, 2018 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.