github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/examples/chaincode/go/chaincode_example01/chaincode_example01.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  
    23  	"github.com/hyperledger/fabric/core/chaincode/shim"
    24  	pb "github.com/hyperledger/fabric/protos/peer"
    25  )
    26  
    27  // SimpleChaincode example simple Chaincode implementation
    28  type SimpleChaincode struct {
    29  }
    30  
    31  var A, B string
    32  var Aval, Bval, X int
    33  
    34  // Init callback representing the invocation of a chaincode
    35  // This chaincode will manage two accounts A and B and will transfer X units from A to B upon invoke
    36  func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
    37  	var err error
    38  	_, args := stub.GetFunctionAndParameters()
    39  	if len(args) != 4 {
    40  		return shim.Error("Incorrect number of arguments. Expecting 4")
    41  	}
    42  
    43  	// Initialize the chaincode
    44  	A = args[0]
    45  	Aval, err = strconv.Atoi(args[1])
    46  	if err != nil {
    47  		return shim.Error("Expecting integer value for asset holding")
    48  	}
    49  	B = args[2]
    50  	Bval, err = strconv.Atoi(args[3])
    51  	if err != nil {
    52  		return shim.Error("Expecting integer value for asset holding")
    53  	}
    54  	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
    55  
    56  	/************
    57  			// Write the state to the ledger
    58  			err = stub.PutState(A, []byte(strconv.Itoa(Aval))
    59  			if err != nil {
    60  				return nil, err
    61  			}
    62  
    63  			stub.PutState(B, []byte(strconv.Itoa(Bval))
    64  			err = stub.PutState(B, []byte(strconv.Itoa(Bval))
    65  			if err != nil {
    66  				return nil, err
    67  			}
    68  	************/
    69  	return shim.Success(nil)
    70  }
    71  
    72  func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
    73  	// Transaction makes payment of X units from A to B
    74  	X, err := strconv.Atoi(args[0])
    75  	if err != nil {
    76  		fmt.Printf("Error convert %s to integer: %s", args[0], err)
    77  		return shim.Error(fmt.Sprintf("Error convert %s to integer: %s", args[0], err))
    78  	}
    79  	Aval = Aval - X
    80  	Bval = Bval + X
    81  	ts, err2 := stub.GetTxTimestamp()
    82  	if err2 != nil {
    83  		fmt.Printf("Error getting transaction timestamp: %s", err2)
    84  		return shim.Error(fmt.Sprintf("Error getting transaction timestamp: %s", err2))
    85  	}
    86  	fmt.Printf("Transaction Time: %v,Aval = %d, Bval = %d\n", ts, Aval, Bval)
    87  	return shim.Success(nil)
    88  }
    89  
    90  func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
    91  	function, args := stub.GetFunctionAndParameters()
    92  	if function == "invoke" {
    93  		return t.invoke(stub, args)
    94  	}
    95  
    96  	return shim.Error("Invalid invoke function name. Expecting \"invoke\"")
    97  }
    98  
    99  func main() {
   100  	err := shim.Start(new(SimpleChaincode))
   101  	if err != nil {
   102  		fmt.Printf("Error starting Simple chaincode: %s", err)
   103  	}
   104  }