Last active
April 3, 2017 04:02
-
-
Save yoninachmany/bce4a90cdab192f50f2a1ccc60264f86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
mapping (address => uint256) etherOf; | |
// 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; | |
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 (_amount < 0) throw; | |
if (amounts[_token][msg.sender] < _amount) 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 < 0) throw; | |
if (_amount < amounts[_token][_seller]) throw; | |
if (_amount < msg.value) throw; | |
amounts[_token][_seller] -= _amount; | |
amounts[_token][this] += _amount; | |
etherOf[_token] -= _amount * prices[_token][_seller]; | |
etherOf[this] += _amount * prices[_token][_seller]; | |
} | |
// 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 (etherOf[msg.sender] < _amount) throw; | |
if (!msg.sender.send(etherOf[msg.sender])) throw; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment