Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions indexer/examples/p-chain/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"fmt"
"log"
"time"

"github.com/ava-labs/avalanchego/indexer"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/proposervm/block"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

// This example program continuously polls for the next P-Chain block
// and prints the ID of the block and its transactions.
func main() {
var (
uri = fmt.Sprintf("%s/ext/index/P/block", primary.LocalAPIURI)
client = indexer.NewClient(uri)
ctx = context.Background()
nextIndex uint64
)
for {
container, err := client.GetContainerByIndex(ctx, nextIndex)
if err != nil {
time.Sleep(time.Second)
log.Printf("polling for next accepted block\n")
continue
}

platformvmBlockBytes := container.Bytes
proposerVMBlock, err := block.Parse(container.Bytes)
if err == nil {
platformvmBlockBytes = proposerVMBlock.Block()
}

platformvmBlock, err := blocks.Parse(blocks.Codec, platformvmBlockBytes)
if err != nil {
log.Fatalf("failed to parse platformvm block: %s\n", err)
}

acceptedTxs := platformvmBlock.Txs()
log.Printf("accepted block %s with %d transactions\n", platformvmBlock.ID(), len(acceptedTxs))

for _, tx := range acceptedTxs {
log.Printf("accepted transaction %s\n", tx.ID())
}

nextIndex++
}
}
2 changes: 1 addition & 1 deletion indexer/examples/x-chain-blocks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func main() {
nextIndex uint64
)
for {
log.Printf("polling for next accepted block\n")
container, err := client.GetContainerByIndex(ctx, nextIndex)
if err != nil {
time.Sleep(time.Second)
log.Printf("polling for next accepted block\n")
continue
}

Expand Down