Skip to content

Commit

Permalink
initial implementation dynamic esdts
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Nov 29, 2024
1 parent cbd9a3c commit 0499702
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 17 deletions.
6 changes: 6 additions & 0 deletions chain/core/src/builtin_func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ pub const SET_USERNAME_FUNC_NAME: &str = "SetUserName";
pub const MIGRATE_USERNAME_FUNC_NAME: &str = "migrateUserName";
pub const DELETE_USERNAME_FUNC_NAME: &str = "DeleteUserName";
pub const UPGRADE_CONTRACT_FUNC_NAME: &str = "upgradeContract";
pub const ESDT_SET_TOKEN_TYPE_FUNC_NAME: &str = "ESDTSetTokenType";
pub const ESDT_MODIFY_ROYALTIES_FUNC_NAME: &str = "ESDTModifyRoyalties";
pub const ESDT_SET_NEW_URIS_FUNC_NAME: &str = "ESDTSetNewURIs";
pub const ESDT_MODIFY_CREATOR_FUNC_NAME: &str = "ESDTModifyCreator";
pub const ESDT_METADATA_RECREATE_FUNC_NAME: &str = "ESDTMetaDataRecreate";
pub const ESDT_METADATA_UPDATE_FUNC_NAME: &str = "ESDTMetaDataUpdate";
47 changes: 46 additions & 1 deletion chain/core/src/types/flags/esdt_local_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const ESDT_ROLE_NFT_BURN: &str = "ESDTRoleNFTBurn";
const ESDT_ROLE_NFT_ADD_URI: &str = "ESDTRoleNFTAddURI";
const ESDT_ROLE_NFT_UPDATE_ATTRIBUTES: &str = "ESDTRoleNFTUpdateAttributes";
const ESDT_ROLE_TRANSFER: &str = "ESDTTransferRole";
const ESDT_ROLE_SET_NEW_URI: &str = "ESDTRoleSetNewURI";
const ESDT_ROLE_MODIFY_ROYALTIES: &str = "ESDTRoleModifyRoyalties";
const ESDT_ROLE_MODIFY_CREATOR: &str = "ESDTRoleModifyCreator";
const ESDT_ROLE_NFT_RECREATE: &str = "ESDTRoleNFTRecreate";
const ESDT_ROLE_NFT_UPDATE: &str = "ESDTRoleNFTUpdate";

#[derive(TopDecode, TopEncode, NestedDecode, NestedEncode, Clone, PartialEq, Eq, Debug, Copy)]
pub enum EsdtLocalRole {
Expand All @@ -25,6 +30,11 @@ pub enum EsdtLocalRole {
NftAddUri,
NftUpdateAttributes,
Transfer,
SetNewUri,
ModifyRoyalties,
ModifyCreator,
NftRecreate,
NftUpdate,
}

impl EsdtLocalRole {
Expand All @@ -39,6 +49,11 @@ impl EsdtLocalRole {
Self::NftAddUri => 6,
Self::NftUpdateAttributes => 7,
Self::Transfer => 8,
Self::SetNewUri => 9,
Self::ModifyRoyalties => 10,
Self::ModifyCreator => 11,
Self::NftRecreate => 12,
Self::NftUpdate => 13,
}
}

Expand All @@ -57,6 +72,11 @@ impl EsdtLocalRole {
Self::NftAddUri => ESDT_ROLE_NFT_ADD_URI,
Self::NftUpdateAttributes => ESDT_ROLE_NFT_UPDATE_ATTRIBUTES,
Self::Transfer => ESDT_ROLE_TRANSFER,
Self::SetNewUri => ESDT_ROLE_SET_NEW_URI,
Self::ModifyRoyalties => ESDT_ROLE_MODIFY_ROYALTIES,
Self::ModifyCreator => ESDT_ROLE_MODIFY_CREATOR,
Self::NftRecreate => ESDT_ROLE_NFT_RECREATE,
Self::NftUpdate => ESDT_ROLE_NFT_UPDATE,
}
}

Expand All @@ -71,13 +91,18 @@ impl EsdtLocalRole {
Self::NftAddUri => EsdtLocalRoleFlags::NFT_ADD_URI,
Self::NftUpdateAttributes => EsdtLocalRoleFlags::NFT_UPDATE_ATTRIBUTES,
Self::Transfer => EsdtLocalRoleFlags::TRANSFER,
Self::SetNewUri => EsdtLocalRoleFlags::SET_NEW_URI,
Self::ModifyRoyalties => EsdtLocalRoleFlags::MODIFY_ROYALTIES,
Self::ModifyCreator => EsdtLocalRoleFlags::MODIFY_CREATOR,
Self::NftRecreate => EsdtLocalRoleFlags::NFT_RECREATE,
Self::NftUpdate => EsdtLocalRoleFlags::NFT_UPDATE,
}
}
}

