The eosfinex Exchange Contract

Facilitating secure & instantaneous on-chain exchange

Kasper Rasmussen
Mar 1 · 7 min read

cleos -u http://[public_eosfinex_node] get abi [exchange_account]
“actions”: [{
“name”: “place”,
“type”: “place”,
“ricardian_contract”: “”
},{
“name”: “cancel”,
“type”: “cancel”,
“ricardian_contract”: “”
},{
“name”: “withdraw”,
“type”: “withdraw”,
“ricardian_contract”: “”
}, …]
“tables”: [{
“name”: “orders”,
“index_type”: “i64”,
“key_names”: [
“id”,
“price”
],
“key_types”: [
“uint64”,
“uint64”
],
“type”: “order”
},{
“name”: “accounts”,
“index_type”: “i64”,
“key_names”: [
“currency”
],
“key_types”: [
“uint64”
],
“type”: “account”
}
]

Accounts Table

The accounts table stores a user’s available balance for a particular currency on the exchange. Before a user can trade, the user must first transfer funds onto the exchange.

cleos transfer [user_account] [exchange_account] “20.00000000 EOS” “Transfer some EOS onto the exchange”cleos transfer [user_account] [exchange_account] “5.00000000 EOX” “Transfer some EOX onto the exchange”
cleos get currency balance [exchange_account] [user_account]
20.00000000 EOS
5.00000000 EOX

Place Action

Orders are sent to the exchange using the contract’s place action. Below are the details of the place action as defined in the ABI.

{
“name”: “place”,
“base”: “”,
“fields”: [
{“name”:”account”, “type”:”name”},
{“name”:”clId”, “type”:”uint64"},
{“name”:”scope”, “type”:”name”},
{“name”:”price”, “type”:”uint64"},
{“name”:”qty”, “type”:”uint64"},
{“name”:”flags”, “type”:”uint8"}
]
}
00000001 Post Only: The order will receive a price reflecting the maker rebate.00000010 Immediate or Cancel: Any size that does not fill immediately will be canceled.00000100 Market: Ignore limit price and trade at the best available price.00001000 Self Trade Prevention: Do not trade with an order having the same account.01000000 Release on Trade: Transfer new funds out of the exchange after a trade.10000000 Sweep Collateral: Transfer collateral balance out of exchange when canceled.
cleos get currency balance [exchange_account] [user_account]
20.00000000 EOS
5.00000000 EOX
cleos push action exchange_account place ‘[user_account 1 eox.eos.b 15000 30000 0]’
cleos get currency balance [exchange_account] [user_account]
15.50000000 EOS
5.00000000 EOX

Orders Table

This table stores the limit order book for each trading pair. Any portion of a limit order that does not fill will be placed in the limit order book. The limit order book can be queried by using the following command.

cleos get table [exchange_account] [order_scope] orders
cleos push action exchange_account place ‘[user_account 1 eox.eos.b 15000 30000 0]’
cleos get table [exchange_account] eox.eos.b orders{
“rows”: [{
“id”: “1234567”,
“account”: “user_account”,
“clId”: 1,
“price”: 15000,
“qty”: 30000,
“reserved”: 0,
“flags”: 0
}],
“more”: false
}

Cancel Action

The cancel action is used to cancel an order in the order book. The cancel action has the following ABI definition.

{
“name”: “cancel”,
“base”: “”,
“fields”: [
{“name”:”account”, “type”:”name”},
{“name”:”id”, “type”:”uint64"},
{“name”:”clId”, “type”:”uint64"},
{“name”:”scope”, “type”:”name”}
]
}
cleos push action [exchange_account] cancel ‘[user_account 1234567 1 eox.eos.b]’
cleos get table [exchange_account] eox.eos.b orders
{
“rows”: [],
“more”: false
}

Withdraw Action

“name”: “withdraw”,
“base”: “”,
“fields”: [
{“name”:”to”, “type”:”name”},
{“name”:”amount”, “type”:”asset”}
]
}
cleos get currency balance [exchange_account] [user_account]
20.00000000 EOS
5.00000000 EOX
cleos push action exchange_account withdraw ‘[user_account “10.00000000 EOS”]’
cleos get currency balance [exchange_account] [user_account]
10.00000000 EOS
5.00000000 EOX

Summary

In the second installment of our weekly eosfinex blog series we looked at the eosfinex exchange contract, as well as how one can interact with the contract directly via the ABI.

Stay up to date with developments below as we approach our March 2019 launch.

eosfinex Project

eosfinex: A high-performance decentralised exchange built on EOS.

Kasper Rasmussen

Written by

Marketing @bitfinex

eosfinex Project

eosfinex: A high-performance decentralised exchange built on EOS.