Skip to content

Commit

Permalink
NamedTuple getter (#18)
Browse files Browse the repository at this point in the history
* added getter for the whole config of an asa

* Note over Tuple limitation

* params normalizer utility

* tests for getters recovered

* minor changes to

* Better utils

* minor fixes in

* WIP: Rename CLI.

* last fixes in branch

* Better README for CLI

* Current directory

Co-authored-by: Matteo Almanza <[email protected]>

* Current path 2.

Co-authored-by: Matteo Almanza <[email protected]>

* minor fixes

* Update README.md

Co-authored-by: Matteo Almanza <[email protected]>

* Update account.py

Co-authored-by: Matteo Almanza <[email protected]>

* Update smart_asa_test.py

Co-authored-by: Matteo Almanza <[email protected]>

* Update smart_asa_test.py

Co-authored-by: Matteo Almanza <[email protected]>

* named tuples in config getter normalizer

* Linting.

* better NamedTuple usage

* metadata hash encoding

* fix getters in readme (#7)

* Contract json (#8)

* fix getters in readme

* added contract interface to repo

* fix linter

* fix readme: getters and abi (#9)

* Add community credits to `README.md`

* Use of `abi.NamedTuple` struct in getter `get_asset_config` (#17)

* Use of  in getter

* removing irrelevant comment

* minor change

* `namedtuple` from `abi.NamedTuple`

* Getting annotation the right way.

Co-authored-by: cusma <[email protected]>

* README: deployed App example

Co-authored-by: deanstef <[email protected]>
Co-authored-by: Matteo Almanza <[email protected]>
Co-authored-by: Adriano Di Luzio <[email protected]>
  • Loading branch information
4 people authored Aug 29, 2022
1 parent 1356fa5 commit ce6ffb2
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 58 deletions.
12 changes: 6 additions & 6 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Smart ASA reference implementation combines the simplicity and security of the Algorand Standard Assets (ASA)s with the composability and programmability of Algorand Smart Contracts. The Smart ASA offers a new, powerful, L1 feature that extends regular ASAs up to the limits of your imagination!

- [Smart ASA ABI JSON](https://github.com/algorandlabs/smart-asa/blob/main/smart_asa_abi.json)
- [Smart ASA App TEAL](https://testnet.algoexplorer.io/application/107042851)

**⚠️ Disclamer: This code is not audited!**

- [Overview](#overview)
Expand Down
3 changes: 1 addition & 2 deletions smart_asa_abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,9 @@
}
],
"returns": {
"type": "((uint64,uint32,bool,string,string),(string,byte[],address,address,address),(address))"
"type": "(uint64,uint32,bool,string,string,string,byte[],address,address,address,address)"
}
}
],
"desc": null,
"networks": {}
}
47 changes: 27 additions & 20 deletions smart_asa_asc.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@
# Descriptive field for the binding of Smart ASA App ID into the Underlying ASA url.
SMART_ASA_APP_BINDING = "smart-asa-app-id:"

# / --- ALIAS
AssetParamsTuple1 = abi.Tuple5[abi.Uint64, abi.Uint32, abi.Bool, abi.String, abi.String]
AssetParamsTuple2 = abi.Tuple5[
abi.String, abi.DynamicArray[abi.Byte], abi.Address, abi.Address, abi.Address
]
AssetParamsTuple3 = abi.Tuple1[abi.Address]
AssetParamsTuple = abi.Tuple3[AssetParamsTuple1, AssetParamsTuple2, AssetParamsTuple3]


# NOTE: The following costs could change over time with protocol upgrades.
OPTIN_COST = 100_000
UINTS_COST = 28_500
Expand Down Expand Up @@ -113,6 +104,20 @@ def schema(cls):
)


class SmartASAConfig(abi.NamedTuple):
total: abi.Field[abi.Uint64]
decimals: abi.Field[abi.Uint32]
default_frozen: abi.Field[abi.Bool]
unit_name: abi.Field[abi.String]
name: abi.Field[abi.String]
url: abi.Field[abi.String]
metadata_hash: abi.Field[abi.DynamicArray[abi.Byte]]
manager_addr: abi.Field[abi.Address]
reserve_addr: abi.Field[abi.Address]
freeze_addr: abi.Field[abi.Address]
clawback_addr: abi.Field[abi.Address]


# / --- --- LOCAL STATE
# NOTE: Local State is needed only if the Smart ASA has `account_frozen`.
# Local State is not needed in case Smart ASA has just "global" `asset_freeze`.
Expand Down Expand Up @@ -768,11 +773,8 @@ def get_optin_min_balance(asset: abi.Asset, *, output: abi.Uint64) -> Expr:
)


# NOTE: This getters returns a Tuple3 of (Tuple5, Tuple5, Tuple1). PyTeal currently supports tuples maximum
# of 5 generic arguments (i.e. Tuple5). For this reason this getters splits the returned parameters in
# three tuples.
@smart_asa_abi.method
def get_asset_config(asset: abi.Asset, *, output: AssetParamsTuple) -> Expr:
def get_asset_config(asset: abi.Asset, *, output: SmartASAConfig) -> Expr:
return Seq(
# Preconditions
getter_preconditions(asset.asset_id()),
Expand All @@ -793,14 +795,19 @@ def get_asset_config(asset: abi.Asset, *, output: AssetParamsTuple) -> Expr:
(reserve_addr := abi.Address()).set(App.globalGet(GlobalState.reserve_addr)),
(freeze_addr := abi.Address()).set(App.globalGet(GlobalState.freeze_addr)),
(clawback_addr := abi.Address()).set(App.globalGet(GlobalState.clawback_addr)),
(params_tuple1 := abi.make(AssetParamsTuple1)).set(
total, decimals, default_frozen, unit_name, name
),
(params_tuple2 := abi.make(AssetParamsTuple2)).set(
url, metadata_hash, manager_addr, reserve_addr, freeze_addr
output.set(
total,
decimals,
default_frozen,
unit_name,
name,
url,
metadata_hash,
manager_addr,
reserve_addr,
freeze_addr,
clawback_addr,
),
(params_tuple3 := abi.make(AssetParamsTuple3)).set(clawback_addr),
output.set(params_tuple1, params_tuple2, params_tuple3),
)


Expand Down
35 changes: 5 additions & 30 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import base64
from collections import namedtuple
from inspect import get_annotations
from typing import Union
from algosdk import constants
from algosdk.future import transaction
from algosdk.v2client import algod
from smart_asa_asc import SmartASAConfig as PyTealSmartASAConfig


def decode_state(state) -> dict[str, Union[int, bytes]]:
Expand Down Expand Up @@ -58,38 +60,11 @@ def assemble_program(algod_client: algod.AlgodClient, source_code: str) -> bytes
return base64.b64decode(compile_response["result"])


# NOTE: getter_params represents a tuple of three tuples. This utility
# will be removed once PyTeal integrates the ABI type NamedTuple
SmartASAConfig = namedtuple(
"SmartASAConfig",
[
"total",
"decimals",
"default_frozen",
"unit_name",
"name",
"url",
"metadata_hash",
"manager_addr",
"reserve_addr",
"freeze_addr",
"clawback_addr",
],
PyTealSmartASAConfig.__class__.__name__,
list(get_annotations(PyTealSmartASAConfig)),
)


def normalize_getter_params(getter_params: list) -> SmartASAConfig:

return SmartASAConfig(
getter_params[0][0],
getter_params[0][1],
getter_params[0][2],
getter_params[0][3],
getter_params[0][4],
getter_params[1][0],
getter_params[1][1],
getter_params[1][2],
getter_params[1][3],
getter_params[1][4],
getter_params[2][0],
)
return SmartASAConfig(*getter_params)

0 comments on commit ce6ffb2

Please sign in to comment.