fabric-samples/off_chain_data/application-go/getAllAssets.go
Stanislav Jakuschevskij 7244639e7c
Add off-chain-data go client application
Created project structure, fixed typos. Implemented connect.go and
getAllAssets.go. The latter uses an assetTransferBasic struct which
provides a simple API for basic asset operations like create, transfer,
etc.

Added transact.go with some util functions. Using google uuid package to
generate random UUIDs for the transactions.

Implemented pretty printing of JSON results.

Implemented app.go entry point with error handling. The existing
commands are getAllAssets, transact and listen. They can be called from
the command line via: "go run . <command> <command> ...". They will be
executed in order and if a command is not known an the application
panics and aborts before executing any of the commands.

Implementing listen.go. Added checkpointer, context setups, call to
BlockEvents and all the interfaces needed for parsing. Started
implementing the interfaces needed to represent a block bottom up in
structs. Finished NamespaceReadWriteSet, ReadWriteSet and
EndorserTransaction.

Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
2025-02-24 13:14:46 +01:00

39 lines
858 B
Go

/*
* Copyright 2024 IBM All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-gateway/pkg/client"
"google.golang.org/grpc"
)
func getAllAssets(clientConnection *grpc.ClientConn) {
id, options := newConnectOptions(clientConnection)
gateway, err := client.Connect(id, options...)
if err != nil {
panic((err))
}
defer gateway.Close()
contract := gateway.GetNetwork(channelName).GetContract(chaincodeName)
smartContract := newAssetTransferBasic(contract)
assets := smartContract.getAllAssets()
fmt.Printf("%s\n", formatJSON(assets))
}
func formatJSON(data []byte) string {
var result bytes.Buffer
if err := json.Indent(&result, data, "", " "); err != nil {
panic(fmt.Errorf("failed to parse JSON: %w", err))
}
return result.String()
}