mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-19 00:15:08 +00:00
- switch to ClientConnInterface - use command type alias for map of commands - add error return to command functions and handle in app.go - inline formatJSON function in getAllAssets.go - replace most panics with error returns - remove error wrapping in listen.go and further down the line - use strconv.ParseUint instead of ParseFloat - use WithCancelCause in transact.go to grab and propagate error from go routine - unmarshal and return []Asset in atb.GetAllAssets - switch to rand package - remove dependency to protobuf reflect package - switch to sync.OnceValues for caching parser - fixed typo in events sample connect.go Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package contract
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"github.com/hyperledger/fabric-gateway/pkg/client"
|
|
)
|
|
|
|
type AssetTransferBasic struct {
|
|
contract *client.Contract
|
|
}
|
|
|
|
func NewAssetTransferBasic(contract *client.Contract) *AssetTransferBasic {
|
|
return &AssetTransferBasic{contract}
|
|
}
|
|
|
|
func (atb *AssetTransferBasic) CreateAsset(anAsset Asset) error {
|
|
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 {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (atb *AssetTransferBasic) TransferAsset(id, newOwner string) (string, error) {
|
|
result, err := atb.contract.Submit(
|
|
"TransferAsset",
|
|
client.WithArguments(
|
|
id,
|
|
newOwner,
|
|
),
|
|
)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(result), nil
|
|
}
|
|
|
|
func (atb *AssetTransferBasic) DeleteAsset(id string) error {
|
|
if _, err := atb.contract.Submit(
|
|
"DeleteAsset",
|
|
client.WithArguments(
|
|
id,
|
|
),
|
|
); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (atb *AssetTransferBasic) GetAllAssets() ([]Asset, error) {
|
|
assetsRaw, err := atb.contract.Evaluate("GetAllAssets")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(assetsRaw) == 0 {
|
|
return []Asset{}, nil
|
|
}
|
|
|
|
var assets []Asset
|
|
if err := json.Unmarshal(assetsRaw, &assets); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return assets, nil
|
|
}
|