4

Getting this error and I know its regarding rent is less or something like that, but how to solve this? by adding more tokens to the program id??

const tx = await program
      .methods
      .vote(true)
      .accounts(
        {
          vote: ammAccount.publicKey
        }
      )
      .signers([ammAccount])
      .rpc()

    console.log(tx)

   const tx1 = await program
      .methods
      .vote(false)
      .accounts(
        {
          vote: ammAccount.publicKey
        }
      )
      .signers([ammAccount])
      .rpc()

These are 2 transactions I am executing one after another on devnet

Program - https://github.com/sk1122/anchor-dex-solana/blob/main/programs/dex/src/lib.rs

gm!

2 Answers 2

1

PROBLEM SOLVED The maximum space limit in Solana is 10,000 bytes. You use 10240. I recommend that the account be PDA because it can be manipulated by you. (which is very insecure). You only store 2 u64 variables (2*8 = 16 bytes). Don't confuse account space with native variable storage. Here I leave a guide for the solana space: https://www.anchor-lang.com/docs/space

Here is your code working.

Create in test folder a "Account.ts", then add :

import * as anchor from "@project-serum/anchor";
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
export const wallet = provider.wallet as anchor.Wallet;

export const VoteAccount = new anchor.web3.PublicKey(
    "83xetaZnC4n37AxTwzUZXFnH8swQCyo9aQ8cypZh8bfA"
);

Create in test folder a "vote.ts", then add :

import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { Dex } from "../target/types/dex";
import { VoteAccount }  from "../tests/Account"

describe("Voting", () => {
    const provider = anchor.AnchorProvider.env();
    anchor.setProvider(provider);
    const program = anchor.workspace.Dex as Program<Dex>;
    const wallet = provider.wallet as anchor.Wallet;

    it('Vote sent successfully', async () => {
        const tx = await program.methods.vote(true)
        .accounts({
            vote: VoteAccount,
            signer: wallet.publicKey,
        })
        .rpc()
        const Account = await program.account.voteAccount.fetch(VoteAccount);
        console.log("----------------------------------------")
        console.log("Tx: ",tx)
        console.log("----------------------------------------")
        console.log("Votes Yes: ", Account.yes.toString())
        console.log("----------------------------------------")
        console.log("Votes No: ", Account.no.toString())
        console.log("----------------------------------------")
        console.log("Bump seed: ", Account.bump.toString())
        console.log("----------------------------------------")
        console.log("Authority: ", Account.authority.toBase58())
        console.log("----------------------------------------")
      }
    )
  }
)

OnChain Code:

use anchor_lang::{
    prelude::*,
    solana_program::pubkey::Pubkey,
};
use std::str::FromStr;

declare_id!("7SCwRKLhG4b4NzqDkKscihYH6vFXpRm9dW3nkQrwaPck");

#[program]
pub mod dex {
    use super::*;

    pub fn create_vote(
        ctx: Context<Create>
    ) 
    -> Result<()> {
        let (_vote_account, bump) = Pubkey::find_program_address(&[ctx.accounts.signer.key().as_ref()], &Pubkey::from_str("7SCwRKLhG4b4NzqDkKscihYH6vFXpRm9dW3nkQrwaPck").unwrap());
        let vote = &mut ctx.accounts.vote;
        vote.authority = ctx.accounts.signer.key();
        vote.bump = bump;
        Ok(())
    }
    pub fn vote(
        ctx: Context<Vote>, 
        vote: bool
    ) -> Result<()> {
        let vote_account = &mut ctx.accounts.vote;
        if vote == true {
            vote_account.yes += 1;
        } else {
            vote_account.no += 1;
        }
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Create<'info> {
    #[account(init, seeds = [signer.key().as_ref()], bump, payer = signer, space = 8 + 8 + 8 + 32 + 1)]
    pub vote: Account<'info, VoteAccount>,
    #[account(mut)]
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>
}
#[derive(Accounts)]
pub struct Vote<'info> {
    #[account(mut, seeds = [signer.key().as_ref()], bump = vote.bump)]
    pub vote: Account<'info, VoteAccount>,
    #[account(mut)]
    pub signer: Signer<'info>,
}

#[account]
pub struct VoteAccount {
    pub authority: Pubkey, // You
    pub bump: u8,
    pub yes: u64, // from 0 to 9223372036854775807
    pub no: u64 // from 0 to 9223372036854775807
}

#[error_code]
pub enum ErrCode {
    #[msg("Authority Key not equal to Swap Authority Key")]
    AuthorityKeyNotEqualToSwapAuthorityKey
}

dex.ts:

import { Dex } from "../target/types/dex";
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { PublicKey, Connection } from '@solana/web3.js'
const { SystemProgram } = anchor.web3;

describe("dex", () => {
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);
  const program = anchor.workspace.Dex as Program<Dex>;
  const wallet = provider.wallet as anchor.Wallet;

  it("Is initialized!", async () => {
    const sig = await provider.connection.requestAirdrop(
      wallet.publicKey,
      10000000
    )
    const [VoteAccount, _bump] = await PublicKey.findProgramAddress(
      [
        wallet.publicKey.toBuffer()
      ],
      program.programId
    )
    const tx = await program.methods.createVote()
      .accounts({
        vote: VoteAccount,
        signer: wallet.publicKey,
        systemProgram: SystemProgram.programId
      }).rpc()
      console.log("----------------------------------------")
      console.log("Tx: ", tx)
      console.log("----------------------------------------")
      console.log("Vote Account:", VoteAccount.toBase58());
      console.log("----------------------------------------")
    
  });
});

Cargo.toml:

[package]
name = "dex"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "dex"

[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []

[dependencies]
anchor-lang = "0.25.0"

Anchor.toml:

[features]
seeds = false
skip-lint = false

[programs.localnet]
dex = "BBf9Y9MctDrsip8ejGpfJhSXGacaYQEscesvAMVzKSsR"

[programs.devnet]
dex = "7SCwRKLhG4b4NzqDkKscihYH6vFXpRm9dW3nkQrwaPck"

[registry]
url = "https://api.apr.dev"

[provider]
cluster = "devnet"
wallet = "/home/mateo/.config/solana/id.json"

[scripts]
create = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/dex.ts"
vote =  "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/vote.ts"

Run anchor run create Then you take the publickey that appears in the terminal and paste it into account.ts. Then run anchor run vote

My GitHub: https://github.com/mateolafalce

2
  • Thanks for the help Mateo, so by using PDA, i am first initializing the account which I can then further use? but not understanding one thing, I am manually send vote account address, so it will know which account I am editing from, so it should update that account instead of reinitiating a new account? or it doesn't work that way Commented Sep 26, 2022 at 4:31
  • Of course!, the from account is variable, so any account can vote. Commented Sep 26, 2022 at 21:57
0

0x0 means that you're attempting to initialize an already initialized account. If tx initializes your vote account then tx1 fails because it tries to initialize it again.

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.