Building on Ethereum: Part 8 – Deployment
Check out the final post of building on Ethereum where we focus on deployment.
Join the DZone community and get the full member experience.Join For Free
in this series, i’m discussing the phases of a project encompassing a non-trivial set of ethereum smart contracts and the react/redux application that communicates with them.
the project, called in-app pro shop , aims to help ethereum developers easily support in-app purchases as erc-721 non-fungible tokens (nfts).
- part 1 – decisions
- part 2 – functionality
- part 3 – setup and test
- part 4 – writing contracts
- part 5 – writing tests
- part 6 – bootstrapping the client
- part 7 – client to contract communications
- part 8 – deployment
a time will come
so far in this series, we’ve seen how to set up a ganache-cli ethereum client that we can use to do local testing. for all intents and purposes, it is the blockchain to us. and the great thing is, no one else can see our code or our transactions. while our accounts get 100 ether by default, we can give ourselves as much as we like using the
option, so we can test to our hearts delight in our safe little cocoon.
but at some point, you have put it out there and let other developers start interacting with your contracts. what’s involved with that?
spreading our wings
first, let’s consider the question of what’s being deployed and where.
according to blockchainhub , the frontend of a so-called “dapp” can be deployed to a decentralized network such as swarm or ipfs . like ethereum itself, these networks use decentralization to ensure that no one computer (and thus no one person or organization) has control of the data or application.
that’s a wonderful concept, but there are a few problems with it at the moment:
- the networks are just too slow.
- the gateways are too few and often offline.
- they serve only static files, which precludes things like server rendering of a react or angular app.
in the future, perhaps, this situation will change, but i doubt it. the main thing that we want distributed is the data, and that’s in our contracts, to be deployed to the blockchain.
build the react app
thus far we’ve been running the local server the create react app scripts offer us while developing. now we need a production build. after the following, you should end up with a “build” folder containing the deployable build. only that folder needs to be deployed.
- in package.json , change the “homepage” entry to be the url you’ll be deploying to. this is important because react infers certain things about the location of assets based on this entry.
build the react application:
npm run app:build
pick a hosting service
so, for this project, deployment only requires an ordinary hosting service. if you have a hosting provider you’re happy with, just skip the following parts. for instance, you could use github pages if you wanted. in this case, we’ve already set up github pages for the project to serve a docs page, so we’ll need to host the app elsewhere.
for our purposes, we’ll use firebase . google offers a most generous free tier with their firebase hosting product. and it’s possible that we will want to extend the application with a backend that tracks usage or provides other off-chain services. firebase’s realtime nosql database, cloud functions, and cloud messaging make it an excellent platform choice for the growth potential it offers.
in order to achieve this, only a few steps are necessary. i won’t go into great detail since it’s an aside from the business of writing an ethereum dapp, much as using styled components for applying css to the ui components was not integral to understanding the process, but was an ancillary decision for making the app look pretty.
if you know firebase, this will be old hat. if not, you might have to read a few paragraphs of their docs along the way while the following these basic steps. it’ll be worth it, though. firebase is awesome. but again, any hosting service will do, and the app itself isn’t dependent upon firebase in any way.
deploy to firebase
we’re currently not talking to the firebase backend from our client, only hosting it, so it’s super easy. in order to deploy the project to firebase:
- go to the firebase website and create an account or log into your console if you already have an account.
- create a new project.
- connect a custom domain or subdomain to your project or take note of the project url (e.g., https://myproj.firebaseapp.com).
install the firebase tools on your development machine if you don’t already have them.
npm install -g firebase-tools
log in with the account used to create the project.
- create a .firebaserc file with the name of the project set as default. the one in the project points to the project name i’ve deployed it to. if you’re deploying a copy of the project for testing, you’ll need to change this to the project name that you just created in step 2.
- create a firebase.json file, which will tell the firebase tools what directory to deploy, what files to ignore when uploading, and what headers to send when someone requests the site. the one in the project should work perfectly without modification.
deploy the application to hosting:
as you’re aware by now, deploying contracts and testing them costs ether. we have plenty of that available to us on our local ganache-cli instance, albeit simulated and only usable locally. but on the ethereum mainnet, we need actual cryptocurrency to deploy.
fortunately, before we have to make that plunge, there are the testnets. and with them, faucets.
pick a testnet
- ropsten – cross-client testnet
- rinkeby – geth client testnet
- kovan – parity client testnet
- görli – poa client testnet
we’ll deploy to ropsten. but how do we do that?
you can deploy via remix, the ethereum browser-based ide. however, with a project of this scale, you don’t want to be cutting and pasting the contracts into the browser. that’s mainly for simple experiments.
we could also deploy with a fully functional network node, which is a bit beyond the scope of what we’re trying to do. that’s generally what you want to have in order to set up an mining operation.
for a non-trivial project such as ours, infura is our easiest way to deploy to a testnet. infura is free to use. it was created within consensys , which also brings us, among other amazing projects, the truffle framework and metamask, which we’re already quite cozy with.
first, however, we need some faux ether so we can deploy to ropsten via infura.
get some ether from a faucet
you presumably have metamask installed by now, but if not, head on over to metamask and install it in your chrome or firefox browser.
if you’re just installing it, while you are there, be sure to watch this video , which is embedded in their page. it describes how to operate metamask and is very helpful if you’re not familiar with it.
next, make sure the ropsten test network is selected as shown in the screenshot.
now go to the metamask ether faucet and request some ether by clicking the big green button. you will be asked to allow the faucet to connect to your account.
you’ll see the transaction id and if you click it, it will take you to etherscan, where you’ll see that the transaction is pending.
in a minute or so the transaction will clear and you should be good to go. you now have enough play ether on ropsten to deploy your contracts.
copy your seed phrase
metamask created an account for your use, into which you just added 1 ether. but in order for us to deploy using that account, we need the “seed phrase” or “mnemonic”. it is a set of words that are used to generate your public and private keys.
- click the account avatar on the right side of the metamask popup.
- click the settings menu option.
- click the security and privacy menu option.
- click the reveal seed words button.
- enter your password to reveal the seed words then click the copy to clipboard button.
deploy with infura
this is a super streamlined process so, hold on to your hat!
- go to the infura website and create an account or log into your dashboard if you already have an account.
- click the “create project” button on the dashboard and give your project a name.
- you’ll see all the keys and urls associated with the project now. copy the project id.
- edit the truffle.js file and update the url for ropsten network with your project id and your metamask mnemonic (which you copied to the clipboard earlier).
- migrate contracts with the command: “truffle deploy –network ropsten”
generating transactions in the app
woohoo. now the contracts are deployed. and your application should talk to it. navigate to the web url where you deployed the app and it should ask if you want to allow the app to connect to your account via metamask. click accept and you should be able to create shops, categories, and items.
if you’re just following along, just head on over to https://iaps-test.futurescale.com and give it a whirl!
that’s an instance of this project deployed as described above. if the app doesn’t appear to connect or you don’t see the metamask popup asking you for permission to connect to your account, kill your browser and launch it again.
after filling out the new shop form, for instance, metamask will show show you the terms of the transaction and ask if you want to confirm it.
in this case, there is no actual ether amount to be transferred, only the gas fee.
viewing transactions and accounts on etherscan
after what seems like forever (maybe half a minute), metamask will notify you that the transaction is complete.
if you click the browser notification, it will take you to etherscan where you can view the details of the completed transaction.
the from and to addresses are links. they take you to the the account that initiated the transaction and the contract that received it.
in the app, when you are connected to a network supported by etherscan, you can view the account on etherscan from the accounts menu. however you get there, etherscan’s page for an address shows all the transactions for that address as well as its balance.
in this installment, we’ve seen how to deploy the application to a hosting provider (firebase), how to get ether from a faucet, how to get our seed phrase from metamask, how to configure truffle to deploy to the ropsten testnet, and finally, how to use infura to do the actual ropsten deployment.
and with that, this series finally comes to an end. however there is a lot more depth to be explored in the codebase, and so i plan to expand on this series in an ebook soon. in the meantime, i hope you’ve enjoyed this tutorial and will get some use out of the project, whether you’re just exploring ethereum, or looking to build on it to enable in-app purchases.
Published at DZone with permission of Cliff Hall, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.