Intro to Ethereum Development
Tools in the toolbox and cools in the coolbox!
Ethereum is rad. I fell in love with the technology and fell down the proverbial rabbit hole, but I’d like to focus on concepts for building cool apps here…
If you take a look at the ethereum.org/developers page you will find a slew of links to great content that will help you get up to speed. I’d like to short cut that a little bit here and bang out a TL;DR about Ethereum smart contract development.
I’ll be using some of my roll-your-own tools that I built specifically for building proof-of-concepts at hackathons. You can check out one such hack called Nifties vs Nfties here.
I have a ton of content on Clevis & Dapparatus available. We’ll use them here to demonstrate some blockchain and smart contract development concepts. Starting in an empty folder or repository we run:
npx clevis init;npm i
It will prompt for a few default directories that you can just yolo through.
(Also, You will need to have nodejs version 10 installed. Check out the Clevis repo for further troubleshooting.)
I will also get a local ganache node up and running with:
npm install -g ganache-cli; ganache-cli
Neat! Okay so what *is* an Ethereum account anyways? Well, it goes back to cryptographic technology from 1983. A key pair consists of a public and private key that have some really cool properties. Let’s focus on signing:
clevis sign "hello world" 0
This will produce a signature `0x8d300f1393…` that we can then recover:
clevis recover "hello world" 0x8d300f13...
This will return the original account that was used to sign the message. Why is this awesome? It means you can prove with maths™ that certain account signed a certain message. That is how transactions work!
This neat trick can even be used in smart contract execution, but we are getting ahead of ourselves…
Let’s also fire up our Dapp with:
npm start
You can open up an incognito (away from MetaMask or other injected web3) browser to localhost:3000 and you will instantly be generated an Ethereum account.
Let’s go ahead and send that account some test ETH with:
clevis send 1 0 0xf458896cca6f6505c5695365c515953e15848d6d
Let’s create a simple example smart contract just to see things end-to-end before we start experimenting:
clevis create SomeContract
Let’s add a name
field just as a test that everything is working right.
clevis compile SomeContractclevis deploy SomeContract 0clevis test publish
This turns our contract into bytecode, deploys it to our local blockchain, and injects the ABI into our Dapp. Let’s make a few edits to the Dapp so we can employ the contract loader to bring in our contract:
Rad so there is our name from the contract in the Dapp. We are wired up!
Let’s slowly walk through a few neat concepts with smart contracts. First, let’s create a function that lets us set the name:
We’ll add some UI in the Dapp to let us set the name:
And a hook in the Dapparatus onUpdate
to update our name
:
I always forget, but if you are using
await
you need to addasync
to the function.
Cool! So now we can read and write from the contract!
Let’s dive into why programmable money is so powerful. Let’s make users pay to update the name.
We’ll make the function payable
and require that they put in more than the last person:
Then compile and deploy the update:
clevis test full
And add some UI to allow the user to put in and send value in the transaction:
Neat! Works!
Okay finally we want to be able to withdraw the ETH but only as the owner:
clevis test full
Now we can withdraw the ETH from the command line as the owner that deployed the contract:
clevis contract withdraw SomeContract 0
RAD.
So we’ve learned about payable
functions, tracking msg.sender
, tracking msg.value
, and requiring certain situations. But what about that signed message?
Okay, You’ll want to dive in and read more about the ecrecover()
function. It can do that recover on-chain and that opens up a whole new world for onboarding and usability when users can just sign messages and they don’t need ETH or gas or MetaMask!
I would start with my Native Meta Transactions tutorial.
Next in a long line of tools you might use is the Open Zeppelin contracts. I have a great tutorial for creating a shitcoin, but basically you can just bring in the OZ contracts and set your symbol and deploy:
What about randomness? This is hard on chain, but I have a tutorial on commit/reveal here!
Conclusion
There is so much great content out there for Ethereum developers. Google is your friend, but I’m always here too! Hit me up @austingriffith everywhere!