Abstracting Value through Resources

Resources are based on linear types, a concept popularised by Rust. Originally its own type in Dijets it was later changed and replaced with two 'abilities': Key and Store. Resource is meant to be a perfect type for storing digital assets, to achieve that it must be non-copyable and non-droppable. At the same time it must be storable and transferable between accounts.

Resource is type with a set of restrictions created to make this type ideally suited and perfectly safe enough to represent digital assets. This is precisely where the concept of encoding ownership of assets comes from. That being said, resource must meet the requirements for these assets: it cannot be copied nor can it be discarded or reused. This guide explains the basics of Resources and the best practices on implementing them in Dijets Methods.

We can start with how the restrictions (as a characteristic trait of a Resource) are implemented in the Rogue language.

Definition#

Resource is a struct that has only key and store abilities:

method M {
struct T has key, store {
field: u8
}
}

The Abilities of key and store#

Key ability allows struct to be used as a storage identifier. In other words, key is an ability to be stored as at top-level and be a storage; while store is the ability to be stored under key. You will see how it works in the next chapter. For now keep in mind that even primitive types have store ability - they can be stored, but yet they don't have key and cannot be used as a top-level containers.

Store ability allows value to be stored. That simple.

Resource in Rogue & Dijets#

Originally resource had its own type in Rogue, but as Rogue matuered in development and abilities were appended to it, it became a much more abstract concept that can be implemented with key and/or store abilities. And although Resource remains an abstraction based on its two abilities of key and store it can still be summarised as follows:

  1. A Resource is stored under account - therefore it exists only when assigned to an account; and can only be accessed through that account;
  2. Account can hold only one resource of one type, and this resource must have the key ability;
  3. Resource can't be copied nor dropped, it can only be stored.
  4. Resource value must be used. When a resource is created or taken from an account, it cannot be dropped and must either be stored or destructured.

Enough theory, let's get to action!