package main import ( "fmt" "github.com/zhigui/zigledger/core/chaincode/shim" pb "github.com/zhigui/zigledger/protos/peer" ) const ( //Transfer = "transfer" BalanceOf = "balanceOf" ) type ERC20Chaincode struct{} //func main() { // err := shim.Start(new(ERC20Chaincode)) // if err != nil { // fmt.Printf("Error starting assetChaincode: %s", err) // } //} func (t *ERC20Chaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { return shim.Success([]byte("Init success.")) } func (t *ERC20Chaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { function, args := stub.GetFunctionAndParameters() switch function { case Transfer: if len(args) != 3 { return shim.Error("Incorrect number of arguments. Expecting 3.") } return t.transfer(stub, args) case BalanceOf: if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 3.") } return t.balanceOf(stub, args) default: return shim.Error("Invalid invoke function name.") } return shim.Success([]byte("success")) } func (t *ERC20Chaincode) transfer(stub shim.ChaincodeStubInterface, args []string) pb.Response { fmt.Println("here is erc20 contract", args) //coinType := args[0] amount := args[1] address := args[2] err := stub.PutState(address, []byte(amount)) if err != nil { return shim.Error("PutState error") } return shim.Success([]byte("success")) } func (t *ERC20Chaincode) balanceOf(stub shim.ChaincodeStubInterface, args []string) pb.Response { balance, err := stub.GetState(args[0]) if err != nil { return shim.Error("GetState error") } fmt.Println("balance of", string(args[0]), string(balance)) return shim.Success([]byte(balance)) }