2

This is a program demonstrating transfer of sols from one key to another.

let fromKeypair = Keypair.generate();
let toKeypair = Keypair.generate();
let transaction = new Transaction();

transaction.add(
  SystemProgram.transfer({
    fromPubkey: fromKeypair.publicKey,
    toPubkey: toKeypair.publicKey,
    lamports: LAMPORTS_PER_SOL,
  }),
);

But it's also said that everything in Solana is an account. So, how could one add sols to just randomly generated key pairs without creating accounts?

Also explain what these "from_pubkey" and "to_pubkey" are when creating an account.

system_instruction::create_account(from_pubkey, to_pubkey, lamports, space, owner);

1 Answer 1

1

The code you posted would not work out of the box.

The fromKeypair is paying for the new account to be created, and would need to have enough SOL to create a new account.

The default system account on solana is 136 bytes and costs 0.00178176 SOL to be rent exempt.

So the system program will create this account when you do the transfer. Using the CLI it would actually ask you if you want to fund that account.

The fromKeypair is the one that is paying for the new account and the toKeypair is the address where the new account will be created.

1
  • made some minor grammar tweaks, hope they're OK, feel free to revert if not.
    – mikemaccana
    Commented Mar 14 at 21:06

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.