<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: C.OG</title>
    <description>The latest articles on DEV Community by C.OG (@cogoo).</description>
    <link>https://dev.to/cogoo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F94592%2F2fef6c9c-3c44-47d0-ac1e-b9f20530a045.png</url>
      <title>DEV Community: C.OG</title>
      <link>https://dev.to/cogoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cogoo"/>
    <language>en</language>
    <item>
      <title>Solana teardown: Walkthrough of the example helloworld program</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Fri, 22 Oct 2021 14:34:39 +0000</pubDate>
      <link>https://dev.to/cogoo/solana-teardown-walkthrough-of-the-example-helloworld-program-18m4</link>
      <guid>https://dev.to/cogoo/solana-teardown-walkthrough-of-the-example-helloworld-program-18m4</guid>
      <description>&lt;p&gt;In this article, we'll walk through the &lt;a href="https://github.com/solana-labs/example-helloworld/blob/master/src/program-rust/src/lib.rs"&gt;helloworld&lt;/a&gt; Rust program line-by-line and begin to unpack how programs on Solana work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who this article is for
&lt;/h2&gt;

&lt;p&gt;This walkthrough assumes that you've written code in any programming language or have a basic understanding of Rust. You don't need prior experience writing programs (smart contracts) on Solana or any other blockchain. I won't spend much time explaining the Rust specific code or idioms but rather focus on the Solana program.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use declaration
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;borsh&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;BorshDeserialize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BorshSerialize&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;solana_program&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;
    &lt;span class="nn"&gt;account_info&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;next_account_info&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;AccountInfo&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;entrypoint&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nn"&gt;entrypoint&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ProgramResult&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nn"&gt;program_error&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ProgramError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nn"&gt;pubkey&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Pubkey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We bring libraries and traits into scope with the &lt;code&gt;use&lt;/code&gt; declaration. It allows us to bind a full path to a new name for easier access.&lt;/p&gt;

&lt;h2&gt;
  
  
  Greeting Account
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(BorshSerialize,&lt;/span&gt; &lt;span class="nd"&gt;BorshDeserialize,&lt;/span&gt; &lt;span class="nd"&gt;Debug)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;GreetingAccount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// number of greetings&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Programs are stateless. If you need to store state between transactions, you can do so using Accounts. In Solana, &lt;strong&gt;EVERYTHING&lt;/strong&gt; is an account. The Solana blockchain only sees a list of accounts of varying sizes. If an account is marked "executable" in its metadata, then it is considered a program. We'll talk more on accounts later, but for now, the understanding we need is that; our Greeting Account can hold data, and this data is in the shape of our &lt;code&gt;GreetingAccount&lt;/code&gt; struct. Our accounts &lt;code&gt;data&lt;/code&gt; property will have a &lt;code&gt;counter&lt;/code&gt; property which we'll use to store a count of all the greetings. This data will persist beyond the lifetime of our program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Entrypoint
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;entrypoint!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;process_instruction&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We register the &lt;code&gt;process_instruction&lt;/code&gt; as the entrypoint symbol which the Solana runtime looks up and calls when invoking the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  process_instruction
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;process_instruction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;program_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Pubkey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;AccountInfo&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;_instruction_data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ProgramResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="c1"&gt;// --snip--&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All programs have the same signature. Let's take an in-depth look at the parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  program_id
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;program_id&lt;/code&gt; is the public key (the address) of the currently executing program. In our case, this will be the public key of our HelloWorld program.&lt;/p&gt;

&lt;h3&gt;
  
  
  accounts
&lt;/h3&gt;

&lt;p&gt;Accounts really deserve their own post. For now, I'll keep it brief. The &lt;code&gt;accounts&lt;/code&gt; is an &lt;strong&gt;ordered&lt;/strong&gt; list of the needed accounts for this transaction. The only properties that an account owner can modify are &lt;code&gt;lamports&lt;/code&gt; and &lt;code&gt;data&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  instruction_data
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;_instruction_data&lt;/code&gt; is specific to the program and is usually used to specify what operations the program should perform. In this helloworld example, we only have one instruction, "greet", so the &lt;code&gt;instruction_data&lt;/code&gt; is unused, hence the preceding &lt;code&gt;_&lt;/code&gt;. That's a Rust idiom to keep the compiler happy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logging
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;msg!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World Rust program entrypoint"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can output logs from our program using the &lt;code&gt;msg!&lt;/code&gt; macro. The caution here is that these messages are visible by viewing the program logs on the &lt;a href="https://explorer.solana.com/tx/2hnw1ugqChuGrVq1GdBk63a3rHk3GvcoyuXLS9MyUneza9M4hYcqGdBUSCr2Hp6djEvzFPv34dV4iMXkGCQJjFft"&gt;solana explorer&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accounts
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;accounts_iter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;next_account_info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;accounts_iter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take the list of &lt;code&gt;accounts&lt;/code&gt; we received via our entrypoint and convert it into an iterable that we can iterate over. The &lt;a href="https://github.com/solana-labs/solana-program-library"&gt;Solana program library&lt;/a&gt; provides the &lt;code&gt;next_account_info&lt;/code&gt; function  that returns the next account in the list.&lt;/p&gt;

&lt;p&gt;It's important to note that programming in Solana requires you to think about your "data model" or "program model", as it were. Since we have to specify all the accounts a transaction will need, we need to think about; what accounts our program needs, the purpose of the account, and the relationships between them. For example, when transferring lamports, an instruction may define the first account as the source and the second as the destination.&lt;/p&gt;

&lt;p&gt;The instructions our program can perform are essentially the API of our program. Therefore, we must know how to call these APIs and what order  we need to provide the accounts. We specify this in an &lt;code&gt;instructions.rs&lt;/code&gt; file. An example of this can be found in the &lt;a href="https://github.com/solana-labs/solana-program-library/blob/master/token/program/src/instruction.rs#L32"&gt;token program&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Validation and Security
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="py"&gt;.owner&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;program_id&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;msg!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Greeted account does not have the correct program id"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;ProgramError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;IncorrectProgramId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's always good practice to validate the accounts you receive and assert that they have the correct permissions and are the accounts you are expecting.&lt;/p&gt;

&lt;p&gt;Since our program needs to modify the &lt;code&gt;data&lt;/code&gt; property, we need to check that the program is the owner of the account we are about to change. If not, we error out of the program.&lt;/p&gt;

&lt;p&gt;Have a look at programs written in the &lt;a href="https://github.com/solana-labs/solana-program-library"&gt;Solana Program Library&lt;/a&gt;. You'll always find an exhaustive list of checks validating the inputs to the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Serialization / Deserialization
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;greeting_account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;GreetingAccount&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;try_from_slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="py"&gt;.data&lt;/span&gt;&lt;span class="nf"&gt;.borrow&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;greeting_account&lt;/span&gt;&lt;span class="py"&gt;.counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hidden in here are a lot of rust idioms, but we'll focus on the deserializing part.&lt;/p&gt;

&lt;p&gt;In Solana, an account can store ANY binary data. The Solana blockchain doesn't know what that data is. It's the responsibility of the program or client to understand it. For you to store data, you incur a storage cost called &lt;em&gt;rent&lt;/em&gt;. Rent is another pretty big topic, so we'll gloss over it for now and do an in-depth look when we do a teardown of the client app in a future post.&lt;/p&gt;

&lt;p&gt;Back to the data. Each account can store its own type of data. In our case, the data we are storing is of type &lt;code&gt;GreetingAccount&lt;/code&gt;. Since we store the data in binary, we need to deserialize it when reading it and serialize it when saving it.&lt;/p&gt;

&lt;p&gt;Let's retake a look at our struct.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[derive(BorshSerialize,&lt;/span&gt; &lt;span class="nd"&gt;BorshDeserialize,&lt;/span&gt; &lt;span class="nd"&gt;Debug)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;GreetingAccount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/// number of greetings&lt;/span&gt;
    &lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To increment the counter, we need to deserialize the binary data into a &lt;code&gt;GreetingAccount&lt;/code&gt;. For this, we use the &lt;a href="https://crates.io/crates/borsh"&gt;borsh&lt;/a&gt; library. Solana doesn't enforce how we serialize or deserialize our data, so the choice of library is up to us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;greeting_account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;GreetingAccount&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;try_from_slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="py"&gt;.data&lt;/span&gt;&lt;span class="nf"&gt;.borrow&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We take the binary data in &lt;code&gt;account.data&lt;/code&gt; and &lt;strong&gt;try&lt;/strong&gt; to deserialize it into a &lt;code&gt;GreetingAccount&lt;/code&gt;. If that fails, we'll get an error.&lt;/p&gt;

&lt;p&gt;You can read more on the &lt;code&gt;.borrow()&lt;/code&gt; syntax from the &lt;a href="https://docs.solana.com/developing/on-chain-programs/developing-rust#data-types"&gt;official Solana docs&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;greeting_account&lt;/span&gt;&lt;span class="py"&gt;.counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have successfully deserialized the data, we can now increment the counter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="n"&gt;greeting_account&lt;/span&gt;&lt;span class="nf"&gt;.serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="py"&gt;.data&lt;/span&gt;&lt;span class="nf"&gt;.borrow_mut&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We call the serialize method on the &lt;code&gt;greeting_acocunt&lt;/code&gt;, which serializes the &lt;code&gt;GreetingAccount&lt;/code&gt; struct and updates the &lt;code&gt;data&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;msg!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Greeted {} time(s)!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;greeting_account&lt;/span&gt;&lt;span class="py"&gt;.counter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then log the new counter and return the result of our program to signal that this transaction was successful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/solana-labs/example-helloworld/blob/master/src/program-rust/src/lib.rs"&gt;helloworld&lt;/a&gt; Rust program&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.solana.com/developing/programming-model/overview"&gt;Solana programming model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.youtube.com/c/Solanalabs/videos"&gt;Solana&lt;/a&gt; YouTube channel&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/solana-labs/solana-program-library"&gt;Solana Program Library&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Footnotes
&lt;/h2&gt;

