Polkadex-Substrate / polkadexTEE-worker

Polkadex Off-chain Orderbook
Apache License 2.0
10 stars 1 forks source link

implement OpenFinex API client #5

Closed brenzi closed 3 years ago

brenzi commented 3 years ago

A minimal functionality includes:

Remarks:

Finex engine management (S2S) websocket protocol

Authentication

The management websocket authenticate use standard JWT - RS256

JWT=$(barong-jwt --uid IDBD12DEB15B)
wscat --connect 'ws://localhost:8080/api/v2/finex/ws' -H "Authorization: Bearer ${JWT}"

Example of JWT:

eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicGVhdGlvIiwiYmFyb25nIl0sImVtYWlsIjoiYWRtaW5AYmFyb25nLmlvIiwiZXhwIjoxNjE3OTU2MjU3LCJpYXQiOjE2MTc5NTI2NTcsImlzcyI6ImJhcm9uZyIsImp0aSI6IjE2MTc5NTI2NTciLCJsZXZlbCI6MywicmVmZXJyYWxfaWQiOm51bGwsInJvbGUiOiJhZG1pbiIsInN0YXRlIjoiYWN0aXZlIiwic3ViIjoic2Vzc2lvbiIsInVpZCI6IlU0ODcyMDU4NjMifQ.ROjIs7OhcsX86ZxxVy5vnBCNheEtOENpI9g7eN4BgbGlz5U3XLg7D9cILw8FEsmfzTf5pSzMRrx59_rD9sI4abim7Nn7eJ8gay7E-vQdFfeu6720ZQkbM3w5hIpq2x6pubzUu88ZCVZA2mLEw2XuQFVtDBCEBT5yWCPIOFSsuVN6wSuANvR2ZpJytJHvzSpeGv_9BxZC0ZtAR4PaP2f_iyX1Zw-71QxB4-BxdZeCUp17ic82DQ7TQU_GgYoQNTQFVBHc_oouj_3XB_B1PIYvbSAoPkS1JalqrwJRIw0nFQfTe09OazACyjGyQpsQqsVBN6WlEjYViV30cGquDRATYg

Resources:

Create orders

Method: mng_create_order

Arguments:

Name Data type Description
1 uid or nickname string User UID or nickname to identify the user
2 market_id string Market identifier for order ex: "btcusd"
3 market_type string Market type ex: "trusted"
3 order_type string - market: "m"
- limit: "l"
- Post only (Must not fill at all or is canceled): "p"
- Fill or kill (Must fully match at a given price or is canceled): "f"
4 side string Used to specify order side, "buy" or "sell"
5 quantity string An amount that placed within the order
6 price string Main (limit) price of the order (optional)

Example:

[1,42,"mng_create_order",["14dQ6XGcrk4njhYB7ihcjHyyKbFKUVCXt5vffTV9yAWcgrbu", "btcusd", "trusted", "l", "buy", "0.250000", "9120.00"]]

Response:

[2,42,"mng_create_order",["1245-2345-6798-123123"]]

Error example:

[2,42,"error",["Message describing the error"]]

Cancel orders

Method: mng_cancel_order_bulk

Arguments:

Name Data type Description
1 uid or nickname string User UID or nickname to identify the user
2 market_id string Market identifier for order ex: "btcusd"
3 orders IDs or UUIDs string List of order IDs or UUIDs to cancel

Example:

[1,42,"mng_cancel_order",["btcusd", ["1245-2345-6798-123123", "1245-2345-6798-123124"]]]

Events

Subscription to events

TODO

Order update events

Argument Description
Market ID Market unique identifier
ID Unique order ID
UUID Unique order uuid
Side "buy" or "sell"
Kind "bid" or "ask"
State Current state of the order
Type Order type
Price Order price
Average Price Average execution price
Volume Current order volume
Origin Volume Origin volume
Executed Volume Executed volume
Trades Count Order trades count
Timestamp Order creation timestamp
[4,"order",["btcusd", 2, "7acbbc84-939d-11ea-a827-1831bf9834b0", "buy", "bid", "done", "limit", "1", "1", "0", "1", "1", 1, 1589211516]]

Trades events

Argument Description
Market ID Market unique identifier
ID Unique trade ID
Price Trade execution price
Amount Trade execution amount
Funds Trade funds, amount * price
Maker OrderID Maker's trade order ID
Maker OrderUUID Maker's trade order UUID
Taker OrderID Taker's trade order ID
Taker OrderUUID Taker's trade order UUID
Maker Side Maker order side
Timestamp Trade timestamp
[4,"mng_trade",["btcusd", 1, "1", "1", "1", 2, "55d78eee-939e-11ea-945f-1831bf9834b0", 3, "55d78eee-939e-11ea-945f-1831bf9834as", "buy", 1589211884]]

Debug helpers

The following functions are not strictly needed but it may help debugging if we let OpenFinex know about deposit and withdrawal events:

Deposit funds

Method: mng_deposit

Arguments:

Name Data type Description
1 uid or nickname string User UID or nickname to identify the user
2 currency_id string Currency identifier of the deposit
3 amount number Amount to deposit
3 tx_id string Transaction ID (optional)

Example:

[1,42,"mng_deposit",["14dQ6XGcrk4njhYB7ihcjHyyKbFKUVCXt5vffTV9yAWcgrbu", "usd", "1500.000000000000", "0x1234657890123456789"]

Response:

[2,42,"mng_deposit",[124]]

Error example:

[2,42,"error",["Message describing the error"]]

Withdraw funds

Method: mng_withdraw

Arguments:

Name Data type Description
1 uid or nickname string User UID or nickname to identify the user
2 currency_id string Currency identifier of the deposit
3 amount number Amount to withdraw
3 tx_id string Transaction ID (optional)

Example:

[1,43,"mng_withdraw",["14dQ6XGcrk4njhYB7ihcjHyyKbFKUVCXt5vffTV9yAWcgrbu", "usd", "1500", "0x1234657890123456789"]

Response:

[2,43,"mng_withdraw",[124]]

Error example:

[2,43,"error",["Message describing the error"]]
haerdib commented 3 years ago

Finally tested it, and a trade event successfully modified the free balances of accounts, hence closing this issue.