Scratch Storage
A scratch space is a temporary storage area used to store values for later use in your program. It consists of 256 scratch slots. Scratch spaces may be uint64 or byte-array and are initialized as uint64(0).
The AVM (Algorand Virtual Machine) enables smart contracts to use scratch space for temporarily storing values during execution. Other contracts in the same atomic transaction group can read this scratch space. However, contracts can only access scratch space from earlier transactions within the same group, not from later ones.
TEAL
In TEAL, you can use the store
/stores
and load
/loads
opcodes to read and write scratch slots. Additionally, you can use gload
/gloads
to read scratch slots from earlier transactions in the same group.
Algorand Python
In Algorand Python, you can directly use these opcodes through the op.Scratch
class. The accessed scratch slot indices or index ranges need to be declared using the scratch_slots
variable during contract declaration.
1from algopy import ARC4Contract, Bytes, UInt64, arc4, op, urange2
3
4class HelloWorld(ARC4Contract, scratch_slots=(1, urange(3, 20))):5 @arc4.abimethod()6 def hello(self) -> bool:7 op.Scratch.store(1, UInt64(5))8 op.Scratch.store(7, Bytes(b"Hello World"))9
10 assert op.Scratch.load_uint64(1) == UInt64(5)11 assert op.Scratch.load_bytes(7) == b"Hello World"12
13 return True
1hello:2 int 53 store 14
5 byte 0x48656c6c6f20576f726c646 store 77
8 load 19 int 510 ==11 assert12
13 load 714 byte 0x48656c6c6f20576f726c6415 ==16 assert17
18 int 1