0

I've written a smart contract for doing a transaction. What I want is when a user has made a transaction once, it should be stored in the smart contract so that whenever they come back, they shouldn't be asked to make the payment again.

Here is my simple smart contract for the transaction:

use anchor_lang::{
    prelude::*,
    solana_program::{program::invoke_signed, system_instruction, program::invoke},
};

declare_id!("4DMFEdzUx65CZRLiuF3dpfRfAvdvmNftQLRuAKq8n3nW");

#[program]
pub mod checkout_transactions {
    use anchor_lang::solana_program::entrypoint::ProgramResult;
    use super::*;

    /// Transfers lamports from one account (must be program owned)
    /// to another account. The recipient can by any account
    pub fn transfer_lamports(ctx: Context<Checkout>
    ) -> ProgramResult {
        
        // const AMOUNT: u64 = 1000000000;
        let ix = system_instruction::transfer(
            &ctx.accounts.from.key(),
            &ctx.accounts.to.key(),
            1_000_000_000,
        );
        invoke(&ix, &[
            ctx.accounts.from.to_account(),
            ctx.accounts.to.to_account_info(),
            // ctx.accounts.owner.to_account_info().clone(),
            ctx.accounts.system_program.to_account_info().clone(),
        ],
        )
    }
    
}


#[derive(Accounts)]
pub struct Checkout<'info> {
    /// CHECK:
    #[account(mut, signer)]
    from: AccountInfo<'info>,
    
    /// CHECK:
    #[account(mut)]
    to: AccountInfo<'info>,
    system_program: Program<'info, System>,
}

Is there a way with which I can store the payment status of a user in a PDA that whether they have paid or not?

PS: I don't want to use a DB for this.

1 Answer 1

3

You can make a payment PDA and check whether it exists and has the data you want on it.

For example you could make a PDA with the seeds ["payment", walletID] and just 'init' the account. When the transaction runs again the payment should fail stating the pda account is already initialised, you can also store 1 byte to show that it has been created and paid.

That's a simple bare bones way of doing it.

Then on the front end you can just check the PDA account data for your 1 byte of data and display the payment option or not.

1
  • Interesting approach @Tony Boyle, I guess you also set the transaction commitment of the payment to finished to ensure it was correctly executed?
    – Ander
    Sep 17, 2022 at 11:11

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.