Understanding The Web

Ethereum Bootcamp: Solidity Core

Posted

Smart contract development can have a variety of use cases. Week 6 of Alchemy University’s Ethereum bootcamp builds on topics from the previous week and explores advanced smart contract topics.  Having introduced concepts such as mappings, arrays, and structs in the previous week, the content for week 6 uses those ideas to introduce more complex subjects.  Students first learn about multi-signature contracts.  This is followed by the concept of smart contract inheritance.  Students then learn about ERC-20 tokens and NFTs.  Week 6 provides students with information to build practical smart contracts.

Multi-Signature Contracts

Multi-sig Overview

Understanding multi-signature contracts is a useful tool for a smart contract developer.  It is a contract that requires multiple signatures from different addresses in order to execute a transaction.  Multi-sig contracts are commonly used as a wallet.

Multi-sig Utility

Multi-sig contracts can serve as a wallet because they can hold and transfer funds.  Because a multi-sig requires multiple signatures, the contract requires the validation of multiple private keys.  This requirement avoids a single point of failure which makes it much harder to compromise funds.  Multi-sigs are designed to provide a greater degree of security against lost or compromised keys.

Single Point of Failure

Week 3 covered externally owned accounts.  Externally owned accounts (EOA) are one of two types of accounts in Ethereum. An EOA directly controls an address and any funds associated with it.  This means that the EOA has ownership of the private keys associated with the address.  In this scenario, there is a single point of failure.  Anyone that gains access to the private keys would be able to control the assets associated with the address.

Smart Contract

In a multi-signature wallet set up, multiple keys are required to approve a transaction.  The contract above is an example of what a multi-sig contract looks like.  The contract can receive ether, but for a transaction to occur, it requires a set number of confirmations which are controlled by various functions in the contract above.

Multi-sig Contract Wallet Use Cases

Multi-sig contracts can have many use cases.  From the perspective of a family, a multi-sig would work well as a contract for inheritance or a will.  Startups can use a multi-sig for business expenses or treasury management. Whenever added security is required for funds that may be public or shared, a multi-signature contract would be useful.

Smart Contract Inheritance

Inheritance Overview

Solidity has features that are found in other programming languages.  One of these features is the concept of inheritance.  Inheritance allows developers to create objects that can be used a base for other objects.  In Solidity, the objects will generally be contracts and interfaces.

The idea is to create a contract that will be “inherited” by another contract.  By using the is keyword, a smart contract can inherit another smart contract.  This creates a parent-child relationship where the inherited contract is the parent and the contract that inherits is the child.  The child inherits state variables and functions from the parent contract with the ability to add behavior as necessary.

Inheritance in Computer Science

In computer science, inheritance allows programmers to create classes built on top of existing classes

The code snippet above is an example of inheritance in JavaScript.  The Animal class is the parent class of Dog.  The Dog class inherits the eat and sleep properties of the Animal class.  These same principles are applied to inheritance in Solidity.

Inheritance in Solidity

Inheritance in Solidity is achieved with the is keyword. 

In the example above, the is keyword points contract B to contract A. Contract A is the parent, or base, contract.  Contract B is the child, or derived, contract. This is also an example of single inheritance.  Single inheritance inherits variables, functions, modifiers, and events from the base contract. 

Multi-level inheritance is similar to single inheritance with an added level of parent-child relationships.

Contract A remains the base contract, but contract C is a child of contract B.  This is also referred to a smart contract inheritance chain. 

Another example of inheritance is hierarchical inheritance. 

In the example above, contract B and C are both children of contract A. Contract B and C are considered siblings through their connection with contract A, but they are not connected in any other way.

Another import form of inheritance is multiple inheritance.

In this example, contract C inherits from both contract A and B.   

Solidity Inheritance Function Syntax

An override is an important concept related to inheritance.  A child contract can override a function from the parent contract. 

In the code snippet above, the parent contract contains a function with the virtual keyword.  This is a declaration that a child contract can override the function. 

In the child contract, the override keyword declares that the function from the parent contract will be overridden.

Inheritance Use Cases

Inheritance is an important and useful concept to understand. Companies such as OpenZepplin produce industry standard smart contracts.  Contracts from OpenZepplin are widely used and audited.  These contracts are often used in projects as parent contracts. 

