What is Smart Contract, A Beginner’s Guide
In our everyday living (whether it’s related to finance, health, politics or so on), there’s always a set of agreements that guides the relationship, oftentimes through an intermediary. These are called contracts and they regulate aspects of our professional lives. They’re the fabrics of modern society. It’s no joke that cryptocurrency breaks so many barriers yet this is one of the few it adheres although in a different way. What does this mean?
WHAT’S A SMART CONTRACT?
Wikipedia might be a fair place to start and it defines a smart contract as a computer program or a transaction protocol that’s intended to automatically execute, control or document legally-relevant events and actions, according to the terms of a contract or an agreement. In essence, these are simply pre-determined agreements between parties that allows them to be absolutely sure of the outcome of any transaction; they can also act as automation software, that triggers another set of actions when a certain condition is met.
Smart contracts allow a high level of trusted agreements/transactions between anonymous parties without the need for a central authority or a legal system. Using smart contracts makes any transaction on a blockchain more transparent, traceable and irreversible.
You might be wondering, why the word ‘smart’. Well, that’s because they are far more functional than the traditional paper contract in so many ways. But before we look at the benefits of smart contract, we need to understand how it works.
HOW A SMART CONTRACT WORKS
It’s important to know that a smart contract doesn’t mean legally-binding by law. Some legal practitioners have argued that a smart contract holds no real legal authority and it’s just binding between parties. But we’re not dwelling on that.
Smart contracts were first proposed by American computer scientist Nick Szabo in 1994, who was also the creator of the ‘Bitgold’. He defined smart contracts as computerized transaction protocols that execute the terms of a contract.
The process of a smart contract isn’t so complicated but I’ll explain simply below:
Creating the codes: Teams come together to define the criteria for the Terms and Conditions (yes, those things we never read) that are needed for a smart contract to work. They then incorporate these rules into a line of codes. More complex operations, such as releasing a fund automatically, have to be encoded using more sophisticated rules.
Execution: A security team, separate from the developers, then tests the logic and does rigorous security testing. The contract is then added to an existing blockchain once authorized. At this point, the smart contract will begin to execute the initial stage of the code. It’s also configured to wait for updates from an ‘oracle’ (a cryptographically secure streaming data source). Once it gets enough information from this ‘oracle’, it starts executing immediately.
Note: byzantine fault-tolerant algorithms secure the smart contract in a decentralized way from attempts to tamper with it. Once a smart contract is deployed, it cannot be updated.
When this code starts running, clients with smart contracts can now interact with one another via the contracts.
BENEFITS OF SMART CONTRACTS
Security: It’s no secret that blockchain technology has one of the highest data encryption software in the world, so it’s complicated to hack any smart contract. Most importantly, each entry on a distributed ledger is linked to the entries before and after, and hackers would have to change the entire chain to change a single record. That’s a challenging procedure to go through to change a single form, so security is guaranteed when using a smart contract.
Transparency and Trust: All encrypted logs of a transaction at every time are shared among participants of that smart contract. Trust is achieved through real-time communication and increased visibility. It means at any day and time, all users of the smart contract can see every single transaction in real-time. So there’s no need for fear or uncertainty because a third party can never have access to the transaction.
Accuracy and speed: When all conditions are met, the contracts are immediately executed without waiting for approval. All transactions are digital, automated and also eliminate the place for error during transactions while providing trusted, reliable and accurate data across multiple parties.
APPLICATION OF SMART CONTRACTS
Let me give you two actual life instances where smart contracts can be used.
The first is the role it plays in the Ethereum network. Sincw Ethereum was first introduced in the blockchain space with its presence in 2015, Vitalik Buterin, a Canadian-Russian programmer, has brought forth new decentralized applications (dApps). However, Ethereum’s success is significantly attributed to the implementation of smart contracts.
The second instance is quite a theory so follow me here. Nigeria is about to have a ground-breaking national election in a few months, and uncertainties and distrust have clouded the entire process and the electoral body, INEC. But Nigeria isn’t the only country that faces corruption during electoral times. Citizens of the United States, for example, also complain of frequent interferences in their elections, either by rival governments or electoral frauds. We’re going to look at how smart contracts can solve these problems.
ETHEREUM BLOCKCHAIN IMPLEMENTATION OF A SMART CONTRACT
Ethereum-based contracts have ambiguous uses and work like a utility player, in football terms. Ethereum allows developers to create their smart contract and run it on the blockchain. Ethereum is the only blockchain network that allows arbitrary codes to be executed on its platform.
Let's quickly understand how smart contracts are being run on the Ethereum network:
- ETHEREUM VIRTUAL MACHINE (EVM): This machine serves as a runtime environment for smart contracts. A simple way to describe the EVM is to view it as a giant super-computer that executes all transactions in the smart contract.
As the name implies, EVM is not a physical computer but a virtual machine. It's a simple state machine, so it can only do one thing simultaneously. It cannot also be used to write a programming language, so it requires a programming language software, Solidity.
- SOLIDITY: A smart contract programming language on EVM, this allows you to perform arbitrary computations but is also used to send and receive tokens, and store states. Solidity is greatly influenced by C++, Python, and Javascript so developers can understand its syntax quickly. It's also a programming language used on the Ethereum network itself.
It's advisable to have a good grasp of Solidity to be able to write smart contracts on Ethereum.
- GAS: This is a fee of the amount of Ether (ETH) required for an Ethereum blockchain network user to conduct a transaction on the network. Gas fees are sometimes used to compensate Ethereum miners for their work in verifying transactions and securing the network.
Computation of smart contracts requires gas to execute, and the more complex the programming language on EVM, the more expensive the gas.
SMART CONTRACT IMPLEMENTATION ON VOTING
There's a pattern of struggle in the general voting system: identity fraud, miscounts and corruption among officials. Blockchain can eliminate most of these issues. For example, with our knowledge of smart contracts, where we can set pre-defined rules and regulations, we can quickly achieve the following:
- No voter can vote from a registered digital identity that's not theirs. So each unique ID is to one specific individual. These eliminate the errors that electoral officials go through during voting.
- Every vote is registered on a blockchain network, and the counting is tallied automatically with no interference from a third party or dependency on a manual process. Therefore, every counting is foolproof.
- Validation is accomplished by the users on the blockchain network itself. It means every single voter can see the result as they vote, more like a system on Twitter where you see the percentage of votes after you vote.
- The voting process can be in a public blockchain, or it could be in a decentralized autonomous organization-based blockchain setup. An electoral body could do this like INEC.
The ledger is then publicly available for future audit and verification by everyone.
ARE SMART CONTRACTS DIFFERENT FROM LEGAL CONTRACTS?
There's one distinct difference between these two types of contracts. The smart legal contract is legally enforceable by law, while the smart contract is not. Note that while a smart legal contract will contain some elements of a smart contract, not every smart contract will be a smart legal contract.
Finally, it's important to note that the blockchain is responsible for single-handedly pushing smart contracts into the future. But there’s still a lot of room for improvement and innovation. However, the growing adoption of smart contracts continues to highlight their intended transparency, accountability, and efficiency.
How to Create a Smart Contract
Now, you might be thinking, “How do I create a smart contract?” Here’s a guide to get you started on running your smart contracts. Let's get started:
STEP 1: Connect to an Ethereum Network:
There are many to request to enter the Ethereum blockchain. You can launch an Ethereum network using the accessible version of Alchemy. Alchemy is a blockchain developer platform and API that allows you to communicate with the Ethereum chain without running our nodes. While it has an analytical tool, you can use this platform to gain a better understanding of what's underneath the codes. But for the purpose of this article, we're using MetaTask. Now, install MetaTask on your Chrome browser. Once enabled, click on the icon at the top right corner of your browser page. Click it to open another page.
STEP 2: Set up your account:
Click on ''Create wallet'' and follow the prompts. After creating an account, you’ll be sent a secret backup portfolio. Please don't share this with anyone.
Ensure you are on the ''main Ethereum network''. A way to check this is when you notice a checkmark beside the main Ethereum network.
STEP 3: Use fake ether and choose your preferred network:
There are at least five services to choose from on the MetaMask. Please note that all those services are for learning purposes and hold no real ether value. We need fake ethers in your MetaMask wallet to test the smart contracts.
For example, you can pick the Goerli test network, click on Deposits and ''get ether'' from the Goerli faucet. Once you have added your preferred ether amount, you can start writing smart contracts on the Remix Browser IDE in the Solidity programming language.
STEP 4: Write your code or click on sample file:
You can write your codes or click on the ''+'' icon in the top right corner to create a .sol extension file. You can now pick any standard template that fits the token you own.
STEP 5: Compile and Deploy:
Click on the deploy button at the remix Windom Right side to deploy the smart contract on the Ethereum test network. Wait till the transaction is complete. After successfully deploying your code, your smart contract address will not be visible to be copied. Note that tokens will be stored in a user's wallet deploying the smart contract.
To check the tokens in your wallet, go to the metamask window, click add tokens, and enter the smart contract address. You would be able to see the number of tokens there.
You can now repeat the process by using real ETH and switching to the main Ethereum network on MetaMask. You can now repeat the process above.
TOOLS TO IMPLEMENT ETHEREUM SMART CONTRACT
- Web3.js
- Truffle
- Node.js
- Ganache CLI
- Parity
- Visual Studio Code