Skip to main content
Made code show up correctly
Source Link

Urgent : How do I want to transfer solSOL from a PDA account to usera user's wallet?

pub fn cancel_pool(ctx: Context, _pool_id: u64) -> Result<()> { let pool = &mut ctx.accounts.pool; let configuration = &mut ctx.accounts.configuration;

    if pool.pool_status == false {
        return Err(ErrorCode::PoolAlreadyClosed.into());
    }

    let nonce = *(ctx.bumps.get("mun_sol_vault").unwrap());
    // Transfer back additional collateral
    {
        let seeds = &[ctx.accounts.mun_sol_mint.to_account_info().key.as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref(), &[nonce]];
        let signer = &[&seeds[..]];
        
        let cpi_context = CpiContext::new_with_signer(
        ctx.accounts.system_program.to_account_info(), 
        system_program::Transfer {
            from: ctx.accounts.mun_sol_vault.to_account_info(),
            to: ctx.accounts.user_sol_vault.to_account_info()
        },
        signer);
        system_program::transfer(cpi_context, pool.deposit_amount)?;
    }
    
    Ok(())
}

#[derive(Accounts)] #[instruction(_pool_id: u64)] pub struct CancelPool<'info> { #[account( mut, has_one = mun_sol_vault, has_one = mun_sol_mint )] pub configuration: Box<Account<'info, Configuration>>,

// Order.
#[account(
    mut,
    seeds = [
        _pool_id.to_string().as_ref(),
        constants::MUN_POOL_SEED.as_ref(),
        configuration.to_account_info().key().as_ref()
    ],
    bump,
    constraint = pool.mun_sol_vault == mun_sol_vault.key(),
    close = signer
)]
pub pool: Box<Account<'info, LenderPool>>,

pub mun_sol_mint: Box<Account<'info, Mint>>,

#[account(
    mut,
    seeds = [mun_sol_mint.key().as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref()],
    bump,
)]
pub mun_sol_vault: AccountInfo<'info>,

#[account(mut)]
pub user_sol_vault: AccountInfo<'info>,

#[account(
    mut
)]
pub signer: Signer<'info>,
// misc
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>

}

pub fn cancel_pool(ctx: Context<CancelPool>, _pool_id: u64) -> Result<()> {
        let pool = &mut ctx.accounts.pool;
        let configuration = &mut ctx.accounts.configuration;

        if pool.pool_status == false {
            return Err(ErrorCode::PoolAlreadyClosed.into());
        }

        let nonce = *(ctx.bumps.get("mun_sol_vault").unwrap());
        // Transfer back additional collateral
        {
            let seeds = &[ctx.accounts.mun_sol_mint.to_account_info().key.as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref(), &[nonce]];
            let signer = &[&seeds[..]];
            
            let cpi_context = CpiContext::new_with_signer(
            ctx.accounts.system_program.to_account_info(), 
            system_program::Transfer {
                from: ctx.accounts.mun_sol_vault.to_account_info(),
                to: ctx.accounts.user_sol_vault.to_account_info()
            },
            signer);
            system_program::transfer(cpi_context, pool.deposit_amount)?;
        }
        
        Ok(())
    }

#[derive(Accounts)]
#[instruction(_pool_id: u64)]
pub struct CancelPool<'info> {
    #[account(
        mut,
        has_one = mun_sol_vault,
        has_one = mun_sol_mint
    )]
    pub configuration: Box<Account<'info, Configuration>>,

    // Order.
    #[account(
        mut,
        seeds = [
            _pool_id.to_string().as_ref(),
            constants::MUN_POOL_SEED.as_ref(),
            configuration.to_account_info().key().as_ref()
        ],
        bump,
        constraint = pool.mun_sol_vault == mun_sol_vault.key(),
        close = signer
    )]
    pub pool: Box<Account<'info, LenderPool>>,

    pub mun_sol_mint: Box<Account<'info, Mint>>,

    #[account(
        mut,
        seeds = [mun_sol_mint.key().as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref()],
        bump,
    )]
    pub mun_sol_vault: AccountInfo<'info>,

    #[account(mut)]
    pub user_sol_vault: AccountInfo<'info>,

    #[account(
        mut
    )]
    pub signer: Signer<'info>,
    // misc
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>
}

Urgent : I want to transfer sol from PDA account to user wallet

pub fn cancel_pool(ctx: Context, _pool_id: u64) -> Result<()> { let pool = &mut ctx.accounts.pool; let configuration = &mut ctx.accounts.configuration;

    if pool.pool_status == false {
        return Err(ErrorCode::PoolAlreadyClosed.into());
    }

    let nonce = *(ctx.bumps.get("mun_sol_vault").unwrap());
    // Transfer back additional collateral
    {
        let seeds = &[ctx.accounts.mun_sol_mint.to_account_info().key.as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref(), &[nonce]];
        let signer = &[&seeds[..]];
        
        let cpi_context = CpiContext::new_with_signer(
        ctx.accounts.system_program.to_account_info(), 
        system_program::Transfer {
            from: ctx.accounts.mun_sol_vault.to_account_info(),
            to: ctx.accounts.user_sol_vault.to_account_info()
        },
        signer);
        system_program::transfer(cpi_context, pool.deposit_amount)?;
    }
    
    Ok(())
}

#[derive(Accounts)] #[instruction(_pool_id: u64)] pub struct CancelPool<'info> { #[account( mut, has_one = mun_sol_vault, has_one = mun_sol_mint )] pub configuration: Box<Account<'info, Configuration>>,

