Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. pragma solidity ^0.4.8;
  2.  
  3. contract Token {
  4. /// @return total amount of tokens
  5. function totalSupply() constant returns (uint256 supply) {}
  6. /// @param _owner The address from which the balance will be retrieved
  7. /// @return The balance
  8. function balanceOf(address _owner) constant returns (uint256 balance) {}
  9. /// @notice send `_value` token to `_to` from `msg.sender`
  10. /// @param _to The address of the recipient
  11. /// @param _value The amount of token to be transferred
  12. /// @return Whether the transfer was successful or not
  13. function transfer(address _to, uint256 _value) returns (bool success) {}
  14. /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
  15. /// @param _from The address of the sender
  16. /// @param _to The address of the recipient
  17. /// @param _value The amount of token to be transferred
  18. /// @return Whether the transfer was successful or not
  19. function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}
  20. /// @notice `msg.sender` approves `_addr` to spend `_value` tokens
  21. /// @param _spender The address of the account able to transfer the tokens
  22. /// @param _value The amount of wei to be approved for transfer
  23. /// @return Whether the approval was successful or not
  24. function approve(address _spender, uint256 _value) returns (bool success) {}
  25. /// @param _owner The address of the account owning tokens
  26. /// @param _spender The address of the account able to transfer the tokens
  27. /// @return Amount of remaining tokens allowed to spent
  28. function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}
  29. event Transfer(address indexed _from, address indexed _to, uint256 _value);
  30. event Approval(address indexed _owner, address indexed _spender, uint256 _value);
  31. uint public decimals;
  32. string public name;
  33. }
  34.  
  35. contract TokenExchange {
  36. mapping (address => mapping (address => uint256)) amounts;
  37. mapping (address => mapping (address => uint256)) prices;
  38.  
  39. // Allows user to deposit some number of tokens that you are willing to sell for some price.
  40. // Should also check that the user has has given the exchange an allowance of this token
  41. // (and transfer the tokens into the contracts control).
  42. function depositToken(address _token, uint _amount) {
  43. Token t = Token(_token);
  44. if (_amount < 0) throw;
  45. // Note: a bit confused about the tradeoff between security and accuracy
  46. // amount should only change if there is a 'successful' transaction
  47. // (same with withdrawToken)
  48. amounts[_token][msg.sender] += _amount;
  49. if (!t.transferFrom(msg.sender, this, _amount)) throw;
  50. }
  51.  
  52. // Allows users who have previously deposited tokens to reclaim possession of them.
  53. // Also allows users who have bought tokens to claim possession of them.
  54. // The decentralized exchange should use the tokens transfer functon
  55. function withdrawToken(address _token, uint _amount) {
  56. Token t = Token(_token);
  57. if (amounts[_token][msg.sender] < _amount) throw;
  58. if (_amount < 0) throw;
  59. amounts[_token][msg.sender] -= _amount;
  60. if (!t.transfer(msg.sender, _amount)) throw;
  61. }
  62.  
  63. // Returns the number of tokens that a user has deposited of that specific _token.
  64. function getTokenBalance(address _owner, address _token) constant returns (uint256 balance) {
  65. Token t = Token(_token);
  66. return amounts[_token][_owner];
  67. }
  68.  
  69. // Set the price you are willing to sell your tokens for (in Ether)
  70. function setPrice(address _token, uint _price) {
  71. Token t = Token(_token);
  72. if (_price < 0) throw;
  73. prices[_token][msg.sender] = _price;
  74. }
  75.  
  76. // Send this function with at least _amount * _price of token for that
  77. // specific seller, and have the tokens transferred into your ownership
  78. // (within this contract only, not in the token contract yet - that's
  79. // what the withdrawTokens function is for).
  80. function buyToken(address _token, address _seller, uint _amount) payable {
  81. Token t = Token(_token);
  82. if (_amount < amounts[_token][_seller]) throw;
  83. if (!this.send(_amount * prices[_token][_seller])) throw;
  84. amounts[_token][_seller] -= _amount;
  85. amounts[_token][this] += _amount;
  86. }
  87.  
  88. // withdraw _amount of Ether that you have in the exchange (from selling tokens).
  89. function withdrawEther(uint _amount) {
  90. // sender.send for transfering ether
  91. if (_amount < 0) throw;
  92. if (!msg.sender.send(_amount)) throw;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement