Initializing the chaincode

Next, we'll implement the Init function. Init() allows the chaincode to initialize the insuree data to start the claim request. In our case, we will set up and register the insuree person information, as follows:

func (c *ClaimContract) Init(stub shim.ChaincodeStubInterface) pb.Response {
args := stub.GetStringArgs()
if len(args) != 5 {
return shim.Error("Incorrect arguments. Expecting a key and a value")
}
insureeId := args[0]
firstName := args[1]
lastName := args[2]
ssn := args[3]
policyNumber := args[4]
insureeData := Insuree{
Id: insureeId,
FirstName: firstName,
LastName: lastName,
SSN: ssn,
PolicyNumber: policyNumber}
insureeBytes, _ := json.Marshal(insureeData)
err := stub.PutState(insureeId, insureeBytes)
if err != nil {
return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0]))
}
return shim.Success(nil)
}

ChaincodeStubInterface.GetStringArg gets the input arguments. It expects that the length of the arguments should be 5. With all required insurer data, we build Insurer JSON data and encode it to JSON byte strings –json.Marshal(insureeData. Then, we store the key and the value on the ledger. If all went well, it returns a peer.Response object with success to Fabric's client.c.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.67.40