Example Usages
Example Usages
This section contains examples of how Token Transfer Processor can be used in your application logic.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
This section contains examples of how Token Transfer Processor can be used in your application logic.
go mod init example/ttp_processor
go get github.com/stellar/go-stellar-sdk@latest
go mod tidy
package main
import (
"context"
"fmt"
"github.com/stellar/go-stellar-sdk/ingest/ledgerbackend"
"github.com/stellar/go-stellar-sdk/processors/token_transfer"
"github.com/stellar/go-stellar-sdk/support/log"
"github.com/stellar/go-stellar-sdk/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))
}