Skip to content

Instantly share code, notes, and snippets.

@R1j1t
Last active May 19, 2022 10:19
Show Gist options
  • Save R1j1t/8b2d211e77f58a0a353a3498cf037536 to your computer and use it in GitHub Desktop.
Save R1j1t/8b2d211e77f58a0a353a3498cf037536 to your computer and use it in GitHub Desktop.
[WIP]: Smart contract to store graduating student details on hyperledger fabric
package main
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)
type SmartContract struct {
}
type graduation struct {
RollNo string `json:"rollNo"`
Name string `json:"name"`
Year string `json:"year"`
Cpi string `json:"cpi"`
Degree string `json:"degree"`
Major string `json:"major"` //Nil if no major
Minor string `json:"minor"` //Nil if no minor
Minor string `json:"minor"` //Nil if no 2nd minor
}
/*
* The Init method is called when the Smart Contract "fabcar" is instantiated by the blockchain network
* Best practice is to have any Ledger initialization in separate function -- see initLedger()
*/
func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
return shim.Success(nil)
}
func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response{
}
func (s *SmartContract) queryStudent(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting Roll No. only")
}
carAsBytes, _ := APIstub.GetState(args[0])
return shim.Success(carAsBytes)
}
func (s *SmartContract) addStudent(APIstub ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 8 {
return shim.Error("Incorrect number of arguments. Expecting at least 5 arguments")
}
var student = graduation{
RollNo: args[0], Name: args[1], Year: args[2], Cpi: args[3],
Degree: args[4], Major: args[5], Minor: args[6], Minor: args[7]}
studentAsBytes, _ := json.Marshal(student)
APIstub.PutState(studentAsBytes)
return shim.Success(nil)
}
// func(s *SmartContract) updateDetail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment