Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add P-chain indexer API example #1271

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 54 additions & 0 deletions indexer/examples/p-chain/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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"
)

// 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", "https://indexer-demo.avax.network")
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
client = indexer.NewClient(uri)
ctx = context.Background()
nextIndex uint64 = 2_000_000
)
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