Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acct params get #165

Merged
merged 8 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -151,6 +152,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.
"""
ahangsu marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -174,6 +174,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