Skip to content

Instantly share code, notes, and snippets.

@vitiko
Last active May 20, 2018 14:31
Show Gist options
  • Save vitiko/caea36a4a82f61a43c5ddb8bb6866cee to your computer and use it in GitHub Desktop.
Save vitiko/caea36a4a82f61a43c5ddb8bb6866cee to your computer and use it in GitHub Desktop.
Chaincode example
func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
fn, args := stub.GetFunctionAndParameters()
var result string
var err error
if fn == "set" {
result, err = set(stub, args)
} else { // assume 'get' even if fn is nil
result, err = get(stub, args)
}
if err != nil {
return shim.Error(err.Error())
}
// Return the result as success payload
return shim.Success([]byte(result))
}
// Set stores the asset (both key and value) on the ledger.
func set(stub shim.ChaincodeStubInterface, args []string) (string, error) {
if len(args) != 2 {
return "", fmt.Errorf("Incorrect arguments. Expecting a key and a value")
}
err := stub.PutState(args[0], []byte(args[1]))
if err != nil {
return "", fmt.Errorf("Failed to set asset: %s", args[0])
}
return args[1], nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment