Examples

Quick copy paste snippets for Mevlink's streamer-go

Make sure to replace the <api-key-id> and <api-key-secret> in these examples with your own credentials after signing up and purchasing a plan on mevlink.com.

Go Streamer

// mevlink/stream.go

package main

import (
	"encoding/hex"
	"log"
	"time"

	mlstreamer "github.com/mevlink/streamer-go"
	"golang.org/x/crypto/sha3"
)

func main() {

    // create streamer
    var str = mlstreamer.NewStreamer("<api-key-id>", "<api-key-secret>", 1)
    
    // transaction callback
    str.OnTransaction(func(txb []byte, hash mlstreamergo.NullableHash, noticed time.Time, propagated time.Time) {
        //Getting the transaction hash and printing the relevant times
        var hasher = sha3.NewLegacyKeccak256()
        hasher.Write(txb)
        var tx_hash = hasher.Sum(nil)

        log.Println("Got tx '" + hex.EncodeToString(tx_hash) + "'! Was noticed on ", noticed, "and sent on", propagated)
    })

    log.Fatal(str.Stream())
}

Propagation Test

// mevlink/stream.go

package main

import (
	"encoding/hex"
	"log"
	"time"

	mlstreamer "github.com/mevlink/streamer-go"
	"golang.org/x/crypto/sha3"
)

func main() {

    // create streamer
    var str = mlstreamer.NewStreamer("<api-key-id>", "<api-key-secret>", 1)
    
    // transaction callback
    str.OnTransaction(func(txb []byte, hash mlstreamergo.NullableHash, noticed time.Time, propagated time.Time) {
        // Parsing the transaction
	tx := new(types.Transaction)
	err := rlp.DecodeBytes(txb, &tx)

	if err != nil {
		log.Println("error decoding ml tx", err)
	} else {
		log.Println("got tx '"+tx.Hash().String()+"'! noticed on ", noticed, "and sent on", propagated)
		if emittedTxs < 2 {
			str.EmitTransaction(txb)
			if err != nil {
				log.Println("error emitting tx: "+tx.Hash().String(), err)
			}
			emittedTxs++
		} else {
			str.Stop()
			log.Println("Done emitting test transactions.")
		}
	}
    })

    log.Fatal(str.Stream())
}

Last updated