@@ -184,7 +184,7 @@ void rpc_client::send( rpc_request *rptr )
184
184
}
185
185
}
186
186
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 )
188
188
{
189
189
if ( ! n ) {
190
190
return ;
@@ -212,7 +212,7 @@ void rpc_client::send( rpc::upd_price *upds[], const unsigned n )
212
212
jw.add_val ( json_wtr::e_obj );
213
213
jw.add_key ( " jsonrpc" , " 2.0" );
214
214
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 );
216
216
jw.pop ();
217
217
// jw.print();
218
218
if ( upds[ 0 ]->get_is_http () ) {
@@ -888,8 +888,16 @@ class tx_wtr : public net_wtr
888
888
}
889
889
};
890
890
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.
891
895
bool 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
893
901
)
894
902
{
895
903
if ( ! n ) {
@@ -923,28 +931,34 @@ bool rpc::upd_price::build_tx(
923
931
tx.add ( *first.bhash_ ); // recent block hash
924
932
925
933
// 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)
927
942
928
943
// 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
+ }
940
952
941
953
// 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
+ }
948
962
949
963
for ( unsigned i = 0 ; i < n; ++i ) {
950
964
tx.add ( (uint8_t )( n + 2 ) ); // program_id index
@@ -989,10 +1003,17 @@ void rpc::upd_price::request( json_wtr& msg )
989
1003
bool rpc::upd_price::build (
990
1004
net_wtr& wtr, upd_price* upds[], const unsigned n
991
1005
)
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
+ )
992
1013
{
993
1014
bincode tx;
994
1015
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 ) ) {
996
1017
return false ;
997
1018
}
998
1019
static_cast < tx_wtr& >( wtr ).commit ( tx );
@@ -1001,12 +1022,18 @@ bool rpc::upd_price::build(
1001
1022
1002
1023
bool rpc::upd_price::request (
1003
1024
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
1004
1031
)
1005
1032
{
1006
1033
// construct binary transaction
1007
1034
net_buf *bptr = net_buf::alloc ();
1008
1035
bincode tx ( bptr->buf_ );
1009
- if ( ! build_tx ( tx, upds, n ) ) {
1036
+ if ( ! build_tx ( tx, upds, n, cu_units, cu_price ) ) {
1010
1037
return false ;
1011
1038
}
1012
1039
0 commit comments