Handle JSON attributes in rust Smart Contract

Viewed 142

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?

1 Answers

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.

Related