# This is an automatically generated file.
# DO NOT EDIT or your changes may be overwritten
from __future__ import annotations
import base64
import json
from xdrlib3 import Packer, Unpacker
from .base import DEFAULT_XDR_MAX_DEPTH
from .int64 import Int64
from .transaction_result_ext import TransactionResultExt
from .transaction_result_result import TransactionResultResult
__all__ = ["TransactionResult"]
[docs]
class TransactionResult:
"""
XDR Source Code::
struct TransactionResult
{
int64 feeCharged; // actual fee charged for the transaction
union switch (TransactionResultCode code)
{
case txFEE_BUMP_INNER_SUCCESS:
case txFEE_BUMP_INNER_FAILED:
InnerTransactionResultPair innerResultPair;
case txSUCCESS:
case txFAILED:
OperationResult results<>;
case txTOO_EARLY:
case txTOO_LATE:
case txMISSING_OPERATION:
case txBAD_SEQ:
case txBAD_AUTH:
case txINSUFFICIENT_BALANCE:
case txNO_ACCOUNT:
case txINSUFFICIENT_FEE:
case txBAD_AUTH_EXTRA:
case txINTERNAL_ERROR:
case txNOT_SUPPORTED:
// case txFEE_BUMP_INNER_FAILED: handled above
case txBAD_SPONSORSHIP:
case txBAD_MIN_SEQ_AGE_OR_GAP:
case txMALFORMED:
case txSOROBAN_INVALID:
case txFROZEN_KEY_ACCESSED:
void;
}
result;
// reserved for future use
union switch (int v)
{
case 0:
void;
}
ext;
};
"""
def __init__(
self,
fee_charged: Int64,
result: TransactionResultResult,
ext: TransactionResultExt,
) -> None:
self.fee_charged = fee_charged
self.result = result
self.ext = ext
def pack(self, packer: Packer) -> None:
self.fee_charged.pack(packer)
self.result.pack(packer)
self.ext.pack(packer)
@classmethod
def unpack(
cls, unpacker: Unpacker, depth_limit: int = DEFAULT_XDR_MAX_DEPTH
) -> TransactionResult:
if depth_limit <= 0:
raise ValueError("Maximum decoding depth reached")
fee_charged = Int64.unpack(unpacker, depth_limit - 1)
result = TransactionResultResult.unpack(unpacker, depth_limit - 1)
ext = TransactionResultExt.unpack(unpacker, depth_limit - 1)
return cls(
fee_charged=fee_charged,
result=result,
ext=ext,
)
def to_xdr_bytes(self) -> bytes:
packer = Packer()
self.pack(packer)
return packer.get_buffer()
@classmethod
def from_xdr_bytes(cls, xdr: bytes) -> TransactionResult:
unpacker = Unpacker(xdr)
result = cls.unpack(unpacker)
remaining = len(xdr) - unpacker.get_position()
if remaining != 0:
raise ValueError(f"Unexpected trailing {remaining} bytes in XDR data")
return result
def to_xdr(self) -> str:
xdr_bytes = self.to_xdr_bytes()
return base64.b64encode(xdr_bytes).decode()
@classmethod
def from_xdr(cls, xdr: str) -> TransactionResult:
xdr_bytes = base64.b64decode(xdr.encode())
return cls.from_xdr_bytes(xdr_bytes)
def to_json(self) -> str:
return json.dumps(self.to_json_dict())
@classmethod
def from_json(cls, json_str: str) -> TransactionResult:
return cls.from_json_dict(json.loads(json_str))
def to_json_dict(self) -> dict:
return {
"fee_charged": self.fee_charged.to_json_dict(),
"result": self.result.to_json_dict(),
"ext": self.ext.to_json_dict(),
}
@classmethod
def from_json_dict(cls, json_dict: dict) -> TransactionResult:
fee_charged = Int64.from_json_dict(json_dict["fee_charged"])
result = TransactionResultResult.from_json_dict(json_dict["result"])
ext = TransactionResultExt.from_json_dict(json_dict["ext"])
return cls(
fee_charged=fee_charged,
result=result,
ext=ext,
)
def __hash__(self):
return hash(
(
self.fee_charged,
self.result,
self.ext,
)
)
def __eq__(self, other: object):
if not isinstance(other, self.__class__):
return NotImplemented
return (
self.fee_charged == other.fee_charged
and self.result == other.result
and self.ext == other.ext
)
def __repr__(self):
out = [
f"fee_charged={self.fee_charged}",
f"result={self.result}",
f"ext={self.ext}",
]
return f"<TransactionResult [{', '.join(out)}]>"