4

In the code example taken from here we see the following invocation:

    invoke(
        &system_instruction::create_account(
            &user.key,
            &power.key,
            lamports_required,
            account_span as u64,
            program_id,
        ),
        &[
            user.clone(), power.clone(), system_program.clone()
        ]
    )?;

Question is, who signs for this transaction? My guess will be the original signer who signed for the transaction that called the program from which the cross program invocation is being made. Is this correct?

If so, how then is the transaction fee for this CPI calculated? And who pays for it?

1 Answer 1

1

looking at the code it looks like the new program account and the users wallet will be signers and the owner will be the program. So the keypair calling it will be able to change the account data I think. Paying for the rent exemption of the new account (lamports field) and the transaction fees will be also the caller of the transaction. In this case "user"

  public static TransactionInstruction CreateAccount(
  PublicKey fromAccount,
  PublicKey newAccountPublicKey,
  ulong lamports,
  ulong space,
  PublicKey programId)
{
  List<AccountMeta> accountMetaList = new List<AccountMeta>()
  {
    AccountMeta.Writable(fromAccount, true),
    AccountMeta.Writable(newAccountPublicKey, true)
  };
  return new TransactionInstruction()
  {
    ProgramId = SystemProgram.ProgramIdKey.KeyBytes,
    Keys = (IList<AccountMeta>) accountMetaList,
    Data = SystemProgramData.EncodeCreateAccountData(programId, lamports, space)
  };
}

internal static byte[] EncodeCreateAccountData(PublicKey owner, ulong lamports, ulong space)
{
  byte[] data = new byte[52];
  data.WriteU32(0U, 0);
  data.WriteU64(lamports, 4);
  data.WriteU64(space, 12);
  data.WritePubKey(owner, 20);
  return data;
}
7
  • 1
    "There is no new signer, because the account will be owned by the program" okay maybe in this example that is the case? what if a CPI involves setting the owner to another program? What happens then? Can invoke still be used?
    – dade
    Commented Sep 14, 2022 at 19:27
  • 1
    I checked the link to the solanacookbook. That seems to be about PDA. Not sure the example linked to in my question is about PDAs though
    – dade
    Commented Sep 14, 2022 at 19:29
  • 1
    The link you shared uses invoke_signed which I understand comes to play when working with PDAs. The code in my question uses invoke which is not about PDAs. So I am not sure your explanation clears the air :/
    – dade
    Commented Sep 14, 2022 at 19:34
  • Oh sorry i was too quick on that, didnt read properly.
    – Jonas H.
    Commented Sep 14, 2022 at 19:49
  • 1
    i still have questions. The transaction fee is calculated and payed with the first call. This is a second call, that is it is a CPI. How can the fee for this CPI be calculated and known before hand when calculating the fee for the first transaction?
    – dade
    Commented Sep 15, 2022 at 6:02

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.