tutorial-0.html 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>A Minimal Example | ⚓ Anchor</title>
  7. <meta name="generator" content="VuePress 1.8.2">
  8. <link rel="icon" href="data:image/svg+xml,&lt;svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22&gt;&lt;text y=%22.9em%22 font-size=%2290%22&gt;⚓ &lt;/text&gt;&lt;/svg&gt;">
  9. <meta name="description" content="">
  10. <meta name="theme-color" content="#3eaf7c">
  11. <meta name="apple-mobile-web-app-capable" content="yes">
  12. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  13. <link rel="preload" href="/anchor/assets/css/0.styles.21b321ec.css" as="style"><link rel="preload" href="/anchor/assets/js/app.b2f13cd7.js" as="script"><link rel="preload" href="/anchor/assets/js/2.90291299.js" as="script"><link rel="preload" href="/anchor/assets/js/17.29eac49f.js" as="script"><link rel="prefetch" href="/anchor/assets/js/10.99d917b5.js"><link rel="prefetch" href="/anchor/assets/js/11.45e25cc5.js"><link rel="prefetch" href="/anchor/assets/js/12.d30544e0.js"><link rel="prefetch" href="/anchor/assets/js/13.7bb3490d.js"><link rel="prefetch" href="/anchor/assets/js/14.258e35e3.js"><link rel="prefetch" href="/anchor/assets/js/15.243176ff.js"><link rel="prefetch" href="/anchor/assets/js/16.b6cb9e25.js"><link rel="prefetch" href="/anchor/assets/js/18.db5dc2ee.js"><link rel="prefetch" href="/anchor/assets/js/19.b9ce785c.js"><link rel="prefetch" href="/anchor/assets/js/20.50564768.js"><link rel="prefetch" href="/anchor/assets/js/21.b31f2925.js"><link rel="prefetch" href="/anchor/assets/js/3.397fd877.js"><link rel="prefetch" href="/anchor/assets/js/4.f90252b7.js"><link rel="prefetch" href="/anchor/assets/js/5.47891694.js"><link rel="prefetch" href="/anchor/assets/js/6.493f79e9.js"><link rel="prefetch" href="/anchor/assets/js/7.32936426.js"><link rel="prefetch" href="/anchor/assets/js/8.9fc829e7.js"><link rel="prefetch" href="/anchor/assets/js/9.525a5964.js">
  14. <link rel="stylesheet" href="/anchor/assets/css/0.styles.21b321ec.css">
  15. </head>
  16. <body>
  17. <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/anchor/" class="home-link router-link-active"><!----> <span class="site-name">⚓ Anchor</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://book.anchor-lang.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  18. The Anchor Book
  19. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://docs.rs/anchor-lang/latest/anchor_lang/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  20. Rust
  21. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://project-serum.github.io/anchor/ts/index.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  22. TypeScript
  23. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/project-serum/anchor" target="_blank" rel="noopener noreferrer" class="nav-link external">
  24. GitHub
  25. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://book.anchor-lang.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  26. The Anchor Book
  27. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://docs.rs/anchor-lang/latest/anchor_lang/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  28. Rust
  29. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://project-serum.github.io/anchor/ts/index.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  30. TypeScript
  31. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/project-serum/anchor" target="_blank" rel="noopener noreferrer" class="nav-link external">
  32. GitHub
  33. <span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Getting Started</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/anchor/getting-started/introduction.html" class="sidebar-link">Introduction</a></li><li><a href="/anchor/getting-started/installation.html" class="sidebar-link">Installing Dependencies</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Teams</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/anchor/getting-started/projects.html" class="sidebar-link">Projects</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Tutorials</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/anchor/tutorials/tutorial-0.html" aria-current="page" class="active sidebar-link">A Minimal Example</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#clone-the-repo" class="sidebar-link">Clone the Repo</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#starting-a-localnet" class="sidebar-link">Starting a Localnet</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#defining-a-program" class="sidebar-link">Defining a Program</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#building-and-emitting-an-idl" class="sidebar-link">Building and Emitting an IDL</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#deploying" class="sidebar-link">Deploying</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#generating-a-client" class="sidebar-link">Generating a Client</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#workspaces" class="sidebar-link">Workspaces</a></li><li class="sidebar-sub-header"><a href="/anchor/tutorials/tutorial-0.html#next-steps" class="sidebar-link">Next Steps</a></li></ul></li><li><a href="/anchor/tutorials/tutorial-1.html" class="sidebar-link">Arguments and Accounts</a></li><li><a href="/anchor/tutorials/tutorial-2.html" class="sidebar-link">Account Constraints and Access Control</a></li><li><a href="/anchor/tutorials/tutorial-3.html" class="sidebar-link">Cross Program Invocations (CPI)</a></li><li><a href="/anchor/tutorials/tutorial-4.html" class="sidebar-link">Errors</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>CLI</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/anchor/cli/commands.html" class="sidebar-link">Commands</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Source Verification</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/anchor/getting-started/verification.html" class="sidebar-link">Verifiable Builds</a></li><li><a href="/anchor/getting-started/publishing.html" class="sidebar-link">Publishing Source</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="a-minimal-example"><a href="#a-minimal-example" class="header-anchor">#</a> A Minimal Example</h1> <p>Here, we introduce Anchor's core syntax elements and project workflow. This tutorial assumes all
  34. <a href="/anchor/getting-started/installation.html">prerequisites</a> are installed.</p> <h2 id="clone-the-repo"><a href="#clone-the-repo" class="header-anchor">#</a> Clone the Repo</h2> <p>To get started, clone the repo.</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> clone https://github.com/project-serum/anchor
  35. </code></pre></div><p>Next, checkout the tagged branch of the same version of the anchor cli you have installed.</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">git</span> checkout tags/<span class="token operator">&lt;</span>version<span class="token operator">&gt;</span>
  36. </code></pre></div><p>Change directories to the <a href="https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-0" target="_blank" rel="noopener noreferrer">example<span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a>.</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token builtin class-name">cd</span> anchor/examples/tutorial/basic-0
  37. </code></pre></div><p>And install any additional JavaScript dependencies:</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token function">yarn</span> <span class="token function">install</span>
  38. </code></pre></div><h2 id="starting-a-localnet"><a href="#starting-a-localnet" class="header-anchor">#</a> Starting a Localnet</h2> <p>In a separate terminal, start a local network. If you're running solana
  39. for the first time, generate a wallet.</p> <div class="language- extra-class"><pre class="language-text"><code>solana-keygen new
  40. </code></pre></div><p>Then run</p> <div class="language- extra-class"><pre class="language-text"><code>solana-test-validator
  41. </code></pre></div><p>Then, shut it down.</p> <p>The test validator will be used when testing Anchor programs. Make sure to turn off the validator before you begin testing Anchor programs.</p> <details class="custom-block details"><summary>DETAILS</summary> <p>As you'll see later, starting a localnet manually like this is not necessary when testing with Anchor,
  42. but is done for educational purposes in this tutorial.</p></details> <h2 id="defining-a-program"><a href="#defining-a-program" class="header-anchor">#</a> Defining a Program</h2> <p>We define the minimum viable program as follows.</p> <div class="language-rs extra-class"><pre class="language-rs"><code><span class="token keyword">use</span> <span class="token namespace">anchor_lang<span class="token punctuation">::</span>prelude<span class="token punctuation">::</span></span><span class="token operator">*</span><span class="token punctuation">;</span>
  43. <span class="token macro property">declare_id!</span><span class="token punctuation">(</span><span class="token string">&quot;Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  44. <span class="token attribute attr-name">#[program]</span>
  45. <span class="token keyword">mod</span> <span class="token module-declaration namespace">basic_0</span> <span class="token punctuation">{</span>
  46. <span class="token keyword">use</span> <span class="token keyword">super</span><span class="token punctuation">::</span><span class="token operator">*</span><span class="token punctuation">;</span>
  47. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">initialize</span><span class="token punctuation">(</span>_ctx<span class="token punctuation">:</span> <span class="token class-name">Context</span><span class="token operator">&lt;</span><span class="token class-name">Initialize</span><span class="token operator">&gt;</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Result</span><span class="token operator">&lt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  48. <span class="token class-name">Ok</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  49. <span class="token punctuation">}</span>
  50. <span class="token punctuation">}</span>
  51. <span class="token attribute attr-name">#[derive(Accounts)]</span>
  52. <span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">Initialize</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  53. </code></pre></div><ul><li><p><code>#[program]</code> First, notice that a program is defined with the <code>#[program]</code> attribute, where each
  54. inner method defines an RPC request handler, or, in Solana parlance, an &quot;instruction&quot;
  55. handler. These handlers are the entrypoints to your program that clients may invoke, as
  56. we will see soon.</p></li> <li><p><code>Context&lt;Initialize&gt;</code> The first parameter of <em>every</em> RPC handler is the <code>Context</code> struct, which is a simple
  57. container for the currently executing <code>program_id</code> generic over
  58. <code>Accounts</code>--here, the <code>Initialize</code> struct.</p></li> <li><p><code>#[derive(Accounts)]</code> The <code>Accounts</code> derive macro marks a struct containing all the accounts that must be
  59. specified for a given instruction. To understand Accounts on Solana, see the
  60. <a href="https://docs.solana.com/developing/programming-model/accounts" target="_blank" rel="noopener noreferrer">docs<span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a>.
  61. In subsequent tutorials, we'll demonstrate how an <code>Accounts</code> struct can be used to
  62. specify constraints on accounts given to your program. Since this example doesn't touch any
  63. accounts, we skip this (important) detail.</p></li></ul> <h2 id="building-and-emitting-an-idl"><a href="#building-and-emitting-an-idl" class="header-anchor">#</a> Building and Emitting an IDL</h2> <p>After creating a program, you can use the <code>anchor</code> CLI to build and emit an IDL, from which clients
  64. can be generated.</p> <div class="language-bash extra-class"><pre class="language-bash"><code>anchor build
  65. </code></pre></div><details class="custom-block details"><summary>DETAILS</summary> <p>The <code>build</code> command is a convenience combining two steps.</p> <ol><li><code>cargo build-bpf</code></li> <li><code>anchor idl parse -f program/src/lib.rs -o target/idl/basic_0.json</code>.</li></ol></details> <p>Once run, you should see your build artifacts, as usual, in your <code>target/</code> directory. Additionally,
  66. a <code>target/idl/basic_0.json</code> file is created. Inspecting its contents you should see</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
  67. <span class="token property">&quot;version&quot;</span><span class="token operator">:</span> <span class="token string">&quot;0.1.0&quot;</span><span class="token punctuation">,</span>
  68. <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;basic_0&quot;</span><span class="token punctuation">,</span>
  69. <span class="token property">&quot;instructions&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
  70. <span class="token punctuation">{</span>
  71. <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;initialize&quot;</span><span class="token punctuation">,</span>
  72. <span class="token property">&quot;accounts&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  73. <span class="token property">&quot;args&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  74. <span class="token punctuation">}</span>
  75. <span class="token punctuation">]</span>
  76. <span class="token punctuation">}</span>
  77. </code></pre></div><p>From this file a client can be generated. Note that this file is created by parsing the <code>src/lib.rs</code>
  78. file in your program's crate.</p> <div class="custom-block tip"><p class="custom-block-title">TIP</p> <p>If you've developed on Ethereum, the IDL is analogous to the <code>abi.json</code>.</p></div> <h2 id="deploying"><a href="#deploying" class="header-anchor">#</a> Deploying</h2> <p>Once built, we can deploy the program by running</p> <div class="language-bash extra-class"><pre class="language-bash"><code>anchor deploy
  79. </code></pre></div><p>Take note of the program's deployed address. We'll use it next.</p> <h2 id="generating-a-client"><a href="#generating-a-client" class="header-anchor">#</a> Generating a Client</h2> <p>Now that we've built a program, deployed it to a local cluster, and generated an IDL,
  80. we can use the IDL to generate a client to speak to our on-chain program. For example,
  81. see <a href="https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-0/client.js" target="_blank" rel="noopener noreferrer">client.js<span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a>.</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Read the generated IDL.</span>
  82. <span class="token keyword">const</span> idl <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>
  83. <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;fs&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">readFileSync</span><span class="token punctuation">(</span><span class="token string">&quot;./target/idl/basic_0.json&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;utf8&quot;</span><span class="token punctuation">)</span>
  84. <span class="token punctuation">)</span><span class="token punctuation">;</span>
  85. <span class="token comment">// Address of the deployed program.</span>
  86. <span class="token keyword">const</span> programId <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">anchor<span class="token punctuation">.</span>web3<span class="token punctuation">.</span>PublicKey</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;YOUR-PROGRAM-ID&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  87. <span class="token comment">// Generate the program client from IDL.</span>
  88. <span class="token keyword">const</span> program <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">anchor<span class="token punctuation">.</span>Program</span><span class="token punctuation">(</span>idl<span class="token punctuation">,</span> programId<span class="token punctuation">)</span><span class="token punctuation">;</span>
  89. <span class="token comment">// Execute the RPC.</span>
  90. <span class="token keyword">await</span> program<span class="token punctuation">.</span>rpc<span class="token punctuation">.</span><span class="token function">initialize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div><p>Notice how we dynamically created the <code>initialize</code> method under
  91. the <code>rpc</code> namespace.</p> <p>Now, make sure to plugin your program's address into <code>&lt;YOUR-PROGRAM-ID&gt;</code> (a mild
  92. annoyance that we'll address next). In order to run the client, you'll also need the path
  93. to your wallet's keypair you generated when you ran <code>solana-keygen new</code>; you can find it
  94. by running</p> <div class="language-bash extra-class"><pre class="language-bash"><code>solana config get keypair
  95. </code></pre></div><p>Once you've got it, run the client with the environment variable <code>ANCHOR_WALLET</code> set to
  96. that path, e.g.</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">ANCHOR_WALLET</span><span class="token operator">=</span><span class="token operator">&lt;</span>YOUR-KEYPAIR-<span class="token environment constant">PATH</span><span class="token operator">&gt;</span> node client.js
  97. </code></pre></div><p>You just successfully created a client and executed a transaction on your localnet.</p> <h2 id="workspaces"><a href="#workspaces" class="header-anchor">#</a> Workspaces</h2> <p>So far we've seen the basics of how to create, deploy, and make RPCs to a program, but
  98. deploying a program, copy and pasting the address, and explicitly reading
  99. an IDL is all a bit tedious, and can easily get out of hand the more tests and the more
  100. programs you have. For this reason, we introduce the concept of a workspace.</p> <p>Inspecting <a href="https://github.com/project-serum/anchor/tree/master/examples/tutorial/basic-0/tests/basic-0.js" target="_blank" rel="noopener noreferrer">tests/basic-0.js<span><svg xmlns="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"><path fill="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> <polygon fill="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> <span class="sr-only">(opens new window)</span></span></a>, we see the above example can be reduced to</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// Read the deployed program from the workspace.</span>
  101. <span class="token keyword">const</span> program <span class="token operator">=</span> anchor<span class="token punctuation">.</span>workspace<span class="token punctuation">.</span>Basic0<span class="token punctuation">;</span>
  102. <span class="token comment">// Execute the RPC.</span>
  103. <span class="token keyword">await</span> program<span class="token punctuation">.</span>rpc<span class="token punctuation">.</span><span class="token function">initialize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre></div><p>The <code>workspace</code> namespace provides access to all programs in the local project and is
  104. automatically updated to reflect the latest deployment, making it easy to change
  105. your program, update your JavaScript, and run your tests in a fast feedback loop.</p> <div class="custom-block tip"><p class="custom-block-title">NOTE</p> <p>For now, the workspace feature is only available when running the <code>anchor test</code> command,
  106. which will automatically <code>build</code>, <code>deploy</code>, and <code>test</code> all programs against a localnet
  107. in one command.</p></div> <p>Finally, we can run the test. Don't forget to kill the local validator started earlier.
  108. We won't need to start one manually for any future tutorials.</p> <div class="language-bash extra-class"><pre class="language-bash"><code>anchor <span class="token builtin class-name">test</span>
  109. </code></pre></div><h2 id="next-steps"><a href="#next-steps" class="header-anchor">#</a> Next Steps</h2> <p>We've introduced the basic syntax of writing programs in Anchor along with a productive
  110. workflow for building and testing. However, programs aren't all that interesting without
  111. interacting with persistent state. We'll cover that next.</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
  112. <a href="/anchor/getting-started/projects.html" class="prev">
  113. Projects
  114. </a></span> <span class="next"><a href="/anchor/tutorials/tutorial-1.html">
  115. Arguments and Accounts
  116. </a>
  117. </span></p></div> </main></div><div class="global-ui"></div></div>
  118. <script src="/anchor/assets/js/app.b2f13cd7.js" defer></script><script src="/anchor/assets/js/2.90291299.js" defer></script><script src="/anchor/assets/js/17.29eac49f.js" defer></script>
  119. </body>
  120. </html>