![]() ![]() Obviously, this is just the tip of the iceberg when it comes to building a Bitcoin blockchain explorer. We want to HTML being injected into our JSON to be interpreted as raw HTML, and not encoded as special characters.Īfter carrying out the same changes in our BlockView and our block HTMl template, cleaning up our layout template, and adding a few final styling touches, we have our result.īehold, the most basic of blockchain explorers! Final Thoughts Notice that we’re wrapping mark_up_block with raw. In our HTML template, we can replace the contents of our block with the result of mark_up_block: |> Map.replace("nextblockhash", hash_link(block)) |> Map.replace("previousblockhash", hash_link(block)) It replaces the hashes in previousblockhash and nextblockhash with links to those blocks, and JSON encodes the resulting object: Using this function, we can write a function that modifies our block header. In our HeaderView module, our hash_link function would look like this: The first thing we’ll do is write a function in the corresponding view file that converts hashes into links. Let’s transform these hashes into links so users can easily navigate through the blockchain. These hashes, as we would expect, point to the previous and next blocks in the blockchain, respectively. The blocks and block headers we receive from our Bitcoin full node come with previousblockhash field and (usually) a nextblockhash field. Let’s add an extra layer of interactivity to our blockchain viewer. While dumping raw JSON into the DOM is informative, it’s not especially user-friendly. css file, we should set the white - space rule on blocks to preserve this formatting:Ī bit minimal, but beautiful none-the-less. encode! ensures that the resulting JSON string is nicely formatted. The pretty: true option passed into Poison. The simplest way to do this would be to dump the output of Poison. The more bare-bones way to render our blocks and headers, while still being meaningful to our users, is to render the data received from our controllers as JSON code blocks. To keep the scope of this article manageable, we’ll keep our user interface as minimal as possible. Now all we need to do is build out our templates! Our blockchain viewer now correctly routes the user to the appropriate :show route for either the block or block header they’re trying to view, and passes all relevant data to the corresponding template to render. Let’s more on to the final piece of the puzzle: rendering that data. With our routes properly configured, we can fetch data about any full block or block header just by knowings its hash. We use that hash to redirect the user to the header’s :show route. getbestblockhash to fetch the most recently verified block hash from our Bitcoin full node. ![]() Redirect(conn, to: header_path(conn, :show, hash)) With url nil, "result" => result} reason} -> <- Blockchain.getbestblockhash() do That’s because we implemented the Blockchain context module in our last article! Let’s remove the auto-generated contents of Blockchain and copy over the bitcoin_rpc function. Our context will be our interfacing with our Bitcoin full node. When we generated our header and block resources, we also generated a Blockchain context module. All of the data we’re rendering lives in our full node! Our Blockchain Context We don’t be needing Ecto in our blockchain viewer. ![]() Notice that we specified -no-ecto when generating our new project, and -no-schema when generating our block and header resources. Mix Blockchain Header headers -no-schema One for viewing block headers, and another for viewing full blocks: Once we’ve gone ahead and set up our new project, let’s add two new routes to our application. We’ll be using Phoenix 1.3 and the new phx generators that shipped with it: Let’s get to it! Project Scaffoldingįirst things first, let’s create our new Phoenix project. ![]() In this article we’re going to use the Phoenix framework to build a bare-bones blockchain viewer. At this end of the day, we had a small handful of Elixir functions that could retrieve some basic information about the Bitcoin blockchain. Earlier this month we dove into the brave new world of Bitcoin development by writing an Elixir module that could communicate with a Bitcoin full node. ![]()
0 Comments
Leave a Reply. |