fabric-samples/auction-dutch/chaincode-go-auditor/smart-contract/auctionQueries.go
ali 05c06f4931
Fixed comment consistency problem (#844)
* fixed comment consistency problem with erc20 chaincode

Signed-off-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>

* added more comment consistancy fix

Signed-off-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>

* added more comment consistancy fix

Signed-off-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>

* added more comment consistancy fix

Signed-off-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>

* added more comment consistancy fix

Signed-off-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>

Signed-off-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>
Co-authored-by: Ali Shahverdi <ali@Alis-MacBook-Pro.local>
2022-12-14 09:18:21 +01:00

91 lines
2.2 KiB
Go

/*
SPDX-License-Identifier: Apache-2.0
*/
package auction
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-contract-api-go/contractapi"
)
// QueryAuction allows all members of the channel to read a public auction
func (s *SmartContract) QueryAuction(ctx contractapi.TransactionContextInterface, auctionID string) (*Auction, error) {
auctionJSON, err := ctx.GetStub().GetState(auctionID)
if err != nil {
return nil, fmt.Errorf("failed to get auction object %v: %v", auctionID, err)
}
if auctionJSON == nil {
return nil, fmt.Errorf("auction does not exist")
}
var auction *Auction
err = json.Unmarshal(auctionJSON, &auction)
if err != nil {
return nil, err
}
return auction, nil
}
// checkForHigherBid is an internal function that is used to determine if a winning bid has yet to be revealed
func checkForHigherBid(ctx contractapi.TransactionContextInterface, auctionPrice int, revealedBidders map[string]FullBid, bidders map[string]BidHash) error {
// Get MSP ID of peer org
peerMSPID, err := shim.GetMSPID()
if err != nil {
return fmt.Errorf("failed getting the peer's MSPID: %v", err)
}
var error error
error = nil
for bidKey, privateBid := range bidders {
if _, bidInAuction := revealedBidders[bidKey]; bidInAuction {
// bid is already revealed, no action to take
} else {
collection := "_implicit_org_" + privateBid.Org
if privateBid.Org == peerMSPID {
bidJSON, err := ctx.GetStub().GetPrivateData(collection, bidKey)
if err != nil {
return fmt.Errorf("failed to get bid %v: %v", bidKey, err)
}
if bidJSON == nil {
return fmt.Errorf("bid %v does not exist", bidKey)
}
var bid *FullBid
err = json.Unmarshal(bidJSON, &bid)
if err != nil {
return err
}
if bid.Price > auctionPrice {
error = fmt.Errorf("Cannot close auction, bidder has a higher price: %v", err)
}
} else {
hash, err := ctx.GetStub().GetPrivateDataHash(collection, bidKey)
if err != nil {
return fmt.Errorf("failed to read bid hash from collection: %v", err)
}
if hash == nil {
return fmt.Errorf("bid hash does not exist: %s", bidKey)
}
}
}
}
return error
}