44from logger import logger
55from rpcutils import error
66from rpcutils .rpcconnector import RPCConnector
7+ from rpcutils .rpcsocketconnector import RPCSocketConnector
78from . import utils
89from .constants import *
910
@@ -367,7 +368,7 @@ def getTransactionHex(id, params, config):
367368 rawTransaction = RPCConnector .request (
368369 endpoint = config .bitcoincoreRpcEndpoint ,
369370 id = id ,
370- method = GET_TRANSACTION_METHOD ,
371+ method = GET_RAW_TRANSACTION_METHOD ,
371372 params = [params ["txHash" ]]
372373 )
373374
@@ -383,7 +384,6 @@ def getTransactionHex(id, params, config):
383384@rpcmethod .rpcMethod (coin = COIN_SYMBOL )
384385@httpmethod .postHttpMethod (coin = COIN_SYMBOL )
385386def getTransaction (id , params , config ):
386-
387387 logger .printInfo (f"Executing RPC method getTransaction with id { id } and params { params } " )
388388
389389 requestSchema , responseSchema = utils .getMethodSchemas (GET_TRANSACTION )
@@ -393,53 +393,47 @@ def getTransaction(id, params, config):
393393 raise error .RpcBadRequestError (err .message )
394394
395395 try :
396- # Parameters: TransactionId, include_watchonly, verbose
397396 transaction = RPCConnector .request (
398397 endpoint = config .bitcoincoreRpcEndpoint ,
399398 id = id ,
400- method = GET_TRANSACTION_METHOD ,
399+ method = GET_RAW_TRANSACTION_METHOD ,
401400 params = [
402401 params ["txHash" ],
403- True ,
404402 True
405403 ]
406404 )
407405
408- vinAddressBalances = {}
409- transactionAmount = 0
406+ # Check if transaction is confirmed, and obtain block number
407+ if "blockhash" in transaction :
408+ transactionBlock = RPCConnector .request (
409+ endpoint = config .bitcoincoreRpcEndpoint ,
410+ id = id ,
411+ method = GET_BLOCK ,
412+ params = [transaction ["blockhash" ], 1 ]
413+ )
414+ blockNumber = transactionBlock ["height" ]
415+ else :
416+ blockNumber = None
410417
411- if "generated" not in transaction :
412418
413- for vin in transaction ["decoded" ]["vin" ]:
414- inputTransaction = RPCConnector .request (
415- endpoint = config .bitcoincoreRpcEndpoint ,
416- id = id ,
417- method = GET_TRANSACTION_METHOD ,
418- params = [
419- vin ["txid" ],
420- True ,
421- True
422- ]
423- )
419+ transactionDetails = utils .decodeTransactionDetails (transaction , config .bitcoincoreRpcEndpoint )
424420
425- transactionAmount += inputTransaction ["decoded" ]["vout" ][vin ["vout" ]]["value" ]
426- address = inputTransaction ["decoded" ]["vout" ][vin ["vout" ]]["scriptPubKey" ]["addresses" ][0 ]
427- value = inputTransaction ["decoded" ]["vout" ][vin ["vout" ]]["value" ]
428- vinAddressBalances [address ] = value
421+ # Converting all transaction details to str
422+ transactionDetails ["fee" ] = str (transactionDetails ["fee" ])
423+ for input in transactionDetails ["inputs" ]:
424+ input ["amount" ] = str (input ["amount" ])
425+ for output in transactionDetails ["outputs" ]:
426+ output ["amount" ] = str (output ["amount" ])
429427
430428 response = {
431429 "transaction" : {
432- "txHash" : params ["txHash" ],
433- "blockhash" : transaction ["blockhash" ] if transaction ["confirmations" ] >= 1 else None ,
434- "blockNumber" : str (transaction ["blockheight" ]) if transaction ["confirmations" ] >= 1 else None ,
435- "fee" : str (utils .convertToSatoshi (- transaction ["fee" ])) if "generated" not in transaction else "0" ,
436- "transfers" : utils .parseBalancesToTransfers (
437- vinAddressBalances ,
438- transaction ["details" ],
439- - transaction ["fee" ] if "generated" not in transaction else 0 ,
440- transactionAmount
441- ),
442- "data" : transaction ["decoded" ]
430+ "txId" : transaction ["txid" ],
431+ "txHash" : transaction ["hash" ],
432+ "blockNumber" : str (blockNumber ) if blockNumber is not None else blockNumber ,
433+ "fee" : transactionDetails ["fee" ],
434+ "inputs" : transactionDetails ["inputs" ],
435+ "outputs" : transactionDetails ["outputs" ],
436+ "data" : transaction
443437 }
444438 }
445439
0 commit comments