coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.68k stars 1.35k forks source link

Error: User defined types not provided #785

Open naveed949 opened 3 years ago

naveed949 commented 3 years ago

while testing program, getting following error. CMD: anchor test ErrorLog:

0 passing (10s) 1 failing

1) blindbox Is initialized!: Error: User defined types not provided at Function.fieldLayout (node_modules/@project-serum/anchor/dist/cjs/coder/idl.js:96:31) at typeDef.type.fields.map (node_modules/@project-serum/anchor/dist/cjs/coder/idl.js:126:36) at Array.map () at Function.typeDefLayout (node_modules/@project-serum/anchor/dist/cjs/coder/idl.js:125:54) at AccountsCoder.idl.accounts.map (node_modules/@project-serum/anchor/dist/cjs/coder/accounts.js:20:46) at Array.map () at new AccountsCoder (node_modules/@project-serum/anchor/dist/cjs/coder/accounts.js:19:38) at new Coder (node_modules/@project-serum/anchor/dist/cjs/coder/index.js:32:25) at new Program (node_modules/@project-serum/anchor/dist/cjs/program/index.js:54:23) at fs.readdirSync.forEach (node_modules/@project-serum/anchor/dist/cjs/workspace.js:71:44) at Array.forEach () at Object.get (node_modules/@project-serum/anchor/dist/cjs/workspace.js:64:39) at Context.it (tests/blindbox.js:16:38) at processImmediate (internal/timers.js:443:21)

  After debugging I've found following struct is causing issue.
  IDL json formatt:
  `{
  "name": "Boxes",
  "type": {
    "kind": "struct",
    "fields": [
      {
        "name": "authority",
        "type": "publicKey"
      },
      {
        "name": "boxes",
        "type": {
          "defined": "HashMap<u32,NftBox>"
        }
      }
    ]
  }
} `

Program version of same struct:
`

[account]

pub struct Boxes {

pub authority: Pubkey, pub boxes: HashMap<u32, NftBox>, } `

Note: I've also tried BTreeMap instead of HashMap but getting same issue. Please help me

armaniferrante commented 3 years ago

HashMaps aren't currently supported in the IDL.

naveed949 commented 3 years ago

okay @armaniferrante can you please tell what collections are implemented from std::collections as I need to store key-value data in my program ? or please suggest me any for this purpose.

armaniferrante commented 3 years ago

You can approximate a map by using program-derived addresses PDAs. Seeds are your keys, values are your accounts.

naveed949 commented 3 years ago

Trying to understand @armaniferrante For example: I want to store n number of Boxes in my account where each Box is a kind of NFT and identified by its ID. so how will an PDA will store these all n number of Boxes. OR are you saying I'll need n number of PDAs to store 1 Box per PDA. As for now what I can understand one PDA/account can store only one Box, due to the unavailability of map.

please correct me if missing anything