<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Panasthetik on Medium]]></title>
        <description><![CDATA[Stories by Panasthetik on Medium]]></description>
        <link>https://medium.com/@panasthetik?source=rss-351ecf24f9f1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fPPDE6FQ7aAasK9o</url>
            <title>Stories by Panasthetik on Medium</title>
            <link>https://medium.com/@panasthetik?source=rss-351ecf24f9f1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 07:03:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@panasthetik/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[An Intro To Near Dapp Development With Rust, WASM, Parcel and React -Part 3.]]></title>
            <link>https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-3-6e4782dce459?source=rss-351ecf24f9f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6e4782dce459</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[near-protocol]]></category>
            <dc:creator><![CDATA[Panasthetik]]></dc:creator>
            <pubDate>Tue, 29 Mar 2022 02:50:27 GMT</pubDate>
            <atom:updated>2022-03-29T07:08:27.022Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sukc6HX0zVtmY8tJL8Eliw.jpeg" /></figure><p>This article introduces existing Web3 developers to the Near Protocol with a simple, functional, start-to-finish example in three parts.</p><p>For those who are just joining us, feel free to look over <strong>the first two parts of this tutorial</strong> to get up to speed:</p><p>Part 1 can be found <a href="https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-1-e500a3abc597?source=friends_link&amp;sk=216f6365fcd46133ae278961c5260243"><strong>here</strong></a>, and Part 2 is located <a href="https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-2-6bd3a071b74e?source=friends_link&amp;sk=620e52f438cf57780e3c5de22044a704"><strong>here</strong></a><strong>.</strong></p><p><strong>The entire code repository for these articles can be found </strong><a href="https://github.com/Panasthetik/near-starter-app"><strong>here</strong></a><strong>.</strong></p><p>Last time, we continued our developer journey on the Near blockchain by writing some Rust unit tests to run against our smart contract. We then created a deployment sub-account in Near-CLI, deployed our contract to Near Testnet, and were able to interact with it in the developer console.</p><p>For this week’s session, we will integrate our deployed smart contract with a simple React front end. This JavaScript portion of our tutorial can then be adapted to your needs as a starter template (for example, you may choose to build a more comprehensive and responsive interface with Next.js or another framework in the future).</p><p><strong>Some follow-up items before we switch to React / JavaScript</strong></p><p>As noted at the end of the last session, there are a couple of “loose ends” to tie up with our contract deployment before we proceed with our front end. One of these is addressing how we can “upgrade” our smart contract on Near when there are changes to the Rust code or if something doesn’t work as expected.</p><p>One of the advantages of developing on the Near blockchain is this ability to rewrite Rust code, recompile to WASM, and replace contracts completely when we need to <em>at the same contract address</em>.</p><p>If you recall how we first created a sub-account on the Near Testnet before we actually deployed the contract, you will notice the following code for sub-account creation in VSCode terminal:</p><pre>near create-account near-starter-dapp.myaccount.testnet --masterAccount myaccount.testnet</pre><p>In fact, the process for updating the smart contract first depends on <strong>deleting</strong> this sub-account — once the sub-account is deleted, the contract is gone from Near Testnet and can be replaced. In the VSCode terminal, enter:</p><pre>near delete near-starter-dapp.myaccount.testnet myaccount.testnet</pre><p>Notice that you don’t have to state “masterAccount” here like you did with sub-account creation. As long as you’re still signed into Near with the master account, you have the authority to delete any sub-addresses you have created with it like above.</p><p><strong>Upgrading / replacing a smart contract on Near</strong></p><p>The first thing we need to do now is re-create the same sub-account again using the identical process as before (also shown above):</p><pre>near create-account near-starter-dapp.myaccount.testnet --masterAccount myaccount.testnet</pre><p>Of course you could name the new sub-account differently, or keep alternate versions of the contract itself under separate sub-account names on the Testnet.</p><p>However — it’s generally good practice to delete and re-create the smart contract sub-account like shown each time you upgrade, to keep things more organized on Near. This way, we reduce wasteful, duplicate contracts out on the blockchain that aren’t in use!</p><p>IMPORTANT: <em>Now that we have an empty account created again to contain the smart contract, we can make our changes back in Rust. I will leave any potential edits up to the developer, but once we change the Rust code we will also need to DELETE the WASM file in the “res” folder before we re-compile it with our build script. Double check the “res” folder and make sure it is empty before proceeding!</em></p><p>After deleting the following from the “res” folder after your Rust changes…</p><p>“res/near_starter_dapp.wasm”</p><p>Let’s recompile our new revised Rust code with the same build script we’ve been using all along in our tutorial:</p><pre>bash ./build.sh</pre><p>After the compilation finishes you should have a new, updated WASM file in “res”. We can then run a deploy command again, as before:</p><pre>near deploy --accountId near-starter-dapp.myaccount.testnet --wasmFile res/near_starter_dapp.wasm</pre><p>And if all goes smoothly, you should again see the confirmation screen for deployment like last time in your terminal:</p><pre>Starting deployment. Account id: near-starter-dapp.myaccount.testnet, node: https://rpc.testnet.near.org, helper: https://helper.testnet.near.org, file: res/near_starter_dapp.wasm</pre><pre>Transaction Id 3fqzA9vpuKSSsrBry9jE2k2FjoKFZBCdwPU38fTcPVkTTo </pre><pre>see the transaction in the transaction explorer, please open this url in your browser</pre><pre>https://explorer.testnet.near.org/transactions/3fqzA9vpuKSSsrBry9jE2k2FjoKFZBCdwPU38fTcPVkT</pre><p>NOTE: Your specific transaction ID will be different from the above!</p><p>To round out our “loose ends”, the last question(s) that come up from time to time when developing on Near are:</p><p>“Where is that long hexadecimal string for my deployed contract address out on the blockchain?”</p><p>“You know, the one that I need to call in my React application?”</p><p>“I don’t see that big hex string confirmed in the terminal like in Truffle/Hardhat on an EVM chain (or even like in Anchor on Solana, which is also a long hex string).”</p><p>The answer is: <em>there isn’t a long, complicated hex string </em>on Near for the contract instance — we already reserved a human-readable address for our contract! Remember, to deploy a contract on Near, we created the following sub-account:</p><pre>near-starter-dapp.myaccount.testnet</pre><p>This is the <em>actual address</em> we will paste into our React application to call our functions — no complicated hex strings to keep track of here! It would be the same type of readable contract address on Near Mainnet as well, as long as it was available. For example, it could be something like this:</p><pre>near-starter.myaccount.near</pre><p>So now that we have learned how to “upgrade” our smart contract on the Near Testnet, and we know more about how contract addresses work, we’re ready to switch over to React/JavaScript to integrate with a simple front end template!</p><p><strong>Setting up the directories and config files for the front end integration</strong></p><p>The first thing we need to do is move up one level in our folder structure, out of the “contract” folder (the one that holds the Rust/WASM part of the project) and into our root directory:</p><pre>cd ../</pre><pre>near-starter-dapp $</pre><p>Once we change into this root directory (near-starter-dapp) let’s create a “package.json” file with the following dependencies like below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f04183a0927eafe526ad83bbfa102ac/href">https://medium.com/media/5f04183a0927eafe526ad83bbfa102ac/href</a></iframe><p>As we can see, this list is very concise and minimal compared to some other tutorials out there. The reason is that I used Parcel 2.0+ for bundling, transpiling and the dev server, which eliminates the need for “Babel”, “Bundler” and a number of other common dependencies.</p><p>This gives you a basic, working dev template that runs in the browser on a local port, and it can be enhanced later for a final production version.</p><p>Let’s now run our JS dependencies install in Yarn (or NPM if your prefer — I will use Yarn here):</p><pre>near-starter-dapp $</pre><pre>yarn</pre><p>Now that our project dependencies are installed, we need to create two more files that act similarly to other config files you will find in Anchor, Hardhat or Truffle. Usually these are the “config” files in other frameworks, and they identify network information, wallet configuration, and the contract ID for the front end.</p><p>In the root directory (near-starter-dapp), create an “src” folder:</p><pre>near-starter-dapp $</pre><pre>mkdir src</pre><pre>cd src</pre><p>For our Near configuration files, the first of these should be the following inside our new “src” directory. We will name this file “config.js”:</p><p>config.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a078594c24a18da9c2d2740d5872719/href">https://medium.com/media/5a078594c24a18da9c2d2740d5872719/href</a></iframe><p>This file does two things:</p><ol><li>It defines the smart contract that will be called by our Dapp by contract address (CONTRACT_NAME). You can see that the named contract address at the top is the same as the address we reserved / deployed to! In all cases, this should match whatever address you use when deploying.</li><li>It lists network configurations for Near. In our case, we are only using the Testnet configuration (which is the same as “development” in the config), but it’s good to have all of the options (including Mainnet) in this file for future reference.</li></ol><p>The next file we will create in our “src” folder is an API connector file. It initializes the wallet, active account ID, and the smart contract itself. It also defines the functions we will call with our Dapp. We’ll name this file “utils.js” as shown below:</p><p>utils.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/636760f9663890d29259c396d3238c5a/href">https://medium.com/media/636760f9663890d29259c396d3238c5a/href</a></iframe><p>Some important lines of code to pay attention to here are lines 19–24:</p><pre>window.contract = await newContract(window.walletConnection.account(), nearConfig.contractName, { <br>viewMethods: [&quot;exhibition_count&quot;],    <br>changeMethods: [&#39;add_exhibition&#39;, &#39;add_vote&#39;, &#39;list_exhibitions&#39;, })</pre><p>In this code block, we list each smart contract function we will need to call in our front end application — they are divided between “view methods” (methods that are read-only and do not change the contract state) and “change methods” (functions that initiate state change).</p><p>Of course as we expand our Rust code and build out our project, we can add to this function list, and then in React we can incorporate the corresponding functionality for our front end as well.</p><p>Just make sure you “upgrade” the contract out on Testnet (like we learned how to do above) if you make any further changes in Rust!</p><p><strong>Writing the React components</strong></p><p>Before we work on “App.js” and some component files for React, let’s first add some generic files we will need to our “src” directory — “index.js”, “index.html” and “global.css”:</p><p>index.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ab0775754a78d059ecb8773d9ab7e1b/href">https://medium.com/media/2ab0775754a78d059ecb8773d9ab7e1b/href</a></iframe><p>index.html</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69386e544b5e4c4e4aa984a55ca046f6/href">https://medium.com/media/69386e544b5e4c4e4aa984a55ca046f6/href</a></iframe><p>global.css</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1789eeb4f42a670a20ce80605efd5b6/href">https://medium.com/media/b1789eeb4f42a670a20ce80605efd5b6/href</a></iframe><p>With our basic files out of the way, we will now concentrate on our App.js file, the main entry file to our Dapp. In our “src” folder, add the following:</p><p>App.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b132aa72a80c23347eabddd4b80d2bb0/href">https://medium.com/media/b132aa72a80c23347eabddd4b80d2bb0/href</a></iframe><p>Once we have our App.js file completed, we can then add the final two files inside a new “components” sub-directory. First create a new folder “components” inside the “src” directory:</p><pre>near-starter-dapp/src $</pre><pre>mkdir components</pre><pre>cd components</pre><p>Inside our new “components” folder we will create the files, “CreateEvents.js” and “ListEvents.js” :</p><p>CreateEvents.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c51784cca60d00b94b4a26cd28facbc/href">https://medium.com/media/7c51784cca60d00b94b4a26cd28facbc/href</a></iframe><p>ListEvents.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c93e7c638270a1572b82c63ef06d0a15/href">https://medium.com/media/c93e7c638270a1572b82c63ef06d0a15/href</a></iframe><p>If we refer back to our Rust code and the specific function calls and variables, we can identify in the above files the specific items we wrote in Rust:</p><p>Rust functions: add_event, add_vote, list_events</p><p>Rust variables: estimated_budget, total_votes, title, id, description</p><p>With all of these files created and added to the appropriate directories, our final file structure(s) should be:</p><pre>&quot;near-starter-dapp&quot; root folder:</pre><pre>near-starter-dapp--|<br>                   |contract<br>                   |src<br>                   |package.json<br>                   |package-lock.json<br>                   |node_modules</pre><pre>&quot;src&quot; front end folder (in list above) w/ &quot;components&quot; sub-directory:</pre><pre>src-------|<br>          |global.css<br>          |App.js<br>          |utils.js<br>          |config.js<br>          |index.html<br>          |index.js<br>          |components--|<br>                       |CreateEvents.js<br>                       |ListEvents.js</pre><p><strong>Some thoughts on the React portion of this project</strong></p><p>You will notice that this React example, based on an existing Near tutorial template you may have already seen out there, is a <strong>very</strong> bare minimum in terms of functionality and state management.</p><p>For instance, we rely solely on the “UseState” and “UseEffect” hooks for updating our contract state on the blockchain, with no server-side rendering (or more advanced routing) of our pages before we get to displaying them in the browser window.</p><p>As we will learn, this is definitely <strong>not</strong> ideal for production, because of some latency in refreshing this updated state information on the client side.</p><p>For the purposes of this tutorial, we assume that we may need to “refresh” the browser window from time to time to display updated contract state. Since we are only launching this locally on our machine, we are fine with this, but if it were a real production deployment, we would need to implement more advanced React — using Context, Router, and/or Redux, or some other “store” for state management (ex: Next.js with SSR) to pre-render the pages in a timely manner automatically.</p><p>We would also need to deploy to Digital Ocean, Vultr, Heroku, Vercel or some other hosting service that will handle advanced Node applications server-side.</p><p><em>However, this is beyond the scope of this tutorial. It is an excellent topic for another Near article in the future — specifically expanding a Near project with Next.js on the front end and deploying server-side to the cloud! I look forward to contributions from other developers in this area.</em></p><p><strong>(Finally!) Launching our React Dapp on our local machine with Parcel</strong></p><p>So let’s make sure we are in our root directory…</p><pre>near-starter-dapp $</pre><p>And then “npm start” or “yarn start”:</p><pre>npm start</pre><p>We should see the following in the VSCode Terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*wtQ_VT1zquU9QK6LgMk2IQ.jpeg" /></figure><p><em>NOTE: make sure you are </em><strong><em>completely</em></strong><em> “signed out” of any version of this Dapp you may have loaded previously. There is a bug in the Near wallet interface that will “force” a transaction approval repeatedly on a loop if the smart contract code changes or is replaced. The exact cause of this issue is currently being diagnosed.</em></p><p>If we confirm in the browser (Chrome or Firefox are recommended) on localhost:1234 you will see the “Sign-in” page for our Dapp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Ak4QTAeCpWvmhCUjAoeImg.jpeg" /></figure><p>Go ahead and sign in with the Near Wallet as usual (approve the “transaction” for signing into Near Testnet with the “localhost” app):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*SjbEfHYx0J5oV6SVoPS1DQ.jpeg" /></figure><p>We proceed to the “Events” screen: hit the “Add An Event” button to fill in the drop-down toggle React form:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*EVsA_E9gL6Q9ZRRJtn5fuA.jpeg" /></figure><p>Fill in the event form with the Title, suggested Budget and Description of the event as shown and hit “Submit”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*I5LL5etm9t2_ZknZRXYWPg.jpeg" /></figure><p>Once we submit an event, there is a brief “reset” period (latency as described above) and you may need to <strong>refresh</strong> the browser before we will see the updated Events list on the home page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Rg-eEkyAbOyrBJkqIZMsZA.jpeg" /></figure><p>To add a “vote” to a project, you can hit the “Vote” button, <strong>refresh</strong> the page again, and an updated vote count will appear for the project you voted on.</p><p><strong>Some closing thoughts (and room for improvement)</strong></p><p>So we have implemented a very basic end-to-end solution to get started on the Near Protocol, and I hope that this entire tutorial has been as enjoyable for you as it has for me.</p><p>One thing I have been thinking about is incorporating Rust-based checks and balances in our contract logic, and although this could be (yet another) article altogether, we might include the following in any upgrade:</p><ol><li>Allow-listing for addresses: we could implement checks in Rust to assemble a list of “approved” users if the contract owner requires some restriction of who can add to the Events list on the platform.</li><li>Voter-checking: similar to the “allow-list”, this would enforce a logic in Rust of “not-yet-voted” and “already-voted” to make sure a voter can’t vote more than once, or that “owners” can’t vote on their own events.</li><li>Native token contributions to projects on the Event platform: if a native token were to be minted for the platform with initial supply, and allowances were made for registered users, we can log their event donations accordingly. Token balances for project contributions can then be included in our Event data. This could all be done in Rust using the standard FT Token Contract standard for Near.</li><li>Finally as I described above — a redesign of the React portion to incorporate something like Next.js, with its emphasis on Router, “GetStaticProps”, and some server-side rendering (SSR) in general would definitely make our app more responsive and ready for a real production deployment. For this, a multi-page setup would also make sense, since we could use “slugs []” in Next.js to generate a separate, unique page per Event.</li></ol><p>This concludes the third and final part of our tutorial — thank you for completing the project. I encourage you to use this basic template as a foundation for your Near development journey, adapting it to your needs as you see fit for a more robust solution in the future.</p><p>Thanks again for reading, and see you next time!. — Panasthetik.</p><p><strong>Resources:</strong></p><ul><li><a href="https://parceljs.org/">Parcel - The zero configuration build tool for the web.</a></li><li><a href="https://reactjs.org/">React</a></li><li><a href="https://docs.near.org/docs/develop/basics/getting-started">Getting Started | NEAR Documentation</a></li><li><a href="https://docs.rs/near-sdk/latest/near_sdk/">near_sdk - Rust</a></li><li><a href="https://www.rust-lang.org/">Rust</a></li></ul><blockquote>Join Coinmonks<a href="https://t.me/coincodecap"> Telegram Channel</a> and<a href="https://www.youtube.com/c/coinmonks/videos"> Youtube Channel</a> learn about crypto trading and investing</blockquote><h3>Also, Read</h3><ul><li><a href="https://coincodecap.com/buy-monero">How to buy Monero</a> |<a href="https://coincodecap.com/idex-review"> IDEX Review</a> |<a href="https://coincodecap.com/bitkan-trading-bot"> BitKan Trading Bot</a></li><li><a href="https://medium.com/coinmonks/coindcx-review-8444db3621a2">CoinDCX Review</a> |<a href="https://coincodecap.com/crypto-margin-trading-exchanges"> Crypto Margin Trading Exchanges</a></li><li><a href="https://coincodecap.com/red-dog-casino-review">Red Dog Casino Review</a> |<a href="https://coincodecap.com/swyftx-review"> Swyftx Review</a> |<a href="https://coincodecap.com/coingate-review"> CoinGate Review</a></li><li><a href="https://coincodecap.com/bookmap-review-2021-best-trading-software">Bookmap Review</a> |<a href="https://coincodecap.com/crypto-exchange-usa"> 5 Best Crypto Exchanges in the USA</a></li><li><a href="https://coincodecap.com/ftx-futures-trading">How to trade Futures on FTX Exchange</a> |<a href="https://coincodecap.com/okex-vs-binance"> OKEx vs Binance</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e4782dce459" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-3-6e4782dce459">An Intro To Near Dapp Development With Rust, WASM, Parcel and React -Part 3.</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Intro To Near Dapp Development With Rust, WASM, Parcel and React -Part 2.]]></title>
            <link>https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-2-6bd3a071b74e?source=rss-351ecf24f9f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bd3a071b74e</guid>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[near-protocol]]></category>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Panasthetik]]></dc:creator>
            <pubDate>Tue, 15 Mar 2022 02:51:44 GMT</pubDate>
            <atom:updated>2022-03-15T03:15:49.116Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*KbB_oVuIU2XXs6P6QyTuag.jpeg" /></figure><p>This article introduces existing Web3 developers to the Near Protocol with a simple, functional, start-to-finish example in three parts.</p><p>For those who are just joining us, feel free to look over Part 1 of this tutorial <a href="https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-1-e500a3abc597?source=friends_link&amp;sk=216f6365fcd46133ae278961c5260243"><strong>here</strong></a><strong> </strong>to get up to speed!</p><p><strong>The entire code repository for Part 1 and Part 2 of this tutorial can be found </strong><a href="https://github.com/Panasthetik/near-starter-app"><strong>here</strong></a><strong>.</strong></p><p>Last week, we started our developer journey on the Near blockchain by covering some of the foundational prerequisites for creating a smart contract to deploy on Near Testnet, and were able to set up our Near wallet and Rust environment accordingly. We then finished Part 1 by doing a preliminary build of our contract to WASM using Cargo and the “build.sh” bash script inside our “contract” directory.</p><p>For today’s session, we will continue our project by first writing some Rust unit tests to run against our smart contract. We will then create a deployment sub-account in Near-CLI, deploy our contract to Near Testnet, and actually be able to interact with it in the developer console before we proceed to our React front-end integration in Part 3.</p><p>So let’s continue!</p><p><strong>Writing Rust unit tests.</strong></p><p>First, let’s take a second look at our ‘Event’ contract, and see which functions we need to test against in our Rust unit tests. If we open up “lib.rs” again inside our “src” folder, we see the following as written last time:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b5750058ca585ccfc07c5945a2c05b2/href">https://medium.com/media/8b5750058ca585ccfc07c5945a2c05b2/href</a></iframe><p>Since we know that this contract needs to 1) allow users signed in with their Near wallet to create an event, and 2) give users the ability to vote on the listed events — we can immediately designate a few functions to target with our unit tests.</p><p>The easiest way to do this in Rust is to first identify the two functions that initiate contract state change:</p><ol><li>add_event()</li></ol><p>2. add_vote()</p><p>We will examine “add_event” and its associated functions first —</p><p>Lines 43–55 (in “lib.rs” above):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6eb4cb29f0e61b446f518df7254519b/href">https://medium.com/media/a6eb4cb29f0e61b446f518df7254519b/href</a></iframe><p>The “add event” function is the primary function in our contract — it opens up a project in the events listing with Title, Description, and suggested Budget. Once we call the “event” struct (which we declared our “models.rs” file) with “Event::new”, we are able to create a new event on the Near Blockchain that will later be referenced by our front end and displayed on the home page of our Dapp.</p><p>To verify that an event was added on-chain, we also have two other helper functions that first display the list of events…</p><pre>// lines 57-61 (in “lib.rs” above)</pre><pre>pub fn list_events()</pre><pre>...</pre><p>…and then also track the count of total events/projects within our contract…</p><pre>// lines 63-65 (in “lib.rs” above)</pre><pre>pub fn event_count()</pre><pre>...</pre><p>What we need to do is write a unit test to verify that this overall function works as expected, and therefore let’s add the following to the <em>bottom</em> of the “lib.rs” file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f81c0c2901fa23669a4742f7b08d9ae5/href">https://medium.com/media/f81c0c2901fa23669a4742f7b08d9ae5/href</a></iframe><p><em>IMPORTANT: the file above should be pasted outside the main “Contract” implementation braces (after line 82 in “lib.rs”). If you include the test code in the main Contract body, you will get a compilation error in Cargo that tells you it’s out of scope!!</em></p><p><strong>Important things to consider in our test code above.</strong></p><p>One of the awesome things about doing our smart contracts in Rust, is that we can also write our unit tests in Rust and run them in Cargo, leveraging the incredible compiler and error-handling capabilities straight away before we even think about doing our final deployment build to WASM.</p><p>Looking at the unit test we started with above, which tests against the “add_event” function, it becomes obvious just how readable and understandable this system is, compared to what might be a much more “verbose” testing environment in JavaScript, for example (i.e. Mocha).</p><p>So for the “add_event” function called from our main Contract:</p><pre>contract.add_event()</pre><p>We set up a test simulation environment (also called a “context” in our Near SDK) to be able to imagine a scenario where “alice” represents a Near account (see “alice.testnet” in the set-up code), interacts with a fresh contract instance (“Contract::new”)and then creates a new event <strong>within our “create_project” unit test</strong>.</p><p>Another related Contract function called in this overall test is assigned to the “result” variable as follows:</p><pre>let result = contract.event_count()</pre><p>What this does is verify our result for “add_event()” — it makes sure that a new event was indeed created in our test, and that the total events is then “1”:</p><pre>assert_eq!(result, 1);</pre><p>Great! So why not run this test and see if we pass, just initiating a context with Near SDK and having Cargo do the rest?</p><p>In your terminal, in the “contract” folder, execute the test script:</p><pre>bash ./test.sh</pre><p>Just like before during our preliminary build, Cargo will assemble our crates and dependencies, compile our code, create the “context” simulation environment from Near SDK, and run our unit test with a fresh Contract instance as it would be deployed on a real blockchain. Once this process is completed, you should see the following in your VSCode terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Ev74pACb6S-EcMorbuQ-lA.jpeg" /></figure><p>Awesome, our first Rust unit test passed and it indicates that in the simulated “context” environment, a new event was created!</p><p>The next function we will build a unit test for is the “add_vote()” function. This is represented here in our main contract code:</p><p>Lines 67–75 (above, in “lib.rs”):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6473cd0f217775e875b3d9f11dccc245/href">https://medium.com/media/6473cd0f217775e875b3d9f11dccc245/href</a></iframe><p>This function takes an event, makes it “mutable” so it can be modified with “votes” and executes the process of adding this information to the event mapped by event ID (“id: usize”). A “voter” here is anyone who is signed in to the Dapp with their Near wallet, which automatically gives them access to this function.</p><p><em>For additional information about Near accounts (“predecessor,” “signer” and “current”) please consult the official Near SDK documentation </em><a href="https://www.near-sdk.io/zero-to-hero/beginner/actions"><em>here</em></a><em> as it is well beyond the scope of this article.</em></p><p>There is also a “helper” function at the end of the main contract (after add_vote) called “get_total_votes()” that helps confirm that a vote was added, and that we will also reference for our unit test:</p><p>Lines 77–81 (above, in “lib.rs”):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e57fdb0c2b99c9f9aa431579274bc38e/href">https://medium.com/media/e57fdb0c2b99c9f9aa431579274bc38e/href</a></iframe><p><strong>For our add_voter unit test below, we will create a very similar structure to what we did for our add_project unit test above.</strong> We will add the code below to the end of our unit tests at the bottom of “lib.rs”, as a second test block inside the “mod tests” braces {} (I will recap the entire “lib.rs” file shortly, so all the closures are clear):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47959756dfa23b03777db7dd449fee15/href">https://medium.com/media/47959756dfa23b03777db7dd449fee15/href</a></iframe><p>Here, we do the same “context” building and account initialization for “alice” and “Contract::new” that we did for the previous test. In this case though, we added the code here instead…</p><pre>contract.add_vote(0)</pre><p>…where “0” is the event ID, and then…</p><pre>let result = contract.get_total_votes(0)</pre><pre>assert_eq!(result, 1)</pre><p>One vote being exactly correct given that “alice” has both created a project here (project ID “0” in the events list), <strong>and</strong> cast a vote as well (“1” vote for project ID “0”).</p><p><strong>Wrapping up unit tests in Cargo.</strong></p><p>For now we will only include these two Rust unit tests, and if everything is correct we should have the following as our complete “lib.rs” file ready to run with <em>both </em>tests:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1501b4195653fabf6e43d74466141a57/href">https://medium.com/media/1501b4195653fabf6e43d74466141a57/href</a></iframe><p>In our VSCode terminal inside the “contract” folder, let’s run our “test.sh” script again as follows:</p><pre>bash ./test.sh</pre><p>After a brief compilation period like always, we should now see both unit tests passing on the console like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*Yd03s9qNCua87MX5XBo5UQ.jpeg" /></figure><p>Our Rust unit tests have passed, and we are ready to do a final build of our contract to WASM and then deploy it to the Near Testnet!</p><p><strong>Setting up our WASM file before deployment and preparing a Near Testnet contract sub-account.</strong></p><p>To briefly review what we did at the beginning of Part 1, we can recall that first we created a Near Testnet account at “wallet.testnet.near” as shown in the browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*WnuGpoxVljnQoTwQe4FWlw.jpeg" /></figure><p>For the remainder of this lesson, we will spend most of our time in the VSCode terminal and the Near CLI. So first, let’s repeat our account sign-in process that we did in Part 1:</p><pre>near login</pre><p>With that, we go though the pop-up transaction approval in our browser until we get something similar to this again back in the VSCode terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*GPzqTTe_6fYW4G9N9geW-A.jpeg" /></figure><p>Next, we need to do <strong>something totally new </strong>— we need to assign a sub-account as a placeholder to “contain” our smart contract deployment on Near before we upload it.</p><p>Usually, it is a best practice to make this sub-account an address that derives from the main Testnet wallet you use, like so:</p><p>near-starter-dapp.&lt;your account name&gt;.testnet</p><p>…or, if our main account is “myaccount”:</p><p>near-starter-dapp.myaccount.testnet</p><p>So in the terminal using “myaccount.testnet” as our example base wallet address (yours will be different but should follow the same structure), we need to enter the following, paying close attention to the syntax here for Near CLI:</p><pre>near create-account near-starter-dapp.myaccount.testnet --masterAccount myaccount.testnet</pre><p>As a response, in the terminal you should then see this confirmation that your sub-account was created:</p><pre>Saving key to &#39;/Users/&lt;yourusername&gt;/.near-credentials/testnet/near-starter-dapp.myaccount.testnet.json&#39;</pre><pre>Account near-starter-dapp.myaccount.testnet for network &quot;testnet&quot; was created.</pre><p>The keypair for this sub-account is stored in a .json file as part of your “.near-credentials” directory on your local machine, and now we are ready to prepare our contract for deployment at this sub-account address on Near.</p><p>Back in the “contract” folder of our project, we will now run “build.sh” one last time:</p><pre>bash ./build.sh</pre><p>Once this build is completed, check in the “res” directory inside our main “contract” folder. There should be a file called: “<strong>near_starter_dapp.wasm</strong>”</p><p>Again in the terminal, let’s run the code below as our deploy script — this will actually start the deployment process to our sub-account:</p><pre>near deploy --accountId near-starter-dapp.myaccount.testnet --wasmFile res/near_starter_dapp.wasm</pre><p>If all goes successfully, you should see this confirmation in the console that your Near testnet smart contract is now deployed:</p><pre>Starting deployment. Account id: near-starter-dapp.myaccount.testnet, node: https://rpc.testnet.near.org, helper: https://helper.testnet.near.org, file: res/near_starter_dapp.wasm</pre><pre>Transaction Id 3fqzA9vpuKSSsrBry9jE2k2FjoKFZBCdwPU38fTcPVkT</pre><pre>To see the transaction in the transaction explorer, please open this url in your browser</pre><pre>https://explorer.testnet.near.org/transactions/3fqzA9vpuKSSsrBry9jE2k2FjoKFZBCdwPU38fTcPVkT</pre><p>PLEASE NOTE — your transaction ID will be different from the above!</p><p>I hope your contract deployed successfully — I know this process has been a long road, but I think once you have actually deployed on Near, you can agree that not only is this process pretty painless, it is very developer-friendly in its approach.</p><p><strong>Smart contract interaction with the NEAR-CLI: Let’s talk to our contract!</strong></p><p>Now that we have our contract on the Near Testnet, we can do the kind of contract calls many of you have already done via RPC on Ethereum or Solana. Here is an example of a contract call that actually creates a new event listing in our smart contract, just like in our unit tests. If we paste this exactly in our console:</p><pre>near call near-starter-dapp.myaccount.testnet add_event &#39;{&quot;title&quot;: &quot;New Contemporary Art Show&quot;, &quot;estimated_budget&quot;: 300, &quot;description&quot;:&quot;Lots of amazing artists from all over the world, from the fields of painting, sculpture and photography&quot;}&#39; --accountId myaccount.testnet</pre><p>We will get the following result:</p><pre>Scheduling a call: near-starter-dapp.myaccount.testnet.add_event({&quot;title&quot;: &quot;New Contemporary Art Show&quot;, &quot;estimated_budget&quot;: 300, &quot;description&quot;:&quot;Lots of amazing artists from all over the world, from the fields of painting, sculpture and photography&quot;})</pre><pre>Doing account.functionCall()</pre><pre>Receipt: 6Dm3ez8cYjB5xnpqQiJPVREk9zh6MJGeXo8pjKw58ksP</pre><pre>Log [near-starter-dapp.myaccount.testnet]: Added a new event!</pre><pre>Transaction Id 3dXoVaUV68Nm5kj1qS4A1cXavPJbsc2nwewBVtAk3BSj</pre><pre>To see the transaction in the transaction explorer, please open this url in your browser</pre><pre><a href="https://explorer.testnet.near.org/transactions/3dXoVaUV68Nm5kj1qS4A1cXavPJbsc2nwewBVtAk3BSj">https://explorer.testnet.near.org/transactions/3dXoVaUV68Nm5kj1qS4A1cXavPJbsc2nwewBVtAk3BSj</a></pre><p>Wow! It worked, and our contract now has been deployed and it even has an event added already.</p><p>Now, how can we confirm that the contract <em>really</em> contains our “contemporary art” event out there on Near? Sure we can just check the explorer URL as indicated in the terminal above. But what about just from the CLI?</p><p>Well, that’s even easier — we will just do a couple more contract calls, starting with:</p><pre>near call near-starter-dapp.myaccount.testnet list_events --accountId myaccount.testnet</pre><p>And when we run it, we will get this in return:</p><pre>Scheduling a call: near-starter-dapp.myaccount.testnet.list_events()<br>Doing account.functionCall()<br>Transaction Id ASzDxXDHqVT82gs5kM3wwfcezJ75Wk9NGCizvvY7f5Ci<br>To see the transaction in the transaction explorer, please open this url in your browser<br><a href="https://explorer.testnet.near.org/transactions/ASzDxXDHqVT82gs5kM3wwfcezJ75Wk9NGCizvvY7f5Ci">https://explorer.testnet.near.org/transactions/ASzDxXDHqVT82gs5kM3wwfcezJ75Wk9NGCizvvY7f5Ci</a><br>[<br>  {<br>    id: 0,<br>    creator: &#39;myaccount.testnet&#39;,<br>    created_at: 1646505825772610600,<br>    title: &#39;New Contemporary Art Show&#39;,<br>    estimated_budget: 300,<br>    total_votes: 0,<br>    description: &#39;Lots of amazing artists from all over the world, from the fields of painting, sculpture and photography&#39;,<br>    votes: []<br>  },<br>]</pre><p>If we wanted to repeat this same process to add a vote to our contract for the event (at project ID “0”), we can just run this:</p><pre>near call near-starter-dapp.myaccount.testnet add_vote &#39;{&quot;id&quot;:0}&#39; --accountId myaccount.testnet</pre><p>Then, to verify our votes we can repeat the above “list_events” call in the console and we should finally have this result:</p><pre>Scheduling a call: near-starter-dapp.myaccount.testnet.list_events()<br>Doing account.functionCall()<br>Transaction Id ASzDxXDHqVT82gs5kM3wwfcezJ75Wk9NGCizvvY7f5Ci<br>To see the transaction in the transaction explorer, please open this url in your browser<br><a href="https://explorer.testnet.near.org/transactions/ASzDxXDHqVT82gs5kM3wwfcezJ75Wk9NGCizvvY7f5Ci">https://explorer.testnet.near.org/transactions/ASzDxXDHqVT82gs5kM3wwfcezJ75Wk9NGCizvvY7f5Ci</a><br>[<br>  {<br>    id: 0,<br>    creator: &#39;myaccount.testnet&#39;,<br>    created_at: 1646505825772610600,<br>    title: &#39;New Contemporary Art Show&#39;,<br>    estimated_budget: 300,<br>    total_votes: 1,<br>    description: &#39;Lots of amazing artists from all over the world, from the fields of painting, sculpture and photography&#39;,<br>    votes: [myaccount.testnet]<br>  },<br>]</pre><p><strong>Finishing up this section, next steps and what we will do in Part 3</strong>.</p><p>If you’re still here, I want to extend a huge congratulations for deploying your first smart contract on Near, doing the Rust unit tests, setting up your Near developer environment, doing some simple contract calls, and making it this far generally!</p><p>For next week’s session, we will tie up a few “loose ends” from Part 2 by illustrating how to quickly “upgrade” your contract by clearing out the sub-address, recreating it in Near CLI, rebuilding the WASM file, and re-deploying once your Rust contract has been changed/edited.</p><p><strong>For the third and final part of this tutorial, we will be switching gears quite a bit by moving over to the React / JavaScript world and connecting our smart contract to a simple but elegant front-end template! This template is based on an existing design, although I have simplified it immensely by using the latest Parcel exclusively as the bundler, transpiler, and final dev server.</strong></p><p>As we will ultimately see, it should be easy to make this project your main starter kit for Near development in the future.</p><p>Thanks again for reading, and see you next time… — Panasthetik</p><p><strong>Resources:</strong></p><ul><li><a href="https://docs.near.org/docs/develop/basics/getting-started">Getting Started | NEAR Documentation</a></li><li><a href="https://docs.rs/near-sdk/latest/near_sdk/">near_sdk - Rust</a></li><li><a href="https://www.rust-lang.org/">Rust</a></li></ul><blockquote><em>Join Coinmonks</em><a href="https://t.me/coincodecap"><em> Telegram Channel</em></a><em> and</em><a href="https://www.youtube.com/c/coinmonks/videos"><em> Youtube Channel</em></a><em> learn about crypto trading and investing</em></blockquote><h3>Also, Read</h3><ul><li><a href="https://medium.com/coinmonks/3commas-review-an-excellent-crypto-trading-bot-2020-1313a58bec92">3Commas Review</a> |<a href="https://coincodecap.com/pionex-review-exchange-with-crypto-trading-bot"> Pionex Review</a> |<a href="https://medium.com/coinmonks/coinrule-review-2021-a-beginner-friendly-crypto-trading-bot-daf0504848ba"> Coinrule review</a></li><li><a href="https://medium.com/coinmonks/ledger-vs-ngrave-zero-7e40f0c1d694">Ledger vs Ngrave</a> |<a href="https://medium.com/coinmonks/ledger-nano-s-vs-x-battery-hardware-price-storage-59a6663fe3b0"> Ledger nano s vs x</a> |<a href="https://medium.com/coinmonks/binance-review-ee10d3bf3b6e"> Binance Review</a></li><li><a href="https://medium.com/coinmonks/bybit-exchange-review-dbd570019b71">Bybit Exchange Review</a> |<a href="https://coincodecap.com/bityard-reivew"> Bityard Review</a> |<a href="https://coincodecap.com/jet-bot-review"> Jet-Bot Review</a></li><li><a href="https://medium.com/coinmonks/3commas-vs-pionex-vs-cryptohopper-best-crypto-bot-6a98d2baa203">3Commas vs Cryptohopper</a> |<a href="https://medium.com/coinmonks/earn-crypto-interest-b10b810fdda3"> Earn crypto interest</a></li><li>The Best Bitcoin<a href="https://medium.com/coinmonks/hardware-wallets-dfa1211730c6"> Hardware wallet</a> |<a href="https://medium.com/coinmonks/bitbox02-review-your-swiss-bitcoin-hardware-wallet-c36c88fff29"> BitBox02 Review</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bd3a071b74e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-2-6bd3a071b74e">An Intro To Near Dapp Development With Rust, WASM, Parcel and React -Part 2.</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[An Intro To Near Dapp Development With Rust, WASM, Parcel and React -Part 1.]]></title>
            <link>https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-1-e500a3abc597?source=rss-351ecf24f9f1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e500a3abc597</guid>
            <category><![CDATA[near-protocol]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dapps]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[Panasthetik]]></dc:creator>
            <pubDate>Wed, 09 Mar 2022 00:32:34 GMT</pubDate>
            <atom:updated>2022-03-09T08:29:36.605Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*PshID33DzhVzihyRDWXicg.jpeg" /></figure><p>This article will introduce existing Web3 developers to the Near Protocol with a simple, functional, start-to-finish example in three parts.</p><p><strong>The entire code repository for Part 1 and Part 2 of this tutorial can be found </strong><a href="https://github.com/Panasthetik/near-starter-app"><strong>here</strong></a><strong>.</strong></p><p>If you’re coming from Ethereum or even Solana, one of the things that makes Near different as of now (March 2022) is that there aren’t a lot of very recent tutorials for developing a basic, fully-functional application with an upgradeable template — front-end, back-end, and unit tests.</p><p>In addition, I think the fact that the protocol’s contracts are written in either Rust or AssemblyScript (then compiled to WASM) tends to discourage would-be developers from jumping right in.</p><p>Lastly — both the Near blockchain and the underlying token have, in my opinion been under-represented overall in various news articles and coverage as well as developer tutorials like this one.</p><p>I hope to help alleviate these issues with this article, making the Near Protocol more accessible to new creators and also helping to on-board potential users with some of the many things that make Near an awesome, fun, fast, inspiring blockchain with enormous potential. So let’s get started!</p><p><strong>The Basics.</strong></p><p>First, this tutorial assumes a few things straight away to make our intro to Near easier:</p><ol><li>You have already developed initially on Ethereum, an EVM or a layer-2 chain, or on Solana or similar, and have experience with dev tools like Truffle, Hardhat, Anchor or Foundry.</li><li>You’re running either Linux or MacOS and have Rust installed, including the latest stable version, the Cargo package manager, and the Rust extension/analyzer for VSCode (We will use VSCode for this article).</li><li>Node.js should be installed 14.19.0–17.1.0, and your NPM is at least version 8.3.0+</li><li>You are familiar with smart contract deployment, gas fees, wallet accounts, transaction explorers, etc. — fundamental blockchain primitives.</li><li>Lastly — although basic Rust is helpful and React experience is great, you don’t have to know either one at an expert level to understand this example and how everything interacts.</li></ol><p><strong>Setting Up Our Near Testnet Wallet.</strong></p><p>The first thing we will look at is creating a wallet/account on the Near Testnet and getting some test Near tokens to deploy on the network. You can go <a href="http://wallet.testnet.near.org/">here</a> to learn how to do that, as it is very similar to other wallets (MetaMask, SolFlare):</p><p><a href="https://wallet.testnet.near.org/">NEAR Wallet</a></p><p>An interesting difference with Near already, which can make it more user-friendly, is that Near account addresses appear as human-readable text (ex: “myaccount.testnet” for testing, or “myaccount.near” for mainnet). As long as your suggested name isn’t already taken you can get creative with these account names. Also, no need to reserve a separate ENS-like name (.ETH) or .SOL like on Solana — with Near you have this capability right out the box and your deployed contracts can be mapped directly to your chosen addresses!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*kNwH1NSC19B0sOZXbOW9Cw.jpeg" /></figure><p>When we actually deploy smart contracts on-chain, we will choose sub-account names as a best practice to contain the contract address, like the following:</p><p>contract.myaccount.testnet (for testing)</p><p>contract.myaccount.near (for mainnet)</p><p>One thing to note about Near is that a smart contract can only be deployed one per sub-address, which keeps things organized and can be upgraded (we will see how to upgrade or replace contracts later in this series).</p><p>Once we have our Near test account created we should have a sample Near test token balance to start with, which will help us with the next step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*WnuGpoxVljnQoTwQe4FWlw.jpeg" /></figure><p><strong>Setting Up Our Environment.</strong></p><p>The best way to start organizing our dev environment is to install the Near CLI as follows using NPM:</p><pre>npm install near-cli</pre><p>With that installed correctly, you should be able to verify in your terminal the current version as:</p><pre>near --version</pre><p>Next, using Near CLI we will log into the Near Testnet with the following:</p><pre>near login</pre><p>When we run this, a browser screen should pop up indicating a login request at “rpc.testnet.near.org”, which should (if you opened your near account correctly) ask that you approve the login transaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*C_Jq_f8NBDZSoIPxN6FuPw.jpeg" /></figure><p>Once approved, you will see a screen indicating that your login was successful and that you can now close the browser window and return to VSCode terminal. In the terminal, there is a clear message that you are logged into Near CLI using the stated .testnet address:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*GPzqTTe_6fYW4G9N9geW-A.jpeg" /></figure><p>Great! So we have a Near wallet account (myaccount.testnet) along with the CLI installed, we’ve already logged into the Near testnet, and we’re now ready to start a new project in VSCode which will contain our project directories.</p><p><em>NOTE: Although there is already an NPX package to create a Near boilerplate containing Rust or AssemblyScript starter kits (with React), I have simplified this process quite a bit in this tutorial by eliminating JavaScript testing methods and avoiding Node dependencies such as Webpack, Babel, Jest, Nodemon, ShellJS and a few others. I think in some cases these don’t always work together right out of the box, and we can avoid time diagnosing JS/NPM errors and instead focus on our Near deployment.</em></p><p><em>For our Near smart contract, we will create this project from scratch and write the unit tests in Rust. We will use the latest Parcel and React exclusively for the front-end later on in Part 3. I hope this approach will make things slightly easier for the new Near developer as a result!</em></p><p>So in your VSCode terminal, we will start with making a new directory as follows:</p><pre>mkdir near-starter-dapp</pre><pre>cd near-starter-dapp</pre><p>Within this main directory, let’s make a folder called “contract” which will contain our Rust code and build files like so:</p><pre>mkdir contract </pre><pre>cd contract</pre><p>Inside this ‘contract’ directory, we will need to do two things — initialize a new Rust project using Cargo, and write a few useful scripts for building and testing our Rust code before we compile to WASM.</p><p>Inside ‘contract’, enter the following:</p><pre>cargo init --lib</pre><p>As we can see, this creates an empty Rust project and provides us with what we need directory-wise to start writing our smart contract. The “src” folder contains the Rust files, while the “cargo.toml” will specify some requirements and dependencies for our project.</p><p>Before we proceed with our Rust code, there is one important thing we will need to add to our Rust configuration to be able to compile our contracts correctly, which is the WASM target for deployment. Enter the following in the terminal:</p><pre>rustup target add wasm32-unknown-unknown</pre><p>Once we do this, and our directory is ready for Rust, let’s create some simple shell scripts we will need to perform simple build and testing terminal operations within this “contract” directory. We will create the following scripts:</p><p>build.sh</p><pre>#!/bin/bash</pre><pre>set -e</pre><pre>RUSTFLAGS=&#39;-C link-arg=-s&#39; cargo build --target wasm32-unknown-unknown --release</pre><pre>cp target/wasm32-unknown-unknown/release/*.wasm ./res/</pre><p>build.bat</p><pre>cargo build --target wasm32-unknown-unknown --release</pre><pre>copy target\wasm32-unknown-unknown\release\*.wasm res</pre><p>test.sh</p><pre>#!/bin/bash</pre><pre>set -e</pre><pre>cargo test -- --nocapture</pre><p>After these scripts are created, our “contract” directory should look like this:</p><pre>contract--|<br>          |src<br>          |build.sh<br>          |build.bat<br>          |test.sh<br>          |cargo.toml<br>          |cargo.lock</pre><p>We won’t worry about the cargo.lock file for now, but we will want to edit the <strong>cargo.toml</strong> file with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86a58d05dd5054b8fc26de8475ae221b/href">https://medium.com/media/86a58d05dd5054b8fc26de8475ae221b/href</a></iframe><p>Once we have this directory structure set up, let’s create one final directory in our “contracts” folder:</p><pre>mkdir res</pre><p>Our final “contract” directory structure is:</p><pre>contract--|<br>          |src<br>          |build.sh<br>          |build.bat<br>          |test.sh<br>          |cargo.toml<br>          |cargo.lock<br>          |res</pre><p>Now our Rust environment is set up and we’re ready to write our smart contract for Near!</p><p><strong>Starting our Near smart contract</strong></p><p>Let’s examine the “src” directory within our main “contracts” folder.</p><p>Inside, there is a starter Rust file named “lib.rs” — open this file in VSCode and let’s briefly look at it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b50551a26f3084be6c54c7281352143e/href">https://medium.com/media/b50551a26f3084be6c54c7281352143e/href</a></iframe><p>All this does at the moment is run a simple test function, and if we now run our “test.sh” script (from our “contract” folder root), the file is quickly compiled and tested with the result in the terminal as:</p><pre>running 1 test<br>test tests::it_works ... ok</pre><p>Now we want take our “lib.rs” file and <strong>completely</strong> overwrite it with the following code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b5750058ca585ccfc07c5945a2c05b2/href">https://medium.com/media/8b5750058ca585ccfc07c5945a2c05b2/href</a></iframe><p><strong>Some thoughts and explanation on our ‘Event’ contract.</strong></p><p>Now, this is quite a lot of code, and since this isn’t a Rust tutorial primarily, I will leave it to the developer to research what every import, struct and function does in this file.</p><p>One important point concerning Rust macros and the Near SDK — just like Solana, Elrond and other Rust-based blockchains, the SDK handles a substantial amount of abstraction (which means it “wraps” and greatly abbreviates your code). If we were to type all of what the code above does in low-level Rust, we would have many more lines of code to write, and it would be much more intimidating for onboarding.</p><p>To briefly sum up what our smart contract is doing, this project creates a simple app for listing proposed events with Title, Description and a suggested Budget. These events can be created by anyone signed into the app using their Near wallet, and then anyone signed into the app can also Vote on the proposed events listed.</p><p>We will work though some of the details as we create our frontend, as well as suggest improvements to the underlying functionality when we deploy.</p><p>But first, let’s add two more Rust files to our project so that we have the necessary crates and models (constants/utility functions) to supplement our main application file.</p><p>First, we will create a file called “utils.rs” and save it with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18e034202cdcd8f8ca45b1091130d46f/href">https://medium.com/media/18e034202cdcd8f8ca45b1091130d46f/href</a></iframe><p>Next, we create a file “models.rs” and save the following in it as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e57efe49778bf9fefdf9aa401aebd51d/href">https://medium.com/media/e57efe49778bf9fefdf9aa401aebd51d/href</a></iframe><p>Our ‘models.rs’ file contains some important structs (mainly the ‘Event’ struct and its implementation block) to import into our main “lib.rs” file. These can be added to later on depending on other things we might want to feature in our overall contract (such as calendar, progress, treasury or members directory structs for example).</p><p>The “utils.rs” file is designed to contain some core variables, constants and functions for various unit conversions. Like “models.rs” this file can also be adapted later on as new functionality is introduced. Right now this file just holds two generic functions to aid Promise and serialization.</p><p>After finishing these three files, all of them are in the “src” directory and ready for a preliminary “build” to see if our project compiles and is ready for testing and deployment.</p><p><strong>Building the smart contract in Cargo and making sure it compiles.</strong></p><p>In the root of “contract”, run the “build.sh” script as follows (this approach will defer between Mac and Linux — in Linux you don’t need “bash” before the terminal command, on Mac you proceed like below) :</p><pre>bash ./build.sh</pre><p>After a brief waiting period for Cargo to download crates, install all Rust dependencies, compile and assemble everything, we should have a few new things in our “contract” directory.</p><p>First, you’ll notice a newly-created “target” folder. This is the standard Rust build folder provided with the Cargo package manager, and it makes output file organization easy and straightforward. Inside this “target” folder are preliminary builds (binaries) of our Rust application, and one of these builds is in a special “wasm32-unknown-unknown” folder.</p><p>For now, we can disregard these files in “target” because what we want for the Near Blockchain is the WASM file we specifically copied (automated using our “build.bat” script) to our <strong><em>other</em></strong> build directory — the one called “res”.</p><p>Inside “res”, we see one file “<strong>near_starter_dapp.wasm</strong>” and this is the file we will need to eventually deploy on Near. We actually copied this from “target” for convenience, so we don’t have to dig several levels deep through our target/build directories to find this file every time we make a change and re-compile.</p><p><strong><em>A quick note on error messaging in Rust</em></strong><em>: you may have done a preliminary build (which shows completed) and yet you still see numerous warnings in the terminal (accented in yellow). Don’t worry, we still compiled our contract successfully! If there were any “red” warnings you would see more serious errors and the project would not compile until addressed. For now, this is totally fine:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*_y_WYo1bl11jnK9K9oc9Yg.jpeg" /></figure><p><em>As we move forward with our projects with a recent, stable version of Rust, the Cargo package manager and rustc compiler will work hard to tell you what optimized, current and correct code is. Some of the warnings will be related to versions of the Near SDK we specify in “cargo.toml”, while others have to do with slightly older syntax or Rust versioning itself.</em></p><p><strong>Next steps for our tutorial, and end of Part 1.</strong></p><p>Without going into too much more technical detail for this example — we have used some methods/macros from Near SDK version 3, while others (for the tests in our next chapter) will use a new, improved “VMContext” builder (a test simulation environment macro) from the Near SDK version 4 which is still currently under development. For our case overall as indicated in “cargo.toml”, we can keep the SDK version as “4.0.0-pre.4” for this entire tutorial and not have to downgrade to the earlier version used in most tutorials out there.</p><p>Speaking of testing, why not write a few unit tests right now in Rust that we can run before our final WASM build so we know it does what is required?</p><p>Fortunately, writing our tests in Rust is very easy, and of course we already have our test script (test.sh) ready on our “contract” root once unit tests are added to our our main contract file (lib.rs). Also, for numerous reasons it’s preferable to write our tests in the native language we use to write our contracts (in our case, Rust) so we can learn from Cargo and the compiler early on whether our code runs correctly, or we need to tweak it before we compile it down to our “release” file in WASM for contract deployment.</p><p><strong>In the next part of this tutorial, we will write some Rust unit tests, run them against our smart contract, create a deployment sub-account in Near-CLI, deploy our contract to Near Testnet, and actually be able to interact with it in the console before we proceed to our React front-end integration in Part Three.</strong></p><p><strong>I hope you enjoyed the first part of this three-part series introducing the Near Protocol to new developers like you!</strong></p><p>Thanks for reading, and see you next time… — Panasthetik.</p><p><strong>Resources:</strong></p><ul><li><a href="https://docs.near.org/docs/develop/basics/getting-started">Getting Started | NEAR Documentation</a></li><li><a href="https://docs.rs/near-sdk/latest/near_sdk/index.html">near_sdk - Rust</a></li><li><a href="https://www.rust-lang.org/">Rust</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e500a3abc597" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/an-intro-to-near-dapp-development-with-rust-wasm-parcel-and-react-part-1-e500a3abc597">An Intro To Near Dapp Development With Rust, WASM, Parcel and React -Part 1.</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>