hyperledger-template

Hyperledger Fabric Smart-Contract Template

https://github.com/block-foundation/hyperledger-template

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (9.0%) to scientific vocabulary

Keywords

block-foundation blockchain blockfoundation hyperledger hyperledger-fabric smart-contracts template
Last synced: 6 months ago · JSON representation ·

Repository

Hyperledger Fabric Smart-Contract Template

Basic Info
Statistics
  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • Open Issues: 1
  • Releases: 0
Topics
block-foundation blockchain blockfoundation hyperledger hyperledger-fabric smart-contracts template
Created over 2 years ago · Last pushed over 1 year ago
Metadata Files
Readme Changelog Contributing Funding License Code of conduct Citation Codeowners Security Support Governance Authors

README.md

[![GitHub License](https://img.shields.io/github/license/block-foundation/blocktxt?style=flat-square&logo=readthedocs&logoColor=FFFFFF&label=&labelColor=%23041B26&color=%23041B26&link=LICENSE)](https://github.com/block-foundation/hyperledger-template/blob/main/LICENSE) [![devContainer](https://img.shields.io/badge/Container-Remote?style=flat-square&logo=visualstudiocode&logoColor=%23FFFFFF&label=Remote&labelColor=%23041B26&color=%23041B26)](https://vscode.dev/redirect?url=vscode://ms-vscode-remote.remote-containers/cloneInVolume?url=https://github.com/block-foundation/hyperledger-template)

Block Foundation Logo

Hyperledger Template

Block Foundation


Block Foundation Brand

Contents



[![Report a Bug](https://img.shields.io/badge/Report%20a%20Bug-GitHub?style=flat-square&&logoColor=%23FFFFFF&color=%23E1E4E5)](https://github.com/block-foundation/hyperledger-template/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&projects=&template=bug_report.yml) [![Request a Feature](https://img.shields.io/badge/Request%20a%20Feature-GitHub?style=flat-square&&logoColor=%23FFFFFF&color=%23E1E4E5)](https://github.com/block-foundation/hyperledger-template/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&projects=&template=feature_request.yml) [![Ask a Question](https://img.shields.io/badge/Ask%20a%20Question-GitHub?style=flat-square&&logoColor=%23FFFFFF&color=%23E1E4E5)](https://github.com/block-foundation/hyperledger-template/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&projects=&template=question.yml) [![Make a Suggestion](https://img.shields.io/badge/Make%20a%20Suggestion-GitHub?style=flat-square&&logoColor=%23FFFFFF&color=%23E1E4E5)](https://github.com/block-foundation/hyperledger-template/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&projects=&template=suggestion.yml) [![Start a Discussion](https://img.shields.io/badge/Start%20a%20Discussion-GitHub?style=flat-square&&logoColor=%23FFFFFF&color=%23E1E4E5)](https://github.com/block-foundation/hyperledger-template/issues/new?assignees=&labels=Needs%3A+Triage+%3Amag%3A%2Ctype%3Abug-suspected&projects=&template=discussion.yml)

Introduction

Below is a basic example of a chaincode (smart contract) in Hyperledger Fabric. This is written in Go, which is one of the languages you can use to create chaincode in Hyperledger Fabric. This simple example chaincode models assets (items) that can be created, read, updated, deleted, and listed.

``` go package main

import ( "fmt"

"github.com/hyperledger/fabric-contract-api-go/contractapi"

)

type SmartContract struct { contractapi.Contract }

type Item struct { ID string json:"id" Name string json:"name" Price int json:"price" }

func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error { items := []Item{ {ID: "item1", Name: "Item 1", Price: 100}, {ID: "item2", Name: "Item 2", Price: 200}, {ID: "item3", Name: "Item 3", Price: 300}, }

for _, item := range items {
    itemJSON, err := json.Marshal(item)
    if err != nil {
        return err
    }

    err = ctx.GetStub().PutState(item.ID, itemJSON)
    if err != nil {
        return fmt.Errorf("failed to put to world state. %v", err)
    }
}

return nil

}

func (s *SmartContract) CreateItem(ctx contractapi.TransactionContextInterface, id string, name string, price int) error { item := Item{ ID: id, Name: name, Price: price, }

itemJSON, err := json.Marshal(item)
if err != nil {
    return err
}

return ctx.GetStub().PutState(id, itemJSON)

}

func (s SmartContract) ReadItem(ctx contractapi.TransactionContextInterface, id string) (Item, error) { itemJSON, err := ctx.GetStub().GetState(id) if err != nil { return nil, fmt.Errorf("failed to read from world state. %v", err) }

if itemJSON == nil {
    return nil, fmt.Errorf("the asset %s does not exist", id)
}

var item Item
err = json.Unmarshal(itemJSON, &item)
if err != nil {
    return nil, err
}

return &item, nil

}

func (s *SmartContract) UpdateItem(ctx contractapi.TransactionContextInterface, id string, name string, price int) error { item, err := s.ReadItem(ctx, id) if err != nil { return err }

item.Name = name
item.Price = price

itemJSON, err := json.Marshal(item)
if err != nil {
    return err
}

return ctx.GetStub().PutState(id, itemJSON)

}

func (s *SmartContract) DeleteItem(ctx contractapi.TransactionContextInterface, id string) error { exists, err := s.ItemExists(ctx, id) if err != nil { return err } if !exists { return fmt.Errorf("the asset %s does not exist", id) }

return ctx.GetStub().DelState(id)

}

func (s *SmartContract) ItemExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) { itemJSON, err := ctx.GetStub().GetState(id) if err != nil { return false, fmt.Errorf("failed to read from world state. %v", err) }

return itemJSON != nil, nil

}

func (s SmartContract) GetAllItems(ctx contractapi.TransactionContextInterface) ([]Item, error) { resultsIterator, err := ctx.GetStub().GetStateByRange("", "") if err != nil { return nil, err } defer resultsIterator.Close()

var items []*Item
for resultsIterator.HasNext() {
    queryResponse, err := resultsIterator.Next()
    if err != nil {
        return nil, err
    }

    var item Item
    err = json.Unmarshal(queryResponse.Value, &item)
    if err != nil {
        return nil, err
    }
    items = append(items, &item)
}

return items, nil

}

func main() { chaincode, err := contractapi.NewChaincode(new(SmartContract)) if err != nil { fmt.Printf("Error create example chaincode: %s", err.Error()) return }

if err := chaincode.Start(); err != nil {
    fmt.Printf("Error starting example chaincode: %s", err.Error())
}

} ```

This chaincode allows for the following operations:

  1. Initialize the ledger with some items (InitLedger function).
  2. Create an item (CreateItem function).
  3. Read an item (ReadItem function).
  4. Check if an item exists (ItemExists function).
  5. Update an item (UpdateItem function).
  6. Delete an item (DeleteItem function).
  7. Get all items (GetAllItems function).

Each item has an ID, a name, and a price. Remember to use the correct import paths for the fmt, json, and github.com/hyperledger/fabric-contract-api-go/contractapi packages.

Directory structure

The directory structure for a Hyperledger Fabric Chaincode (smart contract):

sh - fabric-item-contract - chaincode - item-contract - item-contract.go // The chaincode file - test - item-contract_test.go // Unit tests for your smart contract - scripts - startFabric.sh // Scripts for setting up and running the network - testAPI.sh // Scripts for testing the API - doc - item-contract.md // Documentation for your chaincode - network - // Contains the configuration for your network - client - // Optional, for client side code if needed - README.md // Main project readme - .gitignore // For excluding files from version control

This is a fairly standard structure for a Fabric chaincode project.

  • Your actual chaincode would live inside the chaincode directory.
  • The test directory contains the tests for your chaincode.
  • The scripts directory contains shell scripts for setting up your environment and running your application.
  • The doc directory contains the documentation for your chaincode.
  • The network directory would contain the configurations for your network.
  • The client directory is optional and would contain the client side code for invoking the chaincode, if any.

These directories and their contents will help keep your project organized and will make it easier for others to understand your work.


Colophon

Authors

This is an open-source project by the Block Foundation.

The Block Foundation mission is enabling architects to take back initiative and contribute in solving the mismatch in housing through blockchain technology. Therefore the Block Foundation seeks to unschackle the traditional constraints and construct middle ground between rent and the rigidity of traditional mortgages.

website: www.blockfoundation.io

Development Resources

Contributing

We'd love for you to contribute and to make this project even better than it is today! Please refer to the contribution guidelines for information.

Legal Information

Copyright

Copyright © 2023 Stichting Block Foundation. All Rights Reserved.

License

Except as otherwise noted, the content in this repository is licensed under the Creative Commons Attribution 4.0 International (CC BY 4.0) License, and code samples are licensed under the Apache 2.0 License.

Also see LICENSE and LICENSE-CODE.

Disclaimer

THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Owner

  • Name: Block Foundation
  • Login: block-foundation
  • Kind: organization
  • Email: info@blockfoundation.io
  • Location: Netherlands

The Block Foundation mission is enabling architects to take back initiative and contribute in solving the mismatch in housing through blockchain technology.

Citation (CITATION.cff)

cff-version: 1.2.0
title: Hyperledger Template
version: 0.0.1
date-released: 2023-07-01
url: "https://github.com/block-foundation/hyperledger-template"
message: >-
  If you use this software, please cite it using
  the metadata from this file.
type: software
authors:
  - given-names: Lars Bastiaan
    family-names: van Vianen
    email: lars@blockfoundation.io
    affiliation: Block Foundation
    orcid: "https://orcid.org/0000-0002-8790-8630"

GitHub Events

Total
Last Year

Packages

  • Total packages: 1
  • Total downloads: unknown
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
proxy.golang.org: github.com/block-foundation/hyperledger-template
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 9.2%
Average: 9.7%
Dependent repos count: 10.3%
Last synced: 7 months ago