Skip to content

Commit 2ceaaeb

Browse files
committed
docs: Update program structure docs to include details of constant declarations and free subroutines
1 parent 6180d3c commit 2ceaaeb

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

docs/lg-program-structure.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ An Algorand TypeScript program is declared in a TypeScript module with a file ex
88

99
Algorand TypeScript constructs and types can be imported from the `@algorandfoundation/algorand-typescript` module, or one of its submodules. Compilation artifacts do not need to be exported unless you require them in another module; any non-abstract contract or logic signature discovered in your entry files will be output. Contracts and logic signatures discovered in non-entry files will not be output.
1010

11+
## Constants
12+
13+
Constants declared at the module level have a compile-time constant value or a template variable. Some basic expressions are supported so long as they result in a compile time constant.
14+
15+
```ts
16+
import { uint64 } from '@algorandfoundation/algorand-typescript'
17+
18+
const a: uint64 = 1000
19+
const b: uint64 = 2000
20+
const c: uint64 = a * b
21+
```
22+
23+
## Free Subroutines
24+
25+
Free subroutines can be declared at the module level and called from any contract, logic signature, or other subroutine. Subroutines do not have any compiler output on their own unless they are called by a contract or logic signature.
26+
27+
```ts
28+
import { uint64 } from '@algorandfoundation/algorand-typescript'
29+
30+
function add(a: uint64, b: uint64): uint64 {
31+
return a + b
32+
}
33+
```
34+
1135
## Contracts
1236

1337
A contract in Algorand TypeScript is defined by declaring a class which extends the `Contract`, or `BaseContract` types exported by `@algorandfoundation/algorand-typescript`. See [ABI routing](./abi-routing.md) docs for more on the differences between these two options.
@@ -217,7 +241,9 @@ class HelloWorldContract extends BaseContract {
217241

218242
# Logic Signatures
219243

220-
Logic signatures or smart signatures as they are sometimes referred to are single program constructs which can be used to sign transactions. If the logic defined in the program runs without error, the signature is considered valid - if the program crashes, or returns `0` or `false`, the signature is not valid and the transaction will be rejected. It is possible to delegate signature privileges for any standard account to a logic signature program such that any transaction signed with the logic signature program will pass on behalf of the delegating account provided the program logic succeeds. This is obviously a dangerous proposition and such a logic signature program should be meticulously designed to avoid abuse. You can read more about logic signatures on Algorand [here](https://dev.algorand.co/concepts/smart-contracts/logic-sigs/). Logic signature programs are stateless, and support a different subset of [op codes](https://dev.algorand.co/reference/algorand-teal/opcodes/) to smart contracts.
244+
Logic signatures or smart signatures as they are sometimes referred to are single program constructs which can be used to sign transactions. If the logic defined in the program runs without error, the signature is considered valid - if the program crashes, or returns `0` or `false`, the signature is not valid and the transaction will be rejected. It is possible to delegate signature privileges for any standard account to a logic signature program such that any transaction signed with the logic signature program will pass on behalf of the delegating account provided the program logic succeeds. This is obviously a dangerous proposition and such a logic signature program should be meticulously designed to avoid abuse. You can read more about logic signatures on Algorand [here](https://dev.algorand.co/concepts/smart-contracts/logic-sigs/).
245+
246+
Logic signature programs are stateless, and support a different subset of [op codes](https://dev.algorand.co/reference/algorand-teal/opcodes/) to smart contracts. The LogicSig class should only ever have a `program` method, complex signatures can make use of [free subroutines](#free-subroutines) to break up logic into smaller chunks.
221247

222248
```ts
223249
import { assert, LogicSig, Txn, Uint64 } from '@algorandfoundation/algorand-typescript'

0 commit comments

Comments
 (0)