chaincode:simpledemo

NO IMAGE

package main
import (
"errors"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
)
type SimpleChaincode struct {
}
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
var A, B string    //Entities
var Aval, Bval int //Asset
var err error
if len(args) != 4 {
return nil, errors.New("Incorrect number of arguments. Expecting 4")
}
//Initialize the chiancode
A = args[0]
Aval, err = strconv.Atoi(args[1])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
B = args[2]
Bval, err = strconv.Atoi(args[3])
if err != nil {
return nil, errors.New("Expecting integer value for asset holding")
}
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
//Write the state to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return nil, err
}
return nil, nil
}
//Transaction
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function == "delete" {
return t.delete(stub, args)
}
var A, B string    //Entities
var Aval, Bval int //Asset holdings
var X int          //Transaction value
var err error
if len(args) != 3 {
return nil, errors.New("Incorrect number of arguments. Expecting 3")
}
A = args[0]
B = args[1]
//Get the state from the ledger
Avalbytes, err := stub.GetState(A)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Avalbytes == nil {
return nil, errors.New("Entity not found")
}
Aval, _ = strconv.Atoi(string(Avalbytes))
Bvalbytes, err := stub.GetState(B)
if err != nil {
return nil, errors.New("Failed to get state")
}
if Bvalbytes == nil {
return nil, errors.New("Entity not found")
}
Bval, _ = strconv.Atoi(string(Bvalbytes))
//Perform the execution
X, err = strconv.Atoi(args[2])
if err != nil {
return nil, errors.New("Invalid transaction amount, expecting a integer value")
}
Aval = Aval - X
Bval = Bval   X
fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
//Write the state back to the ledger
err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
if err != nil {
return nil, err
}
err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
if err != nil {
return nil, err
}
return nil, nil
}
//Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting 1")
}
A := args[0]
err := stub.DelState(A)
if err != nil {
return nil, errors.New("Failed to delete state")
}
return nil, nil
}
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
var A string
var err error
if len(args) != 1 {
return nil, errors.New("Incorrect number of arguments. Expecting name of the person to query")
}
A = args[0]
//Get the state from the leger
Avalbytes, err := stub.GetState(A)
if err != nil {
jsonResp := "{\"Error\":\"Failed to get state for "   A   "\"}"
return nil, errors.New(jsonResp)
}
if Avalbytes == nil {
jsonResp := "{\"ERROR\":\"Nil amount for "   A   "\"}"
return nil, errors.New(jsonResp)
}
jsonResp := "{\"Name\":\""   A   "\",\"Amount\":\""   string(Avalbytes)   "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return Avalbytes, nil
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}

package main
import (
"errors"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/util"
)
type SimpleChaincode struct {
}
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
var sum string
var sumVal int
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
sum = args[0]
sumVal, err = strconv.Atoi(args[1])
if err != nil {
return nil, errors.New("Expecting integer value for sum")
}
fmt.Printf("sumVal=%d\n", sumVal)
//wirte the state to the ledger
err = stub.PutState(sum, []byte(strconv.Itoa(sumVal)))
if err != nil {
return nil, err
}
return nil, nil
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
var sum string
var Aval, Bval, sumVal int
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
chaincodeURL := args[0]
sum = args[1]
//Query
f := "query"
queryArgs := util.ToChaincodeArgs(f, "a")
response, err := stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Aval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
queryArgs = util.ToChaincodeArgs(f, "b")
response, err = stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Bval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
//compute sum
sumVal = Aval   Bval
//write ledger
err = stub.PutState(sum, []byte(strconv.Itoa(sumVal)))
if err != nil {
return nil, err
}
fmt.Printf("Invoke chaincode successful. Got sum %d\n", sumVal)
return []byte(strconv.Itoa(sumVal)), nil
}
//Query
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
var sum string
var Aval, Bval, sumVal int
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
chaincodeURL := args[0]
sum = args[1]
//query
f := "query"
queryArgs := util.ToChaincodeArgs(f, "a")
response, err := stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Aval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
queryArgs = util.ToChaincodeArgs(f, "b")
response, err = stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Bval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
// Compute sum
sumVal = Aval   Bval
fmt.Printf("Query chaincode successful. Got sum %d\n", sumVal)
jsonResp := "{\"Name\":\""   sum   "\",\"Value\":\""   strconv.Itoa(sumVal)   "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return []byte(strconv.Itoa(sumVal)), nil
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}