5
pub fn startstuffoff(ctx: Context<StartStuffOff>) -> Result<()> {

        let base_account = &mut ctx.accounts.base_account;

        base_account.total_gifs = 0;
        Ok(())
    }

    pub fn add_gif(ctx: Context<AddGif>) -> Result<()> {

        let base_account = &mut ctx.accounts.base_account;
        base_account.total_gifs += 1;
        Ok(())
    }


>#[derive(Accounts)]<br>
pub struct StartStuffOff<'info> {<br>
    #[account(init, payer = user, space = 10000)]<br>
    pub base_account: Account<'info, BaseAccount>,<br>
    #[account(mut)]<br>
    pub user: Signer<'info>,<br>
    pub system_program: Program <'info, System>,<br>
}<br>

>#[derive(Accounts)]<br>
pub struct AddGif<'info>{<br>
    #[account(mut)]<br>
    pub base_account: Account<'info, BaseAccount>,<br>
}<br>

>#[account]<br>
pub struct BaseAccount{<br>
    pub total_gifs: u64,<br>
}<br>

I have followed the build space tutorial which is quite good. I'm just confused about the aspect where I need to use Signer, Account and base account and how it will be able to grant permission to our specific users to allow them to increment Gif counter in accounts.

Questions

  1. What's the difference between a Signer and Accounts?
  2. Where and when should they be used?
  3. What happens if I use one instead of the other?

1 Answer 1

6

The Signer is the authority of that transaction i.e the wallet that would need to sign with its private key for that transaction.

For example in the StartStuffOff instruction, the user account is specified as the signer since it pays for creation of base_account. This means that verification of its private key would be required for the transaction to continue.

A wallet is required to sign for any transaction that debits it, a sensible constraint that ensures that only the actual owner of the wallet can authorize a transfer of funds from it.

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.