|  | 
|  | 1 | +from collections import OrderedDict | 
|  | 2 | +from os import access | 
|  | 3 | +from typing import List, Optional, Tuple, Union | 
|  | 4 | + | 
|  | 5 | +from algosdk import encoding | 
|  | 6 | +from algosdk.box_reference import BoxReference | 
|  | 7 | + | 
|  | 8 | + | 
|  | 9 | +def translate_to_resource_references( | 
|  | 10 | +    app_id: int, | 
|  | 11 | +    accounts: Optional[List[str]] = None, | 
|  | 12 | +    foreign_assets: Optional[List[int]] = None, | 
|  | 13 | +    foreign_apps: Optional[List[int]] = None, | 
|  | 14 | +    boxes: Optional[List[Tuple[int, bytes]]] = None, | 
|  | 15 | +    holdings: Optional[List[Tuple[int, str]]] = None, | 
|  | 16 | +    locals: Optional[List[Tuple[int, str]]] = None, | 
|  | 17 | +) -> List["ResourceReference"]: | 
|  | 18 | +    """ | 
|  | 19 | +    Convert accounts, apps, assets, boxes, holdings, locals into list of ResourceReference | 
|  | 20 | +    that is suitable for txn.Access field creation. | 
|  | 21 | +
 | 
|  | 22 | +    Args: | 
|  | 23 | +        app_id (int): current application id | 
|  | 24 | +        accounts (list[string], optional): list of additional accounts involved in call | 
|  | 25 | +        foreign_apps (list[int], optional): list of other applications (identified by index) involved in call | 
|  | 26 | +        foreign_assets (list[int], optional): list of assets involved in call | 
|  | 27 | +        boxes(list[(int, bytes)], optional): list of tuples specifying app id and key for boxes the app may access | 
|  | 28 | +        holdings (list[int, str], optional): lists of tuples specifying the asset holdings to be accessed during evaluation of the application; | 
|  | 29 | +            zero (empty) address means sender | 
|  | 30 | +        locals (list[int, str], optional): lists of tuples specifying the local states to be accessed during evaluation of the application; | 
|  | 31 | +            zero (empty) address means sender | 
|  | 32 | +    """ | 
|  | 33 | +    access: List["ResourceReference"] = [] | 
|  | 34 | + | 
|  | 35 | +    def ensure(target: "ResourceReference") -> int: | 
|  | 36 | +        for idx, a in enumerate(access): | 
|  | 37 | +            if ( | 
|  | 38 | +                a.address == target.address | 
|  | 39 | +                and a.asset_id == target.asset_id | 
|  | 40 | +                and a.app_id == target.app_id | 
|  | 41 | +            ): | 
|  | 42 | +                return idx + 1 | 
|  | 43 | +        access.append(target) | 
|  | 44 | +        return len(access) | 
|  | 45 | + | 
|  | 46 | +    for account in accounts or []: | 
|  | 47 | +        ensure(ResourceReference(address=account)) | 
|  | 48 | + | 
|  | 49 | +    for asset in foreign_assets or []: | 
|  | 50 | +        ensure(ResourceReference(asset_id=asset)) | 
|  | 51 | + | 
|  | 52 | +    for app in foreign_apps or []: | 
|  | 53 | +        ensure(ResourceReference(app_id=app)) | 
|  | 54 | + | 
|  | 55 | +    for asset, addr in holdings or []: | 
|  | 56 | +        addr_idx = 0 | 
|  | 57 | +        if addr: | 
|  | 58 | +            addr_idx = ensure(ResourceReference(address=addr)) | 
|  | 59 | +        asset_idx = ensure(ResourceReference(asset_id=asset)) | 
|  | 60 | +        access.append( | 
|  | 61 | +            ResourceReference( | 
|  | 62 | +                holding_reference=HoldingRef( | 
|  | 63 | +                    asset_index=asset_idx, addr_index=addr_idx | 
|  | 64 | +                ) | 
|  | 65 | +            ) | 
|  | 66 | +        ) | 
|  | 67 | + | 
|  | 68 | +    for app, addr in locals or []: | 
|  | 69 | +        app_idx = 0 | 
|  | 70 | +        if app and app != app_id: | 
|  | 71 | +            app_idx = ensure(ResourceReference(app_id=app)) | 
|  | 72 | +        addr_idx = 0 | 
|  | 73 | +        if addr: | 
|  | 74 | +            addr_idx = ensure(ResourceReference(address=addr)) | 
|  | 75 | +        access.append( | 
|  | 76 | +            ResourceReference( | 
|  | 77 | +                locals_reference=LocalsRef( | 
|  | 78 | +                    app_index=app_idx, addr_index=addr_idx | 
|  | 79 | +                ) | 
|  | 80 | +            ) | 
|  | 81 | +        ) | 
|  | 82 | + | 
|  | 83 | +    for app, name in boxes or []: | 
|  | 84 | +        app_idx = 0 | 
|  | 85 | +        if app and app != app_id: | 
|  | 86 | +            app_idx = ensure(ResourceReference(app_id=app)) | 
|  | 87 | +        access.append( | 
|  | 88 | +            ResourceReference( | 
|  | 89 | +                box_reference=BoxReference(app_index=app_idx, name=name) | 
|  | 90 | +            ) | 
|  | 91 | +        ) | 
|  | 92 | + | 
|  | 93 | +    return access | 
|  | 94 | + | 
|  | 95 | + | 
|  | 96 | +class ResourceReference: | 
|  | 97 | +    def __init__( | 
|  | 98 | +        self, | 
|  | 99 | +        address: Optional[str] = None, | 
|  | 100 | +        asset_id: Optional[int] = None, | 
|  | 101 | +        app_id: Optional[int] = None, | 
|  | 102 | +        box_reference: Optional[BoxReference] = None, | 
|  | 103 | +        holding_reference: Optional["HoldingRef"] = None, | 
|  | 104 | +        locals_reference: Optional["LocalsRef"] = None, | 
|  | 105 | +    ): | 
|  | 106 | +        self.app_id = app_id | 
|  | 107 | +        self.address = address | 
|  | 108 | +        self.asset_id = asset_id | 
|  | 109 | +        self.box_reference = box_reference | 
|  | 110 | +        self.holding_reference = holding_reference | 
|  | 111 | +        self.locals_reference = locals_reference | 
|  | 112 | + | 
|  | 113 | +    def dictify(self): | 
|  | 114 | +        d = dict() | 
|  | 115 | +        if self.address: | 
|  | 116 | +            d["d"] = encoding.decode_address(self.address) | 
|  | 117 | +        if self.asset_id: | 
|  | 118 | +            d["s"] = self.asset_id | 
|  | 119 | +        if self.app_id: | 
|  | 120 | +            d["p"] = self.app_id | 
|  | 121 | +        if self.box_reference: | 
|  | 122 | +            d["b"] = self.box_reference.dictify() | 
|  | 123 | +        if self.holding_reference: | 
|  | 124 | +            d["h"] = self.holding_reference.dictify() | 
|  | 125 | +        if self.locals_reference: | 
|  | 126 | +            d["l"] = self.locals_reference.dictify() | 
|  | 127 | +        od = OrderedDict(sorted(d.items())) | 
|  | 128 | +        return od | 
|  | 129 | + | 
|  | 130 | +    @staticmethod | 
|  | 131 | +    def undictify(d): | 
|  | 132 | +        return ResourceReference( | 
|  | 133 | +            address=encoding.encode_address(d["d"]) if "d" in d else "", | 
|  | 134 | +            asset_id=d["s"] if "s" in d else None, | 
|  | 135 | +            app_id=d["p"] if "p" in d else None, | 
|  | 136 | +            box_reference=BoxReference.undictify(d["b"]) if "b" in d else None, | 
|  | 137 | +            holding_reference=( | 
|  | 138 | +                HoldingRef.undictify(d["h"]) if "h" in d else None | 
|  | 139 | +            ), | 
|  | 140 | +            locals_reference=LocalsRef.undictify(d["l"]) if "l" in d else None, | 
|  | 141 | +        ) | 
|  | 142 | + | 
|  | 143 | +    def __eq__(self, value): | 
|  | 144 | +        if not isinstance(value, ResourceReference): | 
|  | 145 | +            return False | 
|  | 146 | +        return ( | 
|  | 147 | +            self.address == value.address | 
|  | 148 | +            and self.asset_id == value.asset_id | 
|  | 149 | +            and self.app_id == value.app_id | 
|  | 150 | +            and self.box_reference == value.box_reference | 
|  | 151 | +            and self.holding_reference == value.holding_reference | 
|  | 152 | +            and self.locals_reference == value.locals_reference | 
|  | 153 | +        ) | 
|  | 154 | + | 
|  | 155 | + | 
|  | 156 | +class LocalsRef: | 
|  | 157 | +    """ | 
|  | 158 | +    Represents a local reference in txn.Access with an app index and address index. | 
|  | 159 | +
 | 
|  | 160 | +    Args: | 
|  | 161 | +        app_index (int): index of the application in the access array | 
|  | 162 | +        addr_index (int): index of the address in the access array | 
|  | 163 | +    """ | 
|  | 164 | + | 
|  | 165 | +    def __init__(self, app_index: int, addr_index: int): | 
|  | 166 | +        self.app_index = app_index | 
|  | 167 | +        self.addr_index = addr_index | 
|  | 168 | + | 
|  | 169 | +    def dictify(self): | 
|  | 170 | +        d = dict() | 
|  | 171 | +        if self.app_index: | 
|  | 172 | +            d["p"] = self.app_index | 
|  | 173 | +        if self.addr_index: | 
|  | 174 | +            d["d"] = self.addr_index | 
|  | 175 | +        od = OrderedDict(sorted(d.items())) | 
|  | 176 | +        return od | 
|  | 177 | + | 
|  | 178 | +    @staticmethod | 
|  | 179 | +    def undictify(d): | 
|  | 180 | +        return LocalsRef( | 
|  | 181 | +            d["p"] if "p" in d else 0, | 
|  | 182 | +            d["d"] if "d" in d else 0, | 
|  | 183 | +        ) | 
|  | 184 | + | 
|  | 185 | +    def __eq__(self, other): | 
|  | 186 | +        if not isinstance(other, LocalsRef): | 
|  | 187 | +            return False | 
|  | 188 | +        return ( | 
|  | 189 | +            self.app_index == other.app_index | 
|  | 190 | +            and self.addr_index == other.addr_index | 
|  | 191 | +        ) | 
|  | 192 | + | 
|  | 193 | + | 
|  | 194 | +class HoldingRef: | 
|  | 195 | +    """ | 
|  | 196 | +    Represents a holding reference in txn.Access with an asset index and address index. | 
|  | 197 | +
 | 
|  | 198 | +    Args: | 
|  | 199 | +        asset_index (int): index of the asset in the access array | 
|  | 200 | +        addr_index (int): index of the address in the access array | 
|  | 201 | +    """ | 
|  | 202 | + | 
|  | 203 | +    def __init__(self, asset_index: int, addr_index: int): | 
|  | 204 | +        self.asset_index = asset_index | 
|  | 205 | +        self.addr_index = addr_index | 
|  | 206 | + | 
|  | 207 | +    def dictify(self): | 
|  | 208 | +        d = dict() | 
|  | 209 | +        if self.asset_index: | 
|  | 210 | +            d["s"] = self.asset_index | 
|  | 211 | +        if self.addr_index: | 
|  | 212 | +            d["d"] = self.addr_index | 
|  | 213 | +        od = OrderedDict(sorted(d.items())) | 
|  | 214 | +        return od | 
|  | 215 | + | 
|  | 216 | +    @staticmethod | 
|  | 217 | +    def undictify(d): | 
|  | 218 | +        return HoldingRef( | 
|  | 219 | +            d["s"] if "s" in d else 0, | 
|  | 220 | +            d["d"] if "d" in d else 0, | 
|  | 221 | +        ) | 
|  | 222 | + | 
|  | 223 | +    def __eq__(self, other): | 
|  | 224 | +        if not isinstance(other, HoldingRef): | 
|  | 225 | +            return False | 
|  | 226 | +        return ( | 
|  | 227 | +            self.asset_index == other.asset_index | 
|  | 228 | +            and self.addr_index == other.addr_index | 
|  | 229 | +        ) | 
0 commit comments