pda-rent-payer.sol 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import "../libraries/system_instruction.sol";
  2. @program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
  3. contract pda_rent_payer {
  4. @payer(payer) // "payer" is the account that pays for creating the account
  5. @seed("rent_vault") // hardcoded seed
  6. constructor(@bump bytes1 bump, uint64 fundLamports) {
  7. // Independently derive the PDA address from the seeds, bump, and programId
  8. (address pda, bytes1 _bump) = try_find_program_address(["rent_vault"], address(this));
  9. // Verify that the bump passed to the constructor matches the bump derived from the seeds and programId
  10. // This ensures that only the canonical pda address can be used to create the account (first bump that generates a valid pda address)
  11. require(bump == _bump, 'INVALID_BUMP');
  12. // Fund the pda account with additional lamports
  13. SystemInstruction.transfer(
  14. tx.accounts.payer.key, // from
  15. tx.accounts.dataAccount.key, // to (the address of the account being created)
  16. fundLamports // amount of lamports to transfer
  17. );
  18. }
  19. @mutableAccount(ownedByProgram)
  20. @mutableAccount(intendedRecipient)
  21. function createNewAccount(uint64 lamports) external {
  22. AccountInfo from = tx.accounts.ownedByProgram; // an account owned by the program
  23. AccountInfo to = tx.accounts.intendedRecipient; // 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. }