Updated on 2022-09-08 GMT+08:00

Sample Chaincode (1.4)

The following is an example of installing and instantiating the account transfer chaincode (1.4). For details about how to debug this chaincode, see the official Fabric examples.
package main 
 
import ( 
   "fmt" 
   "strconv" 
   "github.com/hyperledger/fabric/core/chaincode/shim"
   pb "github.com/hyperledger/fabric/protos/peer" 
)

type SimpleChaincode struct {
}

//Automatically invoked during chaincode instantiation or update to initialize the data status.
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
	//The output information of the println function is displayed in the logs of the chaincode container.
	fmt.Println("ex02 Init")

	//Obtain the parameters transferred by the user to invoke the chaincode.
	_, args := stub.GetFunctionAndParameters()

	var A, B string //Two accounts
	var Aval, Bval int //Balances of the two accounts
	var err error

	//Check whether the number of parameters is 4. If not, an error message is returned.
	if len(args) != 4 {
		return shim.Error("Incorrect number of arguments. Expecting 4")
	}

	A = args[0] //Username of account A
 	Aval, err = strconv.Atoi(args[1]) //Balance in account A
	if err != nil {
		return shim.Error("Expecting integer value for asset holding")
	}

	B = args[2] //Username of account B
	Bval, err = strconv.Atoi(args[3])  //Balance in account B
	if err != nil {
		return shim.Error("Expecting integer value for asset holding")
	}

	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

	//Write the status of account A to the ledger.
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		return shim.Error(err.Error())
	}

	//Write the status of account B to the ledger.
	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
	if err != nil {
		return shim.Error(err.Error())
	}

	return shim.Success(nil)
}

//The function is automatically invoked to query or invoke the ledger data.
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
	fmt.Println("ex02 Invoke")
	//Obtain the function name and parameters transferred by the user to invoke the chaincode.
	function, args := stub.GetFunctionAndParameters()

 	//Check the obtained function name.
	if function == "invoke" {
		//Invoke the invoke function to implement the money transfer operation.
		return t.invoke(stub, args)
	} else if function == "delete" {
		//Invoke the delete function to deregister the account.
		return t.delete(stub, args)
	} else if function == "query" {
		//Invoke the query interface to query the account.
		return t.query(stub, args)
	}
	//If the transferred function name is incorrect, shim.Error() is returned.
	return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
}

//Transfer money between accounts.
func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var A, B string //Accounts A and B
	var Aval, Bval int //Account balance
	var X int //Transfer amount
	var err error

	if len(args) != 3 {
		return shim.Error("Incorrect number of arguments. Expecting 3")
	}

	A = args[0]       //Username of account A
	B = args[1]       //Username of account B

	//Obtain the balance of account A from the ledger.
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		return shim.Error("Failed to get state")
	}
	if Avalbytes == nil {
		return shim.Error("Entity not found")
	}
	Aval, _ = strconv.Atoi(string(Avalbytes))

	//Obtain the balance of account B from the ledger.
	Bvalbytes, err := stub.GetState(B)
	if err != nil {
		return shim.Error("Failed to get state")
	}
	if Bvalbytes == nil {
		return shim.Error("Entity not found")
	}
	Bval, _ = strconv.Atoi(string(Bvalbytes))

	//X indicates the transfer amount.
	X, err = strconv.Atoi(args[2])
	if err != nil {
		return shim.Error("Invalid transaction amount, expecting a integer value")
	}
	//Transfer
	Aval = Aval - X
	Bval = Bval + X
	fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)

	//Update the balance of account A after the transfer.
	err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
	if err != nil {
		return shim.Error(err.Error())
	}

	//Update the balance of account B after the transfer.
	err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
	if err != nil {
		return shim.Error(err.Error())
	}

	return shim.Success(nil)
}

//Account deregistration
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	if len(args) != 1 {
		return shim.Error("Incorrect number of arguments. Expecting 1")
	}

	A = args[0] //Username

	//Delete the account status from the ledger.
	err := stub.DelState(A)
	if err != nil {
		return shim.Error("Failed to delete state")
	}

	return shim.Success(nil)
}

//Account query
func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
	var A string
	var err error

	if len(args) != 1 {
		return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
	}

	A = args[0]   //Username

	//Obtain the balance of the account from the ledger.
	Avalbytes, err := stub.GetState(A)
	if err != nil {
		jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
		return shim.Error(jsonResp)
	}

	if Avalbytes == nil {
		jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
		return shim.Error(jsonResp)
	}

	jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
	fmt.Printf("Query Response:%s\n", jsonResp)
	//Return the transfer amount.
	return shim.Success(Avalbytes)
}

func main() {
	err := shim.Start(new(SimpleChaincode))
	if err != nil {
		fmt.Printf("Error starting Simple chaincode: %s", err)
	}
}