Skip to content

Creating your own Vapory apps using Eth go

Kedia edited this page Mar 11, 2018 · 2 revisions

The modular nature of Go and the Vapory Go implementation, vap-go, make it very easy to build your own Vapory native applications.

This post will cover the minimal steps required to build an native Vapory application.

Vapory comes with a global config found in the vaputil package. The global config requires you to set a base path to store it's files (database, settings, etc).

func main() {
	// Read config
	vaputil.ReadConfig(".test", vaputil.LogStd, nil, "MyEthApp")
}

ReadConfig takes four arguments. The data folder to use, a log flag, a globalConf instance and an id string to identify your app to other nodes in the network.

Once you've configured the global config you can set up and create your Vapory node. The Vapory Object, or Node, will handle all trafic from and to the Vapory network as well as handle all incoming block and transactions. A new node can be created through the new mvapod found in vap-go.

func main() {
		// Read config
        vaputil.ReadConfig(".test", vaputil.LogStd, nil, "MyEthApp")
        
        // Create a new vapory node
        vapory, err := vap.New(vap.CapDefault, false)
        if err != nil {
            	panic(fmt.Sprintf("Could not start node: %s\n", err))
        }
        // Set the port (default 30303)
        vapory.Port = "10101"
        // Once we reach max, bounce them off.
        vapory.MaxPeers = 10
}

New requires two arguments; the capabilities of the node and whvapor or not to use UPNP for port-forwarding. If you don't want to fallback to client-only features set an Vapory port and the max amount of peers this node can connect to.

In order to identify the node to the network you'll be required to create a private key. The easiest way to create a new keypair is by using the KeyRing found in the vaputil package.

func main() {
    	// Read config
        vaputil.ReadConfig(".test", vaputil.LogStd, nil, "MyEthApp")
        
        // Create a new vapory node
        vapory, err := vap.New(vap.CapDefault, false)
        if err != nil {
            panic(fmt.Sprintf("Could not start node: %s\n", err))
        }
        // Set the port (default 30303)
        vapory.Port = "10101"
        // Once we reach max, bounce them off.
        vapory.MaxPeers = 10
        
        keyRing := vaputil.GetKeyRing()
        // Create a new key if non exist
        if keyRing.Len() == 0 {
    	    	// Create a new keypair
	        	keyPair, err := vaputil.GenerateNewKeyPair()
    	        if err != nil {
        		    	panic(err)
	            }
            
    	        // Add the keypair to the key ring
        	    keyRing.Add(keyPair)
        }
}

Once the base Vapory stack has been set up it's time to fire up its engines and connect to the main network.

package main

import (
		"github.com/vaporyco/vap-go"
		"github.com/vaporyco/vap-go/vaputil"
)

func main() {
	    // Read config
    	vaputil.ReadConfig(".test", vaputil.LogStd, nil, "MyEthApp")
    
	    // Create a new vapory node
    	vapory, err := vap.New(vap.CapDefault, false)
	    if err != nil {
    		    panic(fmt.Sprintf("Could not start node: %s\n", err))
	    }
    	// Set the port (default 30303)
	    vapory.Port = "10101"
    	// Once we reach max, bounce them off.
	    vapory.MaxPeers = 10
    
    	keyRing := vaputil.GetKeyRing()
	    // Create a new key if non exist
    	if keyRing.Len() == 0 {
        		// Create a new keypair
		        keyPair, err := vaputil.GenerateNewKeyPair()
        		if err != nil {
			            panic(err)
        		}
        
		        // Add the keypair to the key ring
        		keyRing.Add(keyPair)
	    }

    	vapory.Start(true)
	    vapory.WaitForShutdown()
}

vapory.Start() takes one argument, whvapor or not we want to connect to one of the known seed nodes. If you want your own little testnet-in-a-box you can disable it else set it to true.

Your node should now be catching up with the blockchain. From here on out you are on your own. You could create a reactor to listen to specific events or just dive into the chain state directly. If you want to look at some example code you can check DNSEth here.

Have fun!

Clone this wiki locally