&lt;p&gt;Thank you for the guidance, support and review of this article.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/therealchaseeb"&gt;@therealchaseeb&lt;/a&gt;, &lt;a href="https://twitter.com/jamesflorentino"&gt;@jamesflorentino&lt;/a&gt; and &lt;a href="https://github.com/FrankC01"&gt;@fastfrank&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Follow me on &lt;a href="https://twitter.com/C_Ogoo"&gt;twitter&lt;/a&gt; to read more about developing on Solana.&lt;/p&gt;

</description>
      <category>solana</category>
      <category>rust</category>
      <category>beginners</category>
      <category>web3</category>
    </item>
    <item>
      <title>Solana: How to send custom instructions via instruction data</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Sat, 16 Oct 2021 15:16:52 +0000</pubDate>
      <link>https://dev.to/cogoo/solana-how-to-send-custom-instructions-via-instruction-data-4g9g</link>
      <guid>https://dev.to/cogoo/solana-how-to-send-custom-instructions-via-instruction-data-4g9g</guid>
      <description>&lt;p&gt;In this article, we'll walk through the process of sending a custom instruction to a solana on-chain program. We'll modify the solana &lt;a href="https://github.com/solana-labs/example-helloworld" rel="noopener noreferrer"&gt;example helloworld&lt;/a&gt; to take two instructions, &lt;code&gt;SayHello&lt;/code&gt; and &lt;code&gt;SayGoodbye&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a look at the final result:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37ke6vjrdsga2mc61cao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37ke6vjrdsga2mc61cao.png" alt="output from "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get started. Picking up from where the &lt;a href="https://github.com/solana-labs/example-helloworld" rel="noopener noreferrer"&gt;helloworld&lt;/a&gt; tutorial leaves us.&lt;/p&gt;
&lt;h2&gt;
  
  
  Define API of the program
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// instruction.rs&lt;/span&gt;
&lt;span class="nd"&gt;#[derive(Debug)]&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;HelloInstruction&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;SayBye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We create an &lt;code&gt;enum&lt;/code&gt; that will contain what operations our program is able to perform.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;instruction.rs&lt;/code&gt; is responsible for decoding &lt;code&gt;instruction_data&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Decode &lt;code&gt;instruction_data&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// instruction.rs
&lt;span class="gi"&gt;+use solana_program::{program_error::ProgramError};
+
&lt;/span&gt;#[derive(Debug)]
&lt;span class="p"&gt;pub enum HelloInstruction {
&lt;/span&gt;    SayHello,
    SayBye,
}
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="gi"&gt;+impl HelloInstruction {
+    pub fn unpack(input: &amp;amp;[u8]) -&amp;gt; Result&amp;lt;Self, ProgramError&amp;gt; {
+        let (&amp;amp;tag, rest) = input
+            .split_first()
+            .ok_or(ProgramError::InvalidInstructionData)?;
+
+        Ok(match tag {
+            0 =&amp;gt; HelloInstruction::SayHello,
+            1 =&amp;gt; HelloInstruction::SayBye,
+            _ =&amp;gt; return Err(ProgramError::InvalidInstructionData),
+        })
+    }
+}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We create an associated function called &lt;code&gt;unpack&lt;/code&gt; which takes the &lt;code&gt;instruction_data&lt;/code&gt; and returns either a variant of the &lt;code&gt;HelloInstruction&lt;/code&gt; or a &lt;code&gt;ProgramError&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// --snip--&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;
    &lt;span class="nf"&gt;.split_first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;.ok_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;ProgramError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InvalidInstructionData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// --snip--&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We split the &lt;code&gt;instruction_data&lt;/code&gt; slice and receive a tuple containing &lt;code&gt;&amp;amp;tag&lt;/code&gt; which is the first element and &lt;code&gt;rest&lt;/code&gt; which are the remaining bytes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;amp;tag&lt;/code&gt; (a number/u8 ranges; 0 - 255) is mapped 1:1 to an operation. This becomes clearer when we create the instruction on the client, more on that later on.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rest&lt;/code&gt; will have the remaining bytes that may contain additional information that the operation will need.&lt;/p&gt;

