forked from algorand/pyteal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
85cffeb
commit b6b2439
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters