Overview
Product Features
Underlying Engine
Strengths
Scenarios
Basic Concepts
package main import ( "errors" "fmt" "strconv" "github.com/hyperledger/fabric-contract-api-go/contractapi" ) // ABstore Chaincode implementation type ABstore struct { contractapi.Contract } 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) } }
// Init is used to initialize two key-value pairs. The parameters entered by the user are KEY1_NAME, VALUE1, KEY2_NAME, VALUE2. func (t *ABstore) Init(ctx contractapi.TransactionContextInterface, A string, Aval int, B string, Bval int) error { fmt.Println("ABstore Init") var err error // Initialize the chaincode fmt.Printf("Aval = %d, Bval = %d\\n", Aval, Bval) // Write the state 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 }
// invoke to transfer value between two keys, enter KEY1_NAME, KEY2_NAME, VALUE func (t *ABstore) Invoke(ctx contractapi.TransactionContextInterface, A, B string, X int) error { var err error var Aval int var Bval int // Get the state from the ledger // TODO: will be nice to have a GetAllState call to 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)) // Perform the execution Aval = Aval - X Bval = Bval + X fmt.Printf("Aval = %d, Bval = %d\\n", Aval, Bval) // Write the state 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 }
// delete is used to delete the specified key from the ledger. Enter KEY_NAME. func (t *ABstore) Delete(ctx contractapi.TransactionContextInterface, A string) error { // Delete the key from the state in ledger err := ctx.GetStub().DelState(A) if err != nil { return fmt.Errorf("Failed to delete state") } return nil }
// query is mainly the value corresponding to the query key. Enter KEY_NAME. func (t *ABstore) Query(ctx contractapi.TransactionContextInterface, A string) (string, error) { var err error // Get the state 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 }
피드백