// TODO: can be done with macros, but I didn't find a public library that does it and is no_std
// we can implement it, it's easy
const ALL_ROLES: [EsdtLocalRole; 8] = [
const ALL_ROLES: [EsdtLocalRole; 13] = [
EsdtLocalRole::Mint,
EsdtLocalRole::Burn,
EsdtLocalRole::NftCreate,
Expand All @@ -86,6 +111,11 @@ const ALL_ROLES: [EsdtLocalRole; 8] = [
EsdtLocalRole::NftAddUri,
EsdtLocalRole::NftUpdateAttributes,
EsdtLocalRole::Transfer,
EsdtLocalRole::SetNewUri,
EsdtLocalRole::ModifyRoyalties,
EsdtLocalRole::ModifyCreator,
EsdtLocalRole::NftRecreate,
EsdtLocalRole::NftUpdate,
];

impl EsdtLocalRole {
Expand All @@ -106,6 +136,11 @@ impl From<u16> for EsdtLocalRole {
6 => Self::NftAddUri,
7 => Self::NftUpdateAttributes,
8 => Self::Transfer,
9 => Self::SetNewUri,
10 => Self::ModifyRoyalties,
11 => Self::ModifyCreator,
12 => Self::NftRecreate,
13 => Self::NftUpdate,
_ => Self::None,
}
}
Expand All @@ -130,6 +165,16 @@ impl<'a> From<&'a [u8]> for EsdtLocalRole {
Self::NftUpdateAttributes
} else if byte_slice == ESDT_ROLE_TRANSFER.as_bytes() {
Self::Transfer
} else if byte_slice == ESDT_ROLE_SET_NEW_URI.as_bytes() {
Self::SetNewUri
} else if byte_slice == ESDT_ROLE_MODIFY_ROYALTIES.as_bytes() {
Self::ModifyRoyalties
} else if byte_slice == ESDT_ROLE_MODIFY_CREATOR.as_bytes() {
Self::ModifyCreator
} else if byte_slice == ESDT_ROLE_NFT_RECREATE.as_bytes() {
Self::NftRecreate
} else if byte_slice == ESDT_ROLE_NFT_UPDATE.as_bytes() {
Self::NftUpdate
} else {
Self::None
}
Expand Down
5 changes: 5 additions & 0 deletions chain/core/src/types/flags/esdt_local_role_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ bitflags! {
const NFT_ADD_URI = 0b00100000;
const NFT_UPDATE_ATTRIBUTES = 0b01000000;
const TRANSFER = 0b10000000;
const SET_NEW_URI = 0b00000001_00000000;
const MODIFY_ROYALTIES = 0b00000010_00000000;
const MODIFY_CREATOR = 0b00000100_00000000;
const NFT_RECREATE = 0b00001000_00000000;
const NFT_UPDATE = 0b00010000_00000000;
}
}

Expand Down
30 changes: 29 additions & 1 deletion chain/core/src/types/flags/esdt_token_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ESDT_TYPE_FUNGIBLE: &[u8] = b"FungibleESDT";
const ESDT_TYPE_NON_FUNGIBLE: &[u8] = b"NonFungibleESDT";
const ESDT_TYPE_SEMI_FUNGIBLE: &[u8] = b"SemiFungibleESDT";
const ESDT_TYPE_META: &[u8] = b"MetaESDT";
const ESDT_TYPE_NON_FUNGIBLE_V2: &[u8] = b"NonFungibleESDTv2";
const ESDT_TYPE_DYNAMIC_NON_FUNGIBLE: &[u8] = b"DynamicNonFungibleESDT";
const ESDT_TYPE_DYNAMIC_SEMI_FUNGIBLE: &[u8] = b"DynamicSemiFungibleESDT";
const ESDT_TYPE_DYNAMIC_META: &[u8] = b"DynamicMetaESDT";
const ESDT_TYPE_INVALID: &[u8] = &[];

// Note: In the current implementation, SemiFungible is never returned
Expand All @@ -19,6 +23,10 @@ pub enum EsdtTokenType {
NonFungible,
SemiFungible,
Meta,
NonFungibleV2,
DynamicNFT,
DynamicSFT,
DynamicMeta,
Invalid,
}

Expand All @@ -37,7 +45,11 @@ impl EsdtTokenType {
Self::NonFungible => 1,
Self::SemiFungible => 2,
Self::Meta => 3,
Self::Invalid => 4,
Self::NonFungibleV2 => 4,
Self::DynamicNFT => 5,
Self::DynamicSFT => 6,
Self::DynamicMeta => 7,
Self::Invalid => 8,
}
}

