nemgo

package module
v0.0.0-...-7375f23 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2018 License: Apache-2.0 Imports: 14 Imported by: 0

README

nemgo

Build Status

A pure golang SDK for the Nem blockchain.

This project is in it's infancy and looking for more contributors! If you are working in Go, are interested in blockchain technologies, or just want to join a friendly open source project you are welcome!

Getting Started

go get the package using the following command:

$ go get github.com/myndshft/nemgo

Open up your favorite text editor, create a new Client and interact with the blockchain!

package main

import (
    "fmt"
    "log"

    "github.com/myndshft/nemgo"
)

// testnet = byte(0x98)

// nemgo.New() will default to a sensible NIS on the mainnet

func main() {
    c, err := nemgo.New()
    if err != nil {
        log.Fatal(err)
    }

    // Custom Client
    c, err := nemgo.New(nemgo.WithNIS("MY.CUSTOM.NIS.HOST:7890", byte(0x68)))
    if err != nil {
        log.Fatal(err)
    }

    // Get account information
    address = "YOUR ACCOUNT ADDRESS"
    actInfo, err := c.AccountInfo(address)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(actInfo)

    // Get the current height of the chain
    height, err := c.Height()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(height)

    // Subscribe to transactions related to an account
    // This will return a go channel
    txs, err := c.SubscribeUnconfirmedTX(address)
    if err != nil {
        log.Fatal(err)
    }
    defer close(txs)
    for tx := range txs {
        fmt.Println(tx)
    }

Helping out

Check out the CONTRIBUTING.md documents in the docs folder. We always welcome any contribution, large or small!

The gopher logo is the work of Renee French. The Nem logo is licensed under CC0 1.0

Documentation

Overview

Package nemgo is a pure golang implementation of an SDK for interacting with the NEM blockchain through the NEM Infrastructure Server (NIS) api.

Index

Examples

Constants

View Source
const (
	// Testnet is a helper const to connect to the testnet
	Testnet = byte(0x98)
	// Mainnet is a helper const to connect to the mainnet
	Mainnet = byte(0x68)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountInfo

type AccountInfo struct {
	// Each account has a unique address. First letter of an address
	// indicate the network the account belongs to. Currently two networks
	// are defined: the test network whose account addresses start with a
	// capital T and the main network whose account addresses always start
	// with a capital N. Addresses have always a length of 40 characters
	// and are base-32 encoded.
	Address string
	// Each account has a balance which is an integer greater or equal to
	// zero and denotes the number of micro NEMs which the account owns.
	// Thus a balance of 123456789 means the account owns 123.456789 NEM.
	// A balance is split into its vested and unvested part.
	// Only the vested part is relevant for the importance calculation.
	// For transfers from one account to another only the balance itself
	// is relevant.
	Balance int
	// vestedBalance contains the vested part of the balance of the account
	// in micro NEM.
	VestedBalance float64
	// Each account is assigned an importance. The importance is a decimal
	// number between 0 and 1. It denotes the probability of an account to
	// harvest the next block in case the account has harvesting turned on
	// and all other accounts are harvesting too. The exact formula for
	// calculating the importance is not public yet.
	// Accounts need at least 10k vested NEM to be included
	// in the importance calculation.
	Importance float64
	// The public key of an account can be used to verify signatures of the
	// account. Only accounts that have already published a transaction have
	// a public key assigned to the account. Otherwise the field is null.
	PublicKey string
	// Label has the label of the account (not used, always null).
	Label string
	// Harvesting is the process of generating new blocks. The field
	// denotes the number of blocks that the account harvested so far.
	// For a new account the number is 0.
	HarvestedBlocks int
}

AccountInfo describes basic information for an account.

type AccountMetadata

type AccountMetadata struct {
	// Status contains the harvesting status of a queried account.
	// The harvesting status can be one of the following values:
	// "UNKNOWN": The harvesting status of the account is not known.
	// "LOCKED": The account is not harvesting.
	// "UNLOCKED": The account is harvesting.
	Status string
	// RemoteStatus contains the status of teh remote harvesting of a queried account.
	// The remote harvesting status can be one of the following values:
	// "REMOTE": The account is a remote account and therefore remoteStatus is not applicable for it.
	// "ACTIVATING": The account has activated remote harvesting but it is not yet active.
	// "ACTIVE": The account has activated remote harvesting and remote harvesting is active.
	// "DEACTIVATING": The account has deactivated remote harvesting but remote harvesting is still active.
	// "INACTIVE": The account has inactive remote harvesting, or it has deactivated remote harvesting
	// and deactivation is operational.
	RemoteStatus string
	// CosignatoryOf is a JSON array of AccountInfo structures.
	// The account is cosignatory for each of the accounts in the array.
	CosignatoryOf []AccountInfo
	// Cosignatories is a JSON array of AccountInfo structures.
	// The array holds all accounts that are a cosignatory for this account.
	Cosignatories []AccountInfo
}

AccountMetadata describes additional information for the account.

type AccountMetadataPair

type AccountMetadataPair struct {
	// Account contains the account object.
	Account AccountInfo `json:"account"`
	// Meta contain the account meta data object.
	Meta AccountMetadata `json:"meta"`
}

AccountMetadataPair includes durable information for an account and additional information about its state.

type Address

type Address string

Address is a Nem account address as a string

func (Address) String

func (a Address) String() string

type Block

type Block struct {
	// BUG(tyler): All float64 in Block should be int
	TimeStamp     float64
	Signature     string
	PrevBlockHash string
	Type          float64
	Transactions  []Transaction
	Version       float64
	Signer        string
	Height        float64
}

Block objects are generated by accounts. If an account generates a block and the block gets included in the block chain, the generating account, called the harvester, gets all the transaction fees for transactions that are included in the block. A harvester will therefore usually include as many transactions as possible. Transactions reflect all account activities. In order for a client to have an up to date balance for every account it is crucial to know about every transaction that occurred and therefore the client must have knowledge about every single block in the chain (one says: the client must be synchronized with the block chain). Whenever timestamps are used, the time reflects the network time. NEM has a time synchronization mechanism which lets all node agree on how many seconds since the nemesis have elapsed. This common time is called network time.

func (*Block) UnmarshalJSON

func (b *Block) UnmarshalJSON(data []byte) error

UnmarshalJSON implements a custom JSON unmarshaller for Blocks

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is used to interact with a NIS

func New

func New(opts ...Option) *Client

New will return a Client object ready to be used defaulting to the NEM mainnet

Example

ExampleNew shows how to create a new NEM client

c := New()
// use c
// you can also create a client using a custom NIS
c = New(WithNIS("YOUR.CUSTOM.NIS.HERE:7890", Mainnet))
// use c
fmt.Println(c)
Output:

func (Client) AccountData

func (c Client) AccountData(acc accountName) (AccountMetadataPair, error)

AccountData gets all information for a given address

func (Client) AccountStatus

func (c Client) AccountStatus(address string) (AccountMetadata, error)

AccountStatus gets the current metadata about an account

func (Client) AllTransactions

func (c Client) AllTransactions(address string) ([]TransactionMetadataPair, error)

AllTransactions will list the most recent transactions either incoming or outgoing

func (Client) BlockInfo

func (c Client) BlockInfo(height int) (Block, error)

BlockInfo will supply individual block info identified by block height.

func (Client) CreateTransaction

func (c Client) CreateTransaction(toAct string, xemAmt int, opts ...Option) (TransactionMetadataPair, error)

CreateTransaction will initialize a transaction on the nem blockchain

func (Client) GetBatchAccountData

func (c Client) GetBatchAccountData(addresses []string) ([]AccountMetadataPair, error)

GetBatchAccountData gets the AccountMetaDataPair of an array of accounts

func (Client) GetDelegated

func (c Client) GetDelegated(address string) (AccountMetadataPair, error)

GetDelegated returns the account meta and data info for the account for which the given account is the delegate account

func (Client) Harvested

func (c Client) Harvested(address string, hash string) ([]HarvestInfo, error)

Harvested gets an array of harvest info objects for an account

func (Client) Height

func (c Client) Height() (int, error)

Height gets the current height of the blockchain

func (Client) IncomingTransactions

func (c Client) IncomingTransactions(address string) ([]TransactionMetadataPair, error)

IncomingTransactions withh list all current pending transactions for a given address. This method is likely to be used in conjunction with the StreamingUnconfirmedTX method to get additional details about the transactions.

func (Client) LastBlock

func (c Client) LastBlock() (Block, error)

LastBlock will get the most recent confirmed block on NEM and return information about the block

func (Client) MosaicsOwned

func (c Client) MosaicsOwned(address string) ([]OwnedMosaic, error)

MosaicsOwned will find information about what mosaics an address currently holds

func (Client) Namespace

func (c Client) Namespace(namespace string) (NamespaceInfo, error)

Namespace will return a NamespaceInfo about a namespace

func (Client) NodeInfo

func (c Client) NodeInfo() (NodeInfo, error)

NodeInfo gets basic information about a node

func (Client) OutgoingTransactions

func (c Client) OutgoingTransactions(address string) ([]TransactionMetadataPair, error)

OutgoingTransactions will list all current pending outgoing transactions for a given address. This method is likely to be used in the conjunction with StreamingUnconfirmedTX method to get additional details about the transactions.

func (Client) RootNamespace

func (c Client) RootNamespace(ID int, PageSize int) ([]NamespaceMetadataPair, error)

RootNamespace will get all the root namespaces in batch of a specified PageSize.

func (Client) Score

func (c Client) Score() (string, error)

Score gets the current score of the blockchain. The higher the score, the better the chain. During synchronization, nodes try to get the best block chain in the network.

func (Client) SubscribeConfirmedTX

func (c Client) SubscribeConfirmedTX(address string) (chan StreamMessage, error)

SubscribeConfirmedTX will take an account address and subscribe to all confirmed transactions at that address

func (Client) SubscribeData

func (c Client) SubscribeData(address string) (chan StreamMessage, error)

SubscribeData will take an account address and subscribe to all account data changes at that address

func (Client) SubscribeErrors

func (c Client) SubscribeErrors() (chan StreamMessage, error)

SubscribeErrors will return a channel subscribed to error messages

func (Client) SubscribeHeight

func (c Client) SubscribeHeight() (chan StreamMessage, error)

SubscribeHeight will return a channel subscribed to block heights

func (Client) SubscribeMoasaicData

func (c Client) SubscribeMoasaicData(address string) (chan StreamMessage, error)

SubscribeMoasaicData will take an account address and subscribe to all mosaic definition changes for that address

func (Client) SubscribeMosaics

func (c Client) SubscribeMosaics(address string) (chan StreamMessage, error)

SubscribeMosaics will take an account address and subscribe to all mosaic changes for that address

func (Client) SubscribeNamespaces

func (c Client) SubscribeNamespaces(address string) (chan StreamMessage, error)

SubscribeNamespaces will take an account address and subscribe to all namespace changes for that address

func (Client) SubscribeRecentTX

func (c Client) SubscribeRecentTX(address string) (chan StreamMessage, error)

SubscribeRecentTX will take an account address and subscribe to all recent transactions at that address

func (Client) SubscribeUnconfirmedTX

func (c Client) SubscribeUnconfirmedTX(address string) (chan StreamMessage, error)

SubscribeUnconfirmedTX will take an account address and subscribe to all unconfirmed transactions at that address

type HarvestInfo

type HarvestInfo struct {
	TimeStamp  int
	Difficulty int
	TotalFee   int
	ID         int
	Height     int
}

HarvestInfo is information about harvested blocks

type NamespaceInfo

type NamespaceInfo struct {
	FQN    string
	Owner  string
	Height int
}

NamespaceInfo contains information about a namespace

type NamespaceMetadata

type NamespaceMetadata struct {
	ID int
}

NamespaceMetadata contains meta information about a namespace

type NamespaceMetadataPair

type NamespaceMetadataPair struct {
	NamespaceInfo     NamespaceInfo
	NamespaceMetaData NamespaceMetadata
}

NamespaceMetadataPair contains info and metadata about a namespace

type Node

type Node struct {
	MetaData metadata
	Endpoint endpoint
	Identity identity
}

Node is a node on the Nem blockchain

type NodeInfo

type NodeInfo struct {
	Node    Node
	NISInfo nisInfo
}

NodeInfo contains extended information about the current node

type Option

type Option func(*Client)

Option can be passed into New() to enable additional configuration of the returned Client

func WithNIS

func WithNIS(host string, network byte) Option

WithNIS allows the user to create a Client connected to a NIS of their choosing The network param must be a single byte representing the network which the host NIS is a member of mainnet = byte(0x68) testnet = byte(0x98)

type OwnedMosaic

type OwnedMosaic struct {
	MosaicID struct {
		NamespaceID string
		Name        string
	}
	Quantity int
}

OwnedMosaic is an array of basic information about a mosaic

type PublicKey

type PublicKey string

PublicKey is a Nem account public key as a string

func (PublicKey) String

func (pk PublicKey) String() string

type StreamMessage

type StreamMessage struct {
	Command string
	Headers struct {
		ContentLength string `json:"content-length,omitempty"`
		ContentType   string `json:"content-type,omitempty"`
		Destination   string `json:"destination,omitempty"`
		Receipt       string `json:"receipt,omitempty"`
		Subscription  string `json:"subscription,omitempty"`
		MessageID     string `json:"message-id,omitempty"`
		Version       string `json:"version,omitempty"`
		HeartBeat     string `json:"heart-beat,omitempty"`
	}
	Body interface{}
}

StreamMessage returns the Command, Headers, and Body of a stomp message

type Transaction

type Transaction struct {
	TimeStamp int
	Amount    int
	Signature string
	Fee       int
	Recipient string
	Type      int
	Deadline  int
	Message   message
	Version   int
	Signer    string
}

Transaction contains information about a transaction

type TransactionMetadata

type TransactionMetadata struct {
	ID     int
	Height int
	// TODO(tyler): This need custom unmarshal
	Hash hash
}

TransactionMetadata contains metadata about a transaction

type TransactionMetadataPair

type TransactionMetadataPair struct {
	Meta        TransactionMetadata
	Transaction Transaction
}

TransactionMetadataPair is a set of metadata and transaction details about a specific transaction

Notes

Bugs

  • All float64 in Block should be int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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