// Order.
#[account(
    mut,
    seeds = [
        _pool_id.to_string().as_ref(),
        constants::MUN_POOL_SEED.as_ref(),
        configuration.to_account_info().key().as_ref()
    ],
    bump,
    constraint = pool.mun_sol_vault == mun_sol_vault.key(),
    close = signer
)]
pub pool: Box<Account<'info, LenderPool>>,

pub mun_sol_mint: Box<Account<'info, Mint>>,

#[account(
    mut,
    seeds = [mun_sol_mint.key().as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref()],
    bump,
)]
pub mun_sol_vault: AccountInfo<'info>,

#[account(mut)]
pub user_sol_vault: AccountInfo<'info>,

#[account(
    mut
)]
pub signer: Signer<'info>,
// misc
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>

}

How do I transfer SOL from a PDA account to a user's wallet?

pub fn cancel_pool(ctx: Context<CancelPool>, _pool_id: u64) -> Result<()> {
        let pool = &mut ctx.accounts.pool;
        let configuration = &mut ctx.accounts.configuration;

        if pool.pool_status == false {
            return Err(ErrorCode::PoolAlreadyClosed.into());
        }

        let nonce = *(ctx.bumps.get("mun_sol_vault").unwrap());
        // Transfer back additional collateral
        {
            let seeds = &[ctx.accounts.mun_sol_mint.to_account_info().key.as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref(), &[nonce]];
            let signer = &[&seeds[..]];
            
            let cpi_context = CpiContext::new_with_signer(
            ctx.accounts.system_program.to_account_info(), 
            system_program::Transfer {
                from: ctx.accounts.mun_sol_vault.to_account_info(),
                to: ctx.accounts.user_sol_vault.to_account_info()
            },
            signer);
            system_program::transfer(cpi_context, pool.deposit_amount)?;
        }
        
        Ok(())
    }

#[derive(Accounts)]
#[instruction(_pool_id: u64)]
pub struct CancelPool<'info> {
    #[account(
        mut,
        has_one = mun_sol_vault,
        has_one = mun_sol_mint
    )]
    pub configuration: Box<Account<'info, Configuration>>,

    // Order.
    #[account(
        mut,
        seeds = [
            _pool_id.to_string().as_ref(),
            constants::MUN_POOL_SEED.as_ref(),
            configuration.to_account_info().key().as_ref()
        ],
        bump,
        constraint = pool.mun_sol_vault == mun_sol_vault.key(),
        close = signer
    )]
    pub pool: Box<Account<'info, LenderPool>>,

    pub mun_sol_mint: Box<Account<'info, Mint>>,

    #[account(
        mut,
        seeds = [mun_sol_mint.key().as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref()],
        bump,
    )]
    pub mun_sol_vault: AccountInfo<'info>,

    #[account(mut)]
    pub user_sol_vault: AccountInfo<'info>,

    #[account(
        mut
    )]
    pub signer: Signer<'info>,
    // misc
    pub system_program: Program<'info, System>,
    pub token_program: Program<'info, Token>
}
Source Link

Urgent : I want to transfer sol from PDA account to user wallet

I'm now developing on solana blockchain. I want to users can deposit and refund their sol on my pool. create_pool works well it means users can deposit their sol to my pool. but cancel_pool error. here is my code.

pub fn cancel_pool(ctx: Context, _pool_id: u64) -> Result<()> { let pool = &mut ctx.accounts.pool; let configuration = &mut ctx.accounts.configuration;

    if pool.pool_status == false {
        return Err(ErrorCode::PoolAlreadyClosed.into());
    }

    let nonce = *(ctx.bumps.get("mun_sol_vault").unwrap());
    // Transfer back additional collateral
    {
        let seeds = &[ctx.accounts.mun_sol_mint.to_account_info().key.as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref(), &[nonce]];
        let signer = &[&seeds[..]];
        
        let cpi_context = CpiContext::new_with_signer(
        ctx.accounts.system_program.to_account_info(), 
        system_program::Transfer {
            from: ctx.accounts.mun_sol_vault.to_account_info(),
            to: ctx.accounts.user_sol_vault.to_account_info()
        },
        signer);
        system_program::transfer(cpi_context, pool.deposit_amount)?;
    }
    
    Ok(())
}

#[derive(Accounts)] #[instruction(_pool_id: u64)] pub struct CancelPool<'info> { #[account( mut, has_one = mun_sol_vault, has_one = mun_sol_mint )] pub configuration: Box<Account<'info, Configuration>>,

// Order.
#[account(
    mut,
    seeds = [
        _pool_id.to_string().as_ref(),
        constants::MUN_POOL_SEED.as_ref(),
        configuration.to_account_info().key().as_ref()
    ],
    bump,
    constraint = pool.mun_sol_vault == mun_sol_vault.key(),
    close = signer
)]
pub pool: Box<Account<'info, LenderPool>>,

pub mun_sol_mint: Box<Account<'info, Mint>>,

#[account(
    mut,
    seeds = [mun_sol_mint.key().as_ref(), constants::MUN_SOL_VAULT_SEED.as_ref()],
    bump,
)]
pub mun_sol_vault: AccountInfo<'info>,

#[account(mut)]
pub user_sol_vault: AccountInfo<'info>,

#[account(
    mut
)]
pub signer: Signer<'info>,
// misc
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>

}

please help me.