Skip to content
Snippets Groups Projects
Commit ccf78a92 authored by Gomez Goiri, Aitor's avatar Gomez Goiri, Aitor
Browse files

Upgrading stats when a slag is sold

parent f5ded723
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ import (
"git.code.tecnalia.com/ledgerbuilder/sdk/shared"
"github.com/mitchellh/mapstructure"
"git.code.tecnalia.com/blockchain/hypercog/controller/stats"
errs "git.code.tecnalia.com/traceblock/sdk/constants"
"git.code.tecnalia.com/traceblock/sdk/controller/base"
"git.code.tecnalia.com/traceblock/sdk/controller/split"
......@@ -224,22 +225,20 @@ func (c BidDecisionController) RejectBid(stub shared.LedgerBuildrStubInterface,
return c.SaveAbstractAsset(stub, biddableAsset)
}
func (c BidDecisionController) AcceptBid(stub shared.LedgerBuildrStubInterface, params shared.LedgerBuildrAsset) protos.Response {
fnName := "BidController:AcceptBid"
func (c BidDecisionController) _acceptBid(stub shared.LedgerBuildrStubInterface, params shared.LedgerBuildrAsset) (*protos.Response, error) {
biddableAsset, bid, err := c.bidResponse(stub, params, true)
if err != nil {
return api.NewApiResponsePtr(fnName, err, nil).SendResponse()
return nil, err
}
if biddableAsset.Quantity < bid.Quantity {
return api.NewApiResponsePtr(fnName, errInvalidBidQuantity, nil).SendResponse()
return nil, errInvalidBidQuantity
}
// TODO check is this transactional?
respAcceptedStock:= c.SaveAbstractAsset(stub, biddableAsset)
if respAcceptedStock.Status != shared.OK {
return respAcceptedStock
return &respAcceptedStock, nil
}
var soldStock *model.TraceableAsset
......@@ -256,7 +255,7 @@ func (c BidDecisionController) AcceptBid(stub shared.LedgerBuildrStubInterface,
subAssets, err := split.SplitAsset(c.TraceblockBaseController, stub, biddableAsset, *splitParams)
if err != nil {
return api.NewApiResponsePtr(fnName, err, nil).SendResponse()
return nil, err
}
newStock := subAssets[0]
......@@ -267,7 +266,7 @@ func (c BidDecisionController) AcceptBid(stub shared.LedgerBuildrStubInterface,
respNewStock:= c.SaveAbstractAsset(stub, &newStock)
if respNewStock.Status != shared.OK {
return respNewStock
return &respNewStock, nil
}
soldStock = &subAssets[1]
......@@ -286,5 +285,19 @@ func (c BidDecisionController) AcceptBid(stub shared.LedgerBuildrStubInterface,
transferredAsset := transfer.TransferAsset(stub, soldStock, transferParams)
return c.SaveAbstractAsset(stub, transferredAsset)
err = stats.RegisterSale(stub, soldStock.Quantity, bid.Price, soldStock.Units)
if err != nil {
return nil, err
}
ret := c.SaveAbstractAsset(stub, transferredAsset)
return &ret, nil
}
func (c BidDecisionController) AcceptBid(stub shared.LedgerBuildrStubInterface, params shared.LedgerBuildrAsset) protos.Response {
ret, err := c._acceptBid(stub, params)
if err != nil {
return api.NewApiResponsePtr("BidController:AcceptBid", err, nil).SendResponse()
}
return *ret
}
\ No newline at end of file
......@@ -94,7 +94,7 @@ func AddOrgIfNotExist(stub shared.LedgerBuildrStubInterface, org string) (err er
}
func RetrieveExistingStats(stub shared.LedgerBuildrStubInterface) (*model.RawStats, *model.RawStats, error) {
func retrieveExistingStats(stub shared.LedgerBuildrStubInterface) (*model.RawStats, *model.RawStats, error) {
org, err := stub.GetOrganization()
if err != nil {
return nil, nil, err
......@@ -113,7 +113,7 @@ func RetrieveExistingStats(stub shared.LedgerBuildrStubInterface) (*model.RawSta
return globalStats, customStats, nil
}
func UpgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawStats, customStats *model.RawStats) (error) {
func upgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawStats, customStats *model.RawStats) (error) {
org, err := stub.GetOrganization()
if err != nil {
return err
......@@ -136,3 +136,38 @@ func UpgradeStats(stub shared.LedgerBuildrStubInterface, globalStats *model.RawS
return nil
}
func RegisterSlag(stub shared.LedgerBuildrStubInterface, quantity uint32, units string) (error) {
globalStats, customStats, err := retrieveExistingStats(stub)
if err != nil {
return err
}
customStats.RegisterSlag(quantity, units)
globalStats.RegisterSlag(quantity, units)
err = upgradeStats(stub, globalStats, customStats)
if err != nil {
return err
}
return nil
}
func RegisterSale(stub shared.LedgerBuildrStubInterface, quantity uint32, price float32, units string) (error) {
globalStats, orgStats, err := retrieveExistingStats(stub)
if err != nil {
return err
}
orgStats.UpgradeSell(quantity, price, units)
orgStats.UpgradeSell(quantity, price, units)
err = upgradeStats(stub, globalStats, orgStats)
if err != nil {
return err
}
return nil
}
\ No newline at end of file
......@@ -24,15 +24,7 @@ func _updateSlagProduction(stub shared.LedgerBuildrStubInterface, ctl shared.Con
status, ok := asst.Get("status")
if ok && status == "slag" {
globalStats, customStats, err := stats.RetrieveExistingStats(stub)
if err != nil {
return nil, err
}
customStats.RegisterSlag(asst.Quantity, asst.Units)
globalStats.RegisterSlag(asst.Quantity, asst.Units)
err = stats.UpgradeStats(stub, globalStats, customStats)
err := stats.RegisterSlag(stub, asst.Quantity, asst.Units)
if err != nil {
return nil, err
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment