1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| package main
import ( "encoding/json" "fmt" "strconv"
"github.com/hyperledger/fabric-contract-api-go/contractapi" )
type SmartContract struct { contractapi.Contract }
type Car struct { Make string `json:"make"` Model string `json:"model"` Colour string `json:"colour"` Owner string `json:"owner"` }
type QueryResult struct { Key string `json:"Key"` Record *Car }
func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface) error { cars := []Car{ Car{Make: "Toyota", Model: "Prius", Colour: "blue", Owner: "Tomoko"}, Car{Make: "Ford", Model: "Mustang", Colour: "red", Owner: "Brad"}, Car{Make: "Hyundai", Model: "Tucson", Colour: "green", Owner: "Jin Soo"}, Car{Make: "Volkswagen", Model: "Passat", Colour: "yellow", Owner: "Max"}, Car{Make: "Tesla", Model: "S", Colour: "black", Owner: "Adriana"}, Car{Make: "Peugeot", Model: "205", Colour: "purple", Owner: "Michel"}, Car{Make: "Chery", Model: "S22L", Colour: "white", Owner: "Aarav"}, Car{Make: "Fiat", Model: "Punto", Colour: "violet", Owner: "Pari"}, Car{Make: "Tata", Model: "Nano", Colour: "indigo", Owner: "Valeria"}, Car{Make: "Holden", Model: "Barina", Colour: "brown", Owner: "Shotaro"}, }
for i, car := range cars { carAsBytes, _ := json.Marshal(car) err := ctx.GetStub().PutState("CAR"+strconv.Itoa(i), carAsBytes)
if err != nil { return fmt.Errorf("Failed to put to world state. %s", err.Error()) } }
return nil }
func (s *SmartContract) CreateCar(ctx contractapi.TransactionContextInterface, carNumber string, make string, model string, colour string, owner string) error { car := Car{ Make: make, Model: model, Colour: colour, Owner: owner, }
carAsBytes, _ := json.Marshal(car)
return ctx.GetStub().PutState(carNumber, carAsBytes) }
func (s *SmartContract) QueryCar(ctx contractapi.TransactionContextInterface, carNumber string) (*Car, error) { carAsBytes, err := ctx.GetStub().GetState(carNumber)
if err != nil { return nil, fmt.Errorf("Failed to read from world state. %s", err.Error()) }
if carAsBytes == nil { return nil, fmt.Errorf("%s does not exist", carNumber) }
car := new(Car) _ = json.Unmarshal(carAsBytes, car)
return car, nil }
func (s *SmartContract) QueryAllCars(ctx contractapi.TransactionContextInterface) ([]QueryResult, error) { startKey := "" endKey := ""
resultsIterator, err := ctx.GetStub().GetStateByRange(startKey, endKey)
if err != nil { return nil, err } defer resultsIterator.Close()
results := []QueryResult{}
for resultsIterator.HasNext() { queryResponse, err := resultsIterator.Next()
if err != nil { return nil, err }
car := new(Car) _ = json.Unmarshal(queryResponse.Value, car)
queryResult := QueryResult{Key: queryResponse.Key, Record: car} results = append(results, queryResult) }
return results, nil }
func (s *SmartContract) ChangeCarOwner(ctx contractapi.TransactionContextInterface, carNumber string, newOwner string) error { car, err := s.QueryCar(ctx, carNumber)
if err != nil { return err }
car.Owner = newOwner
carAsBytes, _ := json.Marshal(car)
return ctx.GetStub().PutState(carNumber, carAsBytes) }
func main() {
chaincode, err := contractapi.NewChaincode(new(SmartContract))
if err != nil { fmt.Printf("Error create fabcar chaincode: %s", err.Error()) return }
if err := chaincode.Start(); err != nil { fmt.Printf("Error starting fabcar chaincode: %s", err.Error()) } }
|