Skip to content

Commit b958865

Browse files
massjhunsaker
authored andcommitted
pyth: Add get_all_products command.
1 parent a758b68 commit b958865

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

pcapps/pyth.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ int usage()
5555
std::cerr << " get_block <slot_number> [options]" << std::endl;
5656
std::cerr << " get_product <prod_key> [options]" << std::endl;
5757
std::cerr << " get_product_list [options]" << std::endl;
58+
std::cerr << " get_all_products [options]" << std::endl;
5859
std::cerr << " get_pub_key <key_pair_file>" << std::endl;
5960
std::cerr << " version" << std::endl;
6061
std::cerr << std::endl;
@@ -1230,6 +1231,68 @@ int on_get_product( int argc, char **argv )
12301231
return 0;
12311232
}
12321233

1234+
int on_get_all_products( int argc, char **argv )
1235+
{
1236+
int opt = 0;
1237+
bool do_json = false;
1238+
commitment cmt = commitment::e_confirmed;
1239+
std::string rpc_host = get_rpc_host();
1240+
std::string key_dir = get_key_store();
1241+
while( (opt = ::getopt(argc,argv, "r:k:c:djh" )) != -1 ) {
1242+
switch(opt) {
1243+
case 'r': rpc_host = optarg; break;
1244+
case 'k': key_dir = optarg; break;
1245+
case 'd': log::set_level( PC_LOG_DBG_LVL ); break;
1246+
case 'j': do_json = true; break;
1247+
case 'c': cmt = str_to_commitment(optarg); break;
1248+
default: return usage();
1249+
}
1250+
}
1251+
if ( cmt == commitment::e_unknown ) {
1252+
std::cerr << "pyth: unknown commitment level" << std::endl;
1253+
return usage();
1254+
}
1255+
1256+
// initialize connection to block-chain
1257+
manager mgr;
1258+
mgr.set_rpc_host( rpc_host );
1259+
mgr.set_dir( key_dir );
1260+
mgr.set_do_tx( false );
1261+
mgr.set_commitment( cmt );
1262+
if ( !mgr.init() || !mgr.bootstrap() ) {
1263+
std::cerr << "pyth: " << mgr.get_err_msg() << std::endl;
1264+
return 1;
1265+
}
1266+
if ( !mgr.has_status( PC_PYTH_HAS_MAPPING ) ) {
1267+
std::cerr << "pyth: mapping not ready, check mapping key ["
1268+
<< mgr.get_mapping_pub_key_file() << "]" << std::endl;
1269+
return 1;
1270+
}
1271+
1272+
// get all products and serialize to stdout
1273+
if ( !do_json ) {
1274+
for (unsigned i=0; i != mgr.get_num_product(); ++i ) {
1275+
product *prod = mgr.get_product(i);
1276+
print_product( prod );
1277+
std::cout << std::endl;
1278+
}
1279+
} else {
1280+
std::cout << "[";
1281+
bool first = true;
1282+
for (unsigned i=0; i != mgr.get_num_product(); ++i ) {
1283+
product *prod = mgr.get_product(i);
1284+
if ( !first ) {
1285+
std::cout << ",";
1286+
}
1287+
print_product_json( prod );
1288+
first = false;
1289+
}
1290+
std::cout << "]";
1291+
}
1292+
1293+
return 0;
1294+
}
1295+
12331296
class get_block_print : public get_block
12341297
{
12351298
public:
@@ -1447,6 +1510,8 @@ int main(int argc, char **argv)
14471510
rc = on_get_product( argc, argv );
14481511
} else if ( cmd == "get_product_list" ) {
14491512
rc = on_get_product_list( argc, argv );
1513+
} else if ( cmd == "get_all_products" ) {
1514+
rc = on_get_all_products( argc, argv );
14501515
} else if ( cmd == "get_block" ) {
14511516
rc = on_get_block( argc, argv );
14521517
} else if ( cmd == "version" ) {

0 commit comments

Comments
 (0)