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

Arc4 ints bytes #4

Merged
merged 5 commits into from
Nov 5, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# pyright: reportMissingModuleSource=false
import typing as t

from algopy import ARC4Contract, GlobalState, UInt64, arc4, urange
from algopy import ARC4Contract, GlobalState, String, UInt64, arc4, urange
from algopy.arc4 import abimethod


Expand All @@ -20,9 +20,49 @@ def arc4_uint64(self, a: arc4.UInt64, b: arc4.UInt64) -> arc4.UInt64:

return arc4.UInt64(c)

@abimethod()
def arc4_uint_n(
self, a: arc4.UInt8, b: arc4.UInt16, c: arc4.UInt32, d: arc4.UInt64
) -> arc4.UInt64:
"""
The encoding of arc4 integers will be smaller if it uses fewer bits.
Ultimately, they are all represented with native UInt64.
"""
assert a.bytes.length == 1
assert b.bytes.length == 2
assert c.bytes.length == 4
assert d.bytes.length == 8

total = a.native + b.native + c.native + d.native

return arc4.UInt64(total)

@abimethod()
def arc4_biguint_n(
self, a: arc4.UInt128, b: arc4.UInt256, c: arc4.UInt512
) -> arc4.UInt512:
"""
Integers with larger bit size are supported up to 512 bits.
Ultimately, they are all represented with native BigUInt.
"""
assert a.bytes.length == 16
assert b.bytes.length == 32
assert c.bytes.length == 64

total = a.native + b.native + c.native

return arc4.UInt512(total)

@abimethod()
def arc4_byte(self, a: arc4.Byte) -> arc4.Byte:
"""
An arc4.Byte is essentially an alias for an 8-bit integer.
"""
return arc4.Byte(a.native + 1)

@abimethod()
def arc4_address_properties(self, address: arc4.Address) -> UInt64:
underlying_bytes = (
underlying_bytes = ( # noqa: F841
address.bytes
) # This will return the underlying bytes of the address.

Expand All @@ -31,7 +71,7 @@ def arc4_address_properties(self, address: arc4.Address) -> UInt64:
) # This will return the account type of the given address.

bal = account.balance # returns the balance of the account
total_asset = (
total_asset = ( # noqa: F841
account.total_assets
) # returns the total assets held in the account

Expand Down Expand Up @@ -92,6 +132,41 @@ def arc4_static_array(self) -> None:
"""


class Arc4DynamicArray(ARC4Contract):

@abimethod()
def arc4_dynamic_array(self, name: arc4.String) -> arc4.String:
"""
Dynamic Arrays have variable size and capacity.
They are similar to native Python lists because they can also append, extend, and pop.
"""
dynamic_string_array = arc4.DynamicArray[arc4.String](arc4.String("Hello"))

extension = arc4.DynamicArray[arc4.String](
arc4.String(" world"), arc4.String(", ")
)
dynamic_string_array.extend(extension)

dynamic_string_array.append(name)
dynamic_string_array.append(arc4.String("!"))
dynamic_string_array.pop()

greeting = String()
for x in dynamic_string_array:
greeting += x.native

return arc4.String(greeting)

@abimethod()
def arc4_dynamic_bytes(self) -> arc4.DynamicBytes:
"""arc4.DynamicBytes are essentially an arc4.DynamicArray[arc4.Bytes] and some convenience methods."""
dynamic_bytes = arc4.DynamicBytes(b"\xFF\xFF\xFF")

dynamic_bytes[0] = arc4.Byte(0)

return dynamic_bytes


class Todo(arc4.Struct):
task: arc4.String
completed: arc4.Bool
Expand Down Expand Up @@ -138,3 +213,22 @@ def return_todo(self, task: arc4.String) -> Todo:
assert exist

return todo_to_return


class Arc4Tuple(ARC4Contract):

@abimethod()
def arc4_tuple(
self,
a: arc4.Tuple[
arc4.UInt8, arc4.String, arc4.UInt64, arc4.DynamicArray[arc4.UInt32]
],
) -> arc4.String:
"""An arc4.Tuple is a heterogeneous collection of arc4 types."""

total = a[0].native + a[2].native

for x in a[3]:
total += x.native

return a[1]
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,3 @@ def deploy(
on_schema_break=algokit_utils.OnSchemaBreak.AppendApp,
on_update=algokit_utils.OnUpdate.AppendApp,
)
name = "world"
response = app_client.hello(name=name)
logger.info(
f"Called hello on {app_spec.contract.name} ({app_client.app_id}) "
f"with name={name}, received: {response.return_value}"
)
Loading