mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 15:35:09 +00:00
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>
93 lines
1.7 KiB
Go
93 lines
1.7 KiB
Go
/*
|
|
* Copyright 2024 IBM All Rights Reserved.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
package main
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hyperledger/fabric-gateway/pkg/client"
|
|
)
|
|
|
|
type Asset struct {
|
|
ID string
|
|
Color string
|
|
Size uint64
|
|
Owner string
|
|
AppraisedValue uint64
|
|
}
|
|
|
|
func NewAsset() Asset {
|
|
id, err := uuid.NewRandom()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return Asset{
|
|
ID: id.String(),
|
|
Color: randomElement(colors),
|
|
Size: uint64(randomInt(maxInitialSize) + 1),
|
|
Owner: randomElement(owners),
|
|
AppraisedValue: uint64(randomInt(maxInitialValue) + 1),
|
|
}
|
|
}
|
|
|
|
type assetTransferBasic struct {
|
|
contract *client.Contract
|
|
}
|
|
|
|
func newAssetTransferBasic(contract *client.Contract) *assetTransferBasic {
|
|
return &assetTransferBasic{contract}
|
|
}
|
|
|
|
func (atb *assetTransferBasic) createAsset(anAsset Asset) {
|
|
if _, err := atb.contract.Submit(
|
|
"CreateAsset",
|
|
client.WithArguments(
|
|
anAsset.ID,
|
|
anAsset.Color,
|
|
strconv.FormatUint(anAsset.Size, 10),
|
|
anAsset.Owner,
|
|
strconv.FormatUint(anAsset.AppraisedValue, 10),
|
|
)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (atb *assetTransferBasic) transferAsset(id, newOwner string) string {
|
|
result, err := atb.contract.Submit(
|
|
"TransferAsset",
|
|
client.WithArguments(
|
|
id,
|
|
newOwner,
|
|
),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return string(result)
|
|
}
|
|
|
|
func (atb *assetTransferBasic) deleteAsset(id string) {
|
|
if _, err := atb.contract.Submit(
|
|
"DeleteAsset",
|
|
client.WithArguments(
|
|
id,
|
|
),
|
|
); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func (atb *assetTransferBasic) getAllAssets() []byte {
|
|
result, err := atb.contract.Evaluate("GetAllAssets")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return result
|
|
}
|