Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions Connector/bch/apirpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,46 @@ def getTransaction(id, params):
if err is not None:
raise rpcerrorhandler.BadRequestError(err.message)

transactionRaw = RPCConnector.request(RPC_ELECTRUM_CASH_ENDPOINT, id, GET_TRANSACTION_METHOD, [params[TX_HASH]])
transaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, DECODE_RAW_TRANSACTION_METHOD, [transactionRaw])
try:
# Parameters: TransactionId, include_watchonly, verbose
transaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_TRANSACTION_METHOD, [params[TX_HASH], True, True])

err = rpcutils.validateJSONRPCSchema(transaction, responseSchema)
vinAddressBalances = {}
transactionAmount = 0

if "generated" not in transaction:

for vin in transaction["decoded"][VIN]:
inputTransaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_TRANSACTION_METHOD, [vin[TX_ID], True, True])

transactionAmount += inputTransaction["decoded"][VOUT][vin[VOUT]][VALUE]
address = inputTransaction["decoded"][VOUT][vin[VOUT]][SCRIPT_PUB_KEY][ADDRESSES][0]
value = inputTransaction["decoded"][VOUT][vin[VOUT]][VALUE]
vinAddressBalances[address] = value

response = {
"transaction": {
BLOCK_HASH: transaction["blockhash"] if transaction[CONFIRMATIONS] >= 1 else None,
"fee": -transaction["fee"] if "generated" not in transaction else 0,
"transfers": utils.parseBalancesToTransfers(
vinAddressBalances,
transaction["details"],
-transaction["fee"] if "generated" not in transaction else 0,
transactionAmount
),
"data": transaction["decoded"]
}
}

except rpcerrorhandler.BadRequestError as err:
logger.printError(f"Transaction {params[TX_HASH]} could not be retrieve: {err}")
return {"transaction": None}

err = rpcutils.validateJSONRPCSchema(response, responseSchema)
if err is not None:
raise rpcerrorhandler.BadRequestError(err.message)

return transaction
return response


@httputils.postMethod
Expand Down
2 changes: 2 additions & 0 deletions Connector/bch/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
GET_BLOCKCHAIN_INFO = "getblockchaininfo"
SYNCING = "syncing"

BTC_CASH_PRECISION = 8

