Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #101 from elmato/extend-abi-serializer
Browse files Browse the repository at this point in the history
Extend abi serializer
  • Loading branch information
elmato authored Jul 28, 2017
2 parents 67c9b8b + 5bdcead commit 79b73d9
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 28 deletions.
70 changes: 70 additions & 0 deletions contracts/exchange/exchange.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"types": [{
"newTypeName": "AccountName",
"type": "Name"
}
],
"structs": [{
"name": "OrderID",
"fields": {
"name" : "AccountName",
"id" : "UInt64"
}
},{
"name" : "Bid",
"fields" : {
"buyer" : "OrderID",
"price" : "UInt128",
"quantity" : "UInt64",
"expiration" : "Time"
}
},{
"name" : "Ask",
"fields" : {
"seller" : "OrderID",
"price" : "UInt128",
"quantity" : "UInt64",
"expiration" : "Time"
}
},{
"name" : "Account",
"fields" : {
"owner" : "AccountName",
"eos_balance" : "UInt64",
"currency_balance" : "UInt64",
"open_orders" : "UInt32"
}
},{
"name" : "BuyOrder",
"base" : "Bid",
"fields" : {
"fill_or_kill" : "UInt8"
}
},{
"name" : "SellOrder",
"base" : "Ask",
"fields" : {
"fill_or_kill" : "UInt8"
}
}
],
"actions": [{
"action": "buy",
"type": "BuyOrder"
},{
"action": "sell",
"type": "SellOrder"
},{
"action": "cancel_buy",
"type": "OrderID"
},{
"action": "cancel_sell",
"type": "OrderID"
}
],
"tables": [
{"table":"bids","type":"Bid"},
{"table":"asks","type":"Ask"},
{"table":"account","type":"Account"}
]
}
49 changes: 48 additions & 1 deletion contracts/exchange/exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void apply_exchange_buy( BuyOrder order ) {
print( "\n No asks found, saving buyer account and storing bid\n" );
assert( !order.fill_or_kill, "order not completely filled" );
Bids::store( bid );
buyer_account.open_orders++;
save( buyer_account );
return;
}
Expand All @@ -162,6 +163,7 @@ void apply_exchange_buy( BuyOrder order ) {
match( bid, buyer_account, lowest_ask, seller_account );

if( lowest_ask.quantity == CurrencyTokens(0) ) {
seller_account.open_orders--;
save( seller_account );
save( buyer_account );
Asks::remove( lowest_ask );
Expand All @@ -175,6 +177,7 @@ void apply_exchange_buy( BuyOrder order ) {
}
print( "lowest_ask >= bid.price or buyer's bid has been filled\n" );

if( bid.quantity && !order.fill_or_kill ) buyer_account.open_orders++;
save( buyer_account );
print( "saving buyer's account\n" );
if( bid.quantity ) {
Expand Down Expand Up @@ -209,6 +212,7 @@ void apply_exchange_sell( SellOrder order ) {
assert( !order.fill_or_kill, "order not completely filled" );
print( "\n No bids found, saving seller account and storing ask\n" );
Asks::store( ask );
seller_account.open_orders++;
save( seller_account );
return;
}
Expand All @@ -220,6 +224,7 @@ void apply_exchange_sell( SellOrder order ) {
match( highest_bid, buyer_account, ask, seller_account );

if( highest_bid.quantity == EosTokens(0) ) {
buyer_account.open_orders--;
save( seller_account );
save( buyer_account );
Bids::remove( highest_bid );
Expand All @@ -231,13 +236,49 @@ void apply_exchange_sell( SellOrder order ) {
break; // buyer's bid should be filled
}
}


if( ask.quantity && !order.fill_or_kill ) seller_account.open_orders++;
save( seller_account );
if( ask.quantity ) {
assert( !order.fill_or_kill, "order not completely filled" );
print( "saving ask\n" );
Asks::store( ask );
return;
}

print( "ask filled\n" );
}

void apply_exchange_cancel_buy( OrderID order ) {
requireAuth( order.name );

Bid bid_to_cancel;
assert( BidsById::get( order, bid_to_cancel ), "bid with this id does not exists" );

auto buyer_account = getAccount( order.name );
buyer_account.eos_balance += bid_to_cancel.quantity;
buyer_account.open_orders--;

Bids::remove( bid_to_cancel );
save( buyer_account );

print( "bid removed\n" );
}

void apply_exchange_cancel_sell( OrderID order ) {
requireAuth( order.name );

Ask ask_to_cancel;
assert( AsksById::get( order, ask_to_cancel ), "ask with this id does not exists" );

auto seller_account = getAccount( order.name );
seller_account.currency_balance += ask_to_cancel.quantity;
seller_account.open_orders--;

Asks::remove( ask_to_cancel );
save( seller_account );

print( "ask removed\n" );
}

} // namespace exchange
Expand Down Expand Up @@ -266,6 +307,12 @@ extern "C" {
case N(sell):
apply_exchange_sell( currentMessage<exchange::SellOrder>() );
break;
case N(cancel_buy):
apply_exchange_cancel_buy( currentMessage<exchange::OrderID>() );
break;
case N(cancel_sell):
apply_exchange_cancel_sell( currentMessage<exchange::OrderID>() );
break;
default:
assert( false, "unknown action" );
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/exchange/exchange.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace exchange {
using Accounts = Table<N(exchange),N(exchange),N(account),Account,uint64_t>;

TABLE2(Bids,exchange,exchange,bids,Bid,BidsById,OrderID,BidsByPrice,Price);
TABLE2(Asks,exchange,exchange,bids,Ask,AsksById,OrderID,AsksByPrice,Price);
TABLE2(Asks,exchange,exchange,asks,Ask,AsksById,OrderID,AsksByPrice,Price);


struct BuyOrder : public Bid { uint8_t fill_or_kill = false; };
Expand Down
63 changes: 42 additions & 21 deletions genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,87 +14,108 @@
"initial_accounts": [{
"name": "inita",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initb",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initc",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initd",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "inite",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initf",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initg",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "inith",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initi",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initj",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initk",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initl",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initm",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initn",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "inito",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initp",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initq",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initr",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "inits",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initt",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
},{
"name": "initu",
"owner_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV"
"active_key": "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV",
"liquid_balance": "1000000.00000000 EOS"
}],
"initial_producers": [{
"owner_name": "inita",
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/wasm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ DEFINE_INTRINSIC_FUNCTION1(env,free,free,none,i32,ptr) {
FC_ASSERT( getFunctionType(call)->parameters.size() == 2 );

// idump((current_validate_context->msg.code)(current_validate_context->msg.type)(current_validate_context->code));
std::vector<Value> args = { Value(uint64_t(current_validate_context->code)),
std::vector<Value> args = { Value(uint64_t(current_validate_context->msg.code)),
Value(uint64_t(current_validate_context->msg.type)) };

auto& state = *current_state;
Expand Down
Loading

0 comments on commit 79b73d9

Please sign in to comment.