Evolution from TEAL to TypeScript
Traditional Algorand smart contracts use TEAL, a low-level stack-based language.
New TypeScript-based frameworks allow writing smart contracts with familiar syntax:
Key Components
Core Class Structure
A contract in Algorand TypeScript is defined by declaring a class that extends the `Contract` type exported by `@algorandfoundation/algorand-typescript`. Contracts which extend the `Contract` type are ARC4 compatible contracts. Any `public` methods on the class will be exposed as ABI methods, callable from other contracts and off-chain clients. `private` and `protected` methods can only be called from within the contract itself, or its subclasses. Note that TypeScript methods are `public` by default if no access modifier is present. For more information, see Program Structure documentation
Example: Hello World Contract
import { Contract } from '@algorandfoundation/algorand-typescript'
/**
* An abstract base class for a simple example contract
*/
abstract class Intermediate extends Contract {
/**
* sayBananas method
* @returns The string "Bananas"
*/
public sayBananas(): string {
return `Bananas`
}
}
/**
* A simple hello world example contract
*/
export default class HelloWorld extends Intermediate {
/**
* sayHello method
* @param firstName The first name of the person to greet
* @param lastName THe last name of the person to greet
* @returns The string "Hello {firstName} {lastName"}
*/
public sayHello(firstName: string, lastName: string): string {
const result = `Hello ${firstName} ${lastName}`
return result
}
}
Global Storage
Global or Application storage is a key/value store of bytes or uint64 values stored against a smart contract application. The number of values used must be declared when the application is first created and will affect the minimum balance requirement for the application. For ARC4 contracts this information is captured in the ARC32 and ARC56 specification files and automatically included in deployments.
import {
GlobalState,
Contract,
uint64,
bytes,
Uint64,
contract,
} from '@algorandfoundation/algorand-typescript';
class DemoContract extends Contract {
// The property name 'globalInt' will be used as the key
globalInt = GlobalState<uint64>({ initialValue: Uint64(1) });
// Explicitly override the key
globalBytes = GlobalState<bytes>({ key: 'alternativeKey' });
}
// If using dynamic keys, state must be explicitly reserved
@contract({ stateTotals: { globalBytes: 5 } })
class DynamicAccessContract extends Contract {
test(key: string, value: string) {
// Interact with state using a dynamic key
const dynamicAccess = GlobalState<string>({ key });
dynamicAccess.value = value;
}
}
If you want more information about Storage, check out this document.
Example: Counter Contract
import { Contract, abimethod, GlobalState, Uint64 } from '@algorandfoundation/algorand-typescript'
import type { uint64 } from '@algorandfoundation/algorand-typescript'
/**
* A contract that increments a counter
*/
export default class Counter extends Contract {
public counter = GlobalState<uint64>({ initialValue: Uint64(0) })
/**
* Increments the counter and returns the new value
* @returns The new counter value
*/
@abimethod()
public increment(): uint64 {
this.counter.value = this.counter.value + 1
return this.counter.value
}
}
ARC4 Contract
Contracts which extend the Contract type are ARC4 compatible contracts. Any public methods on the class will be exposed as ABI methods, callable from other contracts and off-chain clients. private and protected methods can only be called from within the contract itself, or its subclasses. Note that TypeScript methods are public by default if no access modifier is present. A contract is considered valid even if it has no methods, though its utility is questionable.
import { Contract } from '@algorandfoundation/algorand-typescript';
class DoNothingContract extends Contract {}
class HelloWorldContract extends Contract {
sayHello(name: string) {
return `Hello ${name}`;
}
}
Best Practices
For more information, see the Algorand TypeScript documentation.
Swap insights and ask questions about “Build on Algorand”.
Ask a question or share your thoughts about this lesson.