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