Help Center/
Blockchain Service/
Developer Guide (ME-Abu Dhabi Region)/
Chaincode Development/
Go Chaincode Development/
Sample Chaincode (2.0)
Updated on 2023-03-07 GMT+08:00
Sample Chaincode (2.0)
The following is an example of installing and instantiating the account transfer chaincode (2.0). For details about how to debug this chaincode, see the official Fabric examples.
package main import ( "errors" "fmt" "strconv" "github.com/hyperledger/fabric-contract-api-go/contractapi" ) // Chaincode implementation type ABstore struct { contractapi.Contract } // Automatically invoked during chaincode instantiation or update to initialize the chaincode data. func (t *ABstore) Init(ctx contractapi.TransactionContextInterface, A string, Aval int, B string, Bval int) error { // The output information of the println function is recorded in the logs of the chaincode container. fmt.Println("ABstore Init") var err error fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) // Write the status data to the ledger. err = ctx.GetStub().PutState(A, []byte(strconv.Itoa(Aval))) if err != nil { return err } err = ctx.GetStub().PutState(B, []byte(strconv.Itoa(Bval))) if err != nil { return err } return nil } // A transfers X to B. func (t *ABstore) Invoke(ctx contractapi.TransactionContextInterface, A, B string, X int) error { var err error var Aval int var Bval int // Obtain the status data from the ledger. Avalbytes, err := ctx.GetStub().GetState(A) if err != nil { return fmt.Errorf("Failed to get state") } if Avalbytes == nil { return fmt.Errorf("Entity not found") } Aval, _ = strconv.Atoi(string(Avalbytes)) Bvalbytes, err := ctx.GetStub().GetState(B) if err != nil { return fmt.Errorf("Failed to get state") } if Bvalbytes == nil { return fmt.Errorf("Entity not found") } Bval, _ = strconv.Atoi(string(Bvalbytes)) // Transfer Aval = Aval - X Bval = Bval + X fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) // Write the status data back to the ledger. err = ctx.GetStub().PutState(A, []byte(strconv.Itoa(Aval))) if err != nil { return err } err = ctx.GetStub().PutState(B, []byte(strconv.Itoa(Bval))) if err != nil { return err } return nil } // Account deregistration func (t *ABstore) Delete(ctx contractapi.TransactionContextInterface, A string) error { // Delete the account status from the ledger. err := ctx.GetStub().DelState(A) if err != nil { return fmt.Errorf("Failed to delete state") } return nil } // Account query func (t *ABstore) Query(ctx contractapi.TransactionContextInterface, A string) (string, error) { var err error // Obtain the status data from the ledger. Avalbytes, err := ctx.GetStub().GetState(A) if err != nil { jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}" return "", errors.New(jsonResp) } if Avalbytes == nil { jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" return "", errors.New(jsonResp) } jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" fmt.Printf("Query Response:%s\n", jsonResp) return string(Avalbytes), nil } func main() { cc, err := contractapi.NewChaincode(new(ABstore)) if err != nil { panic(err.Error()) } if err := cc.Start(); err != nil { fmt.Printf("Error starting ABstore chaincode: %s", err) } }
Parent topic: Go Chaincode Development
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot