2

I am trying to decode attributes encoded in JSON in an Elrond rust smart contract. I am parsing it with the serde crate. My contract compiles well but when I deploy it, I got an invalid contract code.

Here's my Cargo.toml

[dependencies]
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

Here's the failing deploy tx: https://devnet-explorer.elrond.com/transactions/6579f00950eecec3f3e5280eda463d05e159f6000bf9603e6692a90abed04b0d

How can I handle JSON attributes on a rust Smart Contract, please?

Fargerik
  • 210
  • 2
  • 6

1 Answers1

2

I would strongly recommend against using json for your attributes.

The best way to handle it instead would be to make a struct that represents your attributes and use the serialization/deserialization elrond provides.

Not only would that simplify things a lot for you. But you would also need to store a lot less data in the nft, which would save you and your users a lot of gas. (And with the recent elrond upgrade which makes allocs a lot more expensive it would make a huge difference).

You can take a look here to see how the struct based approach would be handled in a smart contract.

Martin W
  • 733
  • 4
  • 12
  • Thank you for your answer! But how can I make marketplace-friendly attributes so? The most NFTs have attributes like "Hat:Pirate Hat;Background:red" but I have not human readable attributes. – Fargerik Mar 07 '22 at 10:18
  • For the purpose of NFTs, the attributes of the NFT itself should be stored in a .json file that's located somewhere in IPFS, then in the attributes of the NFT you should provide the hash of the IPFS link as metadata. The Elrond API is then able to crawl your IPFS hash and display the attributes of the NFT from the IPFS .json file. It's a tradeoff to avoid filling up the blockchain state with too much data, thus you store the data intensive stuff on IPFS. See: https://docs.elrond.com/developers/nft-tokens/#nftsft-fields – Brother Jder May 18 '22 at 08:35
  • Take a look at MOS-b9b4b2-270a attributes and try to reverse engineer it. You'll see that they have a field in the attributes called metadata:... The hash given to this field constitutes the IPFS hash to the .json file in which the NFT attributes are stored. The Elrond API then displays the attributes from this .json file automatically. – Brother Jder May 18 '22 at 08:49