1

As part of my project, I am using a code from github and modified it according to my requirement. This is a Auction contract. I am using remix IDE for interaction. I tried deploying in the rinkeby test network but no use.

How to use code

  1. Register with a account address and number of tokens. Suppose 25.
  2. Fill items. I chose 3 items.
  3. Place bid giving item id and number of tokens.
  4. Try placing bids with different accounts and different tokens of items.

My problem I modified the code to accept bids only greater than the previous bid. Suppose item 0 has 5 token bid, >5 tokens will only be accepted. This works fine. After accepting the 6 token bid, i wrote the logic to return the unsuccessful bid of 5 tokens to revert and update tokens of that bid owner. This is not happening. I don't know the mistake. Am I doing something wrong?

Code is as follows:

contract Auction {

    // Data
    //Structure to hold details of the item
    struct Item {
        uint itemId; // id of the item
        uint[] itemTokens; //tokens bid in favor of the item
    }

    //Structure to hold the details of a persons
    struct Person {
        uint remainingTokens; // tokens remaining with bidder
        uint personId; // it serves as tokenId as well
        address payable addr; //address of the bidder
    }

    mapping(address => Person) tokenDetails; //address to person 
    //Person [4] bidders;//Array containing 4 person objects
    Person[] bidders; //Array containing 4 person objects

    //Item [3] public items;//Array containing 3 item objects
    Item[] public items; //Array containing 3 item objects
    //address[3] public winners;//Array for address of winners
    address[] public winners; //Array for address of winners
    address payable public beneficiary; //owner of the smart contract

    event BidEvent(address addr, uint itemId, uint count, uint balance);

    //functions

    constructor() public payable { //constructor
        beneficiary = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == beneficiary);
        _;
    }
    function fillitems(uint _itemCount) public payable onlyOwner {
        winners.length = 0;
        items.length = 0;

        uint[] memory emptyArray;

        for (uint i = 0; i < _itemCount; i++) {
            items.push(Item({
                itemId: i,
                itemTokens: emptyArray
            }));
        }

    }


    function register(address payable _addr_bidder, uint _remainingTokens) public payable {

        uint bidderCount = bidders.length;

        for (uint id = 0; id < bidderCount; id++) {
            if (bidders[id].addr == _addr_bidder) {
                revert();
            }
        }

        bidders.push(Person({
            remainingTokens: _remainingTokens,
            personId: bidderCount,
            addr: _addr_bidder
        }));
        tokenDetails[_addr_bidder] = bidders[bidderCount];

    }


    // function transfer(address payable _addr, uint _count) public payable onlyOwner {
    //     _addr.transfer(_count);

    // }


    // function kill() public onlyOwner {
    //     selfdestruct(beneficiary);
    // }

    function bid(uint _itemId, uint _count) public payable {
        /*
            Bids tokens to a particular item.
            Arguments:
            _itemId -- uint, id of the item
            _count -- uint, count of tokens to bid for the item
        */

        if (_itemId > items.length - 1) {
            revert();
        }

         if (tokenDetails[msg.sender].remainingTokens == 0) {
            revert();
        }

        if (tokenDetails[msg.sender].remainingTokens < _count) {
            revert();
        }

        Item storage bidItem = items[_itemId];
        if(items[_itemId].itemTokens.length>0){

            if(CountTokens(items[_itemId].itemTokens) >=  _count){
            revert();
            }
            else{

                uint returnId = mode(items[_itemId].itemTokens);
                uint returnTokens = CountTokens(items[_itemId].itemTokens);

                Person storage bidPerson = bidders[returnId];
                uint currentbalance = tokenDetails[bidPerson.addr].remainingTokens + returnTokens;
                tokenDetails[bidPerson.addr].remainingTokens=currentbalance;
                delete bidItem.itemTokens;

            }
        }
        uint balance = tokenDetails[msg.sender].remainingTokens - _count;


        tokenDetails[msg.sender].remainingTokens = balance;
        bidders[tokenDetails[msg.sender].personId].remainingTokens = balance; //updating the same balance in bidders map.

        for (uint i = 0; i < _count; i++) {
            bidItem.itemTokens.push(tokenDetails[msg.sender].personId);
        }

        emit BidEvent(msg.sender, _itemId, _count, balance);
    }




    function revealWinners() public onlyOwner {

        winners.length = 0;
        address addr;

        for (uint id = 0; id < items.length; id++) {
            Item storage currentItem = items[id];
            addr = address(0);
            if (currentItem.itemTokens.length != 0) {
                // generate random# from block number
                uint randomIndex = mode(currentItem.itemTokens);
                addr = bidders[randomIndex].addr;
            }
            winners.push(addr);
        }
    }

    function mode(uint256[] memory array) public pure returns(uint) {
        uint[] memory count = new uint[](array.length);
        uint number;
        uint maxIndex = 0;
        //uint zero=0;

        for (uint i = 0; i < array.length; i += 1) {
            number = array[i];
            count[number] = (count[number]) + 1;
            if (count[number] > count[maxIndex]) {
                maxIndex = number;
            }
        }

        return maxIndex;
    }

    function CountTokens(uint256[] memory array) public pure returns(uint) {
        uint[] memory count = new uint[](array.length);
        uint number;
        uint maxIndex = 0;
        //uint zero=0;

        for (uint i = 0; i < array.length; i += 1) {
            number = array[i];
            count[number] = (count[number]) + 1;
            if (count[number] > count[maxIndex]) {
                maxIndex = number;
            }
        }

        return count[number];
    }

    function getSender() public view returns(address) {
        return (msg.sender);

    }

    function getLenItems() public view returns(uint) {
        return (items.length);

    }

    function getLenWinners() public view returns(uint) {
        return (winners.length);

    }

    function getWinner(uint id) public view returns(address) {
        return (winners[id]);

    }


    //Miscellaneous methods: Below methods are used to assist Grading. Please DONOT CHANGE THEM.
    function getPersonDetails(uint id) public view returns(uint, uint, address) {
        return (bidders[id].remainingTokens, bidders[id].personId, bidders[id].addr);
    }

}
  • That's a ton of code you just dumped here. Please spend some time doing the preliminary work required to understand what parts of it are actually related to the problem at hand, and then post ONLY those parts. – goodvibration Feb 9 at 7:53
  • I have issue only in bid method and that too updating of remaining tokens. That particular line is not reflecting. – Ramya Feb 9 at 11:21
  • @Ramya Please describe what exactly is the problem, what is the expected output and what is happening. Some functions in your code make no sense like CountTokens and mode . – Ismael Feb 9 at 19:50
  • Once a bid is accepted, tokens get deducted from the bidder account. But if already a bid exists, new bid will be accepted only if it is greater than existing bid. After accepting the new bid, old bid tokens need to given back to the old bidder. Count tokens and mode calculate the tokens and old bidder account id respectively. – Ramya Feb 9 at 21:05
0

Adding this line to code actually solved my problem. bidders[tokenDetails[bidders[returnId].addr].personId].remainingTokens = currentbalance;

| improve this answer | |

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.