1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00
thalos/api
Henrik Hautakoski 5b02dfa53f jsontime: use struct tags instead of setting default format.
Some other package (antelope-go in this case) also sets the default
format and therefore it clashes with our code.
2024-06-22 16:19:48 +02:00
..
message jsontime: use struct tags instead of setting default format. 2024-06-22 16:19:48 +02:00
redis api/redis/subscriber.go: in worker() no need to spawn a goroutine when sending to the channel. 2024-02-07 17:29:54 +01:00
channel.go Merge branch 'table-deltas' 2024-01-21 14:01:08 +01:00
channel_test.go api/channel.go: adding TableDelta channel 2024-01-07 19:11:55 +01:00
client.go Minor fixes. 2024-03-07 18:09:14 +01:00
client_test.go api/client.go: Rework to use a channel instead of callback. 2024-02-04 22:49:49 +01:00
go.mod api: update dependancies 2024-02-02 17:05:29 +01:00
go.sum api: update dependancies 2024-02-02 17:05:29 +01:00
reader.go api/reader.go: Document io.EOF 2024-02-07 17:30:19 +01:00
README.md api/README.md: update. 2024-03-05 15:00:04 +01:00

Thalos Golang API

Usage

The api is designed with go channels.

Example

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/eosswedenorg/thalos/api"
	"github.com/eosswedenorg/thalos/api/message"
	_ "github.com/eosswedenorg/thalos/api/message/json"
	api_redis "github.com/eosswedenorg/thalos/api/redis"
	"github.com/redis/go-redis/v9"
)

func main() {
	// Create redis client
	rdb := redis.NewClient(&redis.Options{})

	sub := api_redis.NewSubscriber(context.Background(), rdb, api_redis.Namespace{
		Prefix:  "ship",
		ChainID: "1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4", // Wax mainnet.
	})

    // Create thalos client
    codec, err := message.GetCodec("json")
    if err != nil {
        fmt.Println("Failed to get json codec")
        return
    }

    client := api.NewClient(sub, codec.Decoder)

    // Subscribe to some channels.
    err = client.Subscribe(
        api.TransactionChannel,
        api.ActionChannel{Contract: "eosio"}.Channel(),
        api.ActionChannel{Name: "mine"}.Channel(),
        api.HeartbeatChannel,
        api.TableDeltaChannel{}.Channel(),
    )

    if err != nil {
        fmt.Println(err)
        return
    }

    // Wait for interrupt in a go routine and close the client.
    go func() {
        sig := make(chan os.Signal)
        signal.Notify(sig, os.Interrupt)

        <-sig
        fmt.Println("Got interrupt")

        client.Close()
    }()

    // Read messages
    for t := range client.Channel() {
        switch msg := t.(type) {
        case error:
            fmt.Println("Error:", msg)
        case message.TransactionTrace:
            fmt.Println("Transaction", msg.BlockNum, msg.ID)
            fmt.Println(msg)
            fmt.Println("---")
        case message.HeartBeat :
            fmt.Println("Heartbeat")
            fmt.Println(msg)
            fmt.Println("---")
        }
    }
}

Message channels and types

There are several types of channels to subscribe to aswell with their respectivly message types.

NOTE: this is not the same as an go channel. all messages will be posted to the same go channel that can be accessed by client.Channel()

Channel type Message type Description
- error Posted if an error occured on the client. There is no channel to subscribe to. error messages will always be posted.
HeartbeatChannel HeartBeat Heartbeat message. Used to know if thalos is there or not if messages are not posted frequently on real channels.
RollbackChannel RollbackMessage This message is posted if the chain has experienced a microfork.
TransactionChannel TransactionTrace Information about an transaction
ActionChannel ActionTrace Information about an action
TableDeltaChannel TableDelta Information about an table change