Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.8;
- contract Token {
- /// @return total amount of tokens
- function totalSupply() constant returns (uint256 supply) {}
- /// @param _owner The address from which the balance will be retrieved
- /// @return The balance
- function balanceOf(address _owner) constant returns (uint256 balance) {}
- /// @notice send `_value` token to `_to` from `msg.sender`
- /// @param _to The address of the recipient
- /// @param _value The amount of token to be transferred
- /// @return Whether the transfer was successful or not
- function transfer(address _to, uint256 _value) returns (bool success) {}
- /// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from`
- /// @param _from The address of the sender
- /// @param _to The address of the recipient
- /// @param _value The amount of token to be transferred
- /// @return Whether the transfer was successful or not
- function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {}
- /// @notice `msg.sender` approves `_addr` to spend `_value` tokens
- /// @param _spender The address of the account able to transfer the tokens
- /// @param _value The amount of wei to be approved for transfer
- /// @return Whether the approval was successful or not
- function approve(address _spender, uint256 _value) returns (bool success) {}
- /// @param _owner The address of the account owning tokens
- /// @param _spender The address of the account able to transfer the tokens
- /// @return Amount of remaining tokens allowed to spent
- function allowance(address _owner, address _spender) constant returns (uint256 remaining) {}
- event Transfer(address indexed _from, address indexed _to, uint256 _value);
- event Approval(address indexed _owner, address indexed _spender, uint256 _value);
- uint public decimals;
- string public name;
- }
- contract TokenExchange {
- mapping (address => mapping (address => uint256)) amounts;
- mapping (address => mapping (address => uint256)) prices;
- // Allows user to deposit some number of tokens that you are willing to sell for some price.
- // Should also check that the user has has given the exchange an allowance of this token
- // (and transfer the tokens into the contracts control).
- function depositToken(address _token, uint _amount) {
- Token t = Token(_token);
- if (_amount < 0) throw;
- // Note: a bit confused about the tradeoff between security and accuracy
- // amount should only change if there is a 'successful' transaction
- // (same with withdrawToken)
- amounts[_token][msg.sender] += _amount;
- if (!t.transferFrom(msg.sender, this, _amount)) throw;
- }
- // Allows users who have previously deposited tokens to reclaim possession of them.
- // Also allows users who have bought tokens to claim possession of them.
- // The decentralized exchange should use the tokens transfer functon
- function withdrawToken(address _token, uint _amount) {
- Token t = Token(_token);
- if (amounts[_token][msg.sender] < _amount) throw;
- if (_amount < 0) throw;
- amounts[_token][msg.sender] -= _amount;
- if (!t.transfer(msg.sender, _amount)) throw;
- }
- // Returns the number of tokens that a user has deposited of that specific _token.
- function getTokenBalance(address _owner, address _token) constant returns (uint256 balance) {
- Token t = Token(_token);
- return amounts[_token][_owner];
- }
- // Set the price you are willing to sell your tokens for (in Ether)
- function setPrice(address _token, uint _price) {
- Token t = Token(_token);
- if (_price < 0) throw;
- prices[_token][msg.sender] = _price;
- }
- // Send this function with at least _amount * _price of token for that
- // specific seller, and have the tokens transferred into your ownership
- // (within this contract only, not in the token contract yet - that's
- // what the withdrawTokens function is for).
- function buyToken(address _token, address _seller, uint _amount) payable {
- Token t = Token(_token);
- if (_amount < amounts[_token][_seller]) throw;
- if (!this.send(_amount * prices[_token][_seller])) throw;
- amounts[_token][_seller] -= _amount;
- amounts[_token][this] += _amount;
- }
- // withdraw _amount of Ether that you have in the exchange (from selling tokens).
- function withdrawEther(uint _amount) {
- // sender.send for transfering ether
- if (_amount < 0) throw;
- if (!msg.sender.send(_amount)) throw;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement