metaplex-foundation / solita

Genrates an SDK API from solana contract IDL.
Apache License 2.0
140 stars 32 forks source link

createInstruction throwing "Cannot read properties of undefined (reading 'length')" #47

Closed CalebEverett closed 2 years ago

CalebEverett commented 2 years ago
    const args: AdminSettingsArgs = {
        platform: platform.publicKey,
    };

    const [adminSettings] = await TokenProgram.findAdminAddress();
    const data = AdminSettings.fromArgs(args);

    const ix = createCreateAdminSettingsInstruction(
        { payer: platform.publicKey, adminSettings } as CreateAdminSettingsInstructionAccounts,
        { data } as CreateAdminSettingsInstructionArgs
    );
 not ok 1 TypeError: Cannot read properties of undefined (reading 'length')
  ---
    operator: error
    stack: |-
      TypeError: Cannot read properties of undefined (reading 'length')
          at Object.write (@metaplex-foundation/beet/src/beets/collections.ts:39:15)
          at BeetWriter.write (@metaplex-foundation/beet/src/read-write.ts:39:10)
          at BeetWriter.writeStruct (@metaplex-foundation/beet/src/read-write.ts:47:6)
          at BeetStruct.serialize (@metaplex-foundation/beet/src/struct.ts:104:12)
          at BeetStruct.write (@metaplex-foundation/beet/src/struct.ts:63:42)
          at BeetWriter.write (@metaplex-foundation/beet/src/read-write.ts:39:10)
          at BeetWriter.writeStruct (@metaplex-foundation/beet/src/read-write.ts:47:6)
          at BeetArgsStruct.serialize (@metaplex-foundation/beet/src/struct.ts:104:12)
          at createCreateAdminSettingsInstruction (js/test/src/generated/instructions/createAdminSettings.ts:68:44)
          at Test.<anonymous> (js/test/test/account.promo.test.ts:70:16)

I may not be providing the arguments the right way, but the types were all ok.

I tracked it down to the following in BeetWriter:

    writeStruct(instance, fields) {
        for (const [key, beet] of fields) {
            const value = instance[key];
            console.log("here", instance, key, value);
            this.write(beet, value);
        }
    }

Here is what gets logged to the console:

here AdminSettings {
  platform: PublicKey8 {
    _bn: <BN: cb6382c18030e5a671517906e664009111da0b2122215f167233be9bca36f51c>
  }
} accountDiscriminator undefined

It looked like the instance in this case was AdminSettings and that it didn't include a key for accountDiscriminator.

Thanks for taking a look.

austbot commented 2 years ago

@thlorenz this seems like a weird issue coming from the generator. @VadimGrozinok

thlorenz commented 2 years ago

I can have a look, but would need more to go on. Do you have a repo where this generated code lives? Also was the IDL generated by shank or anchor?

CalebEverett commented 2 years ago

I didn't have the correct idl generator specified in .solitarc.js.