Skip to content

Commit 9c96c26

Browse files
author
Stan Drozd
committed
pyth.cpp: add upd_price_val for explicit price value updates
1 parent b958865 commit 9c96c26

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

pcapps/pyth.cpp

+92
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ int usage()
4949
std::cerr << " upd_product <product.json> [options]" << std::endl;
5050
std::cerr << " upd_price <price_key> [options]"
5151
<< std::endl;
52+
std::cerr << " upd_price_val <price_key> <value> <confidence> <status> [options]"
53+
<< std::endl;
5254
std::cerr << " upd_test <test_key> <test.json> [options]"
5355
<< std::endl;
5456
std::cerr << " get_balance [<pub_key>] [options]" << std::endl;
@@ -804,9 +806,12 @@ int on_upd_price( int argc, char **argv )
804806
if ( argc < 2 ) {
805807
return usage();
806808
}
809+
810+
// Price Key
807811
std::string pkey( argv[1] );
808812
pub_key pub;
809813
pub.init_from_text( pkey );
814+
810815
argc -= 1;
811816
argv += 1;
812817

@@ -866,6 +871,91 @@ int on_upd_price( int argc, char **argv )
866871
return 0;
867872
}
868873

874+
int on_upd_price_val( int argc, char **argv )
875+
{
876+
if ( argc < 5 ) {
877+
return usage();
878+
}
879+
880+
// Price Key
881+
std::string pkey( argv[1] );
882+
pub_key pub;
883+
pub.init_from_text( pkey );
884+
885+
// Price Value
886+
uint64_t price_value = atoll( argv[2] );
887+
888+
// Confidence
889+
uint64_t confidence = atoll( argv[3] );
890+
891+
// Status
892+
symbol_status price_status = str_to_symbol_status( argv[4] );
893+
894+
// Make sure an unknown status was requested explicitly
895+
if ( price_status == symbol_status::e_unknown && argv[4] != std::string("unknown") ) {
896+
std::cerr << "pyth: unrecognized symbol status " << '"' << argv[4] << '"' << std::endl;
897+
return 1;
898+
}
899+
900+
argc -= 4;
901+
argv += 4;
902+
903+
int opt = 0;
904+
commitment cmt = commitment::e_confirmed;
905+
std::string rpc_host = get_rpc_host();
906+
std::string tx_host = get_rpc_host();
907+
std::string key_dir = get_key_store();
908+
while( (opt = ::getopt(argc,argv, "r:t:k:c:dh" )) != -1 ) {
909+
switch(opt) {
910+
case 'r': rpc_host = optarg; break;
911+
case 't': tx_host = optarg; break;
912+
case 'k': key_dir = optarg; break;
913+
case 'd': log::set_level( PC_LOG_DBG_LVL ); break;
914+
case 'c': cmt = str_to_commitment(optarg); break;
915+
default: return usage();
916+
}
917+
}
918+
if ( cmt == commitment::e_unknown ) {
919+
std::cerr << "pyth: unknown commitment level" << std::endl;
920+
return usage();
921+
}
922+
923+
// initialize connection to block-chain
924+
manager mgr;
925+
mgr.set_rpc_host( rpc_host );
926+
mgr.set_tx_host( tx_host );
927+
mgr.set_dir( key_dir );
928+
mgr.set_do_tx( true );
929+
mgr.set_commitment( cmt );
930+
if ( !mgr.init() || !mgr.bootstrap() ) {
931+
std::cerr << "pyth: " << mgr.get_err_msg() << std::endl;
932+
return 1;
933+
}
934+
// get price and crank
935+
price *ptr = mgr.get_price( pub );
936+
if ( !ptr ) {
937+
std::cerr << "pyth: failed to find price key=" << pkey << std::endl;
938+
return 1;
939+
}
940+
if ( !ptr->has_publisher() ) {
941+
std::cerr << "pyth: missing publisher permission" << std::endl;
942+
return 1;
943+
}
944+
ptr->update(price_value, confidence, price_status);
945+
while( mgr.get_is_tx_send() && !mgr.get_is_err() ) {
946+
mgr.poll();
947+
}
948+
if ( ptr->get_is_err() ) {
949+
std::cerr << "pyth: " << ptr->get_err_msg() << std::endl;
950+
return 1;
951+
}
952+
if ( mgr.get_is_err() ) {
953+
std::cerr << "pyth: " << mgr.get_err_msg() << std::endl;
954+
return 1;
955+
}
956+
return 0;
957+
}
958+
869959
int on_upd_publisher( int argc, char **argv, bool is_add )
870960
{
871961
// get input parameters
@@ -1502,6 +1592,8 @@ int main(int argc, char **argv)
15021592
rc = on_upd_product( argc, argv );
15031593
} else if ( cmd == "upd_price" ) {
15041594
rc = on_upd_price( argc, argv );
1595+
} else if ( cmd == "upd_price_val" ) {
1596+
rc = on_upd_price_val( argc, argv );
15051597
} else if ( cmd == "upd_test" ) {
15061598
rc = on_upd_test( argc, argv );
15071599
} else if ( cmd == "get_pub_key" ) {

0 commit comments

Comments
 (0)