Expand All @@ -47,6 +59,10 @@ impl EsdtTokenType {
Self::NonFungible => ESDT_TYPE_NON_FUNGIBLE,
Self::SemiFungible => ESDT_TYPE_SEMI_FUNGIBLE,
Self::Meta => ESDT_TYPE_META,
Self::NonFungibleV2 => ESDT_TYPE_NON_FUNGIBLE_V2,
Self::DynamicNFT => ESDT_TYPE_DYNAMIC_NON_FUNGIBLE,
Self::DynamicSFT => ESDT_TYPE_DYNAMIC_SEMI_FUNGIBLE,
Self::DynamicMeta => ESDT_TYPE_DYNAMIC_META,
Self::Invalid => ESDT_TYPE_INVALID,
}
}
Expand All @@ -60,6 +76,10 @@ impl From<u8> for EsdtTokenType {
1 => Self::NonFungible,
2 => Self::SemiFungible,
3 => Self::Meta,
4 => Self::NonFungibleV2,
5 => Self::DynamicNFT,
6 => Self::DynamicSFT,
7 => Self::DynamicMeta,
_ => Self::Invalid,
}
}
Expand All @@ -76,6 +96,14 @@ impl<'a> From<&'a [u8]> for EsdtTokenType {
Self::SemiFungible
} else if byte_slice == ESDT_TYPE_META {
Self::Meta
} else if byte_slice == ESDT_TYPE_NON_FUNGIBLE_V2 {
Self::NonFungibleV2
} else if byte_slice == ESDT_TYPE_DYNAMIC_NON_FUNGIBLE {
Self::DynamicNFT
} else if byte_slice == ESDT_TYPE_DYNAMIC_SEMI_FUNGIBLE {
Self::DynamicSFT
} else if byte_slice == ESDT_TYPE_DYNAMIC_META {
Self::DynamicMeta
} else {
Self::Invalid
}
Expand Down
82 changes: 82 additions & 0 deletions framework/base/src/contract_base/wrappers/send_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::marker::PhantomData;

use multiversx_chain_core::types::EsdtTokenType;

use crate::codec::Empty;

use crate::types::ManagedRef;
Expand Down Expand Up @@ -763,4 +765,84 @@ where
.nft_update_attributes(token_id, nft_nonce, new_attributes)
.sync_call()
}

/// Sets the token type for a specific token.
pub fn esdt_set_token_type(&self, token_id: &TokenIdentifier<A>, token_type: EsdtTokenType) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_set_token_type(token_id, token_type)
.sync_call()
}

/// Modifies royalties for a specific token.
pub fn esdt_modify_royalties(
&self,
token_id: &TokenIdentifier<A>,
nonce: u64,
new_royalty: u64,
) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_modify_royalties(token_id, nonce, new_royalty)
.sync_call()
}

/// Sets new uris for a specific token.
pub fn esdt_nft_set_new_uris(
&self,
token_id: &TokenIdentifier<A>,
nonce: u64,
uris: &ManagedVec<A, ManagedBuffer<A>>,
) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_nft_set_new_uris(token_id, nonce, uris)
.sync_call()
}

/// Changes the creator of a specific token into the caller.
pub fn esdt_nft_modify_creator(&self, token_id: &TokenIdentifier<A>, nonce: u64) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_nft_modify_creator(token_id, nonce)
.sync_call()
}

/// Recreates an ESDT token with the newly specified attributes.
pub fn esdt_metadata_recreate<T: codec::TopEncode>(
&self,
token_id: TokenIdentifier<A>,
nonce: u64,
new_attributes: &T,
) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_metadata_recreate(token_id, nonce, new_attributes)
.sync_call()
}

/// Updates an ESDT token with the newly specified attributes.
pub fn esdt_metadata_update<T: codec::TopEncode>(
&self,
token_id: &TokenIdentifier<A>,
nonce: u64,
new_attributes: &T,
) {
Tx::new_tx_from_sc()
.to(ToSelf)
.gas(GasLeft)
.typed(system_proxy::UserBuiltinProxy)
.esdt_metadata_update(token_id, nonce, new_attributes)
.sync_call()
}
}
Loading

0 comments on commit 0499702

Please sign in to comment.