1

Hi I need help for writing below auction code. I want to use modifier however there is some changes in solidity which make me confuse.

I have mentioned below where I need changes within "//"

pragma solidity^0.4.18;

import './AuctionHouse.sol';

contract BidChain is AuctionHouse {

struct biddingData {
    uint endTime;
    uint highestBid;
    address bidder;
    bool initialized;
    mapping (address => bool) flaggedUsers;
}

mapping (uint => biddingData) itemToBiddingMap;

event newBidPlaced(uint itemId, uint bidAmount, address bidder);

//1. add itemOwner modifier
function initiateBid(uint _itemId, uint _endTime) public {
    //2. refactor
    require(msg.sender == itemToOwnerMapping[_itemId]);
    itemToBiddingMap[_itemId] = biddingData(_endTime, 0, msg.sender, 
 true);
}//1. add itemOwner modifier
function initiateBid(uint _itemId, uint _endTime) public {
    //2. refactor
    require(msg.sender == itemToOwnerMapping[_itemId]);
    itemToBiddingMap[_itemId] = biddingData(_endTime, 0, msg.sender, true);
}

//3. add itemOwner modifier
function flagUser(uint _itemId, address _flaggedUser) public {
    //4. refactor
    require(msg.sender == itemToOwnerMapping[_itemId]);
    require(itemToBiddingMap[_itemId].initialized == true);
    itemToBiddingMap[_itemId].flaggedUsers[_flaggedUser] = true;
}

function placeBid(uint _itemId) public payable {
    require(now < itemToBiddingMap[_itemId].endTime);
    require(msg.value > itemToBiddingMap[_itemId].highestBid);

    returnBidAmount(itemToBiddingMap[_itemId].bidder, itemToBiddingMap[_itemId].highestBid);

    itemToBiddingMap[_itemId].highestBid = msg.value;
    itemToBiddingMap[_itemId].bidder = msg.sender;

    emit newBidPlaced(_itemId, msg.value, msg.sender);
}

function returnBidAmount(address _sendee, uint _amount) internal {
    _sendee.transfer(_amount);
}

//5. add itemOwner modifier
function transferItem(uint _itemId, address _newOwner) public {
    //6. refactor
    require(msg.sender == itemToOwnerMapping[_itemId]);
    require(now > itemToBiddingMap[_itemId].endTime);

    itemToOwnerMapping[_itemId] = _newOwner;
    ownerToItemCount[_newOwner]++;
    ownerToItemCount[msg.sender]--;

    msg.sender.transfer(itemToBiddingMap[_itemId].highestBid);
}

//We already moved this modifier here for you
modifier itemOwner(uint _itemId) {
    require(msg.sender == itemToOwnerMapping[_itemId]);
    _;
   }
}
1

You just have to add modifier name after public like

function initiateBid(uint _itemId, uint _endTime) public itemOwner{
require(msg.sender == itemToOwnerMapping[_itemId]);
itemToBiddingMap[_itemId] = biddingData(_endTime, 0, msg.sender,  true);}

Or you can pass parameters into modifier

function initiateBid(uint _itemId, uint _endTime) public itemOwner(params...){//code}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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