Now, we will add the Context in order to use related account with our instruction
#[derive(Accounts)]
pub struct InitVoteAccount<'info> {
#[account(
init,
payer = signer,
space = 8 + 1 + 8 + 8 + 8,
)]
pub vote_account: Account<'info, VoteAccount>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
The #[derive(Accounts)] attribute macro is used to automatically generate the accounts-based Solana instructions needed for the smart contract. InitVoteAccount is the name of the struct that will be defined, and <'info> is a lifetime specifier that indicates a reference to some data that lives for the duration of the contract.
#[derive(Accounts)]
pub struct InitVoteAccount<'info> {
The #[account] attribute macro is used to define an account in the Solana blockchain. The init parameter indicates that this is an initializer account and will be used to create a new account on the blockchain. The payer parameter specifies the signer that will pay for the account creation fees. The space parameter specifies the size of the account in bytes. 8 for account info, 1 for boolean field(we will see) and 3 x 8 is for three u64 field which are pattaya, phuket and krabi :)
#[account(init, payer = signer, space = 8 + 1 + 8 + 8 + 8,)]
This line defines a public variable named vote_account of type Account. The <'info, VoteAccount> specifies the lifetime of the reference and the account data type. The Account struct is provided by the Solana framework and contains the data and metadata for an account on the blockchain.
pub vote_account: Account<'info, VoteAccount>,
This line defines a public variable named signer of type Signer. The #[account(mut)] attribute indicates that this account can be modified. The Signer struct is also provided by the Solana framework and is used to verify that a given account has signed the transaction.
#[account(mut)]
pub signer: Signer<'info>,
This line defines a public variable named system_program of type Program. The Program struct is another Solana framework struct and represents a program that is deployed to the blockchain. The System type parameter specifies the program ID for the System program, which is a built-in program in Solana that provides basic system functions like account creation and transfer.
pub system_program: Program<'info, System>,