Skip to content

Commit

Permalink
Acct params get (algorand#165)
Browse files Browse the repository at this point in the history
* Adding account param getter

* Add to init

* fix op names and type

* adding tests

* allow bytes to be passed

* tweak docs, add require check for any
  • Loading branch information
barnjamin authored and algoidurovic committed Mar 23, 2022
1 parent 85cffeb commit b6b2439
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyteal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ __all__ = [
"AppParam",
"AssetHolding",
"AssetParam",
"AccountParam",
"InnerTxnBuilder",
"InnerTxn",
"InnerTxnAction",
Expand Down
2 changes: 2 additions & 0 deletions pyteal/ast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .global_ import Global, GlobalField
from .app import App, AppField, OnComplete, AppParam
from .asset import AssetHolding, AssetParam
from .acct import AccountParam

# inner txns
from .itxn import InnerTxnBuilder, InnerTxn, InnerTxnAction
Expand Down Expand Up @@ -152,6 +153,7 @@
"AppParam",
"AssetHolding",
"AssetParam",
"AccountParam",
"InnerTxnBuilder",
"InnerTxn",
"InnerTxnAction",
Expand Down
62 changes: 62 additions & 0 deletions pyteal/ast/acct.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import TYPE_CHECKING

from ..types import TealType, require_type
from ..ir import Op
from .expr import Expr
from .maybe import MaybeValue

if TYPE_CHECKING:
from ..compiler import CompileOptions


class AccountParam:
@classmethod
def balance(cls, acct: Expr) -> MaybeValue:
"""Get the current balance in microalgos an account.
Args:
acct: An index into Txn.accounts that corresponds to the application to check or an address available at runtime.
May evaluate to uint64 or an address.
"""
require_type(acct, TealType.anytype)
return MaybeValue(
Op.acct_params_get,
TealType.uint64,
immediate_args=["AcctBalance"],
args=[acct],
)

@classmethod
def minBalance(cls, acct: Expr) -> MaybeValue:
"""Get the minimum balance in microalgos for an account.
Args:
acct: An index into Txn.accounts that corresponds to the application to check or an address available at runtime.
May evaluate to uint64 or an address.
"""
require_type(acct, TealType.anytype)
return MaybeValue(
Op.acct_params_get,
TealType.uint64,
immediate_args=["AcctMinBalance"],
args=[acct],
)

@classmethod
def authAddr(cls, acct: Expr) -> MaybeValue:
"""Get the authorizing address for an account. If the account is not rekeyed, the empty addresss is returned.
Args:
acct: An index into Txn.accounts that corresponds to the application to check or an address available at runtime.
May evaluate to uint64 or an address.
"""
require_type(acct, TealType.anytype)
return MaybeValue(
Op.acct_params_get,
TealType.bytes,
immediate_args=["AcctAuthAddr"],
args=[acct],
)


AccountParam.__module__ = "pyteal"
80 changes: 80 additions & 0 deletions pyteal/ast/acct_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import pytest

from .. import *

# this is not necessary but mypy complains if it's not included
from .. import CompileOptions

options = CompileOptions()
teal4Options = CompileOptions(version=4)
teal5Options = CompileOptions(version=5)
teal6Options = CompileOptions(version=6)


def test_acct_param_balance_valid():
arg = Int(1)
expr = AccountParam.balance(arg)
assert expr.type_of() == TealType.none
assert expr.value().type_of() == TealType.uint64

expected = TealSimpleBlock(
[
TealOp(arg, Op.int, 1),
TealOp(expr, Op.acct_params_get, "AcctBalance"),
TealOp(None, Op.store, expr.slotOk),
TealOp(None, Op.store, expr.slotValue),
]
)

actual, _ = expr.__teal__(teal6Options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)

with TealComponent.Context.ignoreExprEquality():
assert actual == expected


def test_acct_param_min_balance_valid():
arg = Int(0)
expr = AccountParam.minBalance(arg)
assert expr.type_of() == TealType.none
assert expr.value().type_of() == TealType.uint64

expected = TealSimpleBlock(
[
TealOp(arg, Op.int, 0),
TealOp(expr, Op.acct_params_get, "AcctMinBalance"),
TealOp(None, Op.store, expr.slotOk),
TealOp(None, Op.store, expr.slotValue),
]
)

actual, _ = expr.__teal__(teal6Options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)

with TealComponent.Context.ignoreExprEquality():
assert actual == expected


def test_acct_param_auth_addr_valid():
arg = Int(1)
expr = AccountParam.authAddr(arg)
assert expr.type_of() == TealType.none
assert expr.value().type_of() == TealType.bytes

expected = TealSimpleBlock(
[
TealOp(arg, Op.int, 1),
TealOp(expr, Op.acct_params_get, "AcctAuthAddr"),
TealOp(None, Op.store, expr.slotOk),
TealOp(None, Op.store, expr.slotValue),
]
)

actual, _ = expr.__teal__(teal6Options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)

with TealComponent.Context.ignoreExprEquality():
assert actual == expected
1 change: 1 addition & 0 deletions pyteal/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def min_version(self) -> int:
gitxn = OpType("gitxn", Mode.Application, 6)
gitxna = OpType("gitxna", Mode.Application, 6)
gloadss = OpType("gloadss", Mode.Application, 6)
acct_params_get = OpType("acct_params_get", Mode.Application, 6)
# fmt: on


Expand Down

0 comments on commit b6b2439

Please sign in to comment.