Untitled diff

创建于 差异永不过期
202 删除
238 行
38 添加
104 行
pragma solidity ^0.4.0;
pragma solidity ^0.4.0;


//https://github.com/nexusdev/erc20/blob/master/contracts/erc20.sol

contract ERC20Constant {
contract ERC20Constant {
function totalSupply() constant returns (uint supply);
function totalSupply() constant returns (uint supply);
function balanceOf( address who ) constant returns (uint value);
function balanceOf( address who ) constant returns (uint value);
function allowance(address owner, address spender) constant returns (uint _allowance);
function allowance(address owner, address spender) constant returns (uint _allowance);
}
}
contract ERC20Stateful {
contract ERC20Stateful {
function transfer( address to, uint value) returns (bool ok);
function transfer( address to, uint value) returns (bool ok);
function transferFrom( address from, address to, uint value) returns (bool ok);
function transferFrom( address from, address to, uint value) returns (bool ok);
function approve(address spender, uint value) returns (bool ok);
function approve(address spender, uint value) returns (bool ok);
}
}
contract ERC20Events {
contract ERC20Events {
event Transfer(address indexed from, address indexed to, uint value);
event Transfer(address indexed from, address indexed to, uint value);
event Approval( address indexed owner, address indexed spender, uint value);
event Approval( address indexed owner, address indexed spender, uint value);
}
}
contract ERC20 is ERC20Constant, ERC20Stateful, ERC20Events {}
contract ERC20 is ERC20Constant, ERC20Stateful, ERC20Events {}

