@@ -879,20 +879,104 @@ void rpc::upd_price::build_tx( bincode& tx )
879879 sig_.init_from_buf ( (const uint8_t *)(tx.get_buf () + pub_idx) );
880880}
881881
882+ bool rpc::upd_price::build_tx (
883+ bincode& tx, upd_price* upds[], const unsigned n
884+ )
885+ {
886+ if ( ! n ) {
887+ return false ;
888+ }
889+
890+ // signatures section
891+ tx.add_len < 1 >(); // one signature (publish)
892+ size_t pub_idx = tx.reserve_sign ();
893+
894+ // message header
895+ size_t tx_idx = tx.get_pos ();
896+ tx.add ( (uint8_t )1 ); // pub is only signing account
897+ tx.add ( (uint8_t )0 ); // read-only signed accounts
898+ tx.add ( (uint8_t )2 ); // sysvar and program-id are read-only
899+ // unsigned accounts
900+
901+ auto & first = *upds[ 0 ];
902+
903+ // accounts
904+ tx.add_len ( n + 3 ); // n + 3 accounts: publish, symbol{n}, sysvar, program
905+ tx.add ( *first.pkey_ ); // publish account
906+ for ( unsigned i = 0 ; i < n; ++i ) {
907+ tx.add ( *upds[ i ]->akey_ ); // symbol account
908+ }
909+ tx.add ( *(pub_key*)sysvar_clock ); // sysvar account
910+ tx.add ( *first.gkey_ ); // programid
911+
912+ // recent block hash
913+ tx.add ( *first.bhash_ ); // recent block hash
914+
915+ // instructions section
916+ tx.add_len ( n ); // n instruction(s)
917+ for ( unsigned i = 0 ; i < n; ++i ) {
918+ tx.add ( (uint8_t )( n + 2 ) ); // program_id index
919+ tx.add_len < 3 >(); // 3 accounts: publish, symbol, sysvar
920+ tx.add ( (uint8_t )0 ); // index of publish account
921+ tx.add ( (uint8_t )( i + 1 ) ); // index of symbol account
922+ tx.add ( (uint8_t )( n + 1 ) ); // index of sysvar account
923+
924+ auto const & upd = *upds[ i ];
925+
926+ // instruction parameter section
927+ tx.add_len <sizeof (cmd_upd_price)>();
928+ tx.add ( (uint32_t )PC_VERSION );
929+ tx.add ( (int32_t )( upd.cmd_ ) );
930+ tx.add ( (int32_t )( upd.st_ ) );
931+ tx.add ( (int32_t )0 );
932+ tx.add ( upd.price_ );
933+ tx.add ( upd.conf_ );
934+ tx.add ( upd.pub_slot_ );
935+ }
936+
937+ // all accounts need to sign transaction
938+ tx.sign ( pub_idx, tx_idx, *first.ckey_ );
939+ first.sig_ .init_from_buf ( (const uint8_t *)(tx.get_buf () + pub_idx) );
940+
941+ return true ;
942+ }
943+
882944void rpc::upd_price::build ( net_wtr& wtr )
883945{
884- bincode tx;
885- ((tx_wtr&)wtr).init ( tx );
886- build_tx ( tx );
887- ((tx_wtr&)wtr).commit ( tx );
946+ upd_price* upds[] = { this };
947+ build ( wtr, upds, 1 );
888948}
889949
950+
890951void rpc::upd_price::request ( json_wtr& msg )
952+ {
953+ upd_price* upds[] = { this };
954+ request ( msg, upds, 1 );
955+ }
956+
957+ bool rpc::upd_price::build (
958+ net_wtr& wtr, upd_price* upds[], const unsigned n
959+ )
960+ {
961+ bincode tx;
962+ static_cast < tx_wtr& >( wtr ).init ( tx );
963+ if ( ! build_tx ( tx, upds, n ) ) {
964+ return false ;
965+ }
966+ static_cast < tx_wtr& >( wtr ).commit ( tx );
967+ return true ;
968+ }
969+
970+ bool rpc::upd_price::request (
971+ json_wtr& msg, upd_price* upds[], const unsigned n
972+ )
891973{
892974 // construct binary transaction
893975 net_buf *bptr = net_buf::alloc ();
894976 bincode tx ( bptr->buf_ );
895- build_tx ( tx );
977+ if ( ! build_tx ( tx, upds, n ) ) {
978+ return false ;
979+ }
896980
897981 // encode transaction and add to json params
898982 msg.add_key ( " method" , " sendTransaction" );
@@ -904,6 +988,8 @@ void rpc::upd_price::request( json_wtr& msg )
904988 msg.pop ();
905989 msg.pop ();
906990 bptr->dealloc ();
991+
992+ return true ;
907993}
908994
909995void rpc::upd_price::response ( const jtree& jt )
0 commit comments