diff --git a/indexer/examples/p-chain/main.go b/indexer/examples/p-chain/main.go new file mode 100644 index 000000000000..257591f45b0f --- /dev/null +++ b/indexer/examples/p-chain/main.go @@ -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++ + } +} diff --git a/indexer/examples/x-chain-blocks/main.go b/indexer/examples/x-chain-blocks/main.go index 64c40f4597b0..a995f9612bbd 100644 --- a/indexer/examples/x-chain-blocks/main.go +++ b/indexer/examples/x-chain-blocks/main.go @@ -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 }