Skip to main content

Example Usages

This section contains examples of how Token Transfer Processor can be used in your application logic

Prerequisites

//filename: go.mod

require (
// This currently points to a RC release of Protocol-23.
// Please update it to use the latest stable version once P23 is released to pubnet.
github.com/stellar/go horizonclient-v23.0.0-rc
)

You may want to optionally run go mod tidy in your codebase to pull the latest version of the ingest package.

Helper Code

This section contains some helper code that will be used in all the examples.

package main

import (
"context"
"fmt"

"github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/processors/token_transfer"
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"

"google.golang.org/protobuf/encoding/protojson"
)

func panicIf(err error) {
if err != nil {
panic(err)
}
}

// fetchLedgerFromRPC retrieves a ledger using RPCLedgerBackend
func fetchLedgerFromRPC(ledgerSeq uint32) xdr.LedgerCloseMeta {
ctx := context.Background()

// Using a publicly hosted RPC instance
endpoint := "https://mainnet.sorobanrpc.com"

// Configure the RPC Ledger Backend
backend := ledgerbackend.NewRPCLedgerBackend(ledgerbackend.RPCLedgerBackendOptions{
RPCServerURL: endpoint,
})
defer backend.Close()

// Prepare an unbounded range starting from the latest ledger
if err := backend.PrepareRange(ctx, ledgerbackend.BoundedRange(ledgerSeq, ledgerSeq)); err != nil {
log.Fatalf("Failed to prepare range: %v", err)
}

ledger, err := backend.GetLedger(ctx, ledgerSeq)
panicIf(err)
return ledger
}

func printProtoEvent(event *token_transfer.TokenTransferEvent) {
jsonBytes, _ := protojson.MarshalOptions{
Multiline: true,
EmitDefaultValues: true,
Indent: " ",
}.Marshal(event)
fmt.Printf("### Event Type : %v\n", event.GetEventType())
fmt.Println(string(jsonBytes))
}