Source code for digital_world_sdk.dwsa_data_builder
from __future__ import annotations
from . import xdr as dw_xdr
__all__ = ["DwsaDataBuilder"]
[docs]
class DwsaDataBuilder:
"""Supports building :class:`Memo <digital_world_sdk.xdr.DwsaTransactionData>` structures
with various items set to specific values.
This is recommended for when you are building :class:`RestoreFootprint <digital_world_sdk.operation.RestoreFootprint>`,
:class:`ExtendFootprintTTL <digital_world_sdk.operation.ExtendFootprintTTL>` operations to avoid (re)building
the entire data structure from scratch.
By default, an empty instance will be created.
"""
def __init__(self):
self._data = dw_xdr.DwsaTransactionData(
ext=dw_xdr.DwsaTransactionDataExt(0),
resource_fee=dw_xdr.Int64(0),
resources=dw_xdr.DwsaResources(
footprint=dw_xdr.LedgerFootprint(
read_only=[],
read_write=[],
),
disk_read_bytes=dw_xdr.Uint32(0),
write_bytes=dw_xdr.Uint32(0),
instructions=dw_xdr.Uint32(0),
),
)
[docs]
@classmethod
def from_xdr(
cls, dwsa_data: str | dw_xdr.DwsaTransactionData
) -> DwsaDataBuilder:
"""Create a new :class:`DwsaDataBuilder` object from an XDR object.
:param dwsa_data: The XDR object that represents a DwsaTransactionData.
:return: This builder.
"""
data = cls()
if isinstance(dwsa_data, str):
data._data = dw_xdr.DwsaTransactionData.from_xdr(dwsa_data)
else:
data._data = dw_xdr.DwsaTransactionData.from_xdr_bytes(
dwsa_data.to_xdr_bytes()
)
return data
[docs]
def set_resource_fee(self, fee: int) -> DwsaDataBuilder:
"""Sets the "resource" fee portion of the DWSA data.
:param fee: The resource fee to set (int64)
:return: This builder.
"""
self._data.resource_fee = dw_xdr.Int64(fee)
return self
[docs]
def set_read_only(
self, read_only: list[dw_xdr.LedgerKey]
) -> DwsaDataBuilder:
"""Sets the read-only portion of the storage access footprint to be a certain set of ledger keys.
:param read_only: The read-only ledger keys to set.
:return: This builder.
"""
self._data.resources.footprint.read_only = read_only or []
return self
[docs]
def set_read_write(
self, read_write: list[dw_xdr.LedgerKey]
) -> DwsaDataBuilder:
"""Sets the read-write portion of the storage access footprint to be a certain set of ledger keys.
:param read_write: The read-write ledger keys to set.
:return: This builder.
"""
self._data.resources.footprint.read_write = read_write or []
return self
[docs]
def set_resources(
self, instructions: int, disk_read_bytes: int, write_bytes: int
) -> DwsaDataBuilder:
"""Sets up the resource metrics.
You should almost NEVER need this, as its often generated / provided to you
by transaction simulation/preflight from a DWSA RPC server.
:param instructions: Number of CPU instructions (uint32)
:param disk_read_bytes: Number of bytes being read from disk (uint32)
:param write_bytes: Number of bytes being written to disk and memory (uint32)
:return: This builder.
"""
self._data.resources.instructions = dw_xdr.Uint32(instructions)
self._data.resources.disk_read_bytes = dw_xdr.Uint32(disk_read_bytes)
self._data.resources.write_bytes = dw_xdr.Uint32(write_bytes)
return self
[docs]
def build(self):
""":return: a copy of the final data structure."""
return dw_xdr.DwsaTransactionData.from_xdr_bytes(
self._data.to_xdr_bytes()
)