Change word Sold to Balance

This commit is contained in:
projet 2019-04-05 11:23:04 +02:00
parent f3b00deecb
commit 28de7b94ec

View file

@ -57,7 +57,7 @@ type wallet struct {
ObjectType string `json:"docType"` //docType is used to distinguish the various type$
Id string `json:"id"` //the fieldtags are needed to keep case from bounci$
WalletType string `json:"walletType"`
Sold float64 `json:"sold"`
Balance float64 `json:"balance"`
Owner string `json:"owner"`
}
@ -130,9 +130,9 @@ func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
} else if function == "transferWallet" {
// change the owner of a specific wallet
return t.transferWallet(stub, args)
} else if function == "setSoldOnWallet" {
// set a new sold to a wallet
return t.setSoldOnWallet(stub, args)
} else if function == "setBalanceOnWallet" {
// set a new balance to a wallet
return t.setBalanceOnWallet(stub, args)
} else if function == "transaction" {
// make a transaction of X units from a wallet to an other
return t.transaction(stub, args)
@ -529,7 +529,7 @@ func (t *SimpleChaincode) initWallet(stub shim.ChaincodeStubInterface, args []st
walletId := args[0]
walletType := strings.ToLower(args[1])
owner := args[2]
sold := 0.0
balance := 0.0
//authorized_by_association := args[4]
if err != nil {
@ -560,7 +560,7 @@ func (t *SimpleChaincode) initWallet(stub shim.ChaincodeStubInterface, args []st
// ==== Create wallet object and marshal to JSON ====
objectType := "wallet"
wallet := &wallet{objectType, walletId, walletType, sold, owner}
wallet := &wallet{objectType, walletId, walletType, balance, owner}
walletJSONasBytes, err := json.Marshal(wallet)
if err != nil {
return shim.Error(err.Error())
@ -734,7 +734,7 @@ func (t *SimpleChaincode) transferWallet(stub shim.ChaincodeStubInterface, args
// ===========================================================
// set a sold of a wallet by setting a new owner name on the wallet
// ===========================================================
func (t *SimpleChaincode) setSoldOnWallet(stub shim.ChaincodeStubInterface, args []string) pb.Response {
func (t *SimpleChaincode) setBalanceOnWallet(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// 0 1
// "name", "bob"
@ -743,8 +743,8 @@ func (t *SimpleChaincode) setSoldOnWallet(stub shim.ChaincodeStubInterface, args
}
walletId := args[0]
newSold, err := strconv.ParseFloat(args[1], 64)
fmt.Println("- start setSoldOnWallet ", walletId, newSold)
newBalance, err := strconv.ParseFloat(args[1], 64)
fmt.Println("- start setBalanceOnWallet ", walletId, newBalance)
// ==== Check if wallet already exists ====
walletAsBytes, err := stub.GetState(walletId)
@ -762,14 +762,14 @@ func (t *SimpleChaincode) setSoldOnWallet(stub shim.ChaincodeStubInterface, args
}
//if walletToSet.Owner
walletToSet.Sold = walletToSet.Sold + newSold //change the sold
walletToSet.Balance = walletToSet.Balance + newBalance //change the balance
walletJSONasBytes, _ := json.Marshal(walletToSet)
err = stub.PutState(walletId, walletJSONasBytes) //rewrite the wallet
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end setSoldOnWallet (success)")
fmt.Println("- end setBalanceOnWallet (success)")
return shim.Success(nil)
}
@ -805,39 +805,39 @@ func (t *SimpleChaincode) transaction(stub shim.ChaincodeStubInterface, args []s
return shim.Error("This wallet doesn't exists: " + walletTargetId)
}
walletSoldToSet := wallet{}
err = json.Unmarshal(walletAsBytes, &walletSoldToSet) //unmarshal it aka JSON
walletBalanceToSet := wallet{}
err = json.Unmarshal(walletAsBytes, &walletBalanceToSet) //unmarshal it aka JSON
if err != nil {
return shim.Error(err.Error())
}
if walletSoldToSet.Sold <= 0 {
if walletBalanceToSet.Balance <= 0 {
return shim.Error("This wallet is not allowed to make a transaction:" + walletId)
} else {
walletSoldToSet.Sold = walletSoldToSet.Sold - transactionValue //change the sold
walletBalanceToSet.Balance = walletBalanceToSet.Balance - transactionValue //change the balance
}
walletJSONasBytes, _ := json.Marshal(walletSoldToSet)
walletJSONasBytes, _ := json.Marshal(walletBalanceToSet)
err = stub.PutState(walletId, walletJSONasBytes) //rewrite the wallet
if err != nil {
return shim.Error(err.Error())
}
walletTargetSoldToSet := wallet{}
err = json.Unmarshal(walletTargetAsBytes, &walletTargetSoldToSet) //unmarshal it aka JSON
walletTargetBalanceToSet := wallet{}
err = json.Unmarshal(walletTargetAsBytes, &walletTargetBalanceToSet) //unmarshal it aka JSON
if err != nil {
return shim.Error(err.Error())
}
walletTargetSoldToSet.Sold = walletTargetSoldToSet.Sold + transactionValue
walletTargetBalanceToSet.Balance = walletTargetBalanceToSet.Balance + transactionValue
walletTargetJSONasBytes, _ := json.Marshal(walletTargetSoldToSet)
walletTargetJSONasBytes, _ := json.Marshal(walletTargetBalanceToSet)
err = stub.PutState(walletTargetId, walletTargetJSONasBytes) //rewrite the wallet
if err != nil {
return shim.Error(err.Error())
}
fmt.Println("- end setSoldOnWallet (success)")
fmt.Println("- end setBalanceOnWallet (success)")
return shim.Success(nil)
}
@ -1075,4 +1075,3 @@ func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString
return buffer.Bytes(), nil
}