@@ -745,8 +745,8 @@ Contract.prototype._executeMethod = function _executeMethod(){
745745 this . call . apply ( this , Array . prototype . slice . call ( arguments ) ) ;
746746 }
747747 else {
748- //send
749- this . send . apply ( this , Array . prototype . slice . call ( arguments ) ) ;
748+ //submit
749+ this . submit . apply ( this , Array . prototype . slice . call ( arguments ) ) ;
750750 }
751751 break ;
752752
@@ -776,13 +776,19 @@ Contract.prototype._executeMethod = function _executeMethod(){
776776 ContractValue : contractValue ,
777777 ContractData : contractData . toUpperCase ( )
778778 } ;
779+ let txCallbackProperty = { } ;
780+ txCallbackProperty . callbackFunc = args . callback ;
781+ txCallbackProperty . callbackExpect = "send_success" ;
782+ if ( args . options . hasOwnProperty ( "expect" ) ) {
783+ txCallbackProperty . callbackExpect = args . options . expect ;
784+ }
779785 if ( ( typeof args . callback ) != 'function' ) {
780786 let contractObj = this . _parent ;
781787 return new Promise ( function ( resolve , reject ) {
782- handleContractSendTx ( contractObj , sendTxPayment , args . callback , resolve , reject ) ;
788+ handleContractSendTx ( contractObj , sendTxPayment , txCallbackProperty , resolve , reject ) ;
783789 } ) ;
784790 } else {
785- handleContractSendTx ( this . _parent , sendTxPayment , args . callback , null , null ) ;
791+ handleContractSendTx ( this . _parent , sendTxPayment , txCallbackProperty , null , null ) ;
786792 }
787793 break ;
788794 }
@@ -836,8 +842,9 @@ function handleContractCall(curFunObj, callObj, callBack, resolve, reject) {
836842 } ) ;
837843}
838844
839- function handleContractSendTx ( contractObj , sendTxObj , callBack , resolve , reject ) {
845+ function handleContractSendTx ( contractObj , sendTxObj , txCallbackProperty , resolve , reject ) {
840846 let chainSQL = contractObj . chainsql ;
847+ var callBack = txCallbackProperty . callbackFunc ;
841848 var errFunc = function ( error ) {
842849 if ( ( typeof callBack ) == 'function' ) {
843850 callBack ( error , null ) ;
@@ -847,7 +854,7 @@ function handleContractSendTx(contractObj, sendTxObj, callBack, resolve, reject)
847854 } ;
848855 prepareSendTxPayment ( chainSQL , sendTxObj ) . then ( data => {
849856 let signedRet = chainSQL . api . sign ( data . txJSON , chainSQL . connect . secret ) ;
850- submitTxCallTx ( chainSQL , signedRet , callBack , resolve , reject ) ;
857+ submitTxCallTx ( chainSQL , signedRet , txCallbackProperty , resolve , reject ) ;
851858 } ) . catch ( err => {
852859 errFunc ( err ) ;
853860 } ) ;
@@ -871,7 +878,8 @@ function createSendTxPayment(sendTxPayment){
871878 } ;
872879 return txJSON ;
873880}
874- function submitTxCallTx ( chainSQL , signedVal , callBack , resolve , reject ) {
881+ function submitTxCallTx ( chainSQL , signedVal , txCallbackProperty , resolve , reject ) {
882+ var callBack = txCallbackProperty . callbackFunc ;
875883 var isFunction = false ;
876884 if ( ( typeof callBack ) == 'function' )
877885 isFunction = true ;
@@ -891,19 +899,60 @@ function submitTxCallTx(chainSQL, signedVal, callBack, resolve, reject){
891899 }
892900 } ;
893901 //will handle solidity event later
902+ if ( txCallbackProperty . callbackExpect !== "send_success" ) {
903+ chainSQL . event . subscribeTx ( signedVal . id , async function ( err , data ) {
904+ if ( err ) {
905+ errFunc ( err ) ;
906+ } else {
907+ // success
908+ if ( txCallbackProperty . callbackExpect === data . status && data . type === 'singleTransaction' ) {
909+ sucFunc ( {
910+ status : data . status ,
911+ tx_hash : data . transaction . hash ,
912+ } ) ;
913+ }
914+ // failure
915+ if ( data . status == 'db_error'
916+ || data . status == 'db_timeout'
917+ || data . status == 'validate_timeout' ) {
918+ errFunc ( {
919+ status : data . status ,
920+ tx_hash : data . transaction . hash ,
921+ error_message : data . error_message
922+ } ) ;
923+ }
924+ }
925+ } ) . then ( function ( data ) {
926+ // subscribeTx success
927+ } ) . catch ( function ( error ) {
928+ // subscribeTx failure
929+ errFunc ( 'subscribeTx exception.' + error ) ;
930+ } ) ;
931+ }
894932
895933 // submit transaction
896934 chainSQL . api . submit ( signedVal . signedTransaction ) . then ( function ( result ) {
897935 //console.log('submit ', JSON.stringify(result));
898936 if ( result . resultCode !== 'tesSUCCESS' ) {
937+ if ( txCallbackProperty . callbackExpect !== "send_success" ) {
938+ chainSQL . event . unsubscribeTx ( signedVal . id ) . then ( function ( data ) {
939+ // unsubscribeTx success
940+ } ) . catch ( function ( error ) {
941+ // unsubscribeTx failure
942+ errFunc ( 'unsubscribeTx failure.' + error ) ;
943+ } ) ;
944+ }
945+
899946 //return error message
900947 errFunc ( result ) ;
901948 } else {
902949 // submit successfully
903- sucFunc ( {
904- status : 'send_success' ,
905- tx_hash : signedVal . id
906- } ) ;
950+ if ( txCallbackProperty . callbackExpect === "send_success" ) {
951+ sucFunc ( {
952+ status : 'send_success' ,
953+ tx_hash : signedVal . id
954+ } ) ;
955+ }
907956 }
908957 } ) . catch ( function ( error ) {
909958 errFunc ( error ) ;
0 commit comments