&lt;p&gt;We transform the &lt;code&gt;Option&lt;/code&gt; with &lt;code&gt;.ok_or(ProgramError::InvalidInstructionData)?;&lt;/code&gt; into a &lt;code&gt;Result&lt;/code&gt;, if the &lt;code&gt;Option&lt;/code&gt; is of the &lt;code&gt;None&lt;/code&gt; variant we return an &lt;code&gt;InvalidInstructionData&lt;/code&gt; error. More on the &lt;a href="https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or" rel="noopener noreferrer"&gt;ok_or&lt;/a&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;HelloInstruction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;HelloInstruction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SayBye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;Err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;ProgramError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InvalidInstructionData&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now match the &lt;code&gt;tag&lt;/code&gt; to know what operation our program should run. We return an error if none of the known instructions are matched.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update &lt;code&gt;process_instruction&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// lib.rs
// --snip--
&lt;span class="gi"&gt;+pub mod instruction;
+use crate::instruction::HelloInstruction;
&lt;/span&gt;// --snip--
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;pub fn process_instruction(
&lt;/span&gt;    program_id: &amp;amp;Pubkey, // Public key of the account the hello world program was loaded into
    accounts: &amp;amp;[AccountInfo], // The account to say hello to
    instruction_data: &amp;amp;[u8],
) -&amp;gt; ProgramResult {
    msg!("Hello World Rust program entrypoint");
&lt;span class="gi"&gt;+
+   let instruction = HelloInstruction::unpack(instruction_data)?;
+   msg!("Instruction: {:?}", instruction);
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;    // Iterating accounts is safer then indexing
    let accounts_iter = &amp;amp;mut accounts.iter();
&lt;span class="err"&gt;
&lt;/span&gt;    // Get the account to say hello to
    let account = next_account_info(accounts_iter)?;
&lt;span class="err"&gt;
&lt;/span&gt;    // The account must be owned by the program in order to modify its data
    if account.owner != program_id {
        msg!("Greeted account does not have the correct program id");
        return Err(ProgramError::IncorrectProgramId);
    }
&lt;span class="err"&gt;
&lt;/span&gt;    // Increment and store the number of times the account has been greeted
    let mut greeting_account = GreetingAccount::try_from_slice(&amp;amp;account.data.borrow())?;
    greeting_account.counter += 1;
    greeting_account.serialize(&amp;amp;mut &amp;amp;mut account.data.borrow_mut()[..])?;
&lt;span class="err"&gt;
&lt;/span&gt;    msg!("Greeted {} time(s)!", greeting_account.counter);
&lt;span class="err"&gt;
&lt;/span&gt;    Ok(())
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We bring our &lt;code&gt;HelloInstruction&lt;/code&gt; into scope with &lt;code&gt;use crate::instruction::HelloInstruction;&lt;/code&gt; and attempt to decode the instruction with &lt;code&gt;let instruction = HelloInstruction::unpack(instruction_data)?;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For now we'll just log the instruction. Later on we'll modify our code to use this instruction.&lt;/p&gt;

&lt;p&gt;A quick recap of what we've done so far:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Defined our program API with the &lt;code&gt;HelloInstruction&lt;/code&gt; &lt;code&gt;enum&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Unpacked (aka deserialize) our &lt;code&gt;instruction_data&lt;/code&gt; byte array to retrieve the operation from our client&lt;/li&gt;
&lt;li&gt;Logged the decoded instruction&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's &lt;code&gt;build&lt;/code&gt; and &lt;code&gt;re-deploy&lt;/code&gt; our program. If you're unsure how to do this, take a look at the &lt;a href="https://github.com/solana-labs/example-helloworld" rel="noopener noreferrer"&gt;example helloworld docs&lt;/a&gt;. We can now run our client and see what our log message shows.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fql84n6reb1l0uuvgn671.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fql84n6reb1l0uuvgn671.png" alt="output from solana logs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We get an error: &lt;code&gt;Program log: Instruction Err(InvalidInstructionData)&lt;/code&gt; because our client is not passing a valid instruction. Let's fix that.&lt;/p&gt;
&lt;h2&gt;
  
  
  Update &lt;code&gt;sayHello&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// hello_world.ts
// --snip --
&lt;span class="err"&gt;
&lt;/span&gt;/**
 * Say hello
 */
&lt;span class="p"&gt;export async function sayHello(): Promise&amp;lt;void&amp;gt; {
&lt;/span&gt;  console.log('Saying hello to', greetedPubkey.toBase58());
  const instruction = new TransactionInstruction({
    keys: [{pubkey: greetedPubkey, isSigner: false, isWritable: true}],
    programId,
&lt;span class="gd"&gt;-   data: Buffer.alloc(0),
&lt;/span&gt;&lt;span class="gi"&gt;+   data: createSayHelloInstructionData(),
&lt;/span&gt;  });
  await sendAndConfirmTransaction(
    connection,
    new Transaction().add(instruction),
    [payer],
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We replace the call to &lt;code&gt;Buffer.alloc(0)&lt;/code&gt; with our new function &lt;code&gt;createSayHelloInstructionData()&lt;/code&gt; which will return a &lt;code&gt;Buffer&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Create &lt;code&gt;createSayHelloInstructionData&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;For this we'll need to install two libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i @solana/buffer-layout buffer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// hello_world.ts&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;BufferLayout&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@solana/buffer-layout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;buffer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// --snip--&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createSayHelloInstructionData&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataLayout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BufferLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nx"&gt;BufferLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;instruction&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;]);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;dataLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a buffer layout structure with one field; &lt;code&gt;instruction&lt;/code&gt;. This is where we'll encode what operation we want to run in our program, which is a &lt;code&gt;u8&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then allocate a new buffer using the size from the buffer layout that we created earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;dataLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally we encode the instruction (&lt;code&gt;instruction: 0&lt;/code&gt;) into the buffer.&lt;/p&gt;

&lt;p&gt;Th &lt;code&gt;instruction: 0&lt;/code&gt; corresponds to the &lt;code&gt;tag&lt;/code&gt; variable that we get in the first element of the &lt;code&gt;instruction_data&lt;/code&gt; slice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// --snip--&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;input&lt;/span&gt;
    &lt;span class="nf"&gt;.split_first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;.ok_or&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;ProgramError&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InvalidInstructionData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;HelloInstruction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SayHello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;HelloInstruction&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;SayBye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

&lt;span class="c1"&gt;// --snip--&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when we run our client, we see that our instruction has been decoded correctly.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7g6ffw6g5f0ctll4hi3e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7g6ffw6g5f0ctll4hi3e.png" alt="console output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We get the correct operation: &lt;code&gt;Program log: Instruction Ok(SayHello)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Great! we're almost done. Have a go at creating the &lt;code&gt;createSayByeInstructionData&lt;/code&gt; function. Which is identical to the &lt;code&gt;createSayHelloInstructionData&lt;/code&gt; function except for the instruction that we need to send.&lt;/p&gt;

&lt;p&gt;Now let's change our program to decrement the greeting counter when we received a &lt;code&gt;SayBye&lt;/code&gt; operation.&lt;/p&gt;
&lt;h2&gt;
  
  
  Update &lt;code&gt;process_instruction&lt;/code&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;pub fn process_instruction(
&lt;/span&gt;    program_id: &amp;amp;Pubkey, // Public key of the account the hello world program was loaded into
    accounts: &amp;amp;[AccountInfo], // The account to say hello to
    instruction_data: &amp;amp;[u8], // Ignored, all helloworld instructions are hellos
) -&amp;gt; ProgramResult {
    msg!("Hello World Rust program entrypoint");
    let instruction = HelloInstruction::unpack(instruction_data)?;
    msg!("Instruction {:?}", instruction);
&lt;span class="err"&gt;
&lt;/span&gt;    // Iterating accounts is safer then indexing
    let accounts_iter = &amp;amp;mut accounts.iter();
&lt;span class="err"&gt;
&lt;/span&gt;    // Get the account to say hello to
    let account = next_account_info(accounts_iter)?;
&lt;span class="err"&gt;
&lt;/span&gt;    // The account must be owned by the program in order to modify its data
    if account.owner != program_id {
        msg!("Greeted account does not have the correct program id");
        return Err(ProgramError::IncorrectProgramId);
    }
&lt;span class="err"&gt;
&lt;/span&gt;    // Increment and store the number of times the account has been greeted
    let mut greeting_account = GreetingAccount::try_from_slice(&amp;amp;account.data.borrow())?;
&lt;span class="gi"&gt;+
+   match instruction {
+       HelloInstruction::SayHello =&amp;gt; {
&lt;/span&gt;            greeting_account.counter += 1;
&lt;span class="gi"&gt;+       },
+       HelloInstruction::SayBye =&amp;gt; {
+           greeting_account.counter -= 1;
+       },
+       _ =&amp;gt; {}
+   }
+
&lt;/span&gt;    greeting_account.serialize(&amp;amp;mut &amp;amp;mut account.data.borrow_mut()[..])?;
&lt;span class="err"&gt;
&lt;/span&gt;    msg!("Greeted {} time(s)!", greeting_account.counter);
&lt;span class="err"&gt;
&lt;/span&gt;    Ok(())
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now that we've made a change to our program. We need to build and deploy it again.&lt;/p&gt;

&lt;p&gt;Let's run our client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmu1wct70den07565rdvi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmu1wct70den07565rdvi.png" alt="Console output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running our client multiple times to show that our greeting count is being incremented.&lt;/p&gt;

&lt;p&gt;Let's round this up by sending a &lt;code&gt;SayBye&lt;/code&gt; operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;// --snip--
&lt;span class="p"&gt;export async function sayHello(): Promise&amp;lt;void&amp;gt; {
&lt;/span&gt;  console.log('Saying hello to', greetedPubkey.toBase58());
  const instruction = new TransactionInstruction({
    keys: [{pubkey: greetedPubkey, isSigner: false, isWritable: true}],
    programId,
&lt;span class="gd"&gt;-   data: createSayHelloInstructionData(),
&lt;/span&gt;&lt;span class="gi"&gt;+   data: createSayByeInstructionData(),
&lt;/span&gt;  });
  await sendAndConfirmTransaction(
    connection,
    new Transaction().add(instruction),
    [payer],
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createSayByeInstructionData&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataLayout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BufferLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;BufferLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;instruction&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)]);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;dataLayout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;instruction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We run our client again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fonuwgmxjzgtu1rdnpay1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fonuwgmxjzgtu1rdnpay1.png" alt="Console output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our program correctly decodes the &lt;code&gt;SayBye&lt;/code&gt; operation and decrements our greeting count.&lt;/p&gt;

&lt;p&gt;Congratulations! We've managed to modify our program to specify what operation the program should perform.&lt;/p&gt;




&lt;p&gt;Follow me on &lt;a href="https://twitter.com/C_Ogoo" rel="noopener noreferrer"&gt;twitter&lt;/a&gt; to read more about developing on Solana.&lt;/p&gt;

</description>
      <category>solana</category>
      <category>web3</category>
      <category>rust</category>
      <category>beginners</category>
    </item>
    <item>
      <title>VS Code Knowledge Pill: Profiles switching using Workspaces</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Sun, 10 Oct 2021 19:15:10 +0000</pubDate>
      <link>https://dev.to/cogoo/vs-code-knowledge-pill-profiles-switching-using-workspaces-4gl1</link>
      <guid>https://dev.to/cogoo/vs-code-knowledge-pill-profiles-switching-using-workspaces-4gl1</guid>
      <description>&lt;p&gt;I've longed for a profile/settings switcher in VS Code that loads specific settings or extensions depending on a specific project or what programming language I'm using.&lt;/p&gt;

&lt;p&gt;We can use VS Code's workspaces to act as a profile switcher.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an empty workspace&lt;/li&gt;
&lt;li&gt;Enable extensions for the workspace&lt;/li&gt;
&lt;li&gt;Configure workspace specific settings&lt;/li&gt;
&lt;li&gt;Add project to workspace&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I7i4GzgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6l2ygqtu6gn2izaqbddh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I7i4GzgH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6l2ygqtu6gn2izaqbddh.png" alt="Screenshot 2021-10-10 at 20.11.48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This workspace is now configured with all the extensions and settings that I need for working on a Rust project.&lt;/p&gt;

</description>
      <category>vscode</category>
    </item>
    <item>
      <title>Angular: Determine previous page URL</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Fri, 19 Feb 2021 22:32:58 +0000</pubDate>
      <link>https://dev.to/cogoo/angular-determine-previous-page-url-3kh9</link>
      <guid>https://dev.to/cogoo/angular-determine-previous-page-url-3kh9</guid>
      <description>&lt;p&gt;The &lt;code&gt;getCurrentNavigation&lt;/code&gt; method of the angular router returns a property &lt;code&gt;previousNavigation&lt;/code&gt; that has details on the previous navigation.&lt;/p&gt;

&lt;p&gt;This can be used to determine the previous page url, or even if there was no previous navigation (initial load of a particular route).&lt;/p&gt;

&lt;p&gt;It's worth noting that this method only works when used in the &lt;code&gt;constructor&lt;/code&gt; of a components class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;SomeComponent&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;previousNavigation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getCurrentNavigation&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;previousNavigation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;previousUrl&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;previousNavigation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;finalUrl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More on the &lt;a href="https://angular.io/api/router/Navigation"&gt;Router.getCurrentNavigation() method&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
    </item>
    <item>
      <title>RxJS Patterns: Creating an Action Stream</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Wed, 09 Oct 2019 18:15:50 +0000</pubDate>
      <link>https://dev.to/cogoo/angular-the-action-stream-rxjs-439m</link>
      <guid>https://dev.to/cogoo/angular-the-action-stream-rxjs-439m</guid>
      <description>&lt;p&gt;&lt;a href="https://rxjs.dev"&gt;RxJS&lt;/a&gt; is an extremely powerful observable based library, used at the very core of Angular, that allows us to write our applications in a truly reactive paradigm.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rxjs.dev/guide/operators#creation-operators"&gt;Creation operators&lt;/a&gt; are standalone functions that can create a new observable.&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;::val&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: 1, 2, 3&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These type of operators pose a new challenge when you want to follow a truly reactive paradigm.&lt;/p&gt;

&lt;blockquote data-lang="en"&gt;
&lt;p&gt;In &lt;a href="https://twitter.com/hashtag/rxjs?src=hash&amp;amp;ref_src=twsrc%5Etfw"&gt;#rxjs&lt;/a&gt; .subscribe() is where reactive programming ends&lt;/p&gt;— Michael Rx Hladky (&lt;a class="mentioned-user" href="https://dev.to/michael_hladky"&gt;@michael_hladky&lt;/a&gt;) &lt;a href="https://twitter.com/Michael_Hladky/status/1180316203937681410?ref_src=twsrc%5Etfw"&gt;October 5, 2019&lt;/a&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using observables comes with a greater responsibility of always unsubscribing. Fortunately Angular provides an &lt;code&gt;async&lt;/code&gt; pipe that handles the subscription and un-subscription logic for us. This worked great until I had to make a http post request. &lt;/p&gt;

&lt;p&gt;There was no obvious way to use the &lt;code&gt;async&lt;/code&gt; pipe in this context, as this request is made upon a user action. And I really, really, did not want to use &lt;code&gt;.subscribe()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Cue the action stream:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- some.component.html --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"{ postAction: postStream$ | async } as postStream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We subscribe to a &lt;code&gt;postStream$&lt;/code&gt; using the &lt;code&gt;async&lt;/code&gt; pipe. The observable will not emit any values until the http request has returned, therefore, we wrap the subscription in an object. This will be a truthy value, so the &lt;code&gt;*ngIf&lt;/code&gt; condition will be met, which allows our template to always be rendered.&lt;/p&gt;

&lt;p&gt;The complete template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- some.component.html --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"{ postAction: postStream$ | async } as postStream"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Click the button to see the magic happen :)&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;(click)=&lt;/span&gt;&lt;span class="s"&gt;"onBtnClick()"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Post data to back end&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;

  &lt;span class="c"&gt;&amp;lt;!-- You can use this to show a success or error message, or loading state --&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"postStream.postAction"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Saved!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our component file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.component.ts&lt;/span&gt;

&lt;span class="c1"&gt;// create your action stream&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;postAction$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Subject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create an action stream; simply a subject that we'll call &lt;code&gt;.next()&lt;/code&gt; on when an event in our template has taken place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.component.ts&lt;/span&gt;

&lt;span class="c1"&gt;// subscribe to this post stream (high order observable) in your template to start listening &lt;/span&gt;
&lt;span class="c1"&gt;// for values emitted by the action stream&lt;/span&gt;
&lt;span class="nx"&gt;postStream$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postAction$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;// pick the correct operator for your use case&lt;/span&gt;
  &lt;span class="nx"&gt;exhaustMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this could be your http post&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a higher order observable &lt;code&gt;postStream$&lt;/code&gt;, that has an outer observable, our action stream we created earlier, and an inner observable that we map to. For this example we use a creation operator &lt;code&gt;of()&lt;/code&gt; but this could be a http post request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.component.ts&lt;/span&gt;

&lt;span class="nx"&gt;onBtnClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postAction$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then have a method on the class that will trigger our action stream.&lt;/p&gt;

&lt;p&gt;The complete component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.component.ts&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppComponent&lt;/span&gt;  &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// create your action stream&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;postAction$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Subject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// subscribe to this post stream (high order observable) in your template to start listening &lt;/span&gt;
  &lt;span class="c1"&gt;// for values emitted by the action stream&lt;/span&gt;
  &lt;span class="nx"&gt;postStream$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postAction$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;// pick the correct operator for your use case&lt;/span&gt;
    &lt;span class="nx"&gt;exhaustMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my post&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this could be your http post&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;onBtnClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postAction$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this approach, we can avoid having to manually call &lt;code&gt;.subscribe()&lt;/code&gt; when making http post requests.&lt;/p&gt;

&lt;p&gt;You can find the full code on &lt;a href="https://stackblitz.com/edit/rxjs-action-stream"&gt;StackBlitz&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>rxjs</category>
      <category>rxjspatterns</category>
    </item>
    <item>
      <title>Sizing Units in CSS: REM and EM</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Tue, 24 Sep 2019 08:42:12 +0000</pubDate>
      <link>https://dev.to/cogoo/sizing-units-in-css-rem-and-em-4ea2</link>
      <guid>https://dev.to/cogoo/sizing-units-in-css-rem-and-em-4ea2</guid>
      <description>&lt;p&gt;CSS has different units for expressing length. Many units take a length property, such as: &lt;code&gt;font-size&lt;/code&gt;, &lt;code&gt;padding&lt;/code&gt;, &lt;code&gt;width&lt;/code&gt; etc. There are two types of length units: absolute and relative.&lt;/p&gt;

&lt;p&gt;This article focuses on two relative units: REM and EM. We discuss how the browser converts these into absolute units.&lt;/p&gt;

&lt;h2&gt;
  
  
  REM to PX
&lt;/h2&gt;

&lt;p&gt;The browser uses the root element's (&lt;code&gt;html&lt;/code&gt;) font size as the multiplier when converting to pixels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1rem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;font-size&lt;/code&gt; of the &lt;code&gt;h1&lt;/code&gt; will be &lt;code&gt;16px&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The calculation the browser runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# root font size * property rem unit = px value&lt;/span&gt;
            16px &lt;span class="k"&gt;*&lt;/span&gt; 1rem              &lt;span class="o"&gt;=&lt;/span&gt; 16px
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  EM to PX
&lt;/h2&gt;

&lt;p&gt;The browser uses the &lt;code&gt;font-size&lt;/code&gt; of the element that is being styled as the multiplier when converting to pixels. It will bubble up until it gets the first explicit font-size definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;16px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;22px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2em&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;font-size&lt;/code&gt; of the &lt;code&gt;h1&lt;/code&gt; will be &lt;code&gt;44px&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The calculation the browser runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# element font size * property em unit = px value&lt;/span&gt;
               22px &lt;span class="k"&gt;*&lt;/span&gt; 2em              &lt;span class="o"&gt;=&lt;/span&gt; 44px

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this scenario, the element font size is inherited from the parent &lt;code&gt;div&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It's important to remember that &lt;code&gt;font-size&lt;/code&gt; inheritance will come into effect if no &lt;code&gt;font-size&lt;/code&gt; is explicitly set. For &lt;code&gt;rem&lt;/code&gt;, if there is no &lt;code&gt;font-size&lt;/code&gt; explicitly set on the root element, it will inherit the value from the browser settings.&lt;/p&gt;

</description>
      <category>css</category>
      <category>fontsize</category>
      <category>cssunits</category>
    </item>
    <item>
      <title>Git: Exclude a folder when diffing</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Fri, 13 Sep 2019 12:22:42 +0000</pubDate>
      <link>https://dev.to/cogoo/ignore-a-folder-when-doing-a-git-diff-48p2</link>
      <guid>https://dev.to/cogoo/ignore-a-folder-when-doing-a-git-diff-48p2</guid>
      <description>&lt;p&gt;When doing a &lt;code&gt;git diff&lt;/code&gt;, especially between branches, you may be greeted with a mountain of changes that you have to sift through to find what you're looking for. &lt;/p&gt;

&lt;p&gt;Consider excluding certain folders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s1"&gt;':!&amp;lt;folder-name&amp;gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also exclude multiple folders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git diff &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="s1"&gt;':!&amp;lt;folder-name&amp;gt;'&lt;/span&gt; &lt;span class="s1"&gt;':!&amp;lt;other-folder&amp;gt;'&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>git</category>
      <category>gittips</category>
    </item>
    <item>
      <title>TypeScript: How to set a new property on the window object</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Fri, 13 Sep 2019 11:40:02 +0000</pubDate>
      <link>https://dev.to/cogoo/how-to-set-a-new-property-on-the-window-object-in-typescript-3jeh</link>
      <guid>https://dev.to/cogoo/how-to-set-a-new-property-on-the-window-object-in-typescript-3jeh</guid>
      <description>&lt;p&gt;When merging global interfaces in TypeScript, you have to declare the interface in the &lt;code&gt;global&lt;/code&gt; scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;declare&lt;/span&gt; &lt;span class="nb"&gt;global&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Window&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// add you custom properties and methods&lt;/span&gt;
    &lt;span class="nl"&gt;YT&lt;/span&gt;&lt;span class="p"&gt;:(&lt;/span&gt;&lt;span class="nx"&gt;videoID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;YtResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;TypeScript supports &lt;a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html"&gt;declaration merging&lt;/a&gt;, the premise is the same for global objects. The difference is to declare the interface as part of the global scope.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>interfaces</category>
      <category>declarationmerging</category>
      <category>globalaugmentation</category>
    </item>
    <item>
      <title>The Fullstack Angular Developer</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Wed, 17 Jul 2019 07:58:32 +0000</pubDate>
      <link>https://dev.to/cogoo/the-fullstack-angular-developer-ihg</link>
      <guid>https://dev.to/cogoo/the-fullstack-angular-developer-ihg</guid>
      <description>&lt;p&gt;TLDR: The parallels between an Angular and NestJS app are striking. You can share types, functions et al between the two platforms, allowing you, an Angular developer, to write a fully featured Backend application using Angular paradigms.&lt;/p&gt;

&lt;p&gt;My thoughts on the term “Fullstack” are firmly attached to a pendulum. It’s currently swinging in the region of “there is no such thing”. Each discipline alone is too broad to have in-depth knowledge on both. You may be able to spin up an API server with Express, but that doesn’t make you “Fullstack”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/a93jwI0wkWTQs/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/a93jwI0wkWTQs/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nevertheless, the term Fullstack is present in our industry and I've seen a number of job postings that have a React + NodeJS pairing. This got me thinking about what the stack would be in the Angular ecosystem. Behold, NestJS.&lt;/p&gt;

&lt;p&gt;We recently had to build a feature that required some Restful APIs. In order not to be blocked, we went down the "easier" 🤨 route of creating our own with NodeJS. We were already using Nx to manage our monorepo so it was trivial to add NestJS to our project.&lt;/p&gt;

&lt;p&gt;The learning curve was next to nothing. NestJS was heavily inspired by Angular, so you'll feel right at home. TypeScript, Decorators, Services, Modules etc are all featured in Nest. It also does a really good job at abstracting some key concepts when it comes to creating Restful API's. HTTP response codes are automatically returned. Get, Post methods and a majority of other enhanced functionality are handled by decorators.&lt;/p&gt;

&lt;p&gt;The biggest barrier I find when learning a new framework or library is the documentation and lack of available resources. Thankfully this is not an issue with NestJS. The docs are beautiful and due to the rise in popularity of Nest, there are a ton of helpful resources. In addition to that, there are schematics for the Angular CLI, so scaffolding is a breeze.&lt;/p&gt;

&lt;p&gt;Eventually, I see the Angular + Nest stack becoming the de facto for full stack Angular Applications. The low barrier to entry for Angular developers makes it a dream pairing.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>nestjs</category>
      <category>fullstack</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Here we go!</title>
      <dc:creator>C.OG</dc:creator>
      <pubDate>Sat, 27 Apr 2019 10:25:08 +0000</pubDate>
      <link>https://dev.to/cogoo/here-we-go-b25</link>
      <guid>https://dev.to/cogoo/here-we-go-b25</guid>
      <description>&lt;h1&gt;
  
  
  I have conquered it!
&lt;/h1&gt;

&lt;p&gt;"Conquered what?", you'll be asking. I've attempted to start micro blogging over 2 years. For some reason, I still haven't been able to press publish on any of the countless drafts!&lt;/p&gt;

&lt;p&gt;I struggled to decide on what the first post should be about. Does it have to be an explainer of the blog, or just go straight into it; what if it doesn't read well, what if it doesn't sound cohesive? All valid questions.&lt;/p&gt;

&lt;p&gt;As you read this, whether it reads well or not, whether it's cohesive or not. It doesn't matter, because, I have finally been able to press PUBLISH! (Or submit in this case)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/t3DDmrHb2jSQE/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/t3DDmrHb2jSQE/giphy.gif" alt="Celebration"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>microblog</category>
    </item>
  </channel>
</rss>
