Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This account may not be used to pay transaction fees #74

Closed
lingfan opened this issue Nov 4, 2022 · 3 comments
Closed

This account may not be used to pay transaction fees #74

lingfan opened this issue Nov 4, 2022 · 3 comments
Labels

Comments

@lingfan
Copy link

lingfan commented Nov 4, 2022

This account may not be used to pay transaction fees
image

use anchor_lang::prelude::*;
use anchor_lang::solana_program::entrypoint::ProgramResult;

declare_id!("CLvLTsZ8seZ6E19b2FSA4B1BrumUatXiSe2RmDvbtG41");

#[program]
pub mod crowdfunding {
use super::*;
pub fn create(ctx: Context, name: String, description: String) -> ProgramResult {
let campaign = &mut ctx.accounts.campaign;
campaign.name = name;
campaign.description = description;
campaign.amount_donated = 0;
campaign.current_balance = 0;
campaign.admin = *ctx.accounts.user.key;
Ok(())
}
pub fn withdraw(ctx: Context, amount: u64) -> ProgramResult {
let campaign = &mut ctx.accounts.campaign;
let user = &mut ctx.accounts.user;
if campaign.admin != *user.key {
return Err(ProgramError::IncorrectProgramId);
}
let rent_balance = Rent::get()?.minimum_balance(campaign.to_account_info().data_len());

    if **campaign.to_account_info().lamports.borrow() - rent_balance < amount {
        println!("Not enough funds");
        return Err(ProgramError::InsufficientFunds);
    }

    **campaign.to_account_info().try_borrow_mut_lamports()? -= amount;
    campaign.current_balance -= amount;

    **user.to_account_info().try_borrow_mut_lamports()? += amount;
    println!(
        "Current balance {}",
        **campaign.to_account_info().try_borrow_mut_lamports()?
    );
    Ok(())
}

pub fn donate(ctx: Context<Donate>, amount: u64) -> ProgramResult {
    let ix = anchor_lang::solana_program::system_instruction::transfer(
        &ctx.accounts.user.key(),
        &ctx.accounts.campaign.key(),
        amount,
    );

    anchor_lang::solana_program::program::invoke(
        &ix,
        &[
            ctx.accounts.user.to_account_info(),
            ctx.accounts.campaign.to_account_info(),
        ],
    );
    (&mut ctx.accounts.campaign).amount_donated += amount;
    (&mut ctx.accounts.campaign).current_balance += amount;
    Ok(())
}

}
#[derive(Accounts)]
pub struct Create<'info> {
#[account(
init,
payer=user,
space=9000,
seeds =[b"CAMPAIGN_DEMO".as_ref(), user.key().as_ref()],
bump
)]
pub campaign: Account<'info, Campaign>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Withdraw<'info> {
#[account(mut)]
pub campaign: Account<'info, Campaign>,
#[account(mut)]
pub user: Signer<'info>,
}
#[derive(Accounts)]
pub struct Donate<'info> {
#[account(mut)]
pub campaign: Account<'info, Campaign>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct Campaign {
pub admin: Pubkey,
pub name: String,
pub description: String,
pub amount_donated: u64,
pub current_balance: u64,
}
//3S9nB8j5Jod3tP74uWXKcyn1UeTSF2qykqsrTpEDNonf

image

Instruction index: 0
Reason: Cross-program invocation with unauthorized signer or writable account.

@acheroncrypto
Copy link
Member

Hey! This is happening because Anchor let's you init an account even if the account is your wallet. So if you've chosen My address on any of the instructions that inits an account, your account will belong to the program and you will no longer be able to pay fees. Only System Program owned accounts can pay fees and if you look at any explorer, you will see that the wallet you are using is not owned by System Program.

This has been a big problem for a while and I'm planning to contribute for a fix so it doesn't let you init an account if the account's lamports is not 0.

In the meantime, you'd need to use a new wallet instead.

@acheroncrypto
Copy link
Member

You can pull the latest to get create a new wallet option in the wallet settings
image

Added in b8ad562.

@lingfan
Copy link
Author

lingfan commented Nov 6, 2022

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants