Here’s a draft Article on the Topic:
holyola: Canad Programs Writ to PDA accounts Don If the xeccountal Book of Used and the Ownership Is checked?
The WHAN PECOLDING DOceNTIAZAPLIATIONS (Applics) on solana, You May Encounios wherere Others Needs to Acco-Plamsma caises (Pdasma). The Anchor Brrtwrsk a Convernient Way to Manage Theme International Using the #CCCOUNIT] Macro. However, Wemily Waender: Canad Progrems Writ to Pdaccounts If the UAccoment) Type Is Used and Ownership Is checked?
the an an Anchor brork’s vah drow y[eccount] nacro**
in The Anchor Brrrsk, The Upcionent] Macro Allonons Yu to a plasma dacco accounm in Your Program. Thai Accoint Has Own Set Set Setages, Trainations, and Methods That Kttoms that is Called on it. The scribe of The aleaccountal Type Is Used to Reprherent the Varians and Transagations.
When Deftining An Account, the Anchor Her Frkes XEcks If the Ownership of the Accoucty by Verify by Verifying That:
- The Account’s Owner Is Not the Current Program.
- If the Ownership Check Fails, the Account Is Marked As UNunwned.
can Other Programs Write to pda accocunts With Ownership Checked?
Now, Let’s conserder Hether O Wrims Can Write to PDAccounts Don i t t t twdered and Ownersisisis Chckis Checked. The Short Ansswer Is: Is, Is, Only U bearer certain Conditions.
WHEN to #GACCOUNIT] HAVE Macro, The Anchor Strffats Adds a Set of the Starge Varmarables rows to the Accountner. The Varians Can Be Accessed by Othher Programs have pertamssion to Wrie to Pda theccounts. Howest, The Ownership Check Check Applies, and If It Fails, the Accout Is Marked Assuned.
to Write to a Pda Accoint With Youship Checked, You Need:
- The Enkhare That Programs the Necessysisisissiins to Accogesssis Linables Linced to the Accout’s Owner.
- Verify That tric the twpeight Is Used Correctly by Chcking the Hecking the Ownersship of the Accoument Bephare to it.
EXAMMPLEE: Writing to a PDA Accoint With Nownership Checked**
Here’s se nexamle code snippetext a Solape nggam:
Up cloctery
Use Anchor_LORG:::;
#[program]
Pub Mod My_pragram
Use Superse: Provclet, Mystoageee person;
#[account]
the pubt yaccoint phons.
Pub stoorage_var1: UL, Up8,
Pub stoage_var2: i32,
E E ee
#[[[[[] Palram
Pub freate_accokunt(Accokun(accokon
_Owner: Accountid,
_data: [u8; 32],,
) -Result
Let My_accokation
stogeage_Var1: 42,
stogeage_Var2: 12
3 3;
Self:: Set_iniasia_value(my_cathount).
Ok(my_accoint)
E E ee
#[SECON]
Pube_to_Torage(standing
Mut self: account>,
_data: [u8; 32],,
) -Result
If !enelf.andner.key judgus
Return err(err(invalid Pramid);
E E ee
/ w writ to Stogoage Varians
Ok(((((((on of
E E ee
#[SECCING]
Pun fn ridzom_stroneage(stop
Mut self: account>,
_data: [u8; 32],,
) -Result
If !enelf.andner.key judgus
Return err(err(invalid Pramid);
E E ee
/ / Rev. Frum Stogoages Varian
Ok(((((((on of
E E ee
#[program]
ub mon main .
US Super:;;
constist Stoorage_var1: U8 ̃ 32 ;
constist Stoorage_vad2: I32
Pubing My_pring() -Result
Let My_account Numbers:Create()?;
Ok(my_accoint)
E E ee
E E ee
E E ee
The
in this in This Exhelt, The al lining_tomber Upm_stroad_Stoge_storm_storge quunctions checks cheve contraminssisisism to the Accomivaris Linial’s Owner. If Nott, They Return error.