这是竞价智能合约。 我想这样拍卖。如果有人注册了他的产品以进行竞标,那么任何想要购买的人都可以参加此竞标。
但是当我对Remix进行测试时,甚至都无法遵守。 我认为我的编码有问题。 请帮助我。
pragma solidity ^0.5.0;
contract Auction{
//product for auction
struct Product{
string name;
string description;
uint time;
}
//top bid
uint topMoney;
//presen owner of product
mapping (uint => address) productToOwner;
//owner of top bidder
mapping (uint => address) topMoneyOwner;
event Listed(uint id, string name, uint itme);
Product[] public products;
//register product
function listUp(string memory _name, string memory _description) public {
//time limit for bidding, 1 minutes;
uint time = now + 1 minutes;
//index of product
uint id = products.push(Product(_name, _description, time)) - 1;
//initial bid = 0
topMoney=0;
//initial owner of product
productToOwner[id] = msg.sender;
emit Listed(id, _name, time);
}
//bidding
function bidOn() payable public {
if ( topMoney < msg.value){
topMoney = msg.value;
topMoneyOwner[topMoney] = msg.sender;
} else {
msg.sender.transfer(msg.value);
}
}
//bidding end? return (bool)
function _end(uint _id) private view returns (bool) {
require(now >= products[_id].time);
return true;
}
//who is winner? Then, transfer money to owner of product.
function winner(uint _id) public {
require( true == _end(_id));
address(uint160(productToOwner[_id])).transfer(topMoney);
productToOwner[_id] = topMoneyOwner[topMoney];
}
}