Skeleton and Rust Architecture
In this article, you'll learn about the basic architecture behind the FT contract that you'll develop while following this "Zero to Hero" series. You'll discover the contract's layout and you'll see how the Rust files are structured in order to build a feature-complete smart contract.
If you are new to Rust and want to dive into smart contract development, our Quick-start guide is a great place to start.
Introduction
This tutorial presents the code skeleton for the FT smart contract and its file structure. You'll find how all the functions are laid out as well as the missing Rust code that needs to be filled in. Once every file and function has been covered, you'll go through the process of building the mock-up contract to confirm that your Rust toolchain works as expected.
Files structure
The repository comes with many different folders. Each folder represents a different milestone of this tutorial starting with the skeleton folder and ending with the finished contract folder. If you step into any of these folders, you'll find that they each follow a regular Rust project. The file structure for these smart contracts have:
Cargo.toml
file to define the code dependencies (similar topackage.json
in JavaScript and node projects)src
folder where all the Rust source files are storedtarget
folder where the compiledwasm
will output to.
Source files
File | Description |
---|---|
ft_core.rs | Contains the logic for transferring and controlling FTs. This file represents the implementation of the core standard. |
lib.rs | Holds the smart contract initialization functions and dictates what information is kept on-chain. |
metadata.rs | Defines the metadata structure. This file represents the implementation of the metadata extension of the standard. |
storage.rs | Contains the logic for registration and storage. This file represents the implementation of the storage management standard. |
skeleton
├── Cargo.lock
├── Cargo.toml
└── src
├── ft_core.rs
├── lib.rs
├── metadata.rs
└── storage.rs
Explore the code in our GitHub repository.
ft_core.rs
Core logic that allows you to transfer FTs between users and query for important information.
Method | Description |
---|---|
ft_transfer | Transfers a specified amount of FTs to a receiver ID. |
ft_transfer_call | Transfers a specified amount of FTs to a receiver and attempts to perform a cross-contract call on the receiver’s contract to execute the ft_on_transfer method. The implementation of this ft_on_transfer method is up to the contract writer. You’ll see an example implementation in the marketplace section of this tutorial. Once ft_on_transfer finishes executing, ft_resolve_transfer is called to check if things were successful or not. |
ft_total_supply | Returns the total amount of fungible tokens in circulation on the contract. |
ft_balance_of | Returns how many fungible tokens a specific user owns. |
ft_on_transfer | Method that lives on a receiver's contract. It is called when FTs are transferred to the receiver's contract account via the ft_transfer_call method. It returns how many FTs should be refunded back to the sender. |
ft_resolve_transfer | Invoked after the ft_on_transfer is finished executing. This function will refund any FTs not used by the receiver contract and will return the net number of FTs sent to the receiver after the refund (if any). |
Loading...
You'll learn more about these functions in the circulating supply and transfers sections of the tutorial series.
lib.rs
This file outlines what information the contract stores and keeps track of.
Method | Description |
---|---|
new_default_meta | Initializes the contract with default metadata so the user doesn't have to provide any input. In addition, a total supply is passed in which is sent to the owner. |
new | Initializes the contract with the user-provided metadata and total supply. |
The initialization functions (new
, new_default_meta
) can only be called once.
Loading...
You'll learn more about these functions in the define a token section of the tutorial series.
metadata.rs
This file is used to outline the metadata for the Fungible Token itself. In addition, you can define a function to view the contract's metadata which is part of the standard's metadata extension.
Name | Description |
---|---|
FungibleTokenMetadata | This structure defines the metadata for the fungible token. |
ft_metadata | This function allows users to query for the token's metadata. |
Loading...