contract owned { address public owner; function owned() { owner = msg.sender; }
contract owned {
modifier onlyOwner { if (msg.sender != owner) throw; _; }
address public owner;
function transferOwnership(address newOwner) onlyOwner { owner = newOwner; }

function owned() {
owner = msg.sender;
}

modifier onlyOwner {
if (msg.sender != owner) throw;
_;
}

function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
}

contract BECTrader is owned {
// contract can buy or sell tokens for ETH
address public asset;
// prices are in amount of wei per batch of token units
uint256 public becbuyPrice;

uint256 public becsellPrice;
contract TokenTrader is owned {
uint256 public units;

uint256 public becContractBalance;
address public asset; // address of token
uint256 public etcContractBalance;
uint256 public buyPrice; // contact buys lots of token at this price
bool public sellsTokens;
uint256 public sellPrice; // contract sells lots at this price
bool public buysTokens;
uint256 public units; // lot size (token-wei)

bool public sellsTokens; // is contract selling
bool public buysTokens; // is contract buying

event ActivatedEvent(bool sells, bool buys);
event ActivatedEvent(bool sells, bool buys);
event UpdateEvent();
event UpdateEvent();

function BECTrader () {
function TokenTrader (
asset = 0x085fb4f24031eaedbc2b611aa528f22343eb52db;
address _asset,
becbuyPrice = 10100; // 1% fee on buy side
uint256 _buyPrice,
becsellPrice = 10000;
uint256 _sellPrice,
units = 8;
uint256 _units,
sellsTokens = true;
bool _sellsTokens,
buysTokens = true;
bool _buysTokens
etcContractBalance = this.balance;
)
becContractBalance = ERC20(asset).balanceOf(address(this));
{
asset = _asset;
buyPrice = _buyPrice;
sellPrice = _sellPrice;
units = _units;
sellsTokens = _sellsTokens;
buysTokens = _buysTokens;

ActivatedEvent(sellsTokens,buysTokens);
ActivatedEvent(sellsTokens,buysTokens);
}
}

function Owneractivate (bool _sellsTokens,bool _buysTokens) onlyOwner {
// modify trading behavior
function activate (
bool _sellsTokens,
bool _buysTokens
) onlyOwner
{
sellsTokens = _sellsTokens;
sellsTokens = _sellsTokens;
buysTokens = _buysTokens;
buysTokens = _buysTokens;

ActivatedEvent(sellsTokens,buysTokens);
ActivatedEvent(sellsTokens,buysTokens);
}
}

function OwnerSetPrices ( uint256 _buyPrice, uint256 _sellPrice, uint256 _units) onlyOwner {
// allows owner to deposit ETH
becsellPrice = _sellPrice;
// deposit tokens by sending them directly to contract
becbuyPrice = _buyPrice;
// buyers must not send tokens to the contract, use: sell(...)
units = _units;
function deposit() payable onlyOwner {
}
function OwnerdepositETC() payable onlyOwner {
// ower deposits tokens by sending them directly to contract
UpdateEvent();
UpdateEvent();
}
}

function OwnerwithdrawBEC(uint256 _value) onlyOwner returns (bool ok) {
// allow owner to remove trade token
function withdrawAsset(uint256 _value) onlyOwner returns (bool ok)
{
return ERC20(asset).transfer(owner,_value);
return ERC20(asset).transfer(owner,_value);
UpdateEvent();
UpdateEvent();
}
}

function OwnerwithdrawETC(uint256 _value) onlyOwner returns (bool ok)
// allow owner to remove arbitrary tokens
// included just in case contract receives wrong token
function withdrawToken(address _token, uint256 _value) onlyOwner returns (bool ok)
{
return ERC20(_token).transfer(owner,_value);
UpdateEvent();
}

// allow owner to remove ETH
function withdraw(uint256 _value) onlyOwner returns (bool ok)
{
{
if(this.balance >= _value) {
if(this.balance >= _value) {
return owner.send(_value);
return owner.send(_value);
}
}
UpdateEvent();
UpdateEvent();
}
}

function traderBuy() payable {
//user buys token with ETH
function buy() payable {
if(sellsTokens || msg.sender == owner)
if(sellsTokens || msg.sender == owner)
{
{
uint order = msg.value / sellPrice;
uint order = msg.value / becsellPrice;
uint can_sell = ERC20(asset).balanceOf(address(this)) / units;
uint can_sell = ERC20(asset).balanceOf(address(this)) / units;

if(order > can_sell)
if(order > can_sell)
{
{
uint256 change = msg.value - (can_sell * sellPrice);
uint256 change = msg.value - (can_sell * becsellPrice);
order = can_sell;
order = can_sell;
if(!msg.sender.send(change)) throw;
if(!msg.sender.send(change)) throw;
}
}

if(order > 0) {
if(order > 0) {
if(!ERC20(asset).transfer(msg.sender,order * units)) throw;
if(!ERC20(asset).transfer(msg.sender,order * units)) throw;
}
}
UpdateEvent();
UpdateEvent();
}
}
else if(!msg.sender.send(msg.value)) throw; // return user funds if the contract is not selling
else if(!msg.sender.send(msg.value)) throw;
}
}

function traderSell(uint256 amount) {
// user sells token for ETH
// user must set allowance for this contract before calling
function sell(uint256 amount) {
if (buysTokens || msg.sender == owner) {
if (buysTokens || msg.sender == owner) {
uint256 can_buy = this.balance / buyPrice; // token lots contract can buy
uint256 can_buy = this.balance / becbuyPrice;
uint256 order = amount / units; // token lots available
uint256 order = amount / units;

if(order > can_buy) order = can_buy;
if(order > can_buy) order = can_buy; // adjust order for funds

if (order > 0)
if (order > 0)
{
{
// extract user tokens
if(!ERC20(asset).transferFrom(msg.sender, address(this), order*units)) throw;
if(!ERC20(asset).transferFrom(msg.sender, address(this), amount)) throw;
if(!msg.sender.send(order * becbuyPrice)) throw;

// pay user
if(!msg.sender.send(order * buyPrice)) throw;
}
}
UpdateEvent();
UpdateEvent();
}
}
}
}

// sending ETH to contract sells ETH to user
function () payable { traderBuy(); }
function () payable {
buy();
}
}

// This contract deploys TokenTrader contracts and logs the event
// trade pairs are identified with sha3(asset,units)

contract TokenTraderFactory {

event TradeListing(bytes32 bookid, address owner, address addr);
event NewBook(bytes32 bookid, address asset, uint256 units);

mapping( address => bool ) _verify;
mapping( bytes32 => bool ) pairExits;
function verify(address tradeContract) constant returns (
function Ownerdistruct() onlyOwner returns (bool ok) { suicide(owner); }
bool valid,
address asset,
uint256 buyPrice,
uint256 sellPrice,
uint256 units,
bool sellsTokens,
bool buysTokens
) {
valid = _verify[tradeContract];
if(valid) {
TokenTrader t = TokenTrader(tradeContract);
asset = t.asset();
buyPrice =t.buyPrice();
sellPrice = t.sellPrice();
units = t.units();
sellsTokens = t.sellsTokens();
buysTokens = t.buysTokens();
}
}

function createTradeContract(
address _asset,
uint256 _buyPrice,
uint256 _sellPrice,
uint256 _units,
bool _sellsTokens,
bool _buysTokens
) returns (address)
{
if(_buyPrice > _sellPrice) throw; // must make profit on spread
if(_units == 0) throw; // can't sell zero units

address trader = new TokenTrader (
_asset,
_buyPrice,
_sellPrice,
_units,
_sellsTokens,
_buysTokens);

var bookid = sha3(_asset,_units);

_verify[trader] = true; // record that this factory created the trader

TokenTrader(trader).transferOwnership(msg.sender); // set the owner to whoever called the function

if(pairExits[bookid] == false) {
pairExits[bookid] = true;
NewBook(bookid, _asset, _units);
}

TradeListing(bookid,msg.sender,trader);
}

function () {
throw; // Prevents accidental sending of ether to the factory
}
}
}