client

package module
v0.0.0-...-56572fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2023 License: MIT Imports: 5 Imported by: 1

README

Go Reference

nkn-openapi-client

A client library for the NKN OpenAPI written in go

NKN OpenAPI is an NKN blockchain database abstraction with a neat query interface.

Example

package main

import (
	"fmt"
	"log"

	client "github.com/omani/nkn-openapi-client"
)

func checkerr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

func main() {
	c := client.New()
	c.SetAddress("https://openapi.nkn.org/api/v1")

	resp, err := c.GetRegisteredNames()
	checkerr(err)

	for _, n := range resp.Data {
		fmt.Println(n.Name)
	}

	if !resp.HasMore() {
		return
	}
	// we have more than one page. fetch one more page.
	err = c.Next(resp)
	checkerr(err)

	for _, n := range resp.Data {
		fmt.Println(n.Name)
	}
}

Unit tests are available in client_test.go:

Run the tests in the root of the repository with go test.

This repository comes with a simple example CLI app in cmd/.

CLI App

Install

go install github.com/omani/nkn-openapi-client/cmd/nkn-openapi-client@latest

Usage
Clone this repository

git clone https://github.com/omani/nkn-openapi-client

Show transactions of an NKN wallet address:
cd cmd/nkn-openapi-client/
go run main.go transactions --address NKNJ6Tka2rcrqT4FPJTjfoWQLjvahctSiyRF

Output:

Show transactions with a given hash
cd cmd/nkn-openapi-client/
go run main.go transactions --hash 993a8f8ec9ca3a295e8873c573759d11b844c698e0e50f4edd1fa50751c2d879
Show block at a given height
cd cmd/nkn-openapi-client/
go run main.go blocks --height 5648381
Show block with a given hash
cd cmd/nkn-openapi-client/
go run main.go blocks --hash 0d48328a5005e7455c6a5e2a0b5bc346b09fbae129f1775589be83657850656a
Use your own NKN OpenAPI instance by providing a --url flag
cd cmd/nkn-openapi-client/
go run main.go --url https://my-own-nkn-openapi.tld blocks --hash 0d48328a5005e7455c6a5e2a0b5bc346b09fbae129f1775589be83657850656a

Contribution

  • You can fork this, extend it and contribute back.
  • You can contribute with pull requests.

Donations

I accept payments and donations in BTC:

bc1qgezvfp4s0xme8pdv6aaqu9ayfgnv4mejdlv3tx

LICENSE

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BlockHeader

type BlockHeader struct {
	Height            int    `json:"height"`
	SignerPK          string `json:"signerPk"`
	Wallet            string `json:"wallet"`
	BenificiaryWallet string `json:"benificiaryWallet"`
	CreatedAt         string `json:"created_at"`
}

type Client

type Client interface {
	// Addressbook
	GetRegisteredNames() (*ResponseGetRegisteredName, error)
	GetRegisteredNamesByAddress(string) ([]*ResponseGetRegisteredNameByAddress, error)
	GetAddressByRegisteredName(string) (*ResponseGetRegisteredNameByAddress, error)

	// Addresses
	GetAddresses() (*ResponseGetAddresses, error)
	GetSingleAddress(string) (*ResponseGetSingleAddress, error)
	GetAddressTransactions(string) (*ResponseGetAddressTransaction, error)

	// Blocks
	GetAllBlocks() (*ResponseGetBlock, error)
	GetBlockByHeight(int) (*ResponseGetBlockByHeight, error)
	GetBlockByHash(string) (*ResponseGetBlockByHash, error)
	GetTransactionsByBlockHeight(int) (*ResponseGetTransaction, error)
	GetTransactionsByBlockHash(string) (*ResponseGetTransaction, error)

	// Sigchain
	GetAllSigchains() (*ResponseGetSigchain, error)

	// Transactions
	GetAllTransactions() (*ResponseGetAllTransactions, error)
	GetTransactionByHash(string) (*ResponseGetTransaction, error)

	// Statistics
	StatsBlocksPerDay() ([]*ResponseStatsBlocksPerDay, error)
	StatsTransactionsPerDay() ([]*ResponseStatsTransactionsPerDay, error)
	StatsSupplies() (*ResponseStatsSupply, error)

	SetDebug(debug bool)
	SetAddress(address string)

	Next(interface{}) error
}

Client is an NKN OpenAPI client

func New

func New() Client

New returns a new Client

type MetaData

type MetaData struct {
	From         int         `json:"from,omitempty"`
	To           int         `json:"to,omitempty"`
	PerPage      interface{} `json:"per_page,omitempty"`
	CurrentPage  int         `json:"current_page,omitempty"`
	FirstPageURL string      `json:"first_page_url,omitempty"`
	PrevPageUrl  string      `json:"prev_page_url,omitempty"`
	NextPageUrl  string      `json:"next_page_url,omitempty"`
	Path         string      `json:"path,omitempty"`
}

func (*MetaData) HasMore

func (m *MetaData) HasMore() bool

type ResponseGetAddressTransaction

type ResponseGetAddressTransaction struct {
	*MetaData
	Data []struct {
		ID          int    `json:"id"`
		BlockID     int    `json:"block_id"`
		Attributes  string `json:"attributes"`
		Fee         int    `json:"fee"`
		Hash        string `json:"hash"`
		Nonce       string `json:"nonce"`
		TxType      string `json:"txType"`
		BlockHeight int    `json:"block_height"`
		CreatedAt   string `json:"created_at"`
		Payload     *TransactionPayload
	} `json:"data"`
}

type ResponseGetAddresses

type ResponseGetAddresses struct {
	Addresses struct {
		*MetaData
		Data []struct {
			Address            string `json:"address"`
			TransactionCount   int    `json:"count_transactions"`
			FirstTransactionAt string `json:"first_transaction"`
			LastTransactionAt  string `json:"last_transaction"`
			Balance            int    `json:"balance"`
		} `json:"data"`
	} `json:"addresses"`
	SumAddresses int `json:"sumAddresses"`
}

type ResponseGetAllTransactions

type ResponseGetAllTransactions struct {
	Transactions struct {
		*MetaData
		Data []struct {
			ID          int    `json:"id"`
			BlockID     int    `json:"block_id"`
			Attributes  string `json:"attributes"`
			Fee         int    `json:"fee"`
			Hash        string `json:"hash"`
			Nonce       string `json:"nonce"`
			TxType      string `json:"txType"`
			BlockHeight int    `json:"block_height"`
			CreatedAt   string `json:"created_at"`
			Payload     *TransactionPayload
		} `json:"data"`
	} `json:"transactions"`
	AvgSize         float64 `json:"avgSize"`
	SumTransactions int     `json:"sumTransactions"`
}

type ResponseGetBlock

type ResponseGetBlock struct {
	Blocks struct {
		*MetaData
		Data []struct {
			ID                int    `json:"id"`
			Hash              string `json:"hash"`
			Size              int    `json:"size"`
			TransactionsCount int    `json:"transactions_count"`
			Header            *BlockHeader
		} `json:"data"`
	} `json:"blocks"`
	AvgSize   string `json:"avgSize"`
	SumBlocks int    `json:"sumBlocks"`
}

Blocks

type ResponseGetBlockByHash

type ResponseGetBlockByHash struct {
	ID                int    `json:"id"`
	Hash              string `json:"hash"`
	Size              int    `json:"size"`
	TransactionsCount int    `json:"transactions_count"`
	Header            *BlockHeader
}

type ResponseGetBlockByHeight

type ResponseGetBlockByHeight struct {
	ID                int    `json:"id"`
	Hash              string `json:"hash"`
	Size              int    `json:"size"`
	TransactionsCount int    `json:"transactions_count"`
	Header            *BlockHeader
}

type ResponseGetRegisteredName

type ResponseGetRegisteredName struct {
	*MetaData
	Data []struct {
		Name      string `json:"name"`
		PublicKey string `json:"public_key"`
		Address   string `json:"address"`
		ExpiresAt string `json:"expires_at"`
	} `json:"data"`
}

Addressbook

type ResponseGetRegisteredNameByAddress

type ResponseGetRegisteredNameByAddress struct {
	Name      string `json:"name"`
	PublicKey string `json:"public_key"`
	Address   string `json:"address"`
	ExpiresAt string `json:"expires_at"`
}

type ResponseGetSigchain

type ResponseGetSigchain struct {
	*MetaData
	Data []struct {
		ID            int    `json:"id"`
		PayloadID     int    `json:"payload_id"`
		Nonce         int    `json:"nonce"`
		DataSize      int    `json:"dataSize"`
		BlockHash     string `json:"blockHash"`
		SrcID         string `json:"srcId"`
		SrcPubkey     string `json:"srcPubkey"`
		DestID        string `json:"destId"`
		DestPubkey    string `json:"destPubkey"`
		AddedAt       string `json:"added_at"`
		CreatedAt     string `json:"created_at"`
		SigchainElems []*SigchainElems
	} `json:"data"`
}

type ResponseGetSingleAddress

type ResponseGetSingleAddress struct {
	Address            string `json:"address"`
	TransactionCount   int    `json:"count_transactions"`
	FirstTransactionAt string `json:"first_transaction"`
	LastTransactionAt  string `json:"last_transaction"`
	Balance            int    `json:"balance"`
}

Addresses

type ResponseGetTransaction

type ResponseGetTransaction struct {
	ID          int    `json:"id"`
	BlockID     int    `json:"block_id"`
	Attributes  string `json:"attributes"`
	Fee         int    `json:"fee"`
	Hash        string `json:"hash"`
	Nonce       string `json:"nonce"`
	TxType      string `json:"txType"`
	BlockHeight int    `json:"block_height"`
	CreatedAt   string `json:"created_at"`
	Payload     *TransactionPayload
}

type ResponseStatsBlocksPerDay

type ResponseStatsBlocksPerDay struct {
	Stats
}

type ResponseStatsSupply

type ResponseStatsSupply struct {
	MaxSupply         float64 `json:"max_supply"`
	TotalSupply       float64 `json:"total_supply"`
	CirculatingSupply float64 `json:"circulating_supply"`
}

type ResponseStatsTransactionsPerDay

type ResponseStatsTransactionsPerDay struct {
	Stats
}

type SigChain

type SigChain struct {
	ID            int    `json:"id"`
	PayloadID     int    `json:"payload_id"`
	Nonce         int    `json:"nonce"`
	DataSize      int    `json:"dataSize"`
	BlockHash     string `json:"blockHash"`
	SrcID         string `json:"srcId"`
	SrcPubkey     string `json:"srcPubkey"`
	DestID        string `json:"destId"`
	DestPubkey    string `json:"destPubkey"`
	AddedAt       string `json:"added_at"`
	CreatedAt     string `json:"created_at"`
	SigchainElems []*SigchainElems
}

type SigchainElems

type SigchainElems struct {
	ID         int    `json:"id"`
	SigchainID int    `json:"sigchain_id"`
	ID2        string `json:"id2"`
	Pubkey     string `json:"pubkey"`
	Wallet     string `json:"wallet"`
	NextPubkey string `json:"nextPubkey"`
	Mining     bool   `json:"mining"`
	SignAlgo   string `json:"signAlgo"`
	Signature  string `json:"signature"`
	VRF        string `json:"vrf"`
	Proof      string `json:"proof"`
	AddedAt    string `json:"added_at"`
	CreatedAt  string `json:"created_at"`
}

type Stats

type Stats struct {
	Date  string `json:"date"`
	Count int    `json:"count"`
}

Stats

type TransactionPayload

type TransactionPayload struct {
	ID                int            `json:"id"`
	TransactionID     int            `json:"transaction_id"`
	PayloadType       string         `json:"payloadType"`
	Sender            string         `json:"sender"`
	SenderWallet      string         `json:"senderWallet"`
	Recipient         string         `json:"recipient"`
	RecipientWallet   string         `json:"recipientWallet"`
	Amount            common.Fixed64 `json:"amount"`
	Submitter         string         `json:"submitter"`
	Registrant        string         `json:"registrant"`
	RegistrantWallet  string         `json:"registrantWallet"`
	Name              string         `json:"name"`
	Subscriber        string         `json:"subscriber"`
	Identifier        string         `json:"identifier"`
	Topic             string         `json:"topic"`
	Bucket            int            `json:"bucket"`
	Duration          int            `json:"duration"`
	Meta              string         `json:"meta"`
	PublicKey         string         `json:"public_key"`
	RegistrationFee   int            `json:"registration_fee"`
	Nonce             string         `json:"nonce"`
	TXNExpiration     int            `json:"txn_expiration"`
	Symbol            string         `json:"symbol"`
	TotalSupply       int            `json:"total_supply"`
	Precision         int            `json:"precision"`
	NanoPayExpiration int            `json:"nano_pay_expiration"`
	SignerPK          string         `json:"signerPk"`
	AddedAt           string         `json:"added_at"`
	CreatedAt         string         `json:"created_at"`
	GenerateWallet    string         `json:"generateWallet"`
	SubscriberWallet  string         `json:"subscriberWallet"`
	Sigchain          SigChain       `json:"sigchain"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL