Skip to content

Commit c9b8ae5

Browse files
massjhunsaker
authored andcommitted
pythd: Add get_product and get_all_products WS methods.
Add dump_json() methods to pc::price and pc::product so we can have standardized json output across all tools.
1 parent 06ba536 commit c9b8ae5

File tree

8 files changed

+303
-62
lines changed

8 files changed

+303
-62
lines changed

doc/websocket_api.md

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# pyth-client websocket API
2-
The pythd daemon supports a websocket interface based on the jsoni-rpc 2.0 standard. Methods include:
2+
The pythd daemon supports a websocket interface based on the json-rpc 2.0 standard. Methods include:
33

44
- [get_product_list](#get_product_list)
55
- [update_price](#update_price)
66
- [subscribe_price](#subscribe_price)
77
- [subscribe_price_sched](#subscribe_price_sched)
8+
- [get_product](#get_product)
9+
- [get_all_products](#get_all_products)
810

911
Batch requests are processed in the order the requests appear within the batch.
1012

@@ -196,3 +198,178 @@ Where the result is an integer corresponding to a subscription identifier. All s
196198
}
197199
}
198200
```
201+
202+
## get_product
203+
204+
Get full set of data for the given product.
205+
206+
Request looks like:
207+
```
208+
{
209+
"jsonrpc": "2.0",
210+
"method": "get_product",
211+
"params": {
212+
"account": "4aDoSXJ5o3AuvL7QFeR6h44jALQfTmUUCTVGDD6aoJTM"
213+
},
214+
"id" : 1
215+
}
216+
```
217+
218+
A successful response looks something like:
219+
```
220+
{
221+
"jsonrpc": "2.0",
222+
"result": {
223+
"account": "4aDoSXJ5o3AuvL7QFeR6h44jALQfTmUUCTVGDD6aoJTM",
224+
"attr_dict": {
225+
"asset_type": "Crypto",
226+
"symbol": "BTC/USD",
227+
"country": "US",
228+
"quote_currency": "USD",
229+
"description": "BTC/USD",
230+
"tenor": "Spot",
231+
"generic_symbol": "BTCUSD"
232+
},
233+
"price_accounts": [
234+
{
235+
"account": "GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU",
236+
"price_type": "price",
237+
"price_exponent": -8,
238+
"status": "trading",
239+
"price": 4426101900000,
240+
"conf": 4271150000,
241+
"twap": 4433467600000,
242+
"twac": 1304202670,
243+
"valid_slot": 91402257,
244+
"pub_slot": 91402259,
245+
"prev_slot": 91402256,
246+
"prev_price": 4425895500000,
247+
"prev_conf": 3315350000,
248+
"publisher_accounts": [
249+
{
250+
"account": "HekM1hBawXQu6wK6Ah1yw1YXXeMUDD2bfCHEzo25vnEB",
251+
"status": "trading",
252+
"price": 4426958500000,
253+
"conf": 1492500000,
254+
"slot": 91402255
255+
},
256+
{
257+
"account": "GKNcUmNacSJo4S2Kq3DuYRYRGw3sNUfJ4tyqd198t6vQ",
258+
"status": "trading",
259+
"price": 4424690000000,
260+
"conf": 3690000000,
261+
"slot": 91402256
262+
}
263+
]
264+
}
265+
]
266+
},
267+
"id": 1
268+
}
269+
```
270+
271+
## get_all_products
272+
273+
Get full set of data for all products.
274+
275+
Request looks like:
276+
```
277+
{
278+
"jsonrpc": "2.0",
279+
"method": "get_all_products",
280+
"id" : 1
281+
}
282+
```
283+
284+
A successful response looks something like:
285+
```
286+
{
287+
"jsonrpc": "2.0",
288+
"result": [
289+
{
290+
"account": "5uKdRzB3FzdmwyCHrqSGq4u2URja617jqtKkM71BVrkw",
291+
"attr_dict": {
292+
"asset_type": "Crypto",
293+
"symbol": "BCH/USD",
294+
"country": "US",
295+
"quote_currency": "USD",
296+
"description": "BCH/USD",
297+
"tenor": "Spot",
298+
"generic_symbol": "BCHUSD"
299+
},
300+
"price_accounts": [
301+
{
302+
"account": "5ALDzwcRJfSyGdGyhP3kP628aqBNHZzLuVww7o9kdspe",
303+
"price_type": "price",
304+
"price_exponent": -8,
305+
"status": "trading",
306+
"price": 60282000000,
307+
"conf": 26000000,
308+
"twap": 60321475000,
309+
"twac": 22504746,
310+
"valid_slot": 91402601,
311+
"pub_slot": 91402604,
312+
"prev_slot": 91402600,
313+
"prev_price": 60282000000,
314+
"prev_conf": 26000000,
315+
"publisher_accounts": [
316+
{
317+
"account": "HekM1hBawXQu6wK6Ah1yw1YXXeMUDD2bfCHEzo25vnEB",
318+
"status": "trading",
319+
"price": 60282000000,
320+
"conf": 26000000,
321+
"slot": 91402599
322+
},
323+
{
324+
"account": "2V7t5NaKY7aGkwytCWQgvUYZfEr9XMwNChhJEakTExk6",
325+
"status": "unknown",
326+
"price": 0,
327+
"conf": 0,
328+
"slot": 0
329+
}
330+
]
331+
}
332+
]
333+
},
334+
{
335+
"account": "3nuELNFBkbXqsXtnCzphRPCX6toKKYxVDnkyr9pTwB1K",
336+
"attr_dict": {
337+
"asset_type": "Crypto",
338+
"symbol": "SABER/USD",
339+
"country": "US",
340+
"quote_currency": "USD",
341+
"description": "SABER/USD",
342+
"tenor": "Spot",
343+
"generic_symbol": "SABERUSD"
344+
},
345+
"price_accounts": [
346+
{
347+
"account": "8Td9VML1nHxQK6M8VVyzsHo32D7VBk72jSpa9U861z2A",
348+
"price_type": "price",
349+
"price_exponent": -8,
350+
"status": "trading",
351+
"price": 5785000,
352+
"conf": 5000,
353+
"twap": 5856365,
354+
"twac": 10241,
355+
"valid_slot": 91402601,
356+
"pub_slot": 91402604,
357+
"prev_slot": 91402600,
358+
"prev_price": 5785000,
359+
"prev_conf": 5000,
360+
"publisher_accounts": [
361+
{
362+
"account": "GKNcUmNacSJo4S2Kq3DuYRYRGw3sNUfJ4tyqd198t6vQ",
363+
"status": "trading",
364+
"price": 5785000,
365+
"conf": 5000,
366+
"slot": 91402601
367+
}
368+
]
369+
}
370+
]
371+
}
372+
],
373+
"id": 1
374+
}
375+
```

pc/attr_id.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void attr_dict::write_account( net_wtr& wtr )
107107
}
108108
}
109109

110-
void attr_dict::write_json( json_wtr& wtr )
110+
void attr_dict::write_json( json_wtr& wtr ) const
111111
{
112112
str vstr, kstr;
113113
for( unsigned id=1; id < avec_.size(); ++id ) {

pc/attr_id.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace pc
5959
void write_account( net_wtr& );
6060

6161
// serialize to json
62-
void write_json( json_wtr& );
62+
void write_json( json_wtr& ) const;
6363

6464
private:
6565

pc/request.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,11 @@ pub_key *product::get_account()
16741674
return &acc_;
16751675
}
16761676

1677+
const pub_key *product::get_account() const
1678+
{
1679+
return &acc_;
1680+
}
1681+
16771682
str product::get_symbol()
16781683
{
16791684
str sym;
@@ -1797,6 +1802,23 @@ price *product::get_price( price_type pt ) const
17971802
return nullptr;
17981803
}
17991804

1805+
void product::dump_json( json_wtr& wtr ) const
1806+
{
1807+
// assumes the json_wtr has already started an object structure
1808+
wtr.add_key( "account", *get_account() );
1809+
wtr.add_key( "attr_dict", json_wtr::e_obj );
1810+
write_json( wtr );
1811+
wtr.pop();
1812+
wtr.add_key( "price_accounts", json_wtr::e_arr );
1813+
for( unsigned i=0; i != get_num_price(); ++i ) {
1814+
wtr.add_val( json_wtr::e_obj );
1815+
price *ptr = get_price( i );
1816+
ptr->dump_json( wtr );
1817+
wtr.pop();
1818+
}
1819+
wtr.pop();
1820+
}
1821+
18001822
///////////////////////////////////////////////////////////////////////////
18011823
// price
18021824

@@ -1869,6 +1891,11 @@ pub_key *price::get_account()
18691891
return &apub_;
18701892
}
18711893

1894+
const pub_key *price::get_account() const
1895+
{
1896+
return &apub_;
1897+
}
1898+
18721899
uint32_t price::get_version() const
18731900
{
18741901
return pptr_->ver_;
@@ -2190,6 +2217,36 @@ symbol_status price::get_publisher_status( unsigned i ) const
21902217
return (symbol_status)pptr_->comp_[i].agg_.status_;
21912218
}
21922219

2220+
void price::dump_json( json_wtr& wtr ) const
2221+
{
2222+
// assumes the json_wtr has already started an object structure
2223+
wtr.add_key( "account", *get_account() );
2224+
wtr.add_key( "price_type", price_type_to_str( get_price_type() ));
2225+
wtr.add_key( "price_exponent", get_price_exponent() );
2226+
wtr.add_key( "status", symbol_status_to_str( get_status() ) );
2227+
wtr.add_key( "price", get_price() );
2228+
wtr.add_key( "conf", get_conf() );
2229+
wtr.add_key( "twap", get_twap() );
2230+
wtr.add_key( "twac", get_twac() );
2231+
wtr.add_key( "valid_slot", get_valid_slot() );
2232+
wtr.add_key( "pub_slot", get_pub_slot() );
2233+
wtr.add_key( "prev_slot", get_prev_slot() );
2234+
wtr.add_key( "prev_price", get_prev_price() );
2235+
wtr.add_key( "prev_conf", get_prev_conf() );
2236+
wtr.add_key( "publisher_accounts", json_wtr::e_arr );
2237+
for( unsigned i=0; i != get_num_publisher(); ++i ) {
2238+
wtr.add_val( json_wtr::e_obj );
2239+
wtr.add_key( "account", *get_publisher( i ) );
2240+
wtr.add_key( "status", symbol_status_to_str(
2241+
get_publisher_status(i) ) );
2242+
wtr.add_key( "price", get_publisher_price(i) );
2243+
wtr.add_key( "conf", get_publisher_conf(i) );
2244+
wtr.add_key( "slot", get_publisher_slot(i) );
2245+
wtr.pop();
2246+
}
2247+
wtr.pop();
2248+
}
2249+
21932250
///////////////////////////////////////////////////////////////////////////
21942251
// price_sched
21952252

pc/request.hpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ namespace pc
570570
public:
571571
// product account number
572572
pub_key *get_account();
573+
const pub_key *get_account() const;
573574

574575
// symbol from attr_dict
575576
str get_symbol();
@@ -580,6 +581,9 @@ namespace pc
580581
price *get_price( unsigned i ) const;
581582
price *get_price( price_type ) const;
582583

584+
// output full set of data to json writer
585+
void dump_json( json_wtr& wtr ) const;
586+
583587
public:
584588

585589
product( const pub_key& );
@@ -680,20 +684,21 @@ namespace pc
680684
price_sched *get_sched();
681685

682686
// various accessors
683-
pub_key *get_account();
684-
price_type get_price_type() const;
685-
int64_t get_price_exponent() const;
686-
uint32_t get_version() const;
687-
int64_t get_price() const;
688-
uint64_t get_conf() const;
689-
symbol_status get_status() const;
690-
uint32_t get_num_qt() const;
691-
uint64_t get_lamports() const;
692-
int64_t get_twap() const;
693-
uint64_t get_twac() const;
694-
uint64_t get_prev_slot() const;
695-
int64_t get_prev_price() const;
696-
uint64_t get_prev_conf() const;
687+
pub_key *get_account();
688+
const pub_key *get_account() const;
689+
price_type get_price_type() const;
690+
int64_t get_price_exponent() const;
691+
uint32_t get_version() const;
692+
int64_t get_price() const;
693+
uint64_t get_conf() const;
694+
symbol_status get_status() const;
695+
uint32_t get_num_qt() const;
696+
uint64_t get_lamports() const;
697+
int64_t get_twap() const;
698+
uint64_t get_twac() const;
699+
uint64_t get_prev_slot() const;
700+
int64_t get_prev_price() const;
701+
uint64_t get_prev_conf() const;
697702

698703
// get publishers
699704
unsigned get_num_publisher() const;
@@ -710,6 +715,9 @@ namespace pc
710715
// slot of last aggregate price
711716
uint64_t get_pub_slot() const;
712717

718+
// output full set of data to json writer
719+
void dump_json( json_wtr& wtr ) const;
720+
713721
public:
714722

715723
void set_price_type( price_type );

0 commit comments

Comments
 (0)