Resource Creation in Detail
  • 01 Jul 2024
  • 3 Minutes to read
  • Dark
    Light
  • PDF

Resource Creation in Detail

  • Dark
    Light
  • PDF

Article summary

The ResourceBuilder

The ResourceBuilder is your utility for creating new resources. You have a number of resource creation methods to make things easy.

You can start using it like this ResourceBuilder::new_ then complete the building process using either create_with_no_initial_supply() or mint_initial_supply(..).

It’s important to note that create_with_no_initial_supply() will return a ResourceManager where as mint_initial_supply(..) will return a bucket with the created supply of resources.

New Resource Methods

Method

Arguments

Returns

new_fungible()

owner_role : OwnerRole

InProgressResourceBuilder<FungibleResourceType>

new_string_non_fungible()

  • owner_role : OwnerRole

  • <NonFungibleData>

InProgressResourceBuilder<NonFungibleResourceType<StringNonFungibleLocalId, D>>

new_integer_non_fungible()

  • owner_role : OwnerRole

  • <NonFungibleData>

InProgressResourceBuilder<NonFungibleResourceType<IntegerNonFungibleLocalId, D>>

new_bytes_non_fungible()

  • owner_role : OwnerRole

  • <NonFungibleData>

InProgressResourceBuilder<NonFungibleResourceType<BytesNonFungibleLocalId, D>>

new_ruid_non_fungible()

  • owner_role : OwnerRole

  • <NonFungibleData>

InProgressResourceBuilder<NonFungibleResourceType<RUIDNonFungibleLocalId, D>>

There are also a number of helper methods to be aware of which you will commonly chain together with one of the new_ methods above

Method

Arguments

Description

.divisibility(number)

number

The divisibility is a number between 0 and 18 and it represents the number of decimal places that this resource can be split into. For example, if you set the divisibility to 0, people will only be able to send whole amounts of that resource.

Default: 18 You can also use the constants DIVISIBILITY_NONE and DIVISIBILITY_MAXIMUM

.metadata(metadata!)

metadata!

Specify metadata on the resource. This is where you define data that will be displayed on the wallets and explorers, for example. You can find a standard for the metadata keys here.

Note: You can include multiple metadata by calling this method multiple times.

.mint_roles(mint_roles!)

mint_roles!

Specify the AccessRule for MintRoles to provide permission to mint tokens.

.burn_roles(burn_roles!)

burn_roles!

Specify the AccessRule for BurnRoles to provide permission to burn tokens.

.withdraw_roles(withdraw_roles!)

withdraw_roles!

Specify the AccessRule for WithdrawRoles to provide permission to withdraw tokens from a Vault.

.deposit_roles(deposit_roles!)

deposit_roles!

Specify the AccessRule for DepositRoles to provide permission to deposit tokens to a Vault.

.recall_roles(recall_roles!)

recall_roles!

Specify the AccessRule for RecallRoles to provide permission to freeze tokens.

.freezer_roles(freezer_roles!)

freezer_roles!

Specify the AccessRule for FreezeRoles to provide permission to freeze tokens.

More details regarding how to use these methods to set authorization rules on your resources is in the Resource Behaviors coming up next.

Here is an example creating a very simple fungible resource:

let my_token: FungibleBucket = ResourceBuilder::new_fungible(OwnerRole::None)
    .metadata(metadata!(
        init {
            "name" => "My Token", locked;
            "symbol" => "TKN", locked;
        }
    ))
    .divisibility(DIVISIBILITY_NONE)
    .mint_initial_supply(100);

The Non-Fungible variants also require a struct of NonFungibleData as you can see below:

// The top-level struct must derive NonFungibleData.
// Note that marking top-level fields as `#[mutable]` means that the data under
// that field can be updated by the `resource_manager.update_non_fungible_data(...)` method.
//
// All types referenced directly/indirectly also need to derive ScryptoSbor.
// To work with the Manifest Builder, we recommend all types also derive ManifestSbor.
#[derive(ScryptoSbor, ManifestSbor, NonFungibleData)]
struct GameData {
  team_one: String,
  team_two: String,
  section: String,
  seat_number: u16,
  #[mutable]
  promo: Option<String>,
}

#[blueprint]
mod nftblueprint {
    struct NftBlueprint {
    }

    impl NftBlueprint {
        pub fn create_game_nfts() {
            ResourceBuilder::new_integer_non_fungible::<GameData>(OwnerRole::None)
                .metadata(metadata! {
                     init {
                       "name" => "Mavs vs Lakers - 12/25/2023", locked;
                       "description" => "Tickets to the 2023 season of the Dallas Mavericks", locked;
                     }
                 )
                .create_with_no_initial_supply();
        }
    }
}

The ResourceManager

When a resource is created on the Radix network, a “Resource Manager” with a unique address will be associated with it. This manager contains the data of this resource such as its type (fungible or non-fungible), its metadata and its supply among other things. It also allows people with the right authority to do actions on the resource like minting more tokens and updating its metadata. On this page, we will show you how to use the ResourceManager type that Scrypto offers and the various methods you can call on it.

Methods available on ResourceManager

The ResourceManager offers many methods that we listed in the following tables.

Fetching resource information

Method

Description

get_metadata(key)

Returns the metadata associated with the specified key.

resource_type()

Returns a ResourceType that is either non-fungible or fungible.

total_supply()

Returns the total supply of the resource.

non_fungible_exists(NonFungibleLocalId)

Returns whether a token with the specified non-fungible id was minted.

get_non_fungible_data(NonFungibleLocalId)

Returns the data associated with a particular non-fungible token of this resource.

Applying actions

Provided that the correct authority is presented (more information about this here), you can apply the following actions to a resource.

Method

Description

set_metadata(key, value)

Updates a metadata key associated with this resource.

mint(amount)

Mints an amount of fungible tokens.

Note: The resource must be of the fungible type.

mint_non_fungible(&id, data)

Mints a non-fungible token with the specified id and data.

Note: The resource must be of the non-fungible type

update_non_fungible_data(&id, field_name, new_data)

Updates a single field of the non-fungible data associated with a minted non-fungible token of the specified ID.

Updating resource flags

More information on access rules and resource flags here.

Method

Description

set_mintable(access_rule)

Sets the access rule for being able to update the mintable flag.

lock_mintable()

Locks the mintable flag.

set_burnable(access_rule)

Sets the access rule for being able to update the burnable flag.

lock_burnable()

Locks the burnable flag.

set_withdrawable(access_rule)

Sets the access rule for being able to update the withdrawable flag.

lock_withdrawable()

Locks the withdrawable flag.

set_depositable(access_rule)

Sets the access rule for being able to update the depositable flag.

lock_depositable()

Locks the depositable flag.

set_recallable(access_rule)

Sets the access rule for being able to update the recallable flag.

lock_recallable()

Locks the recallable flag.

set_updatable_metadata(access_rule)

Sets the access rule for being able to update the updateable_metadata flag.

lock_updatable_metadata()

Locks the updateable_metadata flag.

set_updatable_non_fungible_data(access_rule)

Sets the access rule for being able to update the updateable_non_fungible_data flag.

lock_updatable_non_fungible_data()

Locks the updateable_non_fungible_data flag.

Related Rust docs


Was this article helpful?