anchor icon indicating copy to clipboard operation
anchor copied to clipboard

Error: Type `BTreeMap<u8,u64>` not found in IDL.

Open tpkeeper opened this issue 1 year ago • 5 comments

⚠️ Don't use issues for support questions ("How does x feature work in Anchor? How to do x in Anchor?"). Use the discord instead https://discord.gg/NHHGSXAnXk (but don't ping maintainers). ⚠️

anchor account xxx return errors: Error: Type BTreeMap<u8,u64> not found in IDL.

tpkeeper avatar Dec 13 '23 06:12 tpkeeper

I too am facing this.

AlgoAlchemist avatar Dec 18 '23 11:12 AlgoAlchemist

@acheroncrypto any technical reason this can't be a thing? Considering bountying it out.

jacobcreech avatar Jun 07 '24 21:06 jacobcreech

The reason for not supporting BTreeMap (or any of the map data structures) has to do with the nature of Solana programming. Beginners usually try to use a global map for everything (especially eth people), which is almost always a worse choice than using PDAs because:

  • PDAs can scale practically to infinity meanwhile map data structures are heavily limited
  • Map data structures need a lot of data for their algorithm to have any meaningful performance improvement impact which will never be the case because of the on-chain memory constraints
  • Mutable global data is an antipattern especially due to write-locks for the writable accounts

If you really want to use a mapped data structure, you can implement a struct with id field (or whatever you want the data to be keyed by):

struct MyStruct {
    id: u16
    // Data ...
}

and use it as Vec<MyStruct which will give you even more flexibility (e.g. multiple keys) than BTreeMap.

acheroncrypto avatar Jun 08 '24 06:06 acheroncrypto

The reason for not supporting BTreeMap (or any of the map data structures) has to do with the nature of Solana programming. Beginners usually try to use a global map for everything (especially eth people), which is almost always a worse choice than using PDAs

While I do agree it is almost always a worse choice, I would still advocate for including being able to have them generated in the IDL, along with sets and hashmaps. Otherwise you lead to a bad developer experience with anyone attempting to use them and later running into issues with them. Same for the few that want to use a BTreeMap, Hashmap, or Set and understand Solana enough to know the alternatives.

A compromise may be to allow the ability to use them in the IDL while presenting a warning stating exactly what you just said on build. We could even help write up something in the docs to link to with the warning so it is clear to anyone using a BTreeMap, Hashmap, or Set.

WDYT about this compromise?

jacobcreech avatar Jun 11 '24 02:06 jacobcreech

I'm not opposed, but I still consider this a footgun.

acheroncrypto avatar Jun 12 '24 07:06 acheroncrypto