@@ -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 () ) {
@@ -888,8 +888,16 @@ class tx_wtr : public net_wtr
888888 }
889889};
890890
891+ // Populates the given tx with the given upd_price requests. This function allows
892+ // specifying the number of requested cu units, and a price per cu unit, to enable
893+ // priority fees. If these parameters are emitted these are left as unspecified in
894+ // the transaction.
891895bool rpc::upd_price::build_tx (
892- bincode& tx, upd_price* upds[], const unsigned n
896+ bincode& tx,
897+ upd_price* upds[],
898+ const unsigned n,
899+ unsigned cu_units,
900+ unsigned cu_price
893901)
894902{
895903 if ( ! n ) {
@@ -923,28 +931,34 @@ bool rpc::upd_price::build_tx(
923931 tx.add ( *first.bhash_ ); // recent block hash
924932
925933 // instructions section
926- tx.add_len ( n + 1 + 1 ); // 1 compute limit instruction, 1 compute unit price instruction, n upd_price instruction(s)
934+ unsigned instruction_count = n; // n upd_price instruction(s)
935+ if ( cu_units > 0 ) {
936+ instruction_count += 1 ; // Extra instruction for specifying number of cus per instructions
937+ }
938+ if ( cu_price > 0 ) {
939+ instruction_count += 1 ; // Extra instruction for specifying compute unit price
940+ }
941+ tx.add_len ( instruction_count ); // 1 compute limit instruction, 1 compute unit price instruction, n upd_price instruction(s)
927942
928943 // Set compute limit
929- tx.add ( (uint8_t )( n + 3 ) ); // compute budget program id index in accounts list
930- tx.add_len <0 >(); // no accounts
931- // compute limit instruction parameters
932- tx.add_len <sizeof (uint8_t ) + sizeof (uint32_t )>(); // uint8_t enum variant + uint32_t requested compute units
933- 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 ;
944+ if ( cu_units > 0 ) {
945+ tx.add ( (uint8_t )( n + 3 ) ); // compute budget program id index in accounts list
946+ tx.add_len <0 >(); // no accounts
947+ // compute limit instruction parameters
948+ tx.add_len <sizeof (uint8_t ) + sizeof (uint32_t )>(); // uint8_t enum variant + uint32_t requested compute units
949+ tx.add ( (uint8_t ) 2 ); // SetComputeLimit enum variant
950+ tx.add ( (uint32_t ) ( cu_units * n ) ); // the budget (scaled for number of instructions)
951+ }
940952
941953 // 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
954+ if ( cu_price > 0 ) {
955+ tx.add ( (uint8_t )( n + 3 ) );
956+ tx.add_len <0 >(); // no accounts
957+ // compute unit price instruction parameters
958+ tx.add_len <sizeof (uint8_t ) + sizeof (uint64_t )>(); // uint8_t enum variant + uint62_t compute price
959+ tx.add ( (uint8_t ) 3 ); // SetComputePrice enum variant
960+ tx.add ( (uint64_t ) cu_price ); // price we are willing to pay per compute unit in Micro Lamports
961+ }
948962
949963 for ( unsigned i = 0 ; i < n; ++i ) {
950964 tx.add ( (uint8_t )( n + 2 ) ); // program_id index
@@ -989,10 +1003,17 @@ void rpc::upd_price::request( json_wtr& msg )
9891003bool rpc::upd_price::build (
9901004 net_wtr& wtr, upd_price* upds[], const unsigned n
9911005)
1006+ {
1007+ return build ( wtr, upds, n, 0 , 0 );
1008+ }
1009+
1010+ bool rpc::upd_price::build (
1011+ net_wtr& wtr, upd_price* upds[], const unsigned n, unsigned cu_units, unsigned cu_price
1012+ )
9921013{
9931014 bincode tx;
9941015 static_cast < tx_wtr& >( wtr ).init ( tx );
995- if ( ! build_tx ( tx, upds, n ) ) {
1016+ if ( ! build_tx ( tx, upds, n, cu_units, cu_price ) ) {
9961017 return false ;
9971018 }
9981019 static_cast < tx_wtr& >( wtr ).commit ( tx );
@@ -1001,12 +1022,18 @@ bool rpc::upd_price::build(
10011022
10021023bool rpc::upd_price::request (
10031024 json_wtr& msg, upd_price* upds[], const unsigned n
1025+ ) {
1026+ return request ( msg, upds, n, 0 , 0 );
1027+ }
1028+
1029+ bool rpc::upd_price::request (
1030+ json_wtr& msg, upd_price* upds[], const unsigned n, unsigned cu_units, unsigned cu_price
10041031)
10051032{
10061033 // construct binary transaction
10071034 net_buf *bptr = net_buf::alloc ();
10081035 bincode tx ( bptr->buf_ );
1009- if ( ! build_tx ( tx, upds, n ) ) {
1036+ if ( ! build_tx ( tx, upds, n, cu_units, cu_price ) ) {
10101037 return false ;
10111038 }
10121039
0 commit comments