Rise In Logo



Build on Aptos

In this lesson, we will break down the vault smart contract.

The contract allows for token deposits, allocations, claims, and withdrawals, with specific access controls.

Key Concepts

Resource Account

In Aptos, a resource account is a special type of account that's typically used to manage resources on behalf of a module. Think of it as a "smart contract account" that can hold assets and execute transactions, but is controlled programmatically rather than by a user with a private key. In your contract, you are creating an access for this resource account for admin, where the admin can sign the transfers on behalf of the vault. You do this with the vault signer.

Events

Events in smart contracts are a way to emit information about important occurrences within the contract. They serve as a log that can be easily queried off-chain.

Signer Capability

A signer capability is a way to delegate the ability to sign transactions on behalf of an account. It's like giving someone power of attorney, but in a very controlled, programmable way.

Contract Structure

Structs

1. Vault: The main struct that holds the vault's state.

2. VaultSignerCapability: Holds the signer capability for the vault's resource account.

3. Event structs: AllocationMadeEvent, AllocationClaimedEvent, TokensDepositedEvent, TokensWithdrawnEvent.

Key Functions

1. init_module: Initializes the vault.

2. deposit_tokens: Allows the admin to deposit tokens into the vault.

3. allocate_tokens: Allows the admin to allocate tokens to specific addresses.

4. claim_tokens: Allows users to claim their allocated tokens.

5. withdraw_tokens: Allows the admin to withdraw unallocated tokens.

View Functions

1. get_balance: Returns the total balance of the vault.

2. get_total_allocated: Returns the total amount of allocated tokens.

3. get_allocation: Returns the allocation for a specific address.

Initialization

The init_module function sets up the vault. It creates a resource account, which acts as the vault's "bank account". This account can hold and transfer tokens, but it's controlled by the smart contract logic, not by a user with a private key.

Depositing Tokens

Only the admin can deposit tokens into the vault. This increases the total balance of the vault.

Allocating Tokens

The admin can allocate tokens to specific addresses. This doesn't transfer tokens, but rather reserves them for future claims. It's like putting a label on some of the tokens saying "reserved for Alice".

Claiming Tokens

When a user claims their tokens, the contract checks if they have an allocation, and if so, transfers the tokens to them. This is like going to the bank and withdrawing money that was set aside for you.

Withdrawing Tokens

The admin can withdraw any tokens that haven't been allocated. This ensures that only truly "free" tokens can be withdrawn.

Security Measures

1. Access Control: Many functions can only be called by the admin.

2. Balance Checks: The contract ensures it never tries to transfer more tokens than it has.

3. Allocation Tracking: The contract carefully tracks allocated vs. unallocated tokens.

This vault contract provides a flexible system for managing token distributions. It allows for controlled deposit, allocation, and withdrawal of tokens, with clear access controls and event logging. Understanding this contract will provide you insight into key Aptos concepts like resource accounts, signer capabilities, and event emission.

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!