Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. pragma solidity ^0.4.11;
  2.  
  3. contract proxyRecipient {
  4. function proxyTransfer(address caller) payable;
  5. }
  6.  
  7. contract proxyAddress {
  8. function() payable {
  9. proxyRecipient(0xfeedfeedfeedfeedfeedfeedfeedfeedfeedfeed).proxyTransfer.value(msg.value)(msg.sender);
  10. }
  11. }
  12.  
  13.  
  14. contract ERC20 {
  15. event Transfer(address indexed from, address indexed to, uint value);
  16. event Approval( address indexed owner, address indexed spender, uint value);
  17. function totalSupply() constant returns (uint supply);
  18. function balanceOf( address who ) constant returns (uint value);
  19. function allowance(address owner, address spender) constant returns (uint _allowance);
  20. function transfer( address to, uint value) returns (bool ok);
  21. function transferFrom( address from, address to, uint value) returns (bool ok);
  22. function approve(address spender, uint value) returns (bool ok);
  23. }
  24.  
  25. contract SafeMath {
  26. function safeMul(uint a, uint b) internal returns (uint) {
  27. uint c = a * b;
  28. assert(a == 0 || c / a == b);
  29. return c;
  30. }
  31.  
  32. function safeDiv(uint a, uint b) internal returns (uint) {
  33. assert(b > 0);
  34. uint c = a / b;
  35. assert(a == b * c + a % b);
  36. return c;
  37. }
  38.  
  39. function safeSub(uint a, uint b) internal returns (uint) {
  40. assert(b <= a);
  41. return a - b;
  42. }
  43.  
  44. function safeAdd(uint a, uint b) internal returns (uint) {
  45. uint c = a + b;
  46. assert(c>=a && c>=b);
  47. return c;
  48. }
  49.  
  50. function max64(uint64 a, uint64 b) internal constant returns (uint64) {
  51. return a >= b ? a : b;
  52. }
  53.  
  54. function min64(uint64 a, uint64 b) internal constant returns (uint64) {
  55. return a < b ? a : b;
  56. }
  57.  
  58. function max256(uint256 a, uint256 b) internal constant returns (uint256) {
  59. return a >= b ? a : b;
  60. }
  61.  
  62. function min256(uint256 a, uint256 b) internal constant returns (uint256) {
  63. return a < b ? a : b;
  64. }
  65.  
  66. function assert(bool assertion) internal {
  67. if (!assertion) {
  68. throw;
  69. }
  70. }
  71. }
  72.  
  73.  
  74. contract owned {
  75. address public owner;
  76.  
  77. function owned() {
  78. owner = msg.sender;
  79. }
  80.  
  81. modifier onlyOwner {
  82. if (msg.sender != owner) throw;
  83. _;
  84. }
  85.  
  86. function transferOwnership(address newOwner) onlyOwner {
  87. owner = newOwner;
  88. }
  89. }
  90.  
  91. contract tradeWallet is SafeMath, owned {
  92.  
  93. struct SELLOFFER
  94. {
  95. ERC20 token;
  96. uint price;
  97. uint units;
  98. uint allowance;
  99. address proxy;
  100. }
  101.  
  102. struct BUYOFFER
  103. {
  104. ERC20 token;
  105. uint price;
  106. uint units;
  107. uint allowance;
  108. }
  109.  
  110. uint public nextOffer = 1;
  111. mapping(address => uint) public proxyMap;
  112. mapping(uint => SELLOFFER) public sellOffers;
  113. mapping(uint => BUYOFFER) public buyOffers;
  114.  
  115. /* soldity: 0.4.11+commit.68ef5810.Emscripten.clang
  116.  
  117. contract proxyAddress {
  118. function() payable {
  119. proxyRecipient(0xfeedfeedfeedfeedfeedfeedfeedfeedfeedfeed).proxyTransfer.value(msg.value)(msg.sender);
  120. }
  121. }
  122.  
  123. */
  124. bytes proxyCode = hex"6060604052341561000c57fe5b5b60c98061001b6000396000f30060606040525b609b5b604080517f833ec7200000000000000000000000000000000000000000000000000000000081523373ffffffffffffffffffffffffffffffffffffffff166004820152905173feedfeedfeedfeedfeedfeedfeedfeedfeedfeed9163833ec72091349160248082019260009290919082900301818588803b1515608757fe5b6125ee5a03f11515609457fe5b505050505b565b0000a165627a7a723058201205452475b8ac3fdba319cceb71647d6ac16a0d19aa9b33c601a616aef1c1060029";
  125.  
  126. function tradeWallet() {
  127. // set the proxy tearget to this contract:
  128. for (uint i = 0; i < 20; i++) {
  129. proxyCode[125-i] = byte(uint8(uint(this) >> (8*i)));
  130. }
  131. }
  132.  
  133. function deployCode(bytes _code) internal returns (address deployedAddress) {
  134. assembly {
  135. deployedAddress := create(0, add(_code, 0x20), mload(_code))
  136. jumpi(invalidJumpLabel, iszero(extcodesize(deployedAddress))) // jumps if no code at addresses
  137. }
  138. _code;
  139. deployedAddress;
  140. }
  141.  
  142. function makeSellOffer(ERC20 token, uint price, uint units, uint allowance) onlyOwner
  143. {
  144. sellOffers[nextOffer] = SELLOFFER(token,price,units,allowance,deployCode(proxyCode));
  145. proxyMap[sellOffers[nextOffer].proxy] = nextOffer;
  146. nextOffer++;
  147. }
  148.  
  149. function makeBuyOffer(ERC20 token, uint price, uint units, uint allowance) onlyOwner
  150. {
  151. buyOffers[nextOffer] = BUYOFFER(token,price,units,allowance);
  152. nextOffer++;
  153. }
  154.  
  155. function setSellAllowance(uint id, uint allowance) onlyOwner {
  156. sellOffers[id].allowance = allowance;
  157. }
  158.  
  159. function min(uint a, uint b) internal returns (uint) {
  160. if(a < b) return a;
  161. return b;
  162. }
  163.  
  164. function takerBuys(address taker, uint id) internal
  165. {
  166. var availableTokens = min(sellOffers[id].token.balanceOf(this),sellOffers[id].allowance);
  167. var unitLots = min( availableTokens / sellOffers[id].units , msg.value / sellOffers[id].price);
  168. var takerPayment = unitLots * sellOffers[id].price;
  169. var makerPayment = unitLots * sellOffers[id].units;
  170. var change = msg.value - takerPayment;
  171.  
  172. sellOffers[id].allowance = safeSub(sellOffers[id].allowance,makerPayment);
  173.  
  174. if(!sellOffers[id].token.transfer(taker,makerPayment)) throw;
  175. if(change > 0) taker.transfer(change);
  176. }
  177.  
  178. function takerSells(uint id, uint unitLots)
  179. {
  180. var availableEther = min(this.balance, buyOffers[id].allowance);
  181. unitLots = min(unitLots, availableEther / buyOffers[id].units);
  182. var takerPayment = unitLots * buyOffers[id].units;
  183. var makerPayment = unitLots * buyOffers[id].price;
  184.  
  185. buyOffers[id].allowance = safeSub(buyOffers[id].allowance,makerPayment);
  186.  
  187. if(!buyOffers[id].token.transferFrom(msg.sender,this,takerPayment)) throw;
  188. msg.sender.transfer(makerPayment);
  189. }
  190.  
  191. function takerBuys(uint id) payable {
  192. takerBuys(msg.sender,id);
  193. }
  194.  
  195. function proxyTransfer(address caller) payable
  196. {
  197. takerBuys(caller,proxyMap[msg.sender]);
  198. }
  199.  
  200. function makerWithdrawEther(uint256 etherValueSmallestUnits) onlyOwner
  201. {
  202. if(etherValueSmallestUnits > this.balance) etherValueSmallestUnits = this.balance;
  203. msg.sender.transfer(etherValueSmallestUnits);
  204. }
  205.  
  206. function makerDepositEther() payable {
  207. }
  208.  
  209. function makerWidtdrawToken(ERC20 token, uint tokenSmallestUnits) onlyOwner {
  210. if(tokenSmallestUnits > token.balanceOf(this)) tokenSmallestUnits = token.balanceOf(this);
  211. token.transfer(msg.sender,tokenSmallestUnits);
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement