<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a></div><!----></nav><ulclass="sidebar-links"><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>Getting Started</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/getting-started/introduction.html"class="sidebar-link">Introduction</a></li><li><ahref="/anchor/getting-started/installation.html"class="sidebar-link">Installing Dependencies</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>Teams</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/getting-started/projects.html"class="sidebar-link">Projects</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading open"><span>Tutorials</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/tutorials/tutorial-0.html"class="sidebar-link">A Minimal Example</a></li><li><ahref="/anchor/tutorials/tutorial-1.html"aria-current="page"class="active sidebar-link">Arguments and Accounts</a><ulclass="sidebar-sub-headers"><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-1.html#clone-the-repo"class="sidebar-link">Clone the Repo</a></li><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-1.html#defining-a-program"class="sidebar-link">Defining a Program</a><ulclass="sidebar-sub-headers"><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-1.html#initialize-instruction"class="sidebar-link">initialize instruction</a></li><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-1.html#update-instruction"class="sidebar-link">update instruction</a></li></ul></li><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-1.html#creating-and-initializing-accounts"class="sidebar-link">Creating and Initializing Accounts</a></li><liclass="sidebar-sub-header"><ahref="/anchor/tutorials/tutorial-1.html#next-steps"class="sidebar-link">Next Steps</a></li></ul></li><li><ahref="/anchor/tutorials/tutorial-2.html"class="sidebar-link">Account Constraints and Access Control</a></li><li><ahref="/anchor/tutorials/tutorial-3.html"class="sidebar-link">Cross Program Invocations (CPI)</a></li><li><ahref="/anchor/tutorials/tutorial-4.html"class="sidebar-link">Errors</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>CLI</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/cli/commands.html"class="sidebar-link">Commands</a></li></ul></section></li><li><sectionclass="sidebar-group depth-0"><pclass="sidebar-heading"><span>Source Verification</span><!----></p><ulclass="sidebar-links sidebar-group-items"><li><ahref="/anchor/getting-started/verification.html"class="sidebar-link">Verifiable Builds</a></li><li><ahref="/anchor/getting-started/publishing.html"class="sidebar-link">Publishing Source</a></li></ul></section></li></ul></aside><mainclass="page"><divclass="theme-default-content content__default"><h1id="arguments-and-accounts"><ahref="#arguments-and-accounts"class="header-anchor">#</a> Arguments and Accounts</h1><p>This tutorial covers the basics of creating and mutating accounts using Anchor.
It's recommended to read <ahref="/anchor/tutorials/tutorial-0.html">Tutorial 0</a> first, as this tutorial will
build on top of it.</p><h2id="clone-the-repo"><ahref="#clone-the-repo"class="header-anchor">#</a> Clone the Repo</h2><p>To get started, clone the repo.</p><divclass="language-bash extra-class"><preclass="language-bash"><code><spanclass="token function">git</span> clone https://github.com/project-serum/anchor
</code></pre></div><p>Change directories to the <ahref="https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-1"target="_blank"rel="noopener noreferrer">example<span><svgxmlns="http://www.w3.org/2000/svg"aria-hidden="true"focusable="false"x="0px"y="0px"viewBox="0 0 100 100"width="15"height="15"class="icon outbound"><pathfill="currentColor"d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygonfill="currentColor"points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><spanclass="sr-only">(opens new window)</span></span></a>.</p><divclass="language-bash extra-class"><preclass="language-bash"><code><spanclass="token builtin class-name">cd</span> anchor/examples/tutorial/basic-1
</code></pre></div><p>And install any additional JavaScript dependencies:</p><divclass="language-bash extra-class"><preclass="language-bash"><code><spanclass="token function">yarn</span><spanclass="token function">install</span>
</code></pre></div><h2id="defining-a-program"><ahref="#defining-a-program"class="header-anchor">#</a> Defining a Program</h2><p>We define our program as follows</p><divclass="language-rs extra-class"><preclass="language-rs"><code><spanclass="token keyword">use</span><spanclass="token namespace">anchor_lang<spanclass="token punctuation">::</span>prelude<spanclass="token punctuation">::</span></span><spanclass="token operator">*</span><spanclass="token punctuation">;</span>
</code></pre></div><p>Some new syntax elements are introduced here.</p><h3id="initialize-instruction"><ahref="#initialize-instruction"class="header-anchor">#</a><code>initialize</code> instruction</h3><p>First, let's start with the initialize instruction. Notice the <code>data</code> argument passed into the program. This argument and any other valid
Rust types can be passed to the instruction to define inputs to the program.</p><p>Additionally,
notice how we take a mutable reference to <code>my_account</code> and assign the <code>data</code> to it. This leads us to
the <code>Initialize</code> struct, deriving <code>Accounts</code>. There are two things to notice about <code>Initialize</code>.</p><ol><li>The <code>my_account</code> field is of type <code>Account<'info, MyAccount></code> and the deserialized data structure is <code>MyAccount</code>.</li><li>The <code>my_account</code> field is marked with the <code>init</code> attribute. This will create a new
account owned by the current program, zero initialized. When using <code>init</code>, one must also provide
<code>payer</code>, which will fund the account creation, <code>space</code>, which defines how large the account should be,
and the <code>system_program</code>, which is required by the runtime for creating the account.</li></ol><detailsclass="custom-block details"><summary>DETAILS</summary><p>All accounts created with Anchor are laid out as follows: <code>8-byte-discriminator || borsh serialized data</code>. The 8-byte-discriminator is created from the first 8 bytes of the
<code>Sha256</code> hash of the account's type--using the example above, <code>sha256("account:MyAccount")[..8]</code>.
The <code>account:</code> is a fixed prefix.</p><p>Importantly, this allows a program to know for certain an account is indeed of a given type.
Without it, a program would be vulnerable to account injection attacks, where a malicious user
specifies an account of an unexpected type, causing the program to do unexpected things.</p><p>On account creation, this 8-byte discriminator doesn't exist, since the account storage is
zeroed. The first time an Anchor program mutates an account, this discriminator is prepended
to the account storage array and all subsequent accesses to the account (not decorated with
<code>#[account(init)]</code>) will check for this discriminator.</p></details><h3id="update-instruction"><ahref="#update-instruction"class="header-anchor">#</a><code>update</code> instruction</h3><p>Similarly, the <code>Update</code> accounts struct is marked with the <code>#[account(mut)]</code> attribute.
Marking an account as <code>mut</code> persists any changes made upon exiting the program.</p><p>Here we've covered the basics of how to interact with accounts. In a later tutorial,
we'll delve more deeply into deriving <code>Accounts</code>, but for now, just know
you must mark an account <code>init</code> when using it for the first time and <code>mut</code>
for persisting changes.</p><h2id="creating-and-initializing-accounts"><ahref="#creating-and-initializing-accounts"class="header-anchor">#</a> Creating and Initializing Accounts</h2><p>We can interact with the program as follows.</p><divclass="language-js extra-class"><preclass="language-js"><code><spanclass="token comment">// The program to execute.</span>
<spanclass="token keyword">const</span> program <spanclass="token operator">=</span> anchor<spanclass="token punctuation">.</span>workspace<spanclass="token punctuation">.</span>Basic1<spanclass="token punctuation">;</span>
<spanclass="token comment">// The Account to create.</span>
<spanclass="token punctuation">}</span><spanclass="token punctuation">)</span><spanclass="token punctuation">;</span></code></pre></div><p>The last element passed into the method is common amongst all dynamically generated
methods on the <code>rpc</code> namespace, containing several options for a transaction. Here,
we specify the <code>accounts</code> field, an object of all the addresses the transaction
needs to touch, and the <code>signers</code> array of all <code>Signer</code> objects needed to sign the
to sign the transaction.</p><detailsclass="custom-block details"><summary>DETAILS</summary><p>If you've developed on Solana before, you might notice two things 1) the ordering of the accounts doesn't
matter and 2) the <code>isWritable</code> and <code>isSigner</code>
options are not specified on the account anywhere. In both cases, the framework takes care
of these details for you, by reading the IDL.</p></details><p>As before, we can run the example tests.</p><divclass="language- extra-class"><preclass="language-text"><code>anchor test
</code></pre></div><h2id="next-steps"><ahref="#next-steps"class="header-anchor">#</a> Next Steps</h2><p>We've covered all the basics of developing applications using Anchor. However, we've
left out one important aspect to ensure the security of our programs--validating input
and access control. We'll cover that next.</p></div><footerclass="page-edit"><!----><!----></footer><divclass="page-nav"><pclass="inner"><spanclass="prev">