Smart Contracts
Generally, blockchains are meant to solve the Byzantines Generals problem: how do you reach consensus without a centralized authority? They do this using different consensus mechanisms such as Proof of Work, Proof of Stake and different variations, hybrid mechanisms and different flavors in between.
Some blockchains have more or less features, make less trade-offs or increase complexity, flexibility and scalability in many different ways. One of the most useful functions a blockchain can provide is support for smart contracts.
Nick Szabo coined the term smart contract 20+ years ago in papers such as this one, describing it as “a computerized transaction protocol that executes the terms of a contract”. Why all the rage now?
What is a smart contract?
Today, the term generally refers to a program running on a decentralized blockchain with it’s functionality defined in the code. In Ethereum-like chains, the code and data are stored at a unique address, such as the HEX smart contract.
They are often written in Solidity and Golang has become quite popular for interacting with contracts and building the backend to make a lot of the stuff work.
Ethereum is currently the most popular platform supporting smart contracts and we’ll see how what projects find more success on PulseChain as it maintains compatibility with Ethereum, but with several advantages. Also, it will be interesting to see which new contracts are launched exclusively on it due to the cheap and faster transactions. Smart contracts are usually executed using some kind of virtual machine, such as in Ethereum with the EVM.
Smart contracts in Ethereum are a type of account.
A router is a powerful tool in carpentry, a packet forwarding device in computer networking, but a “smart contract used to interact with a pool” in cryptocurrency. Uniswap is made up of multiple contracts, one of which enables routing, such as swapping tokens. Read more about them here.
How do you write one?
When coding, most people prefer a modern IDE like Visual Studio Code that is available cross-platform or if you’re real old school, an editor like vi/vim or emacs. You can write code in notepad and compile it to build the program or run it with an interpreter, although the bells and whistles that development editors and IDEs provide make it more than worth your time to learn to use them.
The Remix IDE is an online, web browser based IDE that is great for getting a feel for writing a smart contract. When you’re actually developing and running contracts, you’ll probably want to get install VS Code and the Solidity plugin which provides syntax highlighting, code snippets and generally makes things easier.
But today, we’ll be using Remix to create, deploy and interact with a simple smart contract.
Anatomy of a Smart Contract
To get a feel for things, you can follow a guide like this one to do a Hello World program, just something simple you can play around with in the web IDE. We’ll use basically the same example code they used, just adapted a little bit and have some fun. But, here’s a few basic things to get familiar with before we get started. We won’t be putting many of these to use quite yet, but they are important to understand before we really get into things.
Contract data can live a few different places
Storage (think Hard drive) is much more expensive in gas, as we’re storing data on the blockchain (SLOAD/SSTORE EVM opcodes) and are used to persist data between contract calls, such as the ERC-20 standard function balanceOf() which returns the balance of tokens an address holds
Memory (think RAM) is available when the contract is running and discarded after it completes, so it’s mostly used for operations within the contract
There’s also the Stack, but it’s a little more low level and you probably won’t need to interact with it directly for data storage when coding contracts
There are different scopes of functions and variables
External means they can be called by other contracts
Internal is when they can only be called or read within the contract itself
Public are universal in a way that they can be called both externally and internally
Private are basically the same as Internal, but aren’t visible to derived contracts
Common data types
int (signed integer)
uint (unsigned integer)
bool (true/false)
address (ethereum address)
As well as arrays, structures and mappings
There are also environment variables, built-in functions and plenty more. See this page and also this one for more info on the anatomy side and this reference for understanding more about performance tricks on the blockchain.
Writing a Smart Contract
Go to remix.ethereum.org
File -> New File -> hello.sol
Let’s start coding!
The first line tells the compiler what version of Solidity we’re targeting. For this Hello World example, we’ll say we want it to run on Solidity version range described below (for compatibility reasons).
pragma solidity >=0.5.0 <=0.6.9;
Next we’ll define a contract function, naming it HelloWorld and everything coming after the curly braces being code.
contract HelloWorld {
Now let’s add a function that simply returns “Hello World”. It’s a public function, that has no arguments and returns this particular string from memory. Oh and in case you’re wondering, we declare the function as pure to let the compiler know that it doesn’t need to read or modify state.
function sayHello() public pure returns (string memory) { return “Hello World”; }
The final code should look something like this.
pragma solidity >=0.5.0 <=0.6.9; contract HelloWorld { function sayHello() public pure returns (string memory) { return "Hello World"; } }
(it doesn’t really say hello as much as it returns hello, but you get the point)
Then on the left tab, click on the refresh button-looking icon that is the Solidity compiler -> Compile hello.sol and kick off the compiler process. You may get a warning about missing a license, but you can ignore that for now while we are testing. Once compiled, click the Ethereum icon which says Deploy & run transactions -> Deploy.
Once deployed successfully, click the drop down arrow under Deployed Contracts so we can try out the sayHello() function.
Hooray, it returned “Hello World” — the computer, ahem the “world computer” talked to us! Coding is magic, right?
But this contract doesn’t seem very smart, does it? In fact, many smart contracts don’t do a lot of complicated things. Sure, most are more lines of code and business logic than this simple contract, but the smart part is meant to denote them running on a blockchain, gaining the benefit of unstoppable code, especially on a decentralized network like Ethereum and not necessarily the value they provide.
Technically, the contract was pretty far removed from execution on the Ethereum we know and love. It was actually ran using a JavaScript VM, which is perfect for testing, uses no real gas and so we can modify it, run and test it as many times as we want. Once we’re satisified there, we could deploy it on the Ethereum Testnet and if that went well, Ethereum-Ethereum, aka the Ethereum Mainnet.
What’s the difference?
Smart contracts that run on the public blockchain which you interact with are running on it’s Mainnet. Often, the developers or people who otherwise support the product will setup a Testnet which allows people to experiment and test contracts before official deployment. Instead of setting up nodes to make fees, they set them up to support development on the network. This helps make sure they are working as intended, find and fix the bugs beforehand and figure things out before spending real Ether, for example, to deploy and run the code on Mainnet as it usually doesn’t cost anything to use the Testnet. If you’re just kicking around code and want to test things, you can just use the JavaScript VM which is the easy, free option until you’re ready to move the code further towards the dev (testnet) and production (mainnet) environments.
Now, when interacting with real, deployed tokens on the Ethereum, you can use Etherscan to interact with the contract via Contract -> Read Contract or Write Contract in addition to viewing the source by viewing the Code section.
Conclusion
Now you should now know what a smart contract is, how to write one and how to deploy and interact with it. Welcome to the 1%!
In the next blog, now that you’ve had the primer, we’ll go over how to make an ERC-20 token and deploy it in the real world! For fun, not necessarily for profit… anybody can make a token, but you need a good idea, a solid dev or team behind it and a commitment to getting it right in order to create the value that merits rewards such as crypto riches. Or, buy and hold products that have product-market fit like HEX.com — it’s pretty neat.
The bear market is a great time for learning, getting your finances right and reflecting on the path you’re on and making adjustments to make sure success in inevitable. If you want to take some good, formal classes on smart contracts, I learned a lot from this Coursera class and I’m sure there are plenty of other good ones as well. Also, if you haven’t already, stay up to date with PulseChain, a faster, cheaper Ethereum fork. That’s another product in the cryptosphere that I’m interested in. Now go forth and get smart(er)! Your financial future depends on it.