<?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 James Kehoe on Medium]]></title>
        <description><![CDATA[Stories by James Kehoe on Medium]]></description>
        <link>https://medium.com/@james.m.kehoe?source=rss-afdb8be903d9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*-YMwVr8AxacW1qqPcl0MzA.png</url>
            <title>Stories by James Kehoe on Medium</title>
            <link>https://medium.com/@james.m.kehoe?source=rss-afdb8be903d9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 10:18:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@james.m.kehoe/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[Building a Guestbook dApp with Vue.js and Truffle]]></title>
            <link>https://medium.com/@james.m.kehoe/building-a-guestbook-dapp-with-vue-js-and-truffle-e0c9e3fcdeeb?source=rss-afdb8be903d9------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0c9e3fcdeeb</guid>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[solidity]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[James Kehoe]]></dc:creator>
            <pubDate>Sun, 29 Sep 2019 16:04:24 GMT</pubDate>
            <atom:updated>2019-10-07T16:35:23.176Z</atom:updated>
            <content:encoded><![CDATA[<p>This post is geared towards anyone looking for a start-to-finish tutorial on developing and deploying a blockchain-powered app (in this case, Ethereum), using <a href="https://vuejs.org/">Vue.js</a> to build the front end, and <a href="https://www.trufflesuite.com/">Truffle</a>’s <a href="https://github.com/trufflesuite/drizzle">@drizzle/vue-plugin</a> to add blockchain superpowers.</p><p>You can find the repository for the completed project <a href="https://github.com/jimpeebles/guestbook"><strong>here</strong></a><strong>.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*1sNnpifOS1VIK2_gSO7hpw.png" /><figcaption>What you’ll end up building, with a little css.</figcaption></figure><h3>Prerequisites</h3><p>For this tutorial, you’ll need a few basic tools installed on your system. Note: these instructions were written for a Mac/Linux system, but should apply to Windows also.</p><h4>Install Node</h4><p>If you don’t have <a href="https://nodejs.org/en/">Node.js</a> on your machine, you might want to turn back now. Recommended version for this tutorial: 10.16.1.</p><p>To easily switch between versions of Node on a per-project basis, I’d recommend <a href="https://github.com/nvm-sh/nvm">nvm</a>.</p><h4>Install Truffle</h4><p><a href="https://github.com/trufflesuite">Truffle</a> is a suite of tools designed to help build, compile, test and deploy smart contracts. Install globally.</p><pre>npm install -g truffle</pre><h4>Install Vue CLI</h4><p>Vue CLI is the fastest way to scaffold a Vue.js application with some common packages and tools. Install this globally as well.</p><pre>npm install -g @vue/cli</pre><p>Now let’s build!</p><h3>Initializing the App</h3><p>Create a new Vue app using Vue CLI.</p><pre>vue create guestbook</pre><p>You’ll be promoted to choose some additional packages to include in your app. Choose whatever you might want to use in your project, but <strong>make sure to include Vuex</strong>, as it’s going to serve as the connection between the Vue front end and your smart contracts. It accomplishes this by utilizing Drizzle, another Truffle product that keeps track of smart contract state and allows easy access to methods, data and events.</p><p>Once your app is successfully created, initialize Truffle in the project root.</p><pre>truffle init</pre><p>This will create some files and folders. Your directory structure should end up looking like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*NdCsk5-QmjYR4O-h0Y4FTQ.png" /></figure><p>Open up truffle-config.js and modify module.exports to tell Truffle to place its JSON build output inside the src folder where the front end can access it.</p><pre>contracts_build_directory: &quot;./src/contracts&quot;,</pre><p>Now we can write a simple guestbook contract!</p><h3>Writing the Contract</h3><p>Inside the contracts folder (in the project root, not src/contracts), create a new file called Guestbook.sol and create the contract.</p><pre>pragma solidity &gt;=0.4.21 &lt;0.6.0;</pre><pre>contract Guestbook {<br>  event SignatureAdded(string message, bytes32 name);</pre><pre>  bytes32[] guests;</pre><pre>  function signBook(bytes32 name) public {<br>    guests.push(name);<br>    emit SignatureAdded(&quot;New guest signature!&quot;, name);<br>  }</pre><pre>  function getNames() public view returns (bytes32[] memory) {<br>    return guests;<br>  }<br>}</pre><p>The first line declares which version of the Solidity compiler this contract should use. For our purposes, we’ll use the same range of versions as the contract created by running truffle init.</p><p>The next block is the contract. First we make an event to fire whenever a signature is added. Then we make an array to hold the names. Next, a method to push a name to the array and emit the event to anyone that’s listening. Finally, we create a method that returns the list of names.</p><p>The contract is ready to deploy! But first we need to create a migration to tell Truffle about our new contract. Inside the migrations folder, create a new migration file called 2_deploy_guestbook.js. Add the following contents:</p><pre>const Guestbook = artifacts.require(&quot;Guestbook&quot;);</pre><pre>module.exports = function(deployer) {<br>  deployer.deploy(Guestbook);<br>};</pre><p>Back inside your console in the project root directory, initialize the Truffle dev console.</p><pre>truffle develop</pre><p>You’ll be rewarded with your own personal blockchain! There’s a list of addresses and private keys, as well as the mnemonic you’ll need to connect to this wallet elsewhere. You’ll notice your console has changed to truffle(develop)&gt;. To both compile and migrate your contracts with one fell swoop, simply run:</p><pre>migrate</pre><p>Handy tip — during development you can instead run:</p><p>migrate --reset</p><p>to start with a fresh slate every time you migrate and ensure changes to your contracts propagate.</p><p>If all goes well, you should see a bunch of data related to the successful deployment of the contact to the dev blockchain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*SIUUtXZFUW38UUTXAyHRvg.png" /></figure><h3>Building the Vue App</h3><p>Open a second console, as we need to keep the Truffle REPL around to simulate our Ethereum blockchain. Head into your project root and install <a href="https://github.com/trufflesuite/drizzle">Drizzle Vue Plugin</a>.</p><pre>npm install @drizzle/vue-plugin</pre><p>Next, go into thesrc folder and make a file called drizzleOptions.js. This file will contain the configuration for Drizzle. Add the following code:</p><pre>import Guestbook from &quot;@/contracts/Guestbook.json&quot;;</pre><pre>const options = {<br>  web3: {<br>    block: false,<br>    fallback: {<br>      type: &quot;ws&quot;,<br>      url: &quot;ws://127.0.0.1:9545&quot;<br>    }<br>  },</pre><pre>// The contracts to monitor<br>  contracts: [Guestbook],<br>  events: {<br>    Guestbook: [&quot;SignatureAdded&quot;]<br>  },<br>  polls: {<br>    // check accounts ever 15 seconds<br>    accounts: 15000<br>  }<br>};</pre><pre>export default options;</pre><p>Here, we tell Drizzle where the contract build artifacts are located, register the Guestbook contract, and tell Drizzle which events it should listen for that might be emitted by the contract.</p><p>Open up main.js and make it look like this:</p><pre>import Vue from &quot;vue&quot;<br>import App from &quot;./App.vue&quot;<br>import store from &quot;./store&quot;</pre><pre>import drizzleVuePlugin from &quot;<a href="http://twitter.com/drizzle/vue-plugin">@drizzle/vue-plugin</a>&quot;<br>import drizzleOptions from &quot;./drizzleOptions&quot;</pre><pre>Vue.config.productionTip = false</pre><pre>Vue.use(drizzleVuePlugin, { store, drizzleOptions })</pre><pre>new Vue({<br>  store,<br>  render: h =&gt; h(App)<br>}).$mount(&quot;#app&quot;)</pre><p>Here, we’re importing the Vuex store, the Drizzle plugin, and the options file we just created. Then we’re telling our app to use the plugin, and passing in the store and options file as parameters to the plugin.</p><p>Open App.vue and edit the template section. Be sure to remove any boilerplate code generated by the Vue CLI, such as HelloWorld components or unwanted styles:</p><pre>&lt;template&gt;<br>  &lt;div v-if=&quot;isDrizzleInitialized&quot; id=&quot;app&quot;&gt;<br>    &lt;h1&gt;Sign the Guestbook&lt;/h1&gt;<br>    &lt;drizzle-contract-form<br>      contractName=&quot;Guestbook&quot;<br>      method=&quot;signBook&quot;<br>      :placeholders=&quot;[&#39;Name&#39;]&quot;<br>    /&gt;<br>    &lt;h2&gt;Guests:&lt;/h2&gt;<br>    &lt;ul v-if=&quot;getNames&quot;&gt;<br>      &lt;li v-for=&quot;(name, i) in getNames&quot; :key=&quot;i&quot;&gt;{{ utils.toUtf8(name) }}&lt;/li&gt;<br>    &lt;/ul&gt;<br>  &lt;/div&gt;<br>  &lt;div v-else&gt;<br>    Loading application...<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p>First, the template contains a v-if/else at the root. If Drizzle is not yet initialized, your smart contract data won’t be available and you’ll want to display something else in the meantime.</p><p>There’s a custom component called drizzle-contract-form, which is registered when you install the Drizzle plugin. Given the name of a contract and method, it will generate inputs corresponding to the input parameters expected by the contract. You can also set an array of placeholders which display in the order of parameter values.</p><p>The next section gets the list of names and iterates over them in an unordered list. We’ll build the getNames method shortly, but notice the transformation being applied to the list values. We need to convert the bytes returned by the smart contract into utf-8 strings, unless you prefer displaying hex code. For this, we’ll bring in some utilities.</p><p>Here’s the&lt;script&gt; section of the file:</p><pre>&lt;script&gt;<br>import { mapGetters } from &quot;vuex&quot;</pre><pre>export default {<br>  name: &quot;app&quot;,<br>  computed: {<br>    ...mapGetters(&quot;drizzle&quot;, [&quot;drizzleInstance&quot;, &quot;isDrizzleInitialized&quot;]),</pre><pre>    ...mapGetters(&quot;contracts&quot;, [&quot;getContractData&quot;]),</pre><pre>    getNames() {<br>      let data = this.getContractData({<br>        contract: &quot;Guestbook&quot;,<br>        method: &quot;getNames&quot;<br>      });<br>      if (data === &quot;loading&quot;) return false;<br>      return data<br>    },</pre><pre>    utils() {<br>      return this.drizzleInstance.web3.utils<br>    }<br>  },</pre><pre>  created() {<br>    this.$store.dispatch(&quot;drizzle/REGISTER_CONTRACT&quot;, {<br>      contractName: &quot;Guestbook&quot;,<br>      method: &quot;getNames&quot;,<br>      methodArgs: []<br>    })<br>  }<br>}<br>&lt;/script&gt;</pre><p>Here’s the breakdown:</p><ul><li>Import mapGetters from Vuex to allow access to Vuex store modules created by the plugin as computed properties in our component. See <a href="https://vuex.vuejs.org/guide/getters.html">their documentation</a> for more information about how mapGetters works.</li><li>Use mapGetters to import several Drizzle functions from the store. We need a drizzleInstance for its utilities to transform text. isDrizzleInitialized returns a boolean that we can use to conditionally render the app, and show some type of ‘loading’ indicator when it’s not quite ready. Finally, getContractData allows us to pass in the name and method of a contract and receive its state data in reactive fashion. Neat!</li><li>Get the list of guestbook names using getContractData. Check the return value to avoid breaking anything in the layout.</li><li>Import utilities from drizzleInstance to transform hex to readable text. The Drizzle instance extends <a href="https://web3js.readthedocs.io/en/v1.2.0/web3-utils.html">web3.utils</a> — check out the docs for a better understanding of the types of operations available.</li><li>When the component is created, register the contract, method, and arguments using the REGISTER_CONTRACT action from the plugin. This sets up the contract in Drizzle, and makes its data and functions available to your app, including the getContractData method.</li></ul><p>We’re ready to run the app!</p><h3>Running the App!</h3><p>In your terminal:</p><pre>npm run serve</pre><p>You can also use yarn serve if that’s your preferred flavor. On building successfully, a development server should start up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*K3DCDN400KbgU95-3v8kBg.png" /></figure><p>Head to one of the URLs. We’re going to use <a href="https://metamask.io/">Metamask</a> in this example, but you can use any other web3 browser as long as you can import an account and customize the RPC URL.</p><p>If you’re on Metamask and don’t want to lose your current wallet, make sure you’ve exported your seed phrase (Settings -&gt; Security &amp; Privacy).</p><p>Log out of Metamask, then on the login screen click <strong>Import using account seed phrase</strong>. Use the seed phrase provided by truffle develop and your wallet should populate with the accounts shown in the Truffle console. In your app, you should now see an input field where you can add and submit a name to the list. Try it! Within a few seconds, you should see a name appear in the list below.</p><p><strong>You did it! In the next part of this series, we’ll deploy the smart contracts to the Ropsten Ethereum Testnet. Stay tuned!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0c9e3fcdeeb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>