Rise In Logo

Build on Algorand

Basic Smart Contract with Typescript

Basic Smart Contract with Typescript

Now, let's create the exact same "Counter" contract using TEALScript to see the similarities and differences.

Objective:

  • Write a smart contract using TypeScript syntax
  • Understand the TEALScript class structure
  • Build, deploy, and interact with the contract using the same AlgoKit workflow

Step 1: Initialize the Project

  • In your terminal, navigate to a new directory for this project.
  • Initialize a new AlgoKit project: algokit init
  • This time, when prompted for a template, select tealscript. Accept the defaults for the rest of the setup.
  • AlgoKit will configure a Node.js environment, install dependencies, and create the necessary files.

Step 2: Write the Smart Contract Code

Now, let's proceed with creating the Counter contract using TEALScript.

  • Open contracts/your-project-name.algo.ts: After initializing your project with algokit init and selecting the tealscript template, AlgoKit will create a file in the contracts/ directory. The filename will typically be your-project-name.algo.ts (e.g., my-tealscript-app.algo.ts). Open this file in VS Code or your preferred code editor. It will likely contain some boilerplate "Hello World" code.
  • Delete Existing Content: Clear out all the current content within this .algo.ts file.

Replace with the Counter Contract Code: Paste the following TEALScript code into your contracts/your-project-name.algo.ts file:

import { Contract, GlobalStateKey, UInt64 } from '@algorandfoundation/tealscript';

/**
 * @title CounterApp
 * @notice A simple counter smart contract allowing incrementing, decrementing, and reading a count.
 */
class CounterApp extends Contract {
 /**
  * @notice The main state variable for our counter. It's a global state key holding a UInt64.
  * This means its value will be stored directly on the Algorand blockchain, accessible by the application.
  */
 count = GlobalStateKey<UInt64>();

 /**
  * @notice The constructor method for the CounterApp contract.
  * This method is automatically called by the `@create` decorator when the application is first created on the Algorand blockchain.
  * It initializes the `count` state variable to 0.
  */
 @create
 createApplication(): void {
  this.count.set(0);
 }

 /**
  * @notice Increments the counter by 1.
  * The `@external` decorator makes this method callable from outside the smart contract,
  * typically by a user sending an Application Call transaction.
  */
 @external
 increment(): void {
  this.count.set(this.count.get() + 1);
 }

 /**
  * @notice Decrements the counter by 1.
  * Includes a basic check to prevent the counter from going below zero.
  * The `@external` decorator enables external calls.
  */
 @external
 decrement(): void {
  if (this.count.get() > 0) {
   this.count.set(this.count.get() - 1);
  }
 }

 /**
  * @notice Returns the current value of the counter.
  * The `@external` decorator makes it callable.
  * The `returns` type hint specifies that this method will return a UInt64.
  * Since this method only reads the state and doesn't modify it,
  * it can often be queried off-chain without needing a signed transaction, improving efficiency.
  */
 @external
 getCounter(): UInt64 {
  return this.count.get();
 }
}

Breakdown of the Counter Contract Code (TEALScript):

import { Contract, GlobalStateKey, UInt64 } from '@algorandfoundation/tealscript';:

  • This line imports the necessary components from the @algorandfoundation/tealscript library.
  • Contract: The base class that all TEALScript smart contracts must extend.
  • GlobalStateKey<T>: A utility to define a global application state variable of a specific type T. This variable will be stored directly on the blockchain as part of the application's state.
  • UInt64: Represents an unsigned 64-bit integer, a common data type for quantities and counters on Algorand.

class CounterApp extends Contract { ... }: This defines your smart contract as a TypeScript class named CounterApp, inheriting from the Contract base class provided by TEALScript.

count = GlobalStateKey<UInt64>();:

  • This is how you define a state variable in TEALScript.
  • count is the name of your state variable.
  • GlobalStateKey<UInt64>() declares that count is a global application state key that will store a UInt64 value. This means its value persists on the blockchain.

@create Decorator:

  • This decorator marks the createApplication() method as the application creation handler.
  • This method is executed only once, when the smart contract is first deployed (created) on the blockchain.
  • this.count.set(0); initializes the count state variable to 0 when the contract is created.

@external Decorator:

  • Similar to Beaker's @external, this decorator signifies that a method can be called externally by users or other smart contracts via an Application Call transaction.

increment(): void { ... }:

  • This method increases the count by 1.
  • this.count.get() retrieves the current value of the count variable from the on-chain state.
  • this.count.set(...) updates the count variable with the new value.

decrement(): void { ... }:

  • Similar to increment, but it decreases the count by 1.
  • The if (this.count.get() > 0) check is a simple way to prevent the counter from going into negative numbers.

getCounter(): UInt64 { ... }:

  • This method returns the current value of the count.
  • It's marked with @external to be callable.
  • The return type UInt64 is explicitly specified, leveraging TypeScript's type safety.
  • TEALScript automatically optimizes read-only external methods so that they can often be called efficiently without incurring transaction costs on the chain when used for simple queries.

By replacing the existing boilerplate with this CounterApp code, you've now defined your Algorand smart contract using TEALScript. The next steps in the AlgoKit workflow would typically involve building (compiling) this TEALScript code into TEAL, deploying it to your local test network (AlgoKit Sandbox), and then interacting with its methods using client-side code (likely in TypeScript or JavaScript for a TEALScript project).


Comments

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!

Disclaimer: The information, programs, and events provided on https://risein.com is strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice, nor do we make any representations regarding the value, profitability, or future price of any blockchain or cryptocurrency. Users are encouraged to conduct their own research and consult with licensed financial professionals before engaging in any investment activities. https://risein.com disclaims any responsibility for financial decisions made by users based on the information provided here.