Skip to content

Commit

Permalink
Merge pull request #4 from algorandfoundation/arc4-ints-bytes
Browse files Browse the repository at this point in the history
Arc4 ints bytes
  • Loading branch information
iskysun96 authored Nov 5, 2024
2 parents 23d9aed + 0c4c588 commit 45da0f8
Show file tree
Hide file tree
Showing 14 changed files with 2,329 additions and 18 deletions.
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

0 comments on commit 45da0f8

Please sign in to comment.