@@ -184,7 +184,7 @@ void rpc_client::send( rpc_request *rptr )
184184 }
185185}
186186
187- void rpc_client::send ( rpc::upd_price *upds[], const unsigned n )
187+ void rpc_client::send ( rpc::upd_price *upds[], const unsigned n, unsigned cu_units, unsigned cu_price )
188188{
189189 if ( ! n ) {
190190 return ;
@@ -212,7 +212,7 @@ void rpc_client::send( rpc::upd_price *upds[], const unsigned n )
212212 jw.add_val ( json_wtr::e_obj );
213213 jw.add_key ( " jsonrpc" , " 2.0" );
214214 jw.add_key ( " id" , id );
215- rpc::upd_price::request ( jw, upds, n );
215+ rpc::upd_price::request ( jw, upds, n, cu_units, cu_price );
216216 jw.pop ();
217217// jw.print();
218218 if ( upds[ 0 ]->get_is_http () ) {
@@ -889,7 +889,7 @@ class tx_wtr : public net_wtr
889889};
890890
891891bool rpc::upd_price::build_tx (
892- bincode& tx, upd_price* upds[], const unsigned n
892+ bincode& tx, upd_price* upds[], const unsigned n, unsigned cu_units, unsigned cu_price
893893)
894894{
895895 if ( ! n ) {
@@ -923,28 +923,29 @@ bool rpc::upd_price::build_tx(
923923 tx.add ( *first.bhash_ ); // recent block hash
924924
925925 // instructions section
926- tx.add_len ( n + 1 + 1 ); // 1 compute limit instruction, 1 compute unit price instruction, n upd_price instruction(s)
926+ unsigned instruction_count = n + 1 ; // 1 compute limit instruction, n upd_price instruction(s)
927+ if ( cu_price > 0 ) {
928+ instruction_count += 1 ; // Extra instruction for specifying compute unit price
929+ }
930+ tx.add_len ( instruction_count ); // 1 compute limit instruction, 1 compute unit price instruction, n upd_price instruction(s)
927931
928932 // Set compute limit
929933 tx.add ( (uint8_t )( n + 3 ) ); // compute budget program id index in accounts list
930934 tx.add_len <0 >(); // no accounts
931935 // compute limit instruction parameters
932936 tx.add_len <sizeof (uint8_t ) + sizeof (uint32_t )>(); // uint8_t enum variant + uint32_t requested compute units
933937 tx.add ( (uint8_t ) 2 ); // SetComputeLimit enum variant
934- tx.add ( (uint32_t ) CU_BUDGET_PER_IX * n ); // the budget (scaled for number of instructions)
935-
936- // Requested price per compute unit, in micro lamports
937- // We want to pay 5000 lamports in total, so ((5000/20000) / 8) * (10^6)
938- // assuming upper bound of 20000 CUs and 8 instructions.
939- const uint64_t cu_price_micro_lamports = 31250 ;
938+ tx.add ( (uint32_t ) ( cu_units * n ) ); // the budget (scaled for number of instructions)
940939
941940 // Set compute unit price
942- tx.add ( (uint8_t )( n + 3 ) );
943- tx.add_len <0 >(); // no accounts
944- // compute unit price instruction parameters
945- tx.add_len <sizeof (uint8_t ) + sizeof (uint64_t )>(); // uint8_t enum variant + uint62_t compute price
946- tx.add ( (uint8_t ) 3 ); // SetComputePrice enum variant
947- tx.add ( (uint64_t ) cu_price_micro_lamports ); // price we are willing to pay per compute unit in Micro Lamports
941+ if ( cu_price > 0 ) {
942+ tx.add ( (uint8_t )( n + 3 ) );
943+ tx.add_len <0 >(); // no accounts
944+ // compute unit price instruction parameters
945+ tx.add_len <sizeof (uint8_t ) + sizeof (uint64_t )>(); // uint8_t enum variant + uint62_t compute price
946+ tx.add ( (uint8_t ) 3 ); // SetComputePrice enum variant
947+ tx.add ( (uint64_t ) cu_price ); // price we are willing to pay per compute unit in Micro Lamports
948+ }
948949
949950 for ( unsigned i = 0 ; i < n; ++i ) {
950951 tx.add ( (uint8_t )( n + 2 ) ); // program_id index
@@ -973,40 +974,36 @@ bool rpc::upd_price::build_tx(
973974 return true ;
974975}
975976
976- void rpc::upd_price::build ( net_wtr& wtr )
977+ void rpc::upd_price::build ( net_wtr& )
977978{
978- upd_price* upds[] = { this };
979- build ( wtr, upds, 1 );
980979}
981980
982981
983- void rpc::upd_price::request ( json_wtr& msg )
982+ void rpc::upd_price::request ( json_wtr& )
984983{
985- upd_price* upds[] = { this };
986- request ( msg, upds, 1 );
987984}
988985
989986bool rpc::upd_price::build (
990- net_wtr& wtr, upd_price* upds[], const unsigned n
987+ net_wtr& wtr, upd_price* upds[], const unsigned n, unsigned cu_units, unsigned cu_price
991988)
992989{
993990 bincode tx;
994991 static_cast < tx_wtr& >( wtr ).init ( tx );
995- if ( ! build_tx ( tx, upds, n ) ) {
992+ if ( ! build_tx ( tx, upds, n, cu_units, cu_price ) ) {
996993 return false ;
997994 }
998995 static_cast < tx_wtr& >( wtr ).commit ( tx );
999996 return true ;
1000997}
1001998
1002999bool rpc::upd_price::request (
1003- json_wtr& msg, upd_price* upds[], const unsigned n
1000+ json_wtr& msg, upd_price* upds[], const unsigned n, unsigned cu_units, unsigned cu_price
10041001)
10051002{
10061003 // construct binary transaction
10071004 net_buf *bptr = net_buf::alloc ();
10081005 bincode tx ( bptr->buf_ );
1009- if ( ! build_tx ( tx, upds, n ) ) {
1006+ if ( ! build_tx ( tx, upds, n, cu_units, cu_price ) ) {
10101007 return false ;
10111008 }
10121009
0 commit comments