Rise In Logo

Learn everything about Aptos

Smart Contract Demonstration Part-1

Smart Contract Demonstration

Writing a Smart Contract for Aptos – Introduction to The Vault

Welcome, everyone.

In previous sessions, we explored the fundamentals of the Move language. In this segment, we’ll shift our focus to writing a smart contract designed for the Aptos blockchain.

Overview of the Project

A project structure has already been set up, and in this walkthrough, we’ll build the contract step-by-step. The smart contract we’re developing is called The Vault (VA). Here's how it works:

  • The admin assigns token allocations to user addresses.
  • Users can claim tokens according to their allocations.
  • The admin can only withdraw the portion of tokens that has not been allocated.

For instance, if the Vault holds 100 tokens and 20 tokens are allocated, the admin is only allowed to withdraw the remaining 80 tokens.

Key Components Used

The contract will incorporate the following components:

  • Signer and Coin modules, specifically the Aptos coin.
  • Event handles, which notify the frontend of actions taken within the contract.
  • A dedicated Vault account, giving the Vault a unique on-chain identity.
  • Tables, which are similar to vectors but support key-value pairs for more structured data management.

Error Handling with Codes

To ensure clarity and ease of maintenance, we’ll define error codes for common issues:

  • Unauthorized access – triggered if the transaction sender is not the admin.
  • Insufficient balance – if the contract doesn't hold enough tokens.
  • No allocation – if a user tries to claim tokens without a valid allocation.

These error codes will be stored as constants, making them easier to manage and update across the entire contract.

Events Defined in the Contract

The contract will emit four main events:

  1. AllocationMadeEvent – when the admin allocates tokens to an address.
  2. ClaimEvent – when a user claims their allocated tokens.
  3. TokenDepositEvent – when tokens are deposited into the Vault.
  4. WithdrawEvent – when the admin withdraws unallocated tokens.

Vault Struct and Storage Strategy

The Vault struct is the backbone of the contract. It includes:

  • The admin's address.
  • The Vault's own address.
  • A table of allocations, mapping:
  • Address (key) ➝ Allocation amount (u64) (value).
  • The total allocated token amount.
  • The Vault’s total token balance.
  • Event handles to log on-chain actions.

This design ensures all relevant data is encapsulated within a single struct, making the contract efficient and easy to manage.

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 / events provided on https://patika.dev and https://risein.com are strictly for upskilling and networking purposes related to the technical infrastructure of blockchain platforms. We do not provide financial or investment advice and do not 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://patika.dev and https://risein.com disclaim any responsibility for financial decisions made by users based on information provided here.