0

I have asked same kind of question earlier but not got proper answer.

As I got an answer in my previous question in which I have to use system owned pda account, I have also tried this but getting same error as "Invalid program argument.

Can anyone give me simple implemented code for creating account in any program using pda as signer and payer

2 Answers 2

1

No, it is not possible to use a PDA as the payer in an Anchor init macro.The payer field of the #[account] attribute must be an account with signing rights in order to sign and transmit the transaction. PDAs are not connected to any secret key, hence they cannot sign transactions.

The payer account cannot be a PDA in the sample code and must be an AccountInfo struct with signing privileges. The payer account may be used to initialise the inited_account account.

#[account(mut, signer)]
payer: AccountInfo<'info>,
#[account(init, payer = payer, space = some_space, owner = some_owner)]
inited_account: AccountInfo<'info>,

The signer attribute is used to identify the payer account as having signing rights. This means that the account must be listed among the signatories to the transaction.

The payer account is used to initialise the inited_account account, which causes the lamports required to create the account to be moved from the payer account.

2
  • Can you share some references and articles with me, It will help me to understand it in proper manner. May 1, 2023 at 15:15
  • And here solana.stackexchange.com/questions/688/… it is said that it is possible to use pda as a payer and signer, it's another thing that I have tried and it's not working. May 1, 2023 at 18:38
0

Yes, it is possible to use a PDA as both the signer and payer to create an account in a Solana program. Here's an example of how to do it using the Solana Web3.js library:

const web3 = require('@solana/web3.js');
const { PublicKey, Transaction, SystemProgram } = web3;

// Derive the PDA address
const programId = new PublicKey('program address goes here');
const seed = 'some input data';
const pda = await PublicKey.findProgramAddress([Buffer.from(seed)], 
programId);

// Create a new account using the PDA as the signer and payer
const newAccount = new web3.Account();
const lamports = 1000000; // or however many lamports you want to 
transfer to the new account
const transaction = new Transaction().add(
  SystemProgram.createAccount({
    fromPubkey: pda[0],
    newAccountPubkey: newAccount.publicKey,
    lamports,
    space: 0, // or however much space you need for the account data
    programId,
  })
);
await web3.sendAndConfirmTransaction(connection, transaction, 
[newAccount], {commitment: 'confirmed'});

🔍 In this example, we first use the PublicKey.findProgramAddress function to determine the PDA address, supplying the programme ID and some input data as inputs.

💻 The next step is to construct a new Transaction object and add a SystemProgram.createAccount instruction to it in order to create a new account with the PDA acting as the payer and signer.

📡 Finally, we transmit the transaction to the Solana network and wait for confirmation using the web3.sendAndConfirmTransaction function.

3
  • Hello Ryan, I have asked about its implementation in solana program. May 1, 2023 at 12:04
  • Like how we can create a new account in solana program using CPI in which PDA Account should be signer and payer, it would be great if you give its implementation. May 1, 2023 at 12:06
  • As shown here solana.stackexchange.com/questions/688/… , I have also Implemented but it's not working. May 1, 2023 at 12:08

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.