|
| 1 | +from typing import Literal |
| 2 | + |
| 3 | +import beaker |
| 4 | +import pyteal as pt |
| 5 | + |
| 6 | + |
| 7 | +# util method for converting an Int to u16 bytes |
| 8 | +def to_u16(i: pt.Expr) -> pt.Expr: |
| 9 | + return pt.Suffix(pt.Itob(i), pt.Int(6)) |
| 10 | + |
| 11 | + |
| 12 | +def array_blueprint(app: beaker.Application) -> None: |
| 13 | + @app.external |
| 14 | + def sum_dynamic_array( |
| 15 | + v: pt.abi.DynamicArray[pt.abi.Uint64], *, output: pt.abi.Uint64 |
| 16 | + ) -> pt.Expr: |
| 17 | + """sums the array of ints""" |
| 18 | + return pt.Seq( |
| 19 | + # Use a scratch var to store the running sum |
| 20 | + (running_sum := pt.ScratchVar(pt.TealType.uint64)).store(pt.Int(0)), |
| 21 | + # Iterate over the elements of the array |
| 22 | + pt.For( |
| 23 | + (i := pt.ScratchVar()).store(pt.Int(0)), |
| 24 | + i.load() < v.length(), |
| 25 | + i.store(i.load() + pt.Int(1)), |
| 26 | + ).Do( |
| 27 | + # Access the element with square bracket annotation |
| 28 | + # and call `use` on it to use the value since its a |
| 29 | + # computed type like tuple elements |
| 30 | + v[i.load()].use( |
| 31 | + lambda val: running_sum.store(running_sum.load() + val.get()) |
| 32 | + ) |
| 33 | + ), |
| 34 | + # Set the value we're returning |
| 35 | + output.set(running_sum.load()), |
| 36 | + ) |
| 37 | + |
| 38 | + # While its not advisable to make heavy use dynamic ABI |
| 39 | + # types within the logic of the contract due to the inefficient |
| 40 | + # access to elements, below are some examples of how you |
| 41 | + # might construct a larger array from 2 smaller ones |
| 42 | + @app.external |
| 43 | + def concat_static_arrays( |
| 44 | + a: pt.abi.StaticArray[pt.abi.Uint64, Literal[3]], |
| 45 | + b: pt.abi.StaticArray[pt.abi.Uint64, Literal[3]], |
| 46 | + *, |
| 47 | + output: pt.abi.StaticArray[pt.abi.Uint64, Literal[6]], |
| 48 | + ) -> pt.Expr: |
| 49 | + # Static arrays are easy to concat since there is no |
| 50 | + # length prefix or offsets to track. The typing of the |
| 51 | + # value includes the length explicitly. |
| 52 | + return output.decode(pt.Concat(a.encode(), b.encode())) |
| 53 | + |
| 54 | + @app.external |
| 55 | + def concat_dynamic_arrays( |
| 56 | + a: pt.abi.DynamicArray[pt.abi.Uint64], |
| 57 | + b: pt.abi.DynamicArray[pt.abi.Uint64], |
| 58 | + *, |
| 59 | + output: pt.abi.DynamicArray[pt.abi.Uint64], |
| 60 | + ) -> pt.Expr: |
| 61 | + """demonstrate how two dynamic arrays of static elements could be concat'd""" |
| 62 | + # A Dynamic array of static types is encoded as: |
| 63 | + # [uint16 length, element 0, element 1] |
| 64 | + # so to concat them, we must remove the 2 byte length prefix |
| 65 | + # from each, and prepend the new length (of elements!) as 2 byte integer |
| 66 | + return output.decode( |
| 67 | + pt.Concat( |
| 68 | + pt.Suffix(pt.Itob(a.length() + b.length()), pt.Int(6)), |
| 69 | + pt.Suffix(a.encode(), pt.Int(2)), |
| 70 | + pt.Suffix(b.encode(), pt.Int(2)), |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + @app.external |
| 75 | + def concat_dynamic_string_arrays( |
| 76 | + a: pt.abi.DynamicArray[pt.abi.String], |
| 77 | + b: pt.abi.DynamicArray[pt.abi.String], |
| 78 | + *, |
| 79 | + output: pt.abi.DynamicArray[pt.abi.String], |
| 80 | + ) -> pt.Expr: |
| 81 | + """demonstrate how two dynamic arrays of dynamic elements could be concat'd""" |
| 82 | + # NOTE: this is not efficient (clearly), static types should |
| 83 | + # always be preferred if possible. Otherwise use some encoding |
| 84 | + # other than the abi encoding, which is more for serializing/deserializing data |
| 85 | + |
| 86 | + # A Dynamic array of dynamic types is encoded as: |
| 87 | + # [uint16 length, uint16 pos elem 0, uint16 pos elem 1, elem 0, elem 1] |
| 88 | + # so to concat them, we must remove the 2 byte length prefix |
| 89 | + # from each, and prepend the new length (of elements!) as 2 byte integer |
| 90 | + return pt.Seq( |
| 91 | + # Make a couple bufs for the header (offsets) and elements |
| 92 | + (_head_buf := pt.ScratchVar()).store( |
| 93 | + pt.Suffix(pt.Itob(a.length() + b.length()), pt.Int(6)) |
| 94 | + ), |
| 95 | + # Take the element contents of the 2 arrays |
| 96 | + (_tail_buf := pt.ScratchVar()).store( |
| 97 | + pt.Concat( |
| 98 | + # strip length and positions, now its [elem0, elem1, elem2] |
| 99 | + pt.Suffix(a.encode(), pt.Int(2) + (pt.Int(2) * a.length())), |
| 100 | + pt.Suffix(b.encode(), pt.Int(2) + (pt.Int(2) * b.length())), |
| 101 | + ) |
| 102 | + ), |
| 103 | + # Create the offset value we'll use for the position header |
| 104 | + # we know the first string will start at 2 * combined length |
| 105 | + (offset := pt.ScratchVar()).store(((a.length() + b.length()) * pt.Int(2))), |
| 106 | + # We'll track the current string we're working on here |
| 107 | + (curr_str_len := pt.ScratchVar()).store(pt.Int(0)), |
| 108 | + (cursor := pt.ScratchVar()).store(pt.Int(0)), |
| 109 | + pt.While( |
| 110 | + (cursor.load() + curr_str_len.load()) <= pt.Len(_tail_buf.load()) |
| 111 | + ).Do( |
| 112 | + # Add the offset for this string to the head buf |
| 113 | + _head_buf.store(pt.Concat(_head_buf.load(), to_u16(offset.load()))), |
| 114 | + # Get the length of the current string + 2 bytes for uint16 len |
| 115 | + curr_str_len.store( |
| 116 | + pt.ExtractUint16(_tail_buf.load(), cursor.load()) + pt.Int(2) |
| 117 | + ), |
| 118 | + # update our cursor to point to the next str element |
| 119 | + cursor.store(cursor.load() + curr_str_len.load()), |
| 120 | + # update our offset similarly |
| 121 | + offset.store(offset.load() + curr_str_len.load()), |
| 122 | + ), |
| 123 | + output.decode(pt.Concat(_head_buf.load(), _tail_buf.load())), |
| 124 | + ) |
0 commit comments