account-data.sol 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. @space(space) // "space" allocated to the account (maximum 10240 bytes, maximum space that can be reallocate when creating account in program via a CPI)
  15. constructor(address payer, uint16 space, string _name, uint8 _houseNumber, string _street, string _city) {
  16. // The AddressInfo instance is initialized with the data passed to the constructor
  17. addressInfo = AddressInfo(_name, _houseNumber, _street, _city);
  18. }
  19. // A function to get the addressInfo data stored on the account
  20. function get() public view returns (AddressInfo) {
  21. return addressInfo;
  22. }
  23. // A function to get the size in bytes of the stored AddressInfo
  24. function getAddressInfoSize() public view returns(uint) {
  25. uint size = 0;
  26. size += bytes(addressInfo.name).length;
  27. size += 1; // For houseNumber, which is uint8 and takes 1 byte
  28. size += bytes(addressInfo.street).length;
  29. size += bytes(addressInfo.city).length;
  30. return size;
  31. }
  32. }