Skip to content

Commit

Permalink
token exchanger
Browse files Browse the repository at this point in the history
  • Loading branch information
flasher007 committed Apr 22, 2020
0 parents commit b007deb
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/node_modules
/vendor
/.idea
/.vscode
/.vagrant
Homestead.json
Homestead.yaml
npm-debug.log
yarn-error.log
.env
/build
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**Ethereum Token Exchanger**
228 changes: 228 additions & 0 deletions contracts/Exchange.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
pragma solidity ^0.4.13;


import "./owned.sol";
import "./ExchangerToken.sol";


contract Exchange is owned {

///////////////////////
// GENERAL STRUCTURE //
///////////////////////
struct Offer {

uint amount;
address who;
}

struct OrderBook {

uint higherPrice;
uint lowerPrice;

mapping (uint => Offer) offers;

uint offers_key;
uint offers_length;
}

struct Token {

address tokenContract;

string symbolName;


mapping (uint => OrderBook) buyBook;

uint curBuyPrice;
uint lowestBuyPrice;
uint amountBuyPrices;


mapping (uint => OrderBook) sellBook;
uint curSellPrice;
uint highestSellPrice;
uint amountSellPrices;

}


//we support a max of 255 tokens...
mapping (uint8 => Token) tokens;
uint8 symbolNameIndex;


//////////////
// BALANCES //
//////////////
mapping (address => mapping (uint8 => uint)) tokenBalanceForAddress;

mapping (address => uint) balanceEthForAddress;




////////////
// EVENTS //
////////////




//////////////////////////////////
// DEPOSIT AND WITHDRAWAL ETHER //
//////////////////////////////////
function depositEther() payable {
require(balanceEthForAddress[msg.sender] + msg.value >= balanceEthForAddress[msg.sender]);
balanceEthForAddress[msg.sender] += msg.value;
}

function withdrawEther(uint amountInWei) {
require(balanceEthForAddress[msg.sender] - amountInWei >= 0);
require(balanceEthForAddress[msg.sender] - amountInWei <= balanceEthForAddress[msg.sender]);
balanceEthForAddress[msg.sender] -= amountInWei;
msg.sender.transfer(amountInWei);
}

function getEthBalanceInWei() constant returns (uint){
return balanceEthForAddress[msg.sender];
}


//////////////////////
// TOKEN MANAGEMENT //
//////////////////////

function addToken(string symbolName, address erc20TokenAddress) onlyowner {
require(!hasToken(symbolName));
symbolNameIndex++;
tokens[symbolNameIndex].symbolName = symbolName;
tokens[symbolNameIndex].tokenContract = erc20TokenAddress;
}

function hasToken(string symbolName) constant returns (bool) {
uint8 index = getSymbolIndex(symbolName);
if (index == 0) {
return false;
}
return true;
}


function getSymbolIndex(string symbolName) internal returns (uint8) {
for (uint8 i = 1; i <= symbolNameIndex; i++) {
if (stringsEqual(tokens[i].symbolName, symbolName)) {
return i;
}
}
return 0;
}


function getSymbolIndexOrThrow(string symbolName) returns (uint8) {
uint8 index = getSymbolIndex(symbolName);
require(index > 0);
return index;
}






////////////////////////////////
// STRING COMPARISON FUNCTION //
////////////////////////////////
function stringsEqual(string storage _a, string memory _b) internal returns (bool) {
bytes storage a = bytes(_a);
bytes memory b = bytes(_b);
if (a.length != b.length)
return false;
// @todo unroll this loop
for (uint i = 0; i < a.length; i ++)
if (a[i] != b[i])
return false;
return true;
}


//////////////////////////////////
// DEPOSIT AND WITHDRAWAL TOKEN //
//////////////////////////////////
function depositToken(string symbolName, uint amount) {
uint8 symbolNameIndex = getSymbolIndexOrThrow(symbolName);
require(tokens[symbolNameIndex].tokenContract != address(0));

ERC20Interface token = ERC20Interface(tokens[symbolNameIndex].tokenContract);

require(token.transferFrom(msg.sender, address(this), amount) == true);
require(tokenBalanceForAddress[msg.sender][symbolNameIndex] + amount >= tokenBalanceForAddress[msg.sender][symbolNameIndex]);
tokenBalanceForAddress[msg.sender][symbolNameIndex] += amount;
}

function withdrawToken(string symbolName, uint amount) {
uint8 symbolNameIndex = getSymbolIndexOrThrow(symbolName);
require(tokens[symbolNameIndex].tokenContract != address(0));

ERC20Interface token = ERC20Interface(tokens[symbolNameIndex].tokenContract);

require(tokenBalanceForAddress[msg.sender][symbolNameIndex] - amount >= 0);
require(tokenBalanceForAddress[msg.sender][symbolNameIndex] - amount <= tokenBalanceForAddress[msg.sender][symbolNameIndex]);

tokenBalanceForAddress[msg.sender][symbolNameIndex] -= amount;
require(token.transfer(msg.sender, amount) == true);
}

function getBalance(string symbolName) constant returns (uint) {
uint8 symbolNameIndex = getSymbolIndexOrThrow(symbolName);
return tokenBalanceForAddress[msg.sender][symbolNameIndex];
}





/////////////////////////////
// ORDER BOOK - BID ORDERS //
/////////////////////////////
function getBuyOrderBook(string symbolName) constant returns (uint[], uint[]) {
}


/////////////////////////////
// ORDER BOOK - ASK ORDERS //
/////////////////////////////
function getSellOrderBook(string symbolName) constant returns (uint[], uint[]) {
}



////////////////////////////
// NEW ORDER - BID ORDER //
///////////////////////////
function buyToken(string symbolName, uint priceInWei, uint amount) {
}





////////////////////////////
// NEW ORDER - ASK ORDER //
///////////////////////////
function sellToken(string symbolName, uint priceInWei, uint amount) {
}



//////////////////////////////
// CANCEL LIMIT ORDER LOGIC //
//////////////////////////////
function cancelOrder(string symbolName, bool isSellOrder, uint priceInWei, uint offerKey) {
}



}
126 changes: 126 additions & 0 deletions contracts/ExchangerToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
pragma solidity ^0.4.18;

// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/issues/20
contract ERC20Interface {
// Get the total token supply
function totalSupply() public constant returns (uint256);

// Get the account balance of another account with address _owner
function balanceOf(address _owner) public constant returns (uint256 balance);

// Send _value amount of tokens to address _to
function transfer(address _to, uint256 _value) public returns (bool success);

// Send _value amount of tokens from address _from to address _to
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);

// Allow _spender to withdraw from your account, multiple times, up to the _value amount.
// If this function is called again it overwrites the current allowance with _value.
// this function is required for some DEX functionality
function approve(address _spender, uint256 _value) public returns (bool success);

// Returns the amount which _spender is still allowed to withdraw from _owner
function allowance(address _owner, address _spender) public constant returns (uint256 remaining);

// Triggered when tokens are transferred.
event Transfer(address indexed _from, address indexed _to, uint256 _value);

// Triggered whenever approve(address _spender, uint256 _value) is called.
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}


contract ExchangerToken is ERC20Interface {
string public constant symbol = "EET";
string public constant name = "Example Exchanger Token";
uint8 public constant decimals = 0;
uint256 _totalSupply = 1000000;

// Owner of this contract
address public owner;

// Balances for each account
mapping(address => uint256) balances;

// Owner of account approves the transfer of an amount to another account
mapping(address => mapping(address => uint256)) allowed;

// Functions with this modifier can only be executed by the owner
modifier onlyOwner() {
if (msg.sender != owner) {
revert();
}
_;
}

// Constructor
function FixedSupplyToken() public {
owner = msg.sender;
balances[owner] = _totalSupply;
}

function totalSupply() public constant returns (uint256) {
return _totalSupply;
}

// What is the balance of a particular account?
function balanceOf(address _owner) public constant returns (uint256 balance) {
return balances[_owner];
}

// Transfer the balance from owner's account to another account
function transfer(address _to, uint256 _amount) public returns (bool success) {
if (balances[msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[msg.sender] -= _amount;
balances[_to] += _amount;
Transfer(msg.sender, _to, _amount);
return true;
}
else {
return false;
}
}

// Send _value amount of tokens from address _from to address _to
// The transferFrom method is used for a withdraw workflow, allowing contracts to send
// tokens on your behalf, for example to "deposit" to a contract address and/or to charge
// fees in sub-currencies; the command should fail unless the _from account has
// deliberately authorized the sender of the message via some mechanism; we propose
// these standardized APIs for approval:
function transferFrom(
address _from,
address _to,
uint256 _amount
) public returns (bool success) {
if (balances[_from] >= _amount
&& allowed[_from][msg.sender] >= _amount
&& _amount > 0
&& balances[_to] + _amount > balances[_to]) {
balances[_from] -= _amount;
allowed[_from][msg.sender] -= _amount;
balances[_to] += _amount;
Transfer(_from, _to, _amount);
return true;
}
else {
return false;
}
}


// Allow _spender to withdraw from your account, multiple times, up to the _value amount.
// If this function is called again it overwrites the current allowance with _value.
function approve(address _spender, uint256 _amount) public returns (bool success) {
allowed[msg.sender][_spender] = _amount;
Approval(msg.sender, _spender, _amount);
return true;
}

function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
return allowed[_owner][_spender];
}

}
23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.23;

contract Migrations {
address public owner;
uint public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
Loading

0 comments on commit b007deb

Please sign in to comment.