1

In a scenario where a contract represents a buyer-seller agreement, sellers need to publish for each new bid a new contract/ or new entry in an array within a single same contract, and then a buyer selects the most appropriate offer by consulting the different existing bids (contracts/array).

My question is, how buyers can easily search and consult a blockchain to view the existing bids (contracts) and then select the non selected and appropriate one for him?

To my understanding, the buyer needs to know the address of the different contracts and then send Tx to each of them in order to read the bid content of each of them, is that correct?

What is the best design to implement such business logic in solidity?

1

It would probably not be correct to have each bid represented by its own contract. You may design a contract where each different auction has it's own deployment of the contract (similar to how tokens are deployed), but even that is probably not correct. If there is a well defined pattern for how bids/auctions should be run, you can probably program a single smart contract which creates and manages these events.

It is a bit much to go into full details on how to build a complete, ready for production smart contract, but here is a quick sample which may show you some of the ideas of a basic bidding contract. Comments inline:

pragma solidity ^0.4.25;

contract BiddingContract
{
    // Bids will have a user and an amount
    struct Bid {
        address user;
        uint bidAmount;
    }

    // This array will store the history of all bids
    // Due to the logic in this contract, bids will always be in acending price order
    Bid[] AllBids;

    // We will initialize the contract with a bid for 0 from 0x0
    constructor() public {
        AllBids.push(Bid(0x0, 0));
    }

    // Users will submit a bid here as _bidPrice
    function submitBid(uint _bidPrice) public {
        // The last index of the array is one less than the length
        uint lastIndex = AllBids.length - 1;
        // Ensure this bid is more than the current highest bid (the latest bid)
        require(_bidPrice > AllBids[lastIndex].bidAmount);
        // If it is, then push the new bid to the end of the array
        AllBids.push(Bid(msg.sender, _bidPrice));
    }

    // Getter function to return the "top bid", which is the last index of the array
    function getTopBid() public view returns (address, uint) {
        uint lastIndex = AllBids.length - 1;

        return (AllBids[lastIndex].user, AllBids[lastIndex].bidAmount);
    }

    // Getter function to return the total number of bids
    // Allows you to write a loop which goes through all the bids
    function getNumberOfBids() public view returns (uint) {
        return AllBids.length;
    }

    // Getter function to return a single bid from the array by index
    // Allows you to write a loop which goes through all the bids
    function getBid(uint index) public view returns (address, uint) {
        return (AllBids[index].user, AllBids[index].bidAmount);
    }
}

In order to read data from the contract, we have provided getter functions which will return different bid information. You can call these functions from your front end using something like Web3.js and a local/hosted node.

Again, a contract like this would not be ready for production, but simply demonstrates one way to build a simple bidding contract.

  • Thanks for the helpful example! but after making a bid, the buyer will select one bid and an agreement (contract) will be created between the seller and a buyer. For instance, the purchase of goods will be only completed if a set of rules (to be defined in the agreement logic) are satisfied. To verify and execute the rules, some external inputs maybe needed. Thus, the agreement contract address should be published to allow external entities (for instance IoT devices) to send some data. How this can be handled? – Noureddine Nov 21 '18 at 11:21
  • If there are things that need to be completed outside of the blockchain, then you will need an Oracle to provide that data to the blockchain. Here is an example: oraclize.it – Shawn Tabrizi Nov 21 '18 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.