Giter Club home page Giter Club logo

viz-go-lib's Introduction

viz-go-lib

GoDoc Go Report Card

Golang RPC client library for Viz.

Usage

import "github.com/biter777/viz-go-lib"

Example

This is just a code snippet. Please check the examples directory for more complete and ready to use examples.

	cls,_ := client.NewClient("wss://testnet.viz.world")
	defer cls.Close()

	// Get config.
	log.Println("---> GetConfig()")
	config, err := cls.Database.GetConfig()
	if err != nil {
		return err
	}

	// Use the last irreversible block number as the initial last block number.
	props, err := cls.Database.GetDynamicGlobalProperties()
	if err != nil {
		return err
	}
	lastBlock := props.LastIrreversibleBlockNum

	// Keep processing incoming blocks forever.
	log.Printf("---> Entering the block processing loop (last block = %v)\n", lastBlock)
	for {
		// Get current properties.
		props, err := cls.Database.GetDynamicGlobalProperties()
		if err != nil {
			return err
		}

		// Process new blocks.
		for props.LastIrreversibleBlockNum-lastBlock > 0 {
			block, err := cls.Database.GetBlock(lastBlock)
			if err != nil {
				return err
			}

			// Process the transactions.
			for _, tx := range block.Transactions {
				for _, operation := range tx.Operations {
					switch op := operation.Data().(type) {
					case *types.VoteOperation:
						log.Printf("@%v voted for @%v/%v\n", op.Voter, op.Author, op.Permlink)

						// You can add more cases here, it depends on
						// what operations you actually need to process.
					}
				}
			}

			lastBlock++
		}

		// Sleep for CHAIN_BLOCK_INTERVAL seconds before the next iteration.
		time.Sleep(time.Duration(config.BlockInterval) * time.Second)
	}

Package Organisation

You need to create a Client object to be able to do anything. Then you just need to call NewClient().

Once you create a Client object, you can start calling the methods exported via vizd's RPC endpoint by invoking associated methods on the client object. There are multiple APIs that can be exported, e.g. database and network_broadcast, so the methods on the Client object are also namespaced accoding to these APIs. For example, to call get_block from database, you need to use Client.Database.GetBlock method.

When looking for a method to call, all you need is to turn the method name into CamelCase, e.g. get_config becomes Client.Database.GetConfig.

Status

This package is still under rapid development and it is by no means complete. For now there is no promise considering API stability. Some response objects maybe be typed incorrectly. The package is already usable, though. See the examples directory.

To check the API coverate, please check the README files in the relevat API package in the apis subdirectory.

License

MIT, see the LICENSE file.

viz-go-lib's People

Contributors

biter777 avatar asuleymanov avatar chiliec avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.