Example Usages
This section contains examples of how Token Transfer Processor can be used in your application logic
Prerequisites
You have an existing go module in current directory or create a new module locally for this example exercise.
go mod init example/ttp_processor
Include the Stellar Ingest SDK into your go application module which contains the Token Transfer Processor.
go get github.com/stellar/go-stellar-sdk@latest
go mod tidy
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-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))
}