Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.4.0;
  2.  
  3. contract owned {
  4.     address public owner;
  5.  
  6.     function owned() {
  7.         owner = msg.sender;
  8.     }
  9.  
  10.     modifier onlyOwner {
  11.         if (msg.sender != owner) throw;
  12.         _;
  13.     }
  14.  
  15.     function transferOwnership(address newOwner) onlyOwner {
  16.         owner = newOwner;
  17.     }
  18. }
  19.  
  20.  
  21. //https://github.com/nexusdev/erc20/blob/master/contracts/erc20.sol
  22.  
  23. contract ERC20Constant {
  24.     function totalSupply() constant returns (uint supply);
  25.     function balanceOf( address who ) constant returns (uint value);
  26.     function allowance(address owner, address spender) constant returns (uint _allowance);
  27. }
  28. contract ERC20Stateful {
  29.     function transfer( address to, uint value) returns (bool ok);
  30.     function transferFrom( address from, address to, uint value) returns (bool ok);
  31.     function approve(address spender, uint value) returns (bool ok);
  32. }
  33. contract ERC20Events {
  34.     event Transfer(address indexed from, address indexed to, uint value);
  35.     event Approval( address indexed owner, address indexed spender, uint value);
  36. }
  37. contract ERC20 is ERC20Constant, ERC20Stateful, ERC20Events {}
  38.  
  39.  
  40.  
  41. // contract can buy or sell tokens for ETH
  42. // prices are in amount of wei per batch of token units
  43.  
  44. contract TokenTrader is owned {
  45.  
  46.     address public asset;       // address of token
  47.     uint256 public buyPrice;   // contact buys lots of token at this price
  48.     uint256 public sellPrice;  // contract sells lots at this price
  49.     uint256 public units;       // lot size (token-wei)
  50.    
  51.     bool public sellsTokens;    // is contract selling
  52.     bool public buysTokens;     // is contract buying
  53.    
  54.     event ActivatedEvent(bool sells, bool buys);
  55.     event UpdateEvent();
  56.  
  57.     function TokenTrader (
  58.         address _asset,
  59.         uint256 _buyPrice,
  60.         uint256 _sellPrice,
  61.         uint256 _units,
  62.         bool    _sellsTokens,
  63.         bool    _buysTokens
  64.         )
  65.     {
  66.           asset         = _asset;
  67.           buyPrice     = _buyPrice;
  68.           sellPrice    = _sellPrice;
  69.           units         = _units;
  70.           sellsTokens   = _sellsTokens;
  71.           buysTokens    = _buysTokens;
  72.          
  73.           ActivatedEvent(sellsTokens,buysTokens);
  74.     }
  75.    
  76.     // modify trading behavior
  77.     function activate (
  78.         bool    _sellsTokens,
  79.         bool    _buysTokens
  80.         )
  81.     {
  82.           sellsTokens   = _sellsTokens;
  83.           buysTokens    = _buysTokens;
  84.          
  85.           ActivatedEvent(sellsTokens,buysTokens);
  86.     }
  87.    
  88.     // allows owner to deposit ETH
  89.     // deposit tokens by sending them directly to contract
  90.     // buyers must not send tokens to the contract, use: sell(...)
  91.     function deposit() payable onlyOwner {
  92.     }
  93.    
  94.     // allow owner to remove trade token
  95.     function withdrawAsset(uint256 _value) onlyOwner returns (bool ok)
  96.     {
  97.         return ERC20(asset).transfer(owner,_value);
  98.     }
  99.  
  100.     // allow owner to remove arbitrary tokens
  101.     // included just in case contract receives wrong token
  102.     function withdrawToken(address _token, uint256 _value) onlyOwner returns (bool ok)
  103.     {
  104.         return ERC20(_token).transfer(owner,_value);
  105.     }
  106.  
  107.     // allow owner to remove ETH
  108.     function withdraw(uint256 _value) onlyOwner returns (bool ok)
  109.     {
  110.         if(this.balance >= _value) {
  111.             return owner.send(_value);
  112.         }
  113.     }
  114.    
  115.     //user buys token with ETH
  116.     function buy() payable {
  117.         if(sellsTokens || msg.sender == owner)
  118.         {
  119.             uint order   = msg.value / sellPrice;
  120.             uint can_sell = ERC20(asset).balanceOf(address(this)) / units;
  121.            
  122.             if(order > can_sell)
  123.             {
  124.                 uint256 change = msg.value - (can_sell * sellPrice);
  125.                 order = can_sell;
  126.                 if(!msg.sender.send(change)) throw;
  127.             }
  128.            
  129.             if(order > 0) {
  130.                 if(!ERC20(asset).transfer(msg.sender,order * units)) throw;
  131.             }
  132.             UpdateEvent();
  133.         }
  134.         else throw;  // return user funds if the contract is not selling
  135.     }
  136.    
  137.     // user sells token for ETH
  138.     // user must set allowance for this contract before calling
  139.     function sell(uint256 amount) {
  140.         if (buysTokens || msg.sender == owner) {
  141.             uint256 can_buy = this.balance / buyPrice;  // token lots contract can buy
  142.             uint256 order = amount / units;             // token lots available
  143.            
  144.             if(order > can_buy) order = can_buy;        // adjust order for funds
  145.            
  146.             if (order > 0)
  147.             {
  148.                 // extract user tokens
  149.                 if(!ERC20(asset).transferFrom(msg.sender, address(this), amount)) throw;
  150.                
  151.                 // pay user
  152.                 if(!msg.sender.send(order * buyPrice)) throw;
  153.             }
  154.             UpdateEvent();
  155.         }
  156.     }
  157.  
  158.     // sending ETH to contract sells ETH to user
  159.     function () payable {
  160.         buy();
  161.     }
  162. }
  163.  
  164. // This contract deploys TokenTrader contracts and logs the event
  165. // trade pairs are identified with sha3(asset,units)
  166.  
  167. contract TokenTraderFactory {
  168.    
  169.     event TradeListing(bytes32 bookid, address owner, address addr);
  170.     event NewBook(bytes32 bookid, address asset, uint256 units);
  171.    
  172.     mapping( address => bool ) public verify;
  173.     mapping( bytes32 => bool ) pairExits;
  174.    
  175.     function createTradeContract(      
  176.         address _asset,
  177.         uint256 _buyPrice,
  178.         uint256 _sellPrice,
  179.         uint256 _units,
  180.         bool    _sellsTokens,
  181.         bool    _buysTokens
  182.         ) returns (address)
  183.     {
  184.         if(_buyPrice > _sellPrice) throw; // must make profit on spread
  185.         if(_units == 0) throw;              // can't sell zero units
  186.        
  187.         address trader = new TokenTrader (
  188.                      _asset,
  189.                      _buyPrice,
  190.                      _sellPrice,
  191.                      _units,
  192.                      _sellsTokens,
  193.                      _buysTokens);
  194.          
  195.         var bookid = sha3(_asset,_units);
  196.        
  197.         verify[trader] = true; // record that this factory created the trader
  198.        
  199.         TokenTrader(trader).transferOwnership(msg.sender); // set the owner to whoever called the function
  200.  
  201.         if(pairExits[bookid] == false) {
  202.             pairExits[bookid] = true;
  203.             NewBook(bookid, _asset, _units);
  204.         }
  205.        
  206.         TradeListing(bookid,msg.sender,trader);
  207.     }
  208.  
  209.     function () {
  210.         throw;     // Prevents accidental sending of ether to the factory
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement