From f9110f8bb08526e3e8b3e9905bc772a5c02856dc Mon Sep 17 00:00:00 2001 From: goooo Date: Wed, 26 Sep 2018 17:49:15 +0800 Subject: [PATCH] finish simple function v0.1.0 --- exchange.abi | 39 +++++++++++++++++++++++++++++++++------ exchange.cpp | 35 ++++++++++++++++++++++------------- exchange.hpp | 20 +++++++++----------- 3 files changed, 64 insertions(+), 30 deletions(-) diff --git a/exchange.abi b/exchange.abi index 3580640..97d05e1 100644 --- a/exchange.abi +++ b/exchange.abi @@ -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", @@ -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": [{ @@ -57,7 +80,11 @@ "name": "ask", "type": "ask", "ricardian_contract": "" - } + },{ + "name": "cancel_order", + "type": "cancel_order", + "ricardian_contract": "" + } ], "tables": [{ "name": "orders", diff --git a/exchange.cpp b/exchange.cpp index 9d5a3b0..bc20286 100644 --- a/exchange.cpp +++ b/exchange.cpp @@ -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"); @@ -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) { @@ -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); @@ -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; @@ -120,18 +130,17 @@ 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(); @@ -139,7 +148,7 @@ void exchange::bid(account_name maker, asset quantity, uint64_t price) { 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"); @@ -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)) diff --git a/exchange.hpp b/exchange.hpp index 9d94941..c9c8834 100644 --- a/exchange.hpp +++ b/exchange.hpp @@ -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 @@ -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 > > 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);