|
| 1 | +import type { Account, bytes, uint64 } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { Bytes, Global, LocalState, Txn, arc4, assert, contract } from '@algorandfoundation/algorand-typescript' |
| 3 | + |
| 4 | +/** |
| 5 | + * A contract demonstrating local storage functionality. |
| 6 | + * This contract shows how to use local state storage in an Algorand smart contract, |
| 7 | + * including initialization, reading, writing, and clearing of local state values. |
| 8 | + * Local state is per-account storage that requires accounts to opt-in before use. |
| 9 | + * |
| 10 | + * @stateTotals.localBytes - 4 bytes allocated for local byte storage |
| 11 | + * @stateTotals.localUints - 3 uints allocated for local integer storage |
| 12 | + */ |
| 13 | +@contract({ stateTotals: { localBytes: 4, localUints: 3 } }) |
| 14 | +export default class LocalStorage extends arc4.Contract { |
| 15 | + // example: INIT_LOCAL_STATE |
| 16 | + public localInt = LocalState<uint64>({ key: 'int' }) |
| 17 | + public localIntNoDefault = LocalState<uint64>() |
| 18 | + public localBytes = LocalState<bytes>() |
| 19 | + public localString = LocalState<string>() |
| 20 | + public localBool = LocalState<boolean>() |
| 21 | + public localAccount = LocalState<Account>() |
| 22 | + // example: INIT_LOCAL_STATE |
| 23 | + |
| 24 | + // example: OPT_IN_TO_APPLICATION |
| 25 | + /** |
| 26 | + * Initializes local state values when an account opts into the application. |
| 27 | + * This method can only be called during an OptIn transaction. |
| 28 | + * Sets initial values for all local state variables: |
| 29 | + * - localInt: 100 |
| 30 | + * - localIntNoDefault: 200 |
| 31 | + * - localBytes: 'Silvio' |
| 32 | + * - localString: 'Micali' |
| 33 | + * - localBool: true |
| 34 | + * - localAccount: sender's address |
| 35 | + */ |
| 36 | + @arc4.abimethod({ allowActions: 'OptIn' }) |
| 37 | + public optInToApplication(): void { |
| 38 | + this.localInt(Txn.sender).value = 100 |
| 39 | + this.localIntNoDefault(Txn.sender).value = 200 |
| 40 | + this.localBytes(Txn.sender).value = Bytes('Silvio') |
| 41 | + this.localString(Txn.sender).value = 'Micali' |
| 42 | + this.localBool(Txn.sender).value = true |
| 43 | + this.localAccount(Txn.sender).value = Txn.sender |
| 44 | + } |
| 45 | + // example: OPT_IN_TO_APPLICATION |
| 46 | + |
| 47 | + // example: READ_LOCAL_STATE |
| 48 | + /** |
| 49 | + * Reads and returns all local state values for the transaction sender. |
| 50 | + * @returns A tuple containing: |
| 51 | + * - [0] uint64: The value of localInt |
| 52 | + * - [1] uint64: The value of localIntNoDefault |
| 53 | + * - [2] bytes: The value of localBytes |
| 54 | + * - [3] string: The value of localString |
| 55 | + * - [4] boolean: The value of localBool |
| 56 | + * - [5] Address: The value of localAccount converted to Address type |
| 57 | + */ |
| 58 | + @arc4.abimethod({ readonly: true }) |
| 59 | + public readLocalState(): [uint64, uint64, bytes, string, boolean, arc4.Address] { |
| 60 | + const sender = Txn.sender |
| 61 | + // Convert Account reference type to native Address type for return value |
| 62 | + const accountAddress = new arc4.Address(this.localAccount(sender).value) |
| 63 | + |
| 64 | + return [ |
| 65 | + this.localInt(sender).value, |
| 66 | + this.localIntNoDefault(sender).value, |
| 67 | + this.localBytes(sender).value, |
| 68 | + this.localString(sender).value, |
| 69 | + this.localBool(sender).value, |
| 70 | + accountAddress, |
| 71 | + ] |
| 72 | + } |
| 73 | + // example: READ_LOCAL_STATE |
| 74 | + |
| 75 | + // example: WRITE_LOCAL_STATE |
| 76 | + /** |
| 77 | + * Updates multiple local state values for the transaction sender. |
| 78 | + * Requires the account to be opted into the application. |
| 79 | + * @param valueString - New string value to store |
| 80 | + * @param valueBool - New boolean value to store |
| 81 | + * @param valueAccount - New account address to store |
| 82 | + */ |
| 83 | + @arc4.abimethod() |
| 84 | + public writeLocalState(valueString: string, valueBool: boolean, valueAccount: Account): void { |
| 85 | + // Dynamic keys must be explicitly reserved in the contract's stateTotals configuration |
| 86 | + const sender = Txn.sender |
| 87 | + |
| 88 | + assert(sender.isOptedIn(Global.currentApplicationId), 'Account must opt in to contract first') |
| 89 | + |
| 90 | + this.localString(sender).value = valueString |
| 91 | + this.localBool(sender).value = valueBool |
| 92 | + this.localAccount(sender).value = valueAccount |
| 93 | + |
| 94 | + assert(this.localString(sender).value === valueString) |
| 95 | + assert(this.localBool(sender).value === valueBool) |
| 96 | + assert(this.localAccount(sender).value === valueAccount) |
| 97 | + } |
| 98 | + // example: WRITE_LOCAL_STATE |
| 99 | + |
| 100 | + // example: WRITE_DYNAMIC_LOCAL_STATE |
| 101 | + /** |
| 102 | + * Writes a value to local state using a dynamic key. |
| 103 | + * Demonstrates dynamic key-value storage in local state. |
| 104 | + * @param key - The dynamic key to store the value under |
| 105 | + * @param value - The string value to store |
| 106 | + * @returns The stored string value |
| 107 | + */ |
| 108 | + @arc4.abimethod() |
| 109 | + public writeDynamicLocalState(key: string, value: string): string { |
| 110 | + const sender = Txn.sender |
| 111 | + |
| 112 | + assert(sender.isOptedIn(Global.currentApplicationId), 'Account must opt in to contract first') |
| 113 | + |
| 114 | + const localDynamicAccess = LocalState<string>({ key }) |
| 115 | + |
| 116 | + localDynamicAccess(sender).value = value |
| 117 | + |
| 118 | + assert(localDynamicAccess(sender).value === value) |
| 119 | + |
| 120 | + return localDynamicAccess(sender).value |
| 121 | + } |
| 122 | + // example: WRITE_DYNAMIC_LOCAL_STATE |
| 123 | + |
| 124 | + // example: READ_DYNAMIC_LOCAL_STATE |
| 125 | + /** |
| 126 | + * Reads a value from local state using a dynamic key. |
| 127 | + * @param key - The dynamic key to read the value from |
| 128 | + * @returns The stored string value for the given key |
| 129 | + */ |
| 130 | + @arc4.abimethod() |
| 131 | + public readDynamicLocalState(key: string): string { |
| 132 | + const sender = Txn.sender |
| 133 | + |
| 134 | + assert(sender.isOptedIn(Global.currentApplicationId), 'Account must opt in to contract first') |
| 135 | + |
| 136 | + const localDynamicAccess = LocalState<string>({ key }) |
| 137 | + |
| 138 | + assert(localDynamicAccess(sender).hasValue, 'Key not found') |
| 139 | + |
| 140 | + return localDynamicAccess(sender).value |
| 141 | + } |
| 142 | + // example: READ_DYNAMIC_LOCAL_STATE |
| 143 | + |
| 144 | + // example: CLEAR_LOCAL_STATE |
| 145 | + /** |
| 146 | + * Clears all local state values for the transaction sender. |
| 147 | + * After calling this method, all local state values will be deleted. |
| 148 | + */ |
| 149 | + @arc4.abimethod() |
| 150 | + public clearLocalState(): void { |
| 151 | + const sender = Txn.sender |
| 152 | + |
| 153 | + assert(sender.isOptedIn(Global.currentApplicationId), 'Account must opt in to contract first') |
| 154 | + |
| 155 | + this.localInt(sender).delete() |
| 156 | + this.localIntNoDefault(sender).delete() |
| 157 | + this.localBytes(sender).delete() |
| 158 | + this.localString(sender).delete() |
| 159 | + this.localBool(sender).delete() |
| 160 | + this.localAccount(sender).delete() |
| 161 | + } |
| 162 | + // example: CLEAR_LOCAL_STATE |
| 163 | +} |
0 commit comments