Coding-and-Crypto / Rust-Solana-Tutorial

Examples from Rust Solana Tutorial on YouTube!
Apache License 2.0
108 stars 66 forks source link

The program expected this account to be already initialized. #3

Open tungla20 opened 1 year ago

tungla20 commented 1 year ago

I'm following your code but when I do anchor build, the compiler return this error caused by mint, ownerTokenAccount and buyerTokenAccount. Here is my code:

it("Is initialized!", async () => {
    //create connection to devnet
    const connection = new Connection(
      clusterApiUrl('devnet'),
      'confirmed'
    );
    let minter = await splToken.createMint(connection, wallet.payer, wallet.publicKey, null, 1)
    const tokenAccount = await getOrCreateAssociatedTokenAccount(
      connection,
      wallet.payer,
      minter,
      wallet.publicKey
    )
    await mintTo(
      connection,
      wallet.payer,
      minter,
      tokenAccount.address,
      wallet.publicKey,
      1
    )
    const saleAmount = 1;
    const buyer: anchor.web3.Keypair = await createKeypairFromFile(__dirname + "/keypairs/buyer1.json");
    console.log(`Buyer public key: ${buyer.publicKey}`);
    const buyerTokenAddress = await anchor.utils.token.associatedAddress({
      mint: minter,
      owner: buyer.publicKey,
    });
    console.log(`Request to sell NFT: ${minter} for ${saleAmount} lamports.`);
    console.log(`Owner's Token Address: ${tokenAccount}`);
    console.log(`Buyer's Token Address: ${buyerTokenAddress}`);

    await program.methods.buyNft(
      new anchor.BN(saleAmount)
    )
      .accounts({
        mint: minter,
        ownerTokenAccount: tokenAccount.address,
        ownerAuthority: wallet.publicKey,
        buyerTokenAccount: buyerTokenAddress,
        buyerAuthority: buyer.publicKey,
      })
      .signers([buyer])
      .rpc();

and this is the code in my contract:

pub fn buy_nft(
        ctx: Context<BuyNft>,
        // amount: Option<u64>,
        lamports: Option<u64>,
    ) -> Result<()> {
        system_program::transfer(
            CpiContext::new(
                ctx.accounts.system_program.to_account_info(),
                system_program::Transfer {
                    from: ctx.accounts.buyer_authority.to_account_info(),
                    to: ctx.accounts.owner_authority.to_account_info(),
                },
            ),
            lamports.unwrap(),
        )?;

        associated_token::create(CpiContext::new(
            ctx.accounts.owner_token_account.to_account_info(),
            associated_token::Create {
                payer: ctx.accounts.buyer_authority.to_account_info(),
                associated_token: ctx.accounts.buyer_token_account.to_account_info(),
                authority: ctx.accounts.buyer_authority.to_account_info(),
                mint: ctx.accounts.mint.to_account_info(),
                system_program: ctx.accounts.system_program.to_account_info(),
                token_program: ctx.accounts.token_program.to_account_info(),
            },
        ))?;

        token::transfer(
            CpiContext::new(
                ctx.accounts.token_program.to_account_info(),
                token::Transfer {
                    from: ctx.accounts.owner_token_account.to_account_info(),
                    to: ctx.accounts.buyer_token_account.to_account_info(),
                    authority: ctx.accounts.owner_authority.to_account_info(),
                },
            ),
            1,
        )?;

        Ok(())
    }

In my version, the rent param in associated_token::Create is not existed so I remove it. So can you help me fixing this bug? Thanks