checking-accounts.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import "solana";
  2. @program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
  3. contract checking_accounts {
  4. // The dataAccount is unused in this example, but is a required account when using Solang
  5. @payer(payer) // "payer" is the account that pays to create the dataAccount
  6. constructor(address payer) {}
  7. function checkAccounts(address accountToChange, address accountToCreate) public view {
  8. print("Number of Accounts Provided: {:}".format(tx.accounts.length));
  9. // Find the accounts we are looking for and perform checks on them
  10. for (uint64 i = 0; i < tx.accounts.length; i++) {
  11. if (tx.accounts[i].key == accountToChange) {
  12. print("Found Account To Change");
  13. programOwnerCheck(tx.accounts[i]);
  14. }
  15. if (tx.accounts[i].key == accountToCreate) {
  16. print("Found Account To Create");
  17. notInitializedCheck(tx.accounts[i]);
  18. signerCheck(tx.accounts[i]);
  19. }
  20. }
  21. // (Create account...) (unimplemented)
  22. // (Change account...) (unimplemented)
  23. }
  24. function programOwnerCheck(AccountInfo account) internal pure {
  25. print("Progam Owner Check");
  26. // The owner of this account should be this program
  27. require(account.owner == type(checking_accounts).program_id, "Account to change does not have the correct program id.");
  28. }
  29. function notInitializedCheck(AccountInfo account) internal pure {
  30. print("Check Account Not Initialized");
  31. // This account should not be initialized (has no lamports)
  32. require(account.lamports == 0, "The program expected the account to create to not yet be initialized.");
  33. }
  34. function signerCheck(AccountInfo account) internal pure {
  35. print("Check Account Signed Transaction");
  36. // This account should be a signer on the transaction
  37. require(account.is_signer, "Account required to be a signer");
  38. }
  39. }