The code snippet above is part of OpenZepplin’s Ownable contract.  The part of the contract provides a variable type of address called owner.  The constructor assigns owner to the address that deploys the contract.  The modifier onlyOwner can be placed on functions to ensure only the owner to can execute the function. 

In the example above, the Ownable contract is inherited by MyContract.  The onlyOwner modifier is applied to the changeNumber function.  This example highlights the convenience of inheritance.  The onlyOwner modifier was used in the child contract without being written in the child contract.  MyContract inherited all the properties of Ownable.  Inheritance plays a key role in Ethereum’s popular ERC-20 token.

Erc-20 Tokens

ERC-20 Tokens Overview

Ethereum has the power to tokenize anything.  An ERC-20 token (Ethereum Request for Comment 20) is a representation of an asset on the Ethereum network.  The asset can represent voting rights, cryptocurrencies, shares in a company, or anything else that can be tokenized.  ERC-20 is a technical standard.  It exists to increase compatibility in the Ethereum ecosystem. 

ERC-20 Token Smart Contract

An ERC-20 token smart contract uses a mapping to keep track of fungible tokens.  Tokens that are fungible are equal.  There are no tokens that have special rights or behaviors.  This is what makes an ERC-20 token ideal as a medium of exchange as all the tokens would behave the same.

ERC-20 Token Interface

ERC-20 defines a common interface so that any application can use it in a standard way.  This ensures that tokens created in the Ethereum ecosystem have the same properties.  The interface dictates that a certain number of functions must be present along with some optional functions.  An ERC-20 token must provide the following:

  1. name, symbol, and decimal as optional fields
  2. totalSupply which defines the circulating supply of the token
  3. balanceOf which returns the balance of a particular user
  4. transfer which allows the transfer of tokens from one account to another
  5. approve, transferFrom, and allowance are additional functions that allow

ERC-20 and Inheritance

The snippet above illustrates that an ERC-20 is an interface.  Any smart contract can inherit the ERC-20 interface.

MyContract inherits all the properties form the ERC-20 interface. The ERC-20 standard enables developers to create compatible smart contracts.  While ERC-20 represents fungible tokens, non-fungible tokens are also a defining feature of Ethereum.

What are NFTs

Non-Fungible Tokens

Digital art has elevated NFTs to mainstream prominence.  NFT stands for non-fungible token.  Unlike ERC-20 tokens, NFTs are tokens that have unique characteristics. While digital art has been the most common use case, NFTs can represent any type of ownership on the blockchain.  Fundamentally, an NFT represents digital ownership of some unique good on the blockchain.  In its current form, it is easy to imagine NFTs representing ownership of art, music, and video game assets.  As they evolve, NFTs can potentially extend ownership of real world assets such as titles to cars, deeds to houses, and other legally binding documents onto the blockchain.

NFTs in Practice

As ERC-20 is a standard, NFTs also have associated standards.  The two most common are ERC-721 and ERC-1155.  While all the data associated with ERC-20 tokens are stored on chain, NFTs store some of their data off chain.  NFT data that is stored off chain is referred to as metadata.

Metadata Storage

Metadata storage also has a standard.  As decentralization is a theme in a blockchain ecosystem, there are decentralized storage options such as IPFS and Arweave to store metadata. Storing metadata through centralized services is still an option.  However, the availability of the metadata will be completely dependent on the centralized service.  Services such as IPFS use a peer-to-peer model to host metadata. In a worst-case scenario, self-hosting through IPFS would be an option if there are no peers hosting the data. 

As NFTs have gained popularity, it is an aspect of blockchain development that should not be ignored.  While the current use-case is mainly tied to digital art, the possibilities are limited by the imagination.  Understanding NFT development and metadata storage will be a valuable tool for smart contract developers.

Conclusion

Developers learning smart contract development will be equipped to build the digital infrastructure of the future.  Learning how to build a multi-signature smart contract is a skill that can be applied to many industries.  Multi-sigs can be useful for a start-up in the form of treasury management or a family in the form of a will.  Developers that master smart contract inheritance will save valuable time in the long run.  Using battle-tested smart contracts through the concept of inheritance not only saves developers time but offers enhanced security.  ERC-20 tokens and NFTs are a staple in the current form of smart contract development.  Mastering these concepts will provide developers with tools to bring strong utility to projects.  Week 6 of Alchemy University’s Ethereum Bootcamp provides students with a wealth of knowledge to become strong smart contract developers.