github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/examples/cli_test/token/token.go (about) 1 /* 2 Copyright Ziggurat Corp. 2017 All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package main 8 9 import ( 10 "encoding/json" 11 "fmt" 12 "math/big" 13 "strconv" 14 "strings" 15 16 "github.com/inklabsfoundation/inkchain/core/chaincode/shim" 17 pb "github.com/inklabsfoundation/inkchain/protos/peer" 18 ) 19 20 const ( 21 //func name 22 GetBalance string = "getBalance" 23 GetAccount string = "getAccount" 24 Transfer string = "transfer" 25 Counter string = "counter" 26 Sender string = "sender" 27 ) 28 29 // User chaincode for token operations 30 // After a token issued, users can use this chaincode to make query or transfer operations. 31 type tokenChaincode struct { 32 } 33 34 // Init func 35 func (t *tokenChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { 36 fmt.Println("token user chaincode Init.") 37 return shim.Success([]byte("Init success.")) 38 } 39 40 // Invoke func 41 func (t *tokenChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 42 fmt.Println("token user chaincode Invoke") 43 function, args := stub.GetFunctionAndParameters() 44 45 switch function { 46 case GetBalance: 47 if len(args) != 2 { 48 return shim.Error("Incorrect number of arguments. Expecting 2.") 49 } 50 return t.getBalance(stub, args) 51 52 case GetAccount: 53 if len(args) != 1 { 54 return shim.Error("Incorrect number of arguments. Expecting 1.") 55 } 56 return t.getAccount(stub, args) 57 58 case Transfer: 59 if len(args) != 3 { 60 return shim.Error("Incorrect number of arguments. Expecting 3") 61 } 62 return t.transfer(stub, args) 63 64 case Counter: 65 if len(args) != 1 { 66 return shim.Error("Incorrect number of arguments. Expecting 1") 67 } 68 return t.getCounter(stub, args) 69 70 case Sender: 71 sender, err := stub.GetSender() 72 if err != nil { 73 return shim.Error("Get sender failed.") 74 } 75 return shim.Success([]byte(sender)) 76 77 } 78 79 return shim.Error("Invalid invoke function name. Expecting \"getBalance\", \"getAccount\", \"transfer\", \"counter\" or \"sender\".") 80 } 81 82 // getBalance 83 // Get the balance of a specific token type in an account 84 func (t *tokenChaincode) getBalance(stub shim.ChaincodeStubInterface, args []string) pb.Response { 85 var A string // Address 86 var BalanceType string // Token type 87 var err error 88 89 A = strings.ToLower(args[0]) 90 BalanceType = args[1] 91 // Get the state from the ledger 92 account, err := stub.GetAccount(A) 93 if err != nil { 94 jsonResp := "{\"Error\":\"account not exists\"}" 95 return shim.Error(jsonResp) 96 } 97 98 if account == nil || account.Balance[BalanceType] == nil { 99 jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" 100 return shim.Error(jsonResp) 101 } 102 103 jsonResp := "{\"" + BalanceType + "\":\"" + account.Balance[BalanceType].String() + "\"}" 104 return shim.Success([]byte(jsonResp)) 105 } 106 107 // getAccount 108 // Get the balances of all token types in an account 109 func (t *tokenChaincode) getAccount(stub shim.ChaincodeStubInterface, args []string) pb.Response { 110 var A string // Address 111 var err error 112 113 A = strings.ToLower(args[0]) 114 // Get the state from the ledger 115 account, err := stub.GetAccount(A) 116 if err != nil { 117 jsonResp := "{\"Error\":\"account not exists\"}" 118 return shim.Error(jsonResp) 119 } 120 121 if account == nil { 122 jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" 123 return shim.Error(jsonResp) 124 } 125 balanceJson, jsonErr := json.Marshal(account.Balance) 126 if jsonErr != nil { 127 return shim.Error(jsonErr.Error()) 128 } 129 jsonResp := "{\"Name\":\"" + A + "\",\"Balance\":\"" + string(balanceJson[:]) + "\"}" 130 return shim.Success([]byte(jsonResp)) 131 } 132 133 // transfer 134 // Send tokens to the specified address 135 func (t *tokenChaincode) transfer(stub shim.ChaincodeStubInterface, args []string) pb.Response { 136 var B string // To address 137 var BalanceType string // Token type 138 var err error 139 140 B = strings.ToLower(args[0]) 141 BalanceType = args[1] 142 143 // Amount 144 amount := big.NewInt(0) 145 _, good := amount.SetString(args[2], 10) 146 if !good { 147 return shim.Error("Expecting integer value for amount") 148 } 149 err = stub.Transfer(B, BalanceType, amount) 150 if err != nil { 151 return shim.Error("transfer error" + err.Error()) 152 } 153 return shim.Success(nil) 154 } 155 156 // counter 157 // Get current tx counter of an account 158 func (t *tokenChaincode) getCounter(stub shim.ChaincodeStubInterface, args []string) pb.Response { 159 var A string // Address 160 var err error 161 162 A = strings.ToLower(args[0]) 163 account, err := stub.GetAccount(A) 164 if err != nil { 165 jsonResp := "{\"Error\":\"account not exists\"}" 166 return shim.Error(jsonResp) 167 } 168 169 if account == nil { 170 jsonResp := "{\"Error\":\"account not exists for " + A + "\"}" 171 return shim.Error(jsonResp) 172 } 173 174 jsonResp := "{\"Name\":\"" + A + "\",\"counter\":\"" + string(account.Counter) + "\"}" 175 fmt.Printf("Query Response:%s\n", jsonResp) 176 return shim.Success([]byte(strconv.FormatUint(account.Counter, 10))) 177 } 178 179 func main() { 180 err := shim.Start(new(tokenChaincode)) 181 if err != nil { 182 fmt.Printf("Error starting tokenChaincode: %s", err) 183 } 184 }