Skip to content

Commit

Permalink
fix(examples): pass full inputs to functions
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 committed Sep 14, 2024
1 parent b842edb commit 43b483a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
21 changes: 15 additions & 6 deletions examples/query/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
case "readParams":
readParams(ctx, client)
case "readUtxos":
readUtxos(ctx, client, "71a7498f086d378ec5e558581286629b678be1dd65d5d4e2a5d634ba6fdf8299")
readUtxos(ctx, client, "71a7498f086d378ec5e558581286629b678be1dd65d5d4e2a5d634ba6fdf8299", 0)
case "searchUtxos":
searchUtxos(ctx, client, "60c0359ebb7d0688d79064bd118c99c8b87b5853e3af59245bb97e84d2")
default:
Expand All @@ -49,15 +49,23 @@ func readParams(ctx context.Context, client *utxorpc.UtxorpcClient) {
utxorpc.HandleError(err)
}
fmt.Printf("Response: %+v\n", resp)

if resp.Msg.LedgerTip != nil {
fmt.Printf("Ledger Tip: Slot: %d, Hash: %x\n", resp.Msg.LedgerTip.Slot, resp.Msg.LedgerTip.Hash)
}
if resp.Msg.Values != nil {
fmt.Printf("Cardano: %+v\n", resp.Msg.Values)
}
}

func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr string) {
func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr string, txIndex uint32) {
txHash, err := hex.DecodeString(txHashStr)
if err != nil {
log.Fatalf("failed to decode hex string: %v", err)
}
txoRef := &query.TxoRef{
Hash: txHash,
Hash: txHash,
Index: txIndex,
}

req := connect.NewRequest(&query.ReadUtxosRequest{
Expand All @@ -70,6 +78,8 @@ func readUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, txHashStr str
utxorpc.HandleError(err)
}

fmt.Printf("Response: %+v\n", resp)

if resp.Msg.LedgerTip != nil {
fmt.Printf("Ledger Tip:\n Slot: %d\n Hash: %x\n", resp.Msg.LedgerTip.Slot, resp.Msg.LedgerTip.Hash)
}
Expand Down Expand Up @@ -101,9 +111,6 @@ func searchUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, rawAddress
Address: &cardano.AddressPattern{
ExactAddress: exactAddress,
},
Asset: &cardano.AssetPattern{
// Populate the fields as necessary
},
},
},
},
Expand All @@ -117,6 +124,8 @@ func searchUtxos(ctx context.Context, client *utxorpc.UtxorpcClient, rawAddress
utxorpc.HandleError(err)
}

fmt.Printf("Response: %+v\n", resp)

if resp.Msg.LedgerTip != nil {
fmt.Printf("Ledger Tip:\n Slot: %d\n Hash: %x\n", resp.Msg.LedgerTip.Slot, resp.Msg.LedgerTip.Hash)
}
Expand Down
65 changes: 56 additions & 9 deletions examples/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,69 @@ func main() {
}),
)

// fetchBlock(ctx, client)
followTip(ctx, client, "235f9a217b826276d6cdfbb05c11572a06aef092535b6df8c682d501af59c230", 65017558, nil)
// followTip(ctx, client, "")
// Set mode to "fetchBlock" or "followTip" to select the desired example.
var mode string = "followTip"

switch mode {
case "fetchBlock":
fetchBlock(ctx, client, "235f9a217b826276d6cdfbb05c11572a06aef092535b6df8c682d501af59c230", 65017558, nil)
case "followTip":
followTip(ctx, client, "235f9a217b826276d6cdfbb05c11572a06aef092535b6df8c682d501af59c230", 65017558, nil)
default:
fmt.Println("Unknown mode:", mode)
}
}

func fetchBlock(ctx context.Context, client *utxorpc.UtxorpcClient) {
req := connect.NewRequest(&sync.FetchBlockRequest{})
client.AddHeadersToRequest(req)
func fetchBlock(ctx context.Context, client *utxorpc.UtxorpcClient, blockHash string, blockIndex int64, fieldMaskPaths []string) {
var req *connect.Request[sync.FetchBlockRequest]
var intersect []*sync.BlockRef
var fieldMask *fieldmaskpb.FieldMask

// Construct the BlockRef based on the provided parameters
blockRef := &sync.BlockRef{}
if blockHash != "" {
hash, err := hex.DecodeString(blockHash)
if err != nil {
log.Fatalf("failed to decode hex string: %v", err)
}
blockRef.Hash = hash
}
// We assume blockIndex can be 0 or any positive number
if blockIndex > -1 {
blockRef.Index = uint64(blockIndex)
}

// Only add blockRef to intersect if at least one of blockHash or blockIndex is provided
if blockHash != "" || blockIndex > -1 {
intersect = []*sync.BlockRef{blockRef}
}

// Construct the FieldMask if paths are provided
if len(fieldMaskPaths) > 0 {
fieldMask = &fieldmaskpb.FieldMask{
Paths: fieldMaskPaths,
}
}

// Create the FetchBlockRequest
req = connect.NewRequest(&sync.FetchBlockRequest{
Ref: intersect,
FieldMask: fieldMask,
})

// Print BlockRef details if intersect is provided
if len(intersect) > 0 {
fmt.Printf("Blockref: %d, %x\n", req.Msg.Ref[0].Index, req.Msg.Ref[0].Hash)
}

client.AddHeadersToRequest(req)
fmt.Println("connecting to utxorpc host:", client.URL())
chainSync, err := client.Sync.FetchBlock(ctx, req)
resp, err := client.Sync.FetchBlock(ctx, req)
if err != nil {
utxorpc.HandleError(err)
}
fmt.Println("connected to utxorpc...")
for i, blockRef := range chainSync.Msg.Block {
fmt.Printf("Response: %+v\n", resp)
for i, blockRef := range resp.Msg.Block {
fmt.Printf("Block[%d]:\n", i)
fmt.Printf("Index: %d\n", blockRef.GetCardano().GetHeader().GetSlot())
fmt.Printf("Hash: %x\n", blockRef.GetCardano().GetHeader().GetHash())
Expand Down

0 comments on commit 43b483a

Please sign in to comment.