Skip to content

Commit

Permalink
finish simple function
Browse files Browse the repository at this point in the history
v0.1.0
  • Loading branch information
threadshare committed Sep 26, 2018
1 parent 2082399 commit f9110f8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
39 changes: 33 additions & 6 deletions exchange.abi
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,31 @@
"type": "asset"
},{
"name": "maker",
"type": "name"
}
"type": "account_name"
},{
"name": "contract",
"type": "account_name"
}
]
},{
"name": "bid",
"base": "",
"fields": [{
"name": "maker",
"type": "name"
"type": "account_name"
},{
"name": "quantity",
"type": "asset"
},{
"name": "price",
"type": "uint64"
}
},{
"name": "bid_currency",
"type": "symbol_type"
},{
"name": "bid_contract",
"type": "account_name"
}
]
},{
"name": "ask",
Expand All @@ -45,8 +54,22 @@
},{
"name": "price",
"type": "uint64"
}
},{
"name": "ask_contract",
"type": "account_name"
}
]
},{
"name": "cancel_order",
"base": "",
"fields": [{
"name": "scope",
"type": "uint64"
},{
"name": "order_id",
"type": "uint64"
}
]
}
],
"actions": [{
Expand All @@ -57,7 +80,11 @@
"name": "ask",
"type": "ask",
"ricardian_contract": ""
}
},{
"name": "cancel_order",
"type": "cancel_order",
"ricardian_contract": ""
}
],
"tables": [{
"name": "orders",
Expand Down
35 changes: 22 additions & 13 deletions exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using namespace eosio;

void exchange::add_order(uint64_t scope, account_name maker, asset quantity, uint64_t price) {
void exchange::add_order(uint64_t scope, account_name maker, asset quantity, uint64_t price, account_name contract) {
order_index orders(_self, scope);
print("add order, maker: ", name{maker}, " quantity: ", quantity, " price: ", asset(price, settlement_token_symbol),
"\n");
Expand All @@ -18,10 +18,20 @@ void exchange::add_order(uint64_t scope, account_name maker, asset quantity, uin
r.price = price;
r.quantity = quantity;
r.maker = maker;
r.contract = contract;
});
}
}

void exchange::cancel_order(uint64_t scope, uint64_t order_id) {
order_index orders(_self, scope);

const auto& order = orders.get( order_id, "no order object found" );
withdraw(order.contract, order.maker, order.quantity);
orders.erase( order );

}

void exchange::deposit(account_name contract, account_name user, asset quantity) {
print(name{user}, " deposit ", quantity, "\n");
if (quantity.amount == 0) {
Expand Down Expand Up @@ -82,7 +92,7 @@ asset exchange::asset_min(asset a, asset b) {
}

//sell
void exchange::ask(account_name maker, asset quantity, uint64_t price) {
void exchange::ask(account_name maker, asset quantity, uint64_t price, account_name ask_contract) {
//充值其他代币,卖
//deposit(exchange_token_contract, maker, quantity);

Expand All @@ -108,7 +118,7 @@ void exchange::ask(account_name maker, asset quantity, uint64_t price) {

maker_receive += settlement_token_quantity;
//withdraw(exchange_token_contract, bid_it->maker, exchange_quantity);
transfer(exchange_token_contract, maker, bid_it->maker, exchange_quantity);
transfer(ask_contract, maker, bid_it->maker, exchange_quantity);

left -= exchange_quantity;

Expand All @@ -120,26 +130,25 @@ void exchange::ask(account_name maker, asset quantity, uint64_t price) {
});
}
}
deposit(exchange_token_contract, maker, left);
add_order(N(ask), maker, left, price);
deposit(ask_contract, maker, left);
add_order(N(ask), maker, left, price, ask_contract);

//just support xx/EOS trade now, maybe improve it to xx/xx
withdraw(settlement_token_contract, maker, maker_receive);
}

//buy
void exchange::bid(account_name maker, asset quantity, uint64_t price) {
//充值eos
//deposit(settlement_token_contract, maker, to_settlement_token(quantity, price, false));
void exchange::bid(account_name maker, asset quantity, uint64_t price, symbol_type bid_currency, account_name bid_contract) {

//查找对手
//查找卖单
order_index ask_orders(_self, N(ask));
//查找所有卖价低于price的卖方按照价格时间排序
auto ask_orders_by_price = ask_orders.get_index<N(byprice)>();
auto ask_it = ask_orders_by_price.begin();
auto ask_end = ask_orders_by_price.upper_bound(price);

auto left = quantity;
asset maker_receive(0, exchange_token_symbol);
asset maker_receive(0, bid_currency);

while (left.amount > 0 && ask_it != ask_end) {
print("ask order id: ", ask_it->id, " price: ", ask_it->price, "\n");
Expand All @@ -164,9 +173,9 @@ void exchange::bid(account_name maker, asset quantity, uint64_t price) {
}
}
deposit(settlement_token_contract, maker, to_settlement_token(left, price, false));
add_order(N(bid), maker, left, price);
add_order(N(bid), maker, left, price, settlement_token_contract);

withdraw(exchange_token_contract, maker, maker_receive);
withdraw(bid_contract, maker, maker_receive);
}

EOSIO_ABI(exchange, (bid)(ask))
EOSIO_ABI(exchange, (bid)(ask)(cancel_order))
20 changes: 9 additions & 11 deletions exchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ class exchange : public contract {
using contract::contract;

//@abi action
void bid(account_name maker, asset quantity, uint64_t price);
void bid(account_name maker, asset quantity, uint64_t price, symbol_type bid_currency, account_name bid_contract);

//@abi action
void ask(account_name maker, asset quantity, uint64_t price);
void ask(account_name maker, asset quantity, uint64_t price, account_name ask_contract);

//token contract is not eosio.token it is the op
// account_name settlement_token_contract = N(eosio.token);
account_name settlement_token_contract = N(onedexchange);
symbol_type settlement_token_symbol = S(4, EOS);
//@abi action
void cancel_order(uint64_t scope, uint64_t order_id);

account_name exchange_token_contract = N(onedechange);
// account_name exchange_token_contract = N(eosio.token);
symbol_type exchange_token_symbol = S(3, SYS);
account_name settlement_token_contract = N(eosio.token);
symbol_type settlement_token_symbol = S(4, EOS);

private:
//@abi table
Expand All @@ -38,16 +35,17 @@ class exchange : public contract {
uint64_t price;
asset quantity;
account_name maker;
account_name contract;
uint64_t primary_key() const { return id; }
uint64_t get_price() const { return price; }
EOSLIB_SERIALIZE(orders, (id)(price)(quantity)(maker))
EOSLIB_SERIALIZE(orders, (id)(price)(quantity)(maker)(contract))
};

typedef multi_index<N(orders), orders,
indexed_by<N(byprice), const_mem_fun<orders, uint64_t, &orders::get_price> >
> order_index;

void add_order(uint64_t scope, account_name maker, asset quantity, uint64_t price);
void add_order(uint64_t scope, account_name maker, asset quantity, uint64_t price, account_name contract);

void deposit(account_name contract, account_name user, asset quantity);
void withdraw(account_name contract, account_name user, asset quantity);
Expand Down

0 comments on commit f9110f8

Please sign in to comment.