this is the full program:
use anchor_lang::solana_program::system_program;
use sha256::digest;
declare_id!("H5m1vHR3aHFDctkPZkZRmdecKq2Z5piHBovRaUDHBbd5");
#[program]
pub mod spinner {
use super::*;
// initial spin creation, account is created in order to story spin data.
pub fn first_spin(ctx: Context<CreateSpin>) -> ProgramResult {
let spin: &mut Account<Spin> = &mut ctx.accounts.spin;
let author: &Signer = &ctx.accounts.author;
let clock: Clock = Clock::get().unwrap();
let v = get_spin_vector(clock.slot.to_string(), clock.unix_timestamp.to_string(), author.key.to_string());
spin.author = *author.key;
spin.timestamp = clock.unix_timestamp;
spin.results = v;
msg!("first spin for wallet [{}] spin results [{:?}]", &spin.author, &spin.results);
Ok(())
}
pub fn spin(ctx: Context<UpdateSpin>, spin_amount: u64) -> ProgramResult {
let clock: Clock = Clock::get().unwrap();
//let spin = &mut ctx.accounts.spin;
let payer_info: &mut Account<Spin> = &mut ctx.accounts.spin;
let jackpot = &mut ctx.accounts.jackpot;
let author = &ctx.accounts.author;
let program_info = &ctx.accounts.system_program;
// payer_info.timestamp = clock.unix_timestamp;
// payer_info.results = get_spin_vector(clock.slot.to_string(), clock.unix_timestamp.to_string(), author.key.to_string());
// msg!("spin for existing wallet [{}] spin results [{:?}]", &payer_info.author, &payer_info.results);
//
let transfer_instruction = anchor_lang::solana_program::system_instruction::transfer(
&payer_info.key(),
&jackpot.key(),
spin_amount,
);
msg!("transfering to [{}] from [{}]", &jackpot.key(), &payer_info.key());
msg!("meta data jackpot[{}] data payer [{}]", &jackpot., &payer_info.key());
let required_accounts_for_create = &[
payer_info.to_account_info().clone(),
jackpot.clone(),
program_info.clone()
][..];
invoke(&transfer_instruction, required_accounts_for_create)?;
Ok(())
}
}
#[derive(Accounts)]
pub struct CreateSpin<'info> {
#[account(init, payer = author, space = Spin::LEN)]
pub spin: Account<'info, Spin>,
#[account(mut)]
pub author: Signer<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
#[derive(Accounts)]
pub struct UpdateSpin<'info> {
#[account(mut)]
pub spin: Account<'info, Spin>,
#[account(mut)]
pub jackpot: AccountInfo<'info>,
pub author: AccountInfo<'info>,
#[account(address = system_program::ID)]
pub system_program: AccountInfo<'info>,
}
#[account]
pub struct Spin {
pub author: Pubkey,
pub timestamp: i64,
pub results: Vec<Vec<u32>>,
}
impl Spin {
const LEN: usize = DISCRIMINATOR_LENGTH
+ PUBLIC_KEY_LENGTH // Author.
+ TIMESTAMP_LENGTH // Timestamp.
+ RESULTS_LENGTH; // Content.
}
const DISCRIMINATOR_LENGTH: usize = 8;
const PUBLIC_KEY_LENGTH: usize = 32;
const TIMESTAMP_LENGTH: usize = 8;
const RESULTS_LENGTH: usize = 72;
this is the client test method:
it('can update spin', async () => {
let tweet = anchor.web3.Keypair.generate();
const signature = await program.provider.connection.requestAirdrop(tweet.publicKey, 1000000000);
// Send a tweet and fetch its account.
const author = program.provider.wallet.publicKey;
await program.rpc.firstSpin({
accounts: {
spin: tweet.publicKey,
author: program.provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: [tweet],
});
//Update the Tweet.
await program.rpc.spin(new anchor.BN(10),{
accounts: {
spin: tweet.publicKey,
jackpot:"BGjniJviEPnTFD2TjLFa1vh9CfU7Z7edasG54Hy9HhmP",
author: program.provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
}
});
});
});