0

use anchor_lang::prelude::*; use anchor_spl::token::{self, Token, TokenAccount, Transfer as SplTransfer}; // use solana_program::system_instruction; use solana_program::{account_info::AccountInfo, pubkey::Pubkey};

declare_id!("");

#[program] pub mod bridge { use super::*;

pub fn deposit(
    ctx: Context<TransferSpl>,
    amount: u64
) -> Result<()> {
    assert!(amount > 0, "No amount to transfer");
    let destination = &ctx.accounts.to_ata;
    let source = &ctx.accounts.from_ata;
    let token_program = &ctx.accounts.token_program;
    let authority = &ctx.accounts.from;

    // Transfer tokens from taker to initializer
    let cpi_accounts = SplTransfer {
        from: source.to_account_info(),
        to: destination.to_account_info(),
        authority: authority.to_account_info(),
    };
    let cpi_program = token_program.to_account_info();

    token::transfer(CpiContext::new(cpi_program, cpi_accounts), amount)?;

    let this_account = &mut ctx.accounts.reciept;
    this_account.count += 1;
    this_account.user = ctx.accounts.signer.key();
    this_account.amount = amount;
    Ok(())
}


pub fn claim(
    ctx: Context<TransferSpl>,
    _to: Pubkey
) -> Result<()> {
    let this_account = &mut ctx.accounts.reciept;
    let destination = &ctx.accounts.to_ata;
    let source = &ctx.accounts.from_ata;
    let token_program = &ctx.accounts.token_program;
    let authority = &ctx.accounts.from;

    let cpi_accounts = SplTransfer {
        from: source.to_account_info(),
        to: destination.to_account_info(),
        authority: authority.to_account_info(),
    };
    let cpi_program = token_program.to_account_info();

    token::transfer(CpiContext::new(cpi_program, cpi_accounts), this_account.amount)?;
    Ok(())
}

}

#[derive(Accounts)] //#[instruction()] pub struct TransferSpl<'info> { pub from: Signer<'info>, #[account(mut)] pub from_ata: Account<'info, TokenAccount>, pub to_ata: Account<'info, TokenAccount>, pub token_program: Program<'info, Token>,

#[account(
    init_if_needed,
    payer = signer,
    space = 500,
    seeds = [],
    bump
)]
pub reciept: Account<'info, TransactionInfo>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,

}

#[account] pub struct TransactionInfo { pub count: u128, pub user: Pubkey, pub amount: u64, pub ok: u8 }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.