@@ -50,11 +50,10 @@ export namespace TezosNodeWriter {
5050 */
5151 // TODO: move to an appropriate place
5252 export function forgeOperations ( branch : string , operations : TezosP2PMessageTypes . Operation [ ] ) : string {
53- log . debug ( 'TezosNodeWriter.forgeOperations:' ) ;
54- log . debug ( JSON . stringify ( operations ) ) ;
53+ log . debug ( `TezosNodeWriter.forgeOperations: ${ JSON . stringify ( operations ) } ` ) ;
5554 let encoded = TezosMessageUtils . writeBranch ( branch ) ;
5655 operations . forEach ( m => encoded += TezosMessageCodec . encodeOperation ( m ) ) ;
57-
56+ log . debug ( `TezosNodeWriter.forgeOperations: ${ encoded } ` ) ;
5857 return encoded ;
5958 }
6059
@@ -171,14 +170,16 @@ export namespace TezosNodeWriter {
171170 */
172171 export async function sendOperation ( server : string , operations : TezosP2PMessageTypes . Operation [ ] , signer : Signer , offset : number = 54 ) : Promise < TezosTypes . OperationResult > {
173172 const blockHead = await TezosNodeReader . getBlockAtOffset ( server , offset ) ;
174- const forgedOperationGroup = forgeOperations ( blockHead . hash , operations ) ;
173+ const blockHash = blockHead . hash . slice ( 0 , 51 ) ; // consider throwing an error instead
174+
175+ const forgedOperationGroup = forgeOperations ( blockHash , operations ) ;
175176
176177 const opSignature = await signer . signOperation ( Buffer . from ( TezosConstants . OperationGroupWatermark + forgedOperationGroup , 'hex' ) ) ;
177178
178179 const signedOpGroup = Buffer . concat ( [ Buffer . from ( forgedOperationGroup , 'hex' ) , opSignature ] ) ;
179180 const base58signature = TezosMessageUtils . readSignatureWithHint ( opSignature , signer . getSignerCurve ( ) ) ;
180181 const opPair = { bytes : signedOpGroup , signature : base58signature } ;
181- const appliedOp = await preapplyOperation ( server , blockHead . hash , blockHead . protocol , operations , opPair ) ;
182+ const appliedOp = await preapplyOperation ( server , blockHash , blockHead . protocol , operations , opPair ) ;
182183 const injectedOperation = await injectOperation ( server , opPair ) ;
183184
184185 return { results : appliedOp [ 0 ] , operationGroupID : injectedOperation } ; // TODO
@@ -649,12 +650,8 @@ export namespace TezosNodeWriter {
649650 storageCost : resources . storageCost + fixedOriginationStorageCost
650651 }
651652 }
652-
653653 /**
654- * Dry run the given operation and return consumed resources.
655- *
656- * Note: Estimating an operation on an unrevealed account is not supported and will fail. Remember to prepend
657- * the Reveal operation if required.
654+ * Dry run the given operation
658655 *
659656 * @param {string } server Tezos node to connect to
660657 * @param {string } chainid The chain ID to apply the operation on.
@@ -666,16 +663,7 @@ export namespace TezosNodeWriter {
666663 chainid : string ,
667664 ...operations : TezosP2PMessageTypes . Operation [ ]
668665 ) : Promise < { gas : number , storageCost : number } > {
669- const fake_signature = 'edsigu6xFLH2NpJ1VcYshpjW99Yc1TAL1m2XBqJyXrxcZQgBMo8sszw2zm626yjpA3pWMhjpsahLrWdmvX9cqhd4ZEUchuBuFYy' ;
670- const fake_chainid = 'NetXdQprcVkpaWU' ;
671- const fake_branch = 'BL94i2ShahPx3BoNs6tJdXDdGeoJ9ukwujUA2P8WJwULYNdimmq' ;
672-
673- const response = await performPostRequest ( server , `chains/${ chainid } /blocks/head/helpers/scripts/run_operation` , { chain_id : fake_chainid , operation : { branch : fake_branch , contents : operations , signature : fake_signature } } ) ;
674- const responseText = await response . text ( ) ;
675-
676- parseRPCError ( responseText ) ;
677-
678- const responseJSON = JSON . parse ( responseText ) ;
666+ const responseJSON = dryRunOperation ( server , chainid , ...operations ) ;
679667
680668 let gas = 0 ;
681669 let storageCost = 0 ;
@@ -701,6 +689,36 @@ export namespace TezosNodeWriter {
701689 return { gas, storageCost } ;
702690 }
703691
692+ /**
693+ * Dry run the given operation and return consumed resources.
694+ *
695+ * Note: Estimating an operation on an unrevealed account is not supported and will fail. Remember to prepend
696+ * the Reveal operation if required.
697+
698+ * @param {string } server Tezos node to connect to
699+ * @param {string } chainid The chain ID to apply the operation on.
700+ * @param {TezosP2PMessageTypes.Operation } operations A set of operations to update.
701+ * @returns {Promise<object> } JSON-encoded response
702+ */
703+ export async function dryRunOperation (
704+ server : string ,
705+ chainid : string ,
706+ ...operations : TezosP2PMessageTypes . Operation [ ]
707+ ) : Promise < Response > {
708+ const fake_signature = 'edsigu6xFLH2NpJ1VcYshpjW99Yc1TAL1m2XBqJyXrxcZQgBMo8sszw2zm626yjpA3pWMhjpsahLrWdmvX9cqhd4ZEUchuBuFYy' ;
709+ const fake_chainid = 'NetXdQprcVkpaWU' ;
710+ const fake_branch = 'BL94i2ShahPx3BoNs6tJdXDdGeoJ9ukwujUA2P8WJwULYNdimmq' ;
711+
712+ const response = await performPostRequest ( server , `chains/${ chainid } /blocks/head/helpers/scripts/run_operation` , { chain_id : fake_chainid , operation : { branch : fake_branch , contents : operations , signature : fake_signature } } ) ;
713+ const responseText = await response . text ( ) ;
714+
715+ parseRPCError ( responseText ) ;
716+
717+ const responseJSON = JSON . parse ( responseText ) ;
718+
719+ return responseJSON ;
720+ }
721+
704722 /**
705723 * This function checks if the server response contains an error. There are multiple formats for errors coming
706724 * back from the server, this method attempts to normalized them for downstream parsing.
0 commit comments