pda-rent-payer.sol 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import "./system_instruction.sol";
  2. @program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
  3. contract pda_rent_payer {
  4. bool private value = true;
  5. @payer(payer) // "payer" is the account that pays for creating the account
  6. @seed("rent_vault") // hardcoded seed
  7. @bump(bump) // bump seed to derive the pda
  8. constructor(address payer, bytes1 bump, uint64 fundLamports) {
  9. // Independently derive the PDA address from the seeds, bump, and programId
  10. (address pda, bytes1 _bump) = try_find_program_address(["rent_vault"], type(pda_rent_payer).program_id);
  11. // Verify that the bump passed to the constructor matches the bump derived from the seeds and programId
  12. // This ensures that only the canonical pda address can be used to create the account (first bump that generates a valid pda address)
  13. require(bump == _bump, 'INVALID_BUMP');
  14. // Fund the pda account with additional lamports
  15. SystemInstruction.transfer(
  16. payer, // from
  17. address(this), // to (the address of the account being created)
  18. fundLamports // amount of lamports to transfer
  19. );
  20. }
  21. function createNewAccount(uint64 lamports) public {
  22. AccountInfo from = tx.accounts[0]; // first account must be an account owned by the program
  23. AccountInfo to = tx.accounts[1]; // second account must be the intended recipient
  24. print("From: {:}".format(from.key));
  25. print("To: {:}".format(to.key));
  26. from.lamports -= lamports;
  27. to.lamports += lamports;
  28. }
  29. }