mirror of
https://github.com/planetmint/planetmint.git
synced 2025-11-25 15:05:49 +00:00
Implemented Compose Class for Transactions
This commit is contained in:
parent
43fe80818b
commit
e01a9b96cd
@ -43,55 +43,12 @@ def _group_transaction_by_ids(connection, txids: list):
|
||||
"inputs": _txinputs,
|
||||
"outputs": _txoutputs,
|
||||
"keys": _txkeys,
|
||||
"assets": _txassets,
|
||||
"asset": _txassets,
|
||||
"metadata": _txmeta,
|
||||
}
|
||||
tx_compose = TransactionCompose()
|
||||
_transaction = tx_compose.convert_to_dict(db_results=result_map)
|
||||
|
||||
_obj = {
|
||||
"inputs": [
|
||||
{
|
||||
"fulfillment": _in[1],
|
||||
"fulfills": {"transaction_id": _in[3], "output_index": int(_in[4])} if len(_in[3]) > 0 and len(
|
||||
# TODO Now it is working because of data type cast to INTEGER for field "output_index"
|
||||
_in[4]) > 0 else None,
|
||||
"owners_before": _in[2]
|
||||
} for _in in _txinputs
|
||||
],
|
||||
"outputs": [],
|
||||
"operation": _txobject[1],
|
||||
"metadata": None,
|
||||
"asset": None,
|
||||
"version": _txobject[2],
|
||||
"id": txid,
|
||||
}
|
||||
if _txoutputs[0][7] is None:
|
||||
_obj["outputs"] = [
|
||||
{
|
||||
"amount": _out[1],
|
||||
"condition": {"details": {"type": _out[3], "public_key": _out[4]}, "uri": _out[2]},
|
||||
"public_keys": [_key[3] for _key in _txkeys if _key[2] == _out[5]]
|
||||
} for _out in _txoutputs
|
||||
]
|
||||
else:
|
||||
_obj["outputs"] = [
|
||||
{
|
||||
"amount": _out[1],
|
||||
"condition": {"uri": _out[2], "details": {"subconditions": _out[7]}, "type": _out[3],
|
||||
"treshold": _out[6]},
|
||||
"public_keys": [_key[3] for _key in _txkeys if _key[2] == _out[5]]
|
||||
} for _out in _txoutputs
|
||||
]
|
||||
|
||||
if len(_txobject[3]) > 0:
|
||||
_obj["asset"] = {
|
||||
"id": _txobject[3]
|
||||
}
|
||||
elif len(_txassets) > 0:
|
||||
_obj["asset"] = _txassets[0][1]
|
||||
_obj["metadata"] = _txmeta[0][1] if len(_txmeta) == 1 else None
|
||||
_transactions.append(_obj)
|
||||
tx_compose = TransactionCompose(db_results=result_map)
|
||||
_transaction = tx_compose.convert_to_dict()
|
||||
_transactions.append(_transaction)
|
||||
return _transactions
|
||||
|
||||
|
||||
|
||||
@ -2,11 +2,12 @@ from secrets import token_hex
|
||||
|
||||
|
||||
def _save_keys_order(dictionary):
|
||||
filter_keys = ["asset", "metadata"]
|
||||
if type(dictionary) is dict:
|
||||
keys = list(dictionary.keys())
|
||||
_map = {}
|
||||
for key in keys:
|
||||
_map[key] = _save_keys_order(dictionary=dictionary[key])
|
||||
_map[key] = _save_keys_order(dictionary=dictionary[key]) if key not in filter_keys else None
|
||||
|
||||
return _map
|
||||
elif type(dictionary) is list:
|
||||
@ -15,7 +16,7 @@ def _save_keys_order(dictionary):
|
||||
_map = {}
|
||||
keys = list(dictionary.keys())
|
||||
for key in keys:
|
||||
_map[key] = _save_keys_order(dictionary=dictionary[key])
|
||||
_map[key] = _save_keys_order(dictionary=dictionary[key]) if key not in filter_keys else None
|
||||
|
||||
return _map
|
||||
else:
|
||||
@ -130,5 +131,68 @@ class TransactionDecompose:
|
||||
|
||||
|
||||
class TransactionCompose:
|
||||
def convert_to_dict(self, db_results):
|
||||
transaction_map = db_results["transaction"][4]
|
||||
|
||||
def __init__(self, db_results):
|
||||
self.db_results = db_results
|
||||
self._map = self.db_results["transaction"][4]
|
||||
|
||||
def _get_transaction_operation(self):
|
||||
return self.db_results["transaction"][1]
|
||||
|
||||
def _get_transaction_version(self):
|
||||
return self.db_results["transaction"][2]
|
||||
|
||||
def _get_transaction_id(self):
|
||||
return self.db_results["transaction"][0]
|
||||
|
||||
def _get_asset(self):
|
||||
if len(self.db_results["transaction"][3]) > 0:
|
||||
return {
|
||||
"id": self.db_results["transaction"][3]
|
||||
}
|
||||
elif len(self.db_results["asset"]) > 0:
|
||||
return self.db_results["asset"][0][1]
|
||||
else:
|
||||
return None
|
||||
|
||||
def _get_metadata(self):
|
||||
return self.db_results["metadata"][0][1] if len(self.db_results["metadata"]) == 1 else None
|
||||
|
||||
def _get_inputs(self):
|
||||
_inputs = []
|
||||
for _input in self.db_results["inputs"]:
|
||||
_in = self._map["inputs"].copy()
|
||||
_in["fulfillment"] = _input[1]
|
||||
if _in["fulfills"] is not None:
|
||||
_in["fulfills"]["transaction_id"] = _input[3]
|
||||
_in["fulfills"]["output_index"] = int(_input[4])
|
||||
_in["owners_before"] = _input[2]
|
||||
_inputs.append(_in)
|
||||
return _inputs
|
||||
|
||||
def _get_outputs(self):
|
||||
_outputs = []
|
||||
for _output in self.db_results["outputs"]:
|
||||
_out = self._map["outputs"].copy()
|
||||
_out["amount"] = _out[1]
|
||||
_out["public_keys"] = [_key[3] for _key in self.db_results["keys"] if _key[2] == _output[5]]
|
||||
_out["condition"]["uri"] = _output[2]
|
||||
if self.db_results["outputs"][0][7] is None:
|
||||
_out["condition"]["details"]["type"] = _output[3]
|
||||
_out["condition"]["details"]["public_key"] = _output[4]
|
||||
else:
|
||||
_out["condition"]["details"]["subconditions"] = _output[7]
|
||||
_out["condition"]["type"] = _output[3]
|
||||
_out["condition"]["treshold"] = _output[6]
|
||||
return _outputs
|
||||
|
||||
def convert_to_dict(self):
|
||||
transaction = {k: None for k in list(self._map.keys())}
|
||||
transaction["id"] = self._get_transaction_id()
|
||||
transaction["asset"] = self._get_asset()
|
||||
transaction["metadata"] = self._get_metadata()
|
||||
transaction["version"] = self._get_transaction_version()
|
||||
transaction["operation"] = self._get_transaction_operation()
|
||||
transaction["inputs"] = self._get_inputs()
|
||||
transaction["outputs"] = self._get_outputs()
|
||||
return transaction
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user