Skip to content

Instantly share code, notes, and snippets.

@postpostscript
Created June 16, 2017 23:05
Show Gist options
  • Save postpostscript/1259605ca045cfced2af4170e6dd5e67 to your computer and use it in GitHub Desktop.
Save postpostscript/1259605ca045cfced2af4170e6dd5e67 to your computer and use it in GitHub Desktop.
First draft of a generic tradeable contract
pragma solidity ^0.4.11;
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
contract Ownable {
address public owner;
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable() {
owner = msg.sender;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
if (msg.sender != owner) {
throw;
}
_;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) onlyOwner {
if (newOwner != address(0)) {
owner = newOwner;
}
}
}
pragma solidity ^0.4.0;
import 'Ownable.sol';
contract Tradeable is Ownable {
uint public _minOffer;
function Tradeable(uint minOffer) {
_minOffer = minOffer;
}
function setMinOffer(uint minOffer) onlyOwner {
_minOffer = minOffer;
}
mapping(address => uint) public _offers;
address[] public _offerers;
function offer() payable returns (bool) {
if (msg.value <= _minOffer) return false;
_offers[msg.sender] += msg.value;
_offerers.push(msg.sender);
return true;
}
function withdraw() {
uint value = _offers[msg.sender];
_offers[msg.sender] = 0;
msg.sender.transfer(value);
}
function acceptOffer(address offerOwner) onlyOwner returns (bool) {
if (offerOwner == address(0x0)) return false;
uint value = _offers[offerOwner];
if (this.balance < value) return false;
msg.sender.transfer(value);
transferOwnership(offerOwner);
}
function highestOffer() constant returns (uint) {
uint highest = 0;
for (uint i = 0; i < _offerers.length; i++) {
if (_offers[_offerers[i]] > highest) {
highest = _offers[_offerers[i]];
}
}
return highest;
}
function highestOfferer() constant returns (address) {
uint highest = 0;
address offerer = 0x0;
for (uint i = 0; i < _offerers.length; i++) {
if (_offers[_offerers[i]] > highest) {
highest = _offers[_offerers[i]];
offerer = _offerers[i];
}
}
return offerer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment