I have a design question about a SmartContract. I would like to create a endpoint that deals with SFT and NFT and must access to there attributes to compute the result. Basically, the user send two NFTs and, depends on there attributes, it will receive a new NFT or not. In both case, we return user's NFT.
In this case, is it possible to only send token identifiers to the endpoint instead of NFTs (via payable) and retreive NFT informations directly from the smart contract? It seems boilerplate and gaz consuming to send back NFT from a smart contract each time
In order to retrieve data encoded in the attributes field you have to decode that data back to a struct.
Let's say you have the struct YourStruct
defined as shown below:
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
pub struct YourStruct<M: ManagedTypeApi> {
pub name: ManagedBuffer<M>,
pub timestamp: u64,
pub amount: BigUint<M>,
}
Then to retrieve it, in your endpoint you would do something like this:
let nft_info = self.blockchain().get_esdt_token_data(
&self.blockchain().get_sc_address(),
&token_identifier,
token_nonce,
);
let attributes = nft_info.decode_attributes::<YourStruct<Self::Api>>()?;
For your second question, I think it depends on your particular use case. But it would be considered best practice to send those NFT tokens to your contract.