In the intricate tapestry of TON’s FunC contracts, the dictionary (dict) stands as a venerable sage, a repository of knowledge that keeps track of countless entries and their corresponding narratives. Today, we embark on a journey to unlock the secrets of this sagacious entity, exploring its role in the realm of smart contracts.
The Genesis of a Dictionary
Our quest begins with the creation of a dictionary. In the vast expanse of TON’s digital landscape, this is akin to planting a seed that will grow into a mighty tree, its branches adorned with the names of those who have left their mark. Each name is a unique identifier, a leaf on the tree that bears the imprint of its owner.
global cell data::claimed;
Here, we sow the seed of our dictionary, data::claimed
, a global variable that will serve as the guardian of all claims within our contract.
The Keepers of Knowledge
To interact with our dictionary, we must conjure methods that act as the keepers of knowledge. load_data
and save_data
are the gatekeepers, responsible for the safekeeping of the dictionary’s contents, ensuring that the wisdom within is preserved for posterity.
() load_data() impure inline {
// ... logic for summoning the dictionary's knowledge
}
() save_data() impure inline {
// ... logic for preserving the dictionary's knowledge
}
These methods are akin to the opening and closing of ancient tomes, allowing us to access and update the dictionary’s contents as needed.
The Gateway to our Realm
The recv_internal
method serves as the gateway to our contract, a portal through which messages must pass to be heard. It is the sentry that stands guard, scrutinizing each missive to determine its worthiness.
() recv_internal(int msg_value, cell in_msg_full, slice in_msg_body) impure {
// ... logic for the sentry's scrutiny
}
This sentry checks for the presence of an empty message, the correctness of flags, and the validity of the sender’s address. Only those messages that pass this trial are granted an audience within our realm.
Welcoming New Claimants
Now, let us learn how to welcome new claimants into our dictionary. This is akin to the arrival of a stranger in a distant village, where their name is etched into the village’s ledger, marking their presence and their claim.
data::claimed~udict_set(256, slice_hash(sender_address), begin_cell().store_uint(1, 1).end_cell().begin_parse());
data::size += 1;
save_data();
With the udict_set
method, we inscribe the sender’s address, transformed by the alchemy of hashing, into our dictionary. We also update the data::size
to reflect the growth of our village, and call upon save_data
to ensure that this new knowledge is eternalized.
The Seers of the Dictionary
To peer into the depths of our dictionary, we invoke the seers get_size
and get_value
. These methods allow us to gaze upon the number of claims within our realm and to discern if a specific address has already laid claim to a piece of our digital land.
int get_size() method_id {
// ... logic for the seer to count the claims
}
int get_value(slice address) method_id {
// ... logic for the seer to search for a claim
}
Through the eyes of these seers, we can effortlessly manage and query the contents of our dictionary, much like a sage who can instantly retrieve ancient wisdom from the annals of time.
The Legacy of the Dictionary
In the world of TON’s FunC contracts, the dictionary is a cornerstone of knowledge, a testament to the ingenuity of its creators. It is a tool that enables us to navigate the complex tapestry of data relationships with ease and precision. Through our journey today, we have uncovered the secrets of this ancient sage, equipping us with the knowledge to harness its power and explore the vast expanse of smart contracts.
As we venture forth, let us remember the lessons of the dictionary, a guide that will light our path as we delve deeper into the mysteries of the TON blockchain. May its wisdom be our compass, its knowledge our shield, and its power our sword as we embark on a new era of digital exploration.
The Dictionary in Practice
To truly grasp the power of the dictionary in TON’s FunC contracts, let us consider a practical example. Imagine a decentralized application that requires a system to manage user profiles. Each profile is unique, with attributes such as a username, a profile picture, and a list of friends. The dictionary becomes the backbone of this system, allowing us to store and retrieve user profiles with unparalleled efficiency.
In our hypothetical decentralized application, the dictionary would be utilized to store user profiles, with each user’s address serving as a unique key. When a new user joins the platform, their profile information is hashed and stored in the dictionary using the udict_set
method. This process ensures that each user’s data is securely and uniquely identified within the system.
() create_profile(cell user_profile) impure {
// Hash the user's address to create a unique key
var user_address = /* retrieve user's address */;
var key = slice_hash(user_address);
// Store the user's profile in the dictionary
data::claimed~udict_set(256, key, user_profile);
save_data();
}
The create_profile
function, outlined above, represents the process of adding a new user profile to the dictionary. It takes a cell
containing the user’s profile data, hashes the user’s address to create a unique key, and then stores this data in the dictionary. The save_data
method is called to ensure that this new entry is persistence.
To retrieve a user’s profile, we would implement a function that uses the udict_get
method to access the dictionary. This function would take the user’s address as input, hash it to find the corresponding key, and then retrieve the associated profile data.
(cell) get_profile(slice user_address) impure {
// Hash the user's address to find the key
var key = slice_hash(user_address);
// Retrieve the user's profile from the dictionary
(_, cell user_profile?) = data::claimed~udict_get(256, key);
return user_profile?;
}
The get_profile
function, as illustrated above, retrieves a user’s profile from the dictionary. It hashes the user’s address to find the correct key and then uses the udict_get
method to retrieve the profile data associated with that key.
The Versatility of the Dictionary
The dictionary in TON’s FunC contracts is not limited to simple key-value pairs. It can store complex data structures, making it a powerful tool for a wide range of applications. For instance, in a decentralized finance (DeFi) application, the dictionary could be used to manage user balances, token allowances, or even complex financial contracts.
The dictionary’s ability to handle complex data is due to its underlying data type, the cell
. In TON, a cell
is a versatile data container that can hold up to 1023 bytes of data. This allows for the storage of various data types, including integers, strings, and other cell
structures. When used in conjunction with the dictionary, cell
enables the creation of sophisticated data models that can be efficiently stored and retrieved.
Conclusion
The dictionary in TON’s FunC contracts is a powerful and flexible tool for managing data in a smart contract environment. Its ability to store and retrieve complex data structures makes it an indispensable asset for developers looking to create robust and efficient decentralized applications.
In this tutorial, we have explored the basics of creating and using a dictionary in TON’s FunC contracts. We have learned how to add and retrieve data, and we have seen how the dictionary can be applied in a practical scenario. As we continue to explore the capabilities of TON’s smart contracts, the dictionary will be a constant companion, aiding us in our quest to build innovative and scalable decentralized applications.