3

So I've written a small program that builds and deploys correctly. The initialize function also works.

But now I'm trying to create a Lottery, and TypeScript keeps repeating type error; the error message is:

/home/memewhales/solana_youtube_lottery/lottery-sol/node_modules/ts-node/src/index.ts:859 return new TSError(diagnosticText, diagnosticCodes, diagnostics); ^ TSError: ⨯ Unable to compile TypeScript: client/init2.ts:79:13 - error TS2345: Argument of type '{ lottery: PublicKey; master: PublicKey; authority: PublicKey; systemProgram: PublicKey; }' is not assignable to parameter of type 'ResolvedAccounts<{ name: "lottery"; writable: true; pda: { seeds: [{ kind: "const"; value: [108, 111, 116, 116, 101, 114, 121]; }, { kind: "account"; path: "master.last_id"; account: "master"; }]; }; } | { name: "master"; writable: true; pda: { ...; }; } | { ...; } | { ...; }>'. Object literal may only specify known properties, and 'lottery' does not exist in type 'ResolvedAccounts<{ name: "lottery"; writable: true; pda: { seeds: [{ kind: "const"; value: [108, 111, 116, 116, 101, 114, 121]; }, { kind: "account"; path: "master.last_id"; account: "master"; }]; }; } | { name: "master"; writable: true; pda: { ...; }; } | { ...; } | { ...; }>'.

79 lottery: lotteryPda, ~~~~~~~~~~~~~~~~~~~

Here's my function call on ts:

const ticketPrice = new anchor.BN(1);
// await program.methods.createLottery([]).accounts({
//     authority: masterPda
// }).signers([payer]).rpc();

await program.methods
    // Call the set_favorites instruction handler
    .createLottery(ticketPrice)
    .accounts({
        lottery: lotteryPda,
        master: masterPda,
        authority: payer.publicKey,
        systemProgram: programId
    })
    // Sign the transaction
    .signers([payer])
    .rpc()
// Send the transa

console.log('Lottery created with PDA: ', lotteryPda.toBase58());

Here's the type 'lottery_sol.ts':

"instructions": [ { "name": "createLottery", "discriminator": [ 242, 165, 247, 119, 17, 203, 21, 42 ], "accounts": [ { "name": "lottery", "writable": true, "pda": { "seeds": [ { "kind": "const", "value": [ 108, 111, 116, 116, 101, 114, 121 ] }, { "kind": "account", "path": "master.last_id", "account": "master" } ] } }, { "name": "master", "writable": true, "pda": { "seeds": [ { "kind": "const", "value": [ 109, 97, 115, 116, 101, 114 ] } ] } }, { "name": "authority", "writable": true, "signer": true }, { "name": "systemProgram", "address": "11111111111111111111111111111111" } ], "args": [ { "name": "ticketPrice", "type": "u64" } ] },

The names match up however I don't get this ' PublicKey; }' is not assignable to parameter of type 'ResolvedAccounts<{ name: "lottery"'

Thanks

1
  • I'm getting the value of lootery pda here: ' const [lotteryPda, lotteryBump] = await PublicKey.findProgramAddressSync( [Buffer.from(lotterySeed), Buffer.from((lastId + 1).toString())], programId ); ' Commented Jun 3 at 12:20

4 Answers 4

3

Edited to this

await program.methods
    // Call the set_favorites instruction handler
    .createLottery(ticketPrice)
    .accounts([
        { pubkey: lotteryPda, isSigner: false, isWritable: true },
        { pubkey: masterPda, isSigner: false, isWritable: true },
        { pubkey: payer.publicKey, isSigner: false, isWritable: true },
        { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
    ])
    // Sign the transaction
    .signers([payer])
    .rpc()
// Send the transa

and now works (tuple instead of []) but tbh didn't realized why

1

Don't know why object literial can't work. So declare a obj before using it like the following.

const [balanceAccount, bump] = PublicKey.findProgramAddressSync(
    [Buffer.from("balance_account"), provider.wallet.publicKey.toBuffer()],
    program.programId
);

// define an obj before using
let initAccounts = {
    balanceAccount,
    user: provider.wallet.publicKey,
    systemProgram: SystemProgram.programId,
};
const tx = program.methods.initialize().accounts(initAccounts);
0

You need to specify accountsStrict using program.methods.initialize().accountsStrict(initAccounts) for the types to be successfully instantiated on the tests.

This enforces strict type checking when running the tests and will solve the type errors you're getting from the looser accounts method.

0

This seems to be an idl conversion issue in typescript. Check your program idl for your workspace.

import {ProgramName} from "path/to/idl";

const mainProgram = anchor.workspace.ProgramName as Program<ProgramName>

Remove the type from Program and see if you still get the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.