VERBOSITY_LESS_MODE = 0
VERBOSITY_DEFAULT_MODE = 1
VERBOSITY_MORE_MODE = 2
Expand Down
209 changes: 125 additions & 84 deletions Connector/bch/rpcschemas/gettransaction_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,138 @@
"description": "",
"type": "object",
"properties": {
"txid": {
"type": "string"
},
"hash": {
"type": "string"
},
"version": {
"type": "integer"
},
"size": {
"type": "integer"
},
"vsize": {
"type": "integer"
},
"weight": {
"type": "integer"
},
"locktime": {
"type": "integer"
},
"hex": {
"type": "string"
},
"vin": {
"type": "array",
"items": {
"type": "object",
"properties": {
"coinbase": {
"type": "string"
},
"sequence": {
"type": "integer"
},
"txid": {
"type": "string"
},
"vout": {
"type": "integer"
},
"scriptSig": {
"type": "object",
"transaction": {
"type": [
"object",
"null"
],
"properties": {
"blockHash": {
"type": [
"string",
"null"
]
},
"fee": {
"type": "number"
},
"transfers": {
"type": "array",
"items": {
"properties": {
"asm": {
"from": {
"type": "string"
},
"hex": {
"to": {
"type": "string"
},
"fee": {
"type": "number"
},
"amount": {
"type": "number"
}
}
},
"txinwitness": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
"vout": {
"type": "array",
"items": {
"type": "object",
"properties": {
"value": {
"type": "number"
},
"n": {
"type": "integer"
},
"scriptPubKey": {
"type": "object",
"properties": {
"asm": {
"type": "string"
},
"hex": {
"type": "string"
},
"reqSigs": {
"type": "integer"
},
"type": {
"type": "string"
},
"addresses": {
"type": "array",
"items": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"txid": {
"type": "string"
},
"hash": {
"type": "string"
},
"version": {
"type": "integer"
},
"size": {
"type": "integer"
},
"vsize": {
"type": "integer"
},
"weight": {
"type": "integer"
},
"locktime": {
"type": "integer"
},
"hex": {
"type": "string"
},
"vin": {
"type": "array",
"items": {
"type": "object",
"properties": {
"coinbase": {
"type": "string"
},
"sequence": {
"type": "integer"
},
"txid": {
"type": "string"
},
"vout": {
"type": "integer"
},
"scriptSig": {
"type": "object",
"properties": {
"asm": {
"type": "string"
},
"hex": {
"type": "string"
}
}
},
"txinwitness": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
},
"vout": {
"type": "array",
"items": {
"type": "object",
"properties": {
"value": {
"type": "number"
},
"n": {
"type": "integer"
},
"scriptPubKey": {
"type": "object",
"properties": {
"asm": {
"type": "string"
},
"hex": {
"type": "string"
},
"reqSigs": {
"type": "integer"
},
"type": {
"type": "string"
},
"addresses": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions Connector/bch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,48 @@ def getRequestMethodSchema(name):

def getResponseMethodSchema(name):
return RPC_JSON_SCHEMA_FOLDER + name + SCHEMA_CHAR_SEPARATOR + RESPONSE + SCHEMA_EXTENSION


def parseBalancesToTransfers(vin, vout, fee, amount):

transfers = []
diff = 0

for utxo in vout:

if utxo["category"] == "send":

for address in list(vin.keys()):

voutAmount = -utxo[AMOUNT]
vinAmount = vin[address]

if vinAmount <= (voutAmount + diff):
transfer = {
"from": address,
"to": utxo[ADDRESS],
AMOUNT: vinAmount,
"fee": round(vinAmount * fee / amount, BTC_CASH_PRECISION)
}
del vin[address]
else:
transfer = {
"from": address,
"to": utxo[ADDRESS],
AMOUNT: voutAmount,
"fee": round(voutAmount * fee / amount, BTC_CASH_PRECISION)
}

diff = diff + voutAmount - vinAmount
transfers.append(transfer)

if utxo["category"] in ["generate", "immature", "orphan"]:
transfers.append(
{
"to": utxo[ADDRESS],
"fee": 0,
AMOUNT: utxo[AMOUNT]
}
)

return transfers
40 changes: 36 additions & 4 deletions Connector/btc/apirpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,46 @@ def getTransaction(id, params):
if err is not None:
raise rpcerrorhandler.BadRequestError(err.message)

transactionRaw = RPCConnector.request(RPC_ELECTRUM_ENDPOINT, id, GET_TRANSACTION_METHOD, [params[TX_HASH]])
transaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, DECODE_RAW_TRANSACTION_METHOD, [transactionRaw])
try:
# Parameters: TransactionId, include_watchonly, verbose
transaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_TRANSACTION_METHOD, [params[TX_HASH], True, True])

err = rpcutils.validateJSONRPCSchema(transaction, responseSchema)
vinAddressBalances = {}
transactionAmount = 0

if "generated" not in transaction:

for vin in transaction["decoded"][VIN]:
inputTransaction = RPCConnector.request(RPC_CORE_ENDPOINT, id, GET_TRANSACTION_METHOD, [vin[TX_ID], True, True])

transactionAmount += inputTransaction["decoded"][VOUT][vin[VOUT]][VALUE]
address = inputTransaction["decoded"][VOUT][vin[VOUT]][SCRIPT_PUB_KEY][ADDRESSES][0]
value = inputTransaction["decoded"][VOUT][vin[VOUT]][VALUE]
vinAddressBalances[address] = value

response = {
"transaction": {
BLOCK_HASH: transaction["blockhash"] if transaction[CONFIRMATIONS] >= 1 else None,
"fee": -transaction["fee"] if "generated" not in transaction else 0,
"transfers": utils.parseBalancesToTransfers(
vinAddressBalances,
transaction["details"],
-transaction["fee"] if "generated" not in transaction else 0,
transactionAmount
),
"data": transaction["decoded"]
}
}

except rpcerrorhandler.BadRequestError as err:
logger.printError(f"Transaction {params[TX_HASH]} could not be retrieve: {err}")
return {"transaction": None}

err = rpcutils.validateJSONRPCSchema(response, responseSchema)
if err is not None:
raise rpcerrorhandler.BadRequestError(err.message)

return transaction
return response


@httputils.postMethod
Expand Down
2 changes: 2 additions & 0 deletions Connector/btc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
GET_BLOCKCHAIN_INFO = "getblockchaininfo"
SYNCING = "syncing"

BTC_PRECISION = 8

VERBOSITY_LESS_MODE = 0
VERBOSITY_DEFAULT_MODE = 1
VERBOSITY_MORE_MODE = 2
Expand Down
Loading