Skip to content

Instantly share code, notes, and snippets.

@containerman17
Created September 26, 2024 11:48
Show Gist options
  • Save containerman17/d3369075e8e89c5ce8b0da0178c54e7b to your computer and use it in GitHub Desktop.
Save containerman17/d3369075e8e89c5ce8b0da0178c54e7b to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/ava-labs/hypersdk-starter/consts"
"github.com/ava-labs/hypersdk-starter/vm"
"github.com/ava-labs/hypersdk/api/ws"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/pubsub"
)
var _ chain.Parser = (*MiniParser)(nil)
type MiniParser struct {
}
func (p *MiniParser) Rules(int64) chain.Rules {
return nil // Implement if needed
}
func (p *MiniParser) ActionRegistry() chain.ActionRegistry {
return vm.ActionParser
}
func (p *MiniParser) OutputRegistry() chain.OutputRegistry {
return vm.OutputParser
}
func (p *MiniParser) AuthRegistry() chain.AuthRegistry {
return vm.AuthParser
}
func main() {
scli, err := ws.NewWebSocketClient("http://localhost:9650/ext/bc/"+consts.Name, ws.DefaultHandshakeTimeout, pubsub.MaxPendingMessages, pubsub.MaxReadMessageSize)
if err != nil {
log.Fatal(err)
}
defer scli.Close()
if err := scli.RegisterBlocks(); err != nil {
log.Fatal(err)
}
var lastBlockTime time.Time
for {
block, _, _, err := scli.ListenBlock(context.Background(), &MiniParser{})
if err != nil {
log.Printf("Error listening for block: %v", err)
continue
}
now := time.Now()
if !lastBlockTime.IsZero() {
timeSinceLastBlock := now.Sub(lastBlockTime)
transactions := make([]string, len(block.Txs))
for i, tx := range block.Txs {
transactions[i] = fmt.Sprintf("%+v", *tx)
}
fmt.Printf("Time since last block: %v\nBlock transactions: %+v\n", timeSinceLastBlock, transactions)
}
lastBlockTime = now
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment