account-data.sol 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. @program_id("F1ipperKF9EfD821ZbbYjS319LXYiBmjhzkkf5a26rC")
  2. contract account_data {
  3. // A private instance of the AddressInfo struct
  4. // This is the data that is stored in the account
  5. AddressInfo private addressInfo;
  6. // The AddressInfo struct definition
  7. struct AddressInfo {
  8. string name;
  9. uint8 houseNumber;
  10. string street;
  11. string city;
  12. }
  13. @payer(payer) // "payer" is the account that pays to create the dataAccount
  14. constructor(
  15. @space uint16 space, // "space" allocated to the account (maximum 10240 bytes, maximum space that can be reallocate when creating account in program via a CPI)
  16. string _name,
  17. uint8 _houseNumber,
  18. string _street,
  19. string _city
  20. ) {
  21. // The AddressInfo instance is initialized with the data passed to the constructor
  22. addressInfo = AddressInfo(_name, _houseNumber, _street, _city);
  23. }
  24. // A function to get the addressInfo data stored on the account
  25. function get() public view returns (AddressInfo) {
  26. return addressInfo;
  27. }
  28. // A function to get the size in bytes of the stored AddressInfo
  29. function getAddressInfoSize() public view returns(uint) {
  30. uint size = 0;
  31. size += bytes(addressInfo.name).length;
  32. size += 1; // For houseNumber, which is uint8 and takes 1 byte
  33. size += bytes(addressInfo.street).length;
  34. size += bytes(addressInfo.city).length;
  35. return size;
  36. }
  37. }