Transaction Stream

Setting up Mevlink to get a transaction stream wherever you need it

In your project, make a new directory mevlink and a file called stream.go. This is where we will import and set up streamer-go.

For each transaction we forward to you, you'll get:

  • The raw rlp-encoded transaction bytes

  • The nullable transaction hash (when using the latest version)

  • The time at which the Mevlink node first learned of the transaction's existence (for example, via a NEW_POOLED_TRANSACTION_HASHES request)

  • The time at which the Mevlink node sent the transaction to you over the TCP stream

Imports

First we'll import streamer-go as mlstreamer plus some other packages we'll be making use of to parse the incoming transaction bytes.

// mevlink/stream.go

package main

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

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

Create Streamer

Then in main we'll set up the mlstreamer using the API key credentials you can find here once you've created an account. The API Key Secret is only shown once during the creation of your API Key.

// mevlink/stream.go


func main() {
  var str = mlstreamer.NewStreamer("<api-key-id>", "<api-key-secret>", 1)
}

In the example above, the third parameter, 1 represents the network id for which you want to receive and emit transactions. Currently we only support two networks:

Transaction Callback

Use the transaction callback to perform some action (like adding the transaction to Geth's transaction pool) every time you receive a transaction:

// mevlink/stream.go

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)
  })
}

The ordering of the emitted fields, timing information, and MAC is such that you can choose to consider/respond to transactions before you have timing information or have verified the authentication signature.

In the case that you have your own custom transaction cache, you can also use the transaction hash field to check your cache without having to decode the transaction bytes.

At this point, you can do whatever you'd like with the transaction. However, be aware that the transaction itself has not been verified on our end and the node stream gives no guarantee of transactions being valid or that they will be successfully processed on-chain. All it does is provide transaction that have been seen via P2P node connections.

To verify them using geth or a geth fork, you can add them to geth's transaction pool and check for validation errors. We'll go over one way of doing that in the step.

Full Snippet

Here's a quick and simple example of how you can use streamer-go. Make sure to replace the <api-key-id> and <api-key-secret> with your own credentials after signing up and purchasing a plan on mevlink.com.

// 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())
}

Output

Here's an example of a transaction log we get using the snippet above:

> Got tx 'cc21416c132d6f26524f26a0477df594de057181771fdcba448fcf2ef3b5ace0'! Was noticed on  2023-01-31 19:20:07.205217 +0000 UTC and sent on 2023-01-31 19:20:07.205251 +0000 UTC
> Got tx 'c4218209ed0d96ef3cc61826a2ca2683e6ebd4323d3d9c95e503e76c51e28139'! Was noticed on  2023-01-31 19:20:42.285827 +0000 UTC and sent on 2023-01-31 19:20:42.292164 +0000 UTC
> Got tx '81ef01fff8e733ddc0b7d84b9e63242ec12757e0c8b39316989569bc6299105c'! Was noticed on  2023-01-31 19:21:17.490186 +0000 UTC and sent on 2023-01-31 19:21:17.49043 +0000 UTC

Last updated