Skip to content

cardano-community/koios-go-client

Repository files navigation

Koios Go

Koios API Client Library for Go

Koios API is Elastic Cardano Query Layer!

A consistent query layer for developers to build upon Cardano, with multiple, redundant endpoints that allow for easy scalability.

Koios API Client Library for Go

PkgGoDev

go get github.com/cardano-community/koios-go-client/v4
...
import (
  "github.com/cardano-community/koios-go-client/v4" // imports as package "koios"
)
...

There is CLI Application to interact with Koios API from Command-line see:

koios cli repository for souce and installing instruction of koios cli

Build Status

linux macos windows

Koios API Endpoint Tests

mainnet guild preview preprod

Development Status

GitHub last commit codecov codeql misspell Go Report Card



Usage

See Godoc PkgGoDev

Additionally you can find all usecases by looking source of koio-cli Command-line application koios-cli which utilizes entire API of this library.

NOTE

Library normalizes some of the API responses and constructs Typed response for each end point. If you wish to work with *http.Response directly you can do so by using api client GET,POST, HEAD methods.

Basic usage

package main

import (
	"context"
	"fmt"
	"log"

	koios "github.com/cardano-community/koios-go-client/v4"
)

func main() {
  // Call to koios.New without options is same as calling it with default opts.
  // See godoc for available configuration options.
  // api, err := koios.New(
  // 	koios.Host(koios.MainnetHost),
  // 	koios.APIVersion(koios.DefaultAPIVersion),
  // 	koios.Port(koios.DefaultPort),
  // 	koios.Schema(koios.DefaultSchema),
  // 	koios.HttpClient(koios.DefaultHttpClient),
  // ).
  api, err := koios.New()
  if err != nil {
    log.Fatal(err)
  }

  res, err := api.GetTip(context.Background(), nil)
  if err != nil {
	  log.Fatal(err)
  }
  fmt.Println("status: ", res.Status)
  fmt.Println("statu_code: ", res.StatusCode)

  fmt.Println("abs_slot: ", res.Data.AbsSlot)
  fmt.Println("block_no: ", res.Data.BlockNo)
  fmt.Println("block_time: ", res.Data.BlockTime)
  fmt.Println("epoch: ", res.Data.Epoch)
  fmt.Println("epoch_slot: ", res.Data.EpochSlot)
  fmt.Println("hash: ", res.Data.Hash)
}

Concurrency using goroutines

This library is thread-safe so you can freerly use same api client instance passing it to your goroutines.

Following example uses goroutines to query chain tip from different endpoints.

func main() {
  api, _ := koios.New(
    // limit client request 1 per second even though
    // this example will send requests in goroutines.
    koios.RateLimit(1),
  )
  ctx := context.Background()
  var wg sync.WaitGroup
  servers := []string{
    koios.GuildHost,
    koios.PreviewHost,
    koios.TestnetHost,
    koios.MainnetHost,
    koios.PreProdHost,
    koios.MainnetHost,
    koios.MainnetHostEU,
  }

  // Thanks to rate limit option requests will be made
  // once in a second.
  for _, host := range servers {
    wg.Add(1)
    go func(ctx context.Context, host string) {
      defer wg.Done()
      // switching host by creating light clone of client
      // with new options
      client, err := api.WithOptions(koios.Host(host))
      res, _ := client.GET(ctx, "/tip", nil)
      defer res.Body.Close()
      body, _ := io.ReadAll(res.Body)
      fmt.Println("Host: ", host)
      fmt.Println("Response: ", string(body))
    }(ctx, host)
  }

  wg.Wait()
}

Math on ada, assets and tokens).

Library uses decimal.Decimal data type to represent lovelace and coin values.
Which provides arbitrary-precision fixed-point decimal numbers in go.

For decimal package API see

Contributing

We would love for you to contribute to Koios API Client Library for Go and help make it even better than it is today! As a contributor, here are the guidelines we would like you to follow:

Code of Conduct

Help us keep Koios API Client Library for Go open and inclusive. Please read and follow our Code of Conduct


Got a Question or Problem?

Do not open issues for general support questions as we want to keep GitHub issues for bug reports and feature requests. You've got much better chances of getting your question answered on Koios Telegram Group


Issues and Bugs

If you find a bug in the source code, you can help us by submitting an issue to our GitHub Repository. Even better, you can submit a Pull Request with a fix.


Feature Requests

You can request a new feature by submitting an issue to our GitHub Repository. If you would like to implement a new feature, please submit an issue with a proposal for your work first, to be sure that we can use it. Please consider what kind of change it is:

  • For a Major Feature, first open an issue and outline your proposal so that it can be discussed. This will also allow us to better coordinate our efforts, prevent duplication of work, and help you to craft the change so that it is successfully accepted into the project.
  • Small Features can be crafted and directly submitted as a Pull Request.

Submission Guidelines

Submitting an Issue

Before you submit an issue, please search the issue tracker, maybe an issue for your problem already exists and the discussion might inform you of workarounds readily available.

You can file new issues by filling out our new issue form.


Submitting a Pull Request (PR)

Before you submit your Pull Request (PR) consider the following guidelines:

  1. Search GitHub for an open or closed PR that relates to your submission. You don't want to duplicate effort.

  2. Fork the cardano-community/koios-go-client repo.

  3. Setup you local repository

    git@github.com:<your-github-username>/koios-go-client.git
    cd koios-go-client
    git remote add upstream git@github.com:cardano-community/koios-go-client.git
  4. Make your changes in a new git branch and ensure that you always start from up to date main branch. Repeat this step every time you are about to start woking on new PR.

    e.g. Start new change work to update readme:

    # if you are not in main branch e.g. still on previous work branch
    git checkout main
    git pull --ff upstream main
    git checkout -b update-readme main
  5. Create your patch, including appropriate test cases.

  6. Follow our Coding Rules.

  7. If changes are in source code except documentations then run the full test suite, as described in the developer documentation, and ensure that all tests pass.

  8. Commit your changes using a descriptive commit message that follows our commit message conventions. Adherence to these conventions is necessary because release notes are automatically generated from these messages.

    git add -A
    git commit --signoff
    # or in short
    git commit -sm"docs(markdown): update readme examples"
  9. Push your branch to GitHub:

    git push -u origin update-readme
  10. In GitHub, send a pull request to main branch.

  • If we suggest changes then:
    • Make the required updates.

    • Re-run the test suites to ensure tests are still passing.

    • Rebase your branch and force push to your GitHub repository (this will update your Pull Request):

      git fetch --all
      git rebase upstream main
      git push -uf origin update-readme

That's it! Thank you for your contribution!


After your pull request is merged

After your pull request is merged, you can safely delete your branch and pull the changes from the main (upstream) repository:

  • Delete the remote branch on GitHub either through the GitHub web UI or your local shell as follows:

    git push origin --delete update-readme
  • Check out the main branch:

    git checkout main -f
  • Delete the local branch:

    git branch -D update-readme
  • Update your master with the latest upstream version:

    git pull --ff upstream main

Coding Rules

To ensure consistency throughout the source code, keep these rules in mind as you are working:

  • All features or bug fixes must be tested by one or more specs (unit-tests).
  • All public API methods must be documented.

Commit Message Guidelines

Conventional Commits

We have very precise rules over how our git commit messages can be formatted. This leads to more readable messages that are easy to follow when looking through the project history. Commit messages should be well formatted, and to make that "standardized", we are using Conventional Commits. Our release workflow uses these rules to generate changelogs.


Commit Message Format

Each commit message consists of a header, a body and a footer. The header has a special format that includes a type, a scope and a subject:

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

When maintainers are merging PR merge commit should be edited:

<type>(<scope>): <subject> (#pr)
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

The header is mandatory and the scope of the header is optional.

Any line of the commit message cannot be longer 100 characters! This allows the message to be easier to read on GitHub as well as in various git tools.

The footer should contain a closing reference to an issue if any.

Samples:

docs(markdown): update readme examples

fix(endpoint): update Tip endpoint to latest specs.

description of your change.
refactor(client): change Client GET function signature

change order of client GET method arguments.

BREAKING CHANGE: Clien.Get signature has changed

Revert

If the commit reverts a previous commit, it should begin with revert: , followed by the header of the reverted commit. In the body it should say: This reverts commit <hash>., where the hash is the SHA of the commit being reverted.


Type

Must be one of the following:

  • build: Changes that affect the build system or external dependencies (example scopes: goreleaser, taskfile)
  • chore: Other changes that don't modify src or test files.
  • ci: Changes to our CI configuration files and scripts.
  • dep: Changes related to dependecies e.g. go.mod
  • docs: Documentation only changes (example scopes: markdown, godoc)
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • revert: Reverts a previous commit
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • test: Adding missing tests or correcting existing tests

Scope

The following is the list of supported scopes:

scope description
client API client related changes
endpoint Changes related to api endpoints
godoc Go documentation
markdown Markdown files

Subject

The subject contains a succinct description of the change:

  • use the imperative, present tense: "change" not "changed" nor "changes"
  • don't capitalize the first letter
  • no dot (.) at the end

Body

Just as in the subject, use the imperative, present tense: "change" not "changed" nor "changes". The body should include the motivation for the change and contrast this with previous behavior.

Footer

The footer should contain any information about Breaking Changes and is also the place to reference GitHub issues that this commit Closes.

Breaking Changes should start with the word BREAKING CHANGE: with a space or two newlines. The rest of the commit message is then used for this.

A detailed explanation can be found in this [document][commit-message-format].


Development Documentation

Setup your machine

Prerequisites:

Setup local env

task setup

Lint your code

task lint

Test your change

task test

View code coverage report from in browser (results from task test)

task cover

Credits

GitHub contributors

Original author.
koios-go-client was moved under Cardano Community from howijd/koios-rest-go-client

Koios Go banner design.
Koios Go banner was designed by Egon Elbre egonelbre.com