Sequence
Jump to section
    Tags

    Published 13th November 2022

    Guide to Smart Contracts

    Guide to Smart Contracts Image

    If you’ve been around the blockchain space for a little while, you’ve definitely heard of the term ‘smart contract’. It's the thing which allows blockchains to do cool stuff that most people do when interacting with ‘Web3’. But it's easy to just ignore what exactly smart contracts are, and how they work - so we wanted to provide a guide to smart contracts to get you up to date.

    What are smart contracts?

    Smart contracts are essentially code and states stored on the blockchain which users can use to perform actions on a blockchain. Giving users this ability means that they can go beyond just simply sending and receiving tokens (like is possible on the Bitcoin blockchain) and begin to perform more advanced activities such as lending out tokens to other users, bridging assets from one blockchain to another, creating NFTs, interacting with games and more. 

    When users submit transactions to a blockchain they contain requests to write stuff, such as the transfer of X amount of tokens from one address to another. If the request submitted by the user is valid, the blockchain processes the request and generates an output. On smart contract compatible blockchains, such as Ethereum, processes can also be computed on an EVM (Ethereum virtual machine) which acts as a decentralised computer. The EVM runs smart contract code when a user requests it and can allow for more complex outputs, such as swapping one token for another.

    As an example, to swap a token on the blockchain through smart contracts you firstly require a contract capable of performing the swap (such as Uniswap’s router). The smart contract essentially acts as a middleman, similarly to how traditional exchanges like the NASDAQ do to facilitate users to buy/sell assets. However unlike the NASDAQ there is usually no central party who oversees every transaction. 

    Instead, the code which users interact with to swap tokens is set and submitted to a blockchain for anyone to read. When a user wants to swap tokens they send a request to the Uniswap smart contract saying they would like to swap X token for a certain amount of Y. The smart contract checks the value of both tokens against an oracle (a trusted source which says the current price of tokens) to confirm that the swap is valid, and then facilitates the taking of X token and giving of Y

    Because blockchains are transparent and everything submitted to them is recorded on a public ledger, all users can look at the code within smart contracts and what transactions other users have made at any time in the history of the blockchain – unless the process was performed off chain (but this is another can of worms we wont get into!).

    For a lot of smart contracts, once they are submitted to the blockchain the ‘owner’ address of the smart contract is sent to a burn address – an address no-one has access to (such as this one). This means that theoretically no one can block the use of the smart contract by deleting or changing it – so the only thing you have to trust is the code when interacting with a smart contract.

    Smart Contracts can vary a lot in complexity, some projects such as Token MultiSender have very simple Smart Contracts with only a few functions, whereas more complex applications such as lending platforms like Aave can have thousands.

    Smart contract specifics

    The uses of smart contracts are very wide ranging, and one of the main drivers for the success of blockchains such as Ethereum is due to the many different uses they can facilitate. From NFTs to games, yield bearing vaults to DAO’s, smart contracts are integral for them all to function.

    A main component of nearly all smart contracts are called ‘Ethereum Request for Comment’ or ERC standards. These are the token standards which most Ethereum based smart contracts use. They define a specific set of functions for each type of token and facilitate how applications and smart contracts interact. 

    Developers can propose their own ERCs which then get assessed by Ethereum’s core developers before potentially being implemented for other developers to use. 

    To give an example of some of the ERCs available on Ethereum, these are some of the most popular token standards (in total there are 38 ERC types which have been implemented):

    ERC-20 – The most widely used ERC, it is the standard for fungible tokens. These tokens, like UNI, Matic, USDC etc. are fungible tokens which most closely relate to a currency. 1 USDC is the same as all other USDC, and all ERC-20 tokens are interchangeable with one another.

    ERC-721 – Also known as NFTs (Non-fungible tokens) are tokens which are all unique. Classified as ERC-721 (which is a token type on Ethereum), each of the tokens has its own identifier assigned to it by the smart contract that the tokens are based on. When the tokens are minted they are assigned an ID, and that ID can then carry unique properties.

    ERC-777 – A slightly lesser known standard, this token type is aimed at allowing developers to build extra functionality – such as a smart contract integrated way to recover private keys or mixer functionality for extra privacy. This token had some security issues, so was later replaced by ERC-820, which itself was then superseded by ERC-1820.

    ERC-1155 – Aimed at bundling transactions and making trades more efficient to help reduce on gas fees. This ERC can be used in creating both fungible and non-fungible tokens.

    ERC-4626 – A standard for yield-bearing vaults, to help optimise and unify the parameters. It works in conjunction with ERC-20 and is designed to make integration of vaults easier. This token was implemented

    The development of ERCs is a continuous process, either bringing in a new concept or improving on existing ones. Quite often more complex ERC’s (such as ERC-777) are later superseded by another ERC to help make them more stable and safe.

    Limitations of smart contracts

    However there are still limitations to what smart contracts can do. Blockchains have a set block size which limits how many transactions can be done in a single block to help keep the blockchain stable and allow blocks to regularly be generated so new transactions can be processed. As such smart contract code is limited, the more block space you take up the higher the ‘gas’ fee to submit the transaction, because with every transaction you are bidding against other people to be included so you need to beat a lot of people to get a large transaction included! To get around this you can submit a smaller transaction to move it off-chain (such as using centralised servers to process it) where it isn’t limited by the blockchains capabilities – however this requires you to trust an external source in correctly processing your request and then resubmitting it back to the original blockchain for confirmation. 

    Just because smart contracts are immutable, doesn’t mean they can’t be controlled. Recently ‘Tornado Cash’, a privacy service used to hide the source of crypto by mixing it with other tokens, was blacklisted by the US treasury. This means that any address which has received tokens passed through Tornado Cash are not able to interact with any US based services, such as on/off ramp exchanges. They black listed Tornado Cash due to reports that North Korean hacking groups had been using it to obfuscate the source of their stolen funds. Users can still use the mixing service as the smart contract continues to exists on the blockchain, however will be unable to use any third party service which is compliant with US OFAC (Office of Foreign Assets Control) rules.

    Challenger smart contract blockchains

    We’ve discussed Ethereum a lot in this article because it is the most successful blockchain to integrate smart contracts, however it isn’t the only one to try its hand. After Ethereum’s success with its EVM many other challenger blockchains spun up offering more developer friendly languages which require less learning vs Ethereum’s solidity.

    One of the most prominent competitors is Solana, which boasts the ability to code in Rust, C or C++. This allows a much easier transition into smart contract coding which they think will attract more developers into their ecosystem over Ethereum’s.

    So far, this choice alongside the ability for the Solana blockchain to consistently process transactions at a faster speed with lower cost means it has become the main choice for Web3 game developers to launch on. 

    Cardano (ADA) also decided on a different choice, using a language called ‘Haskell’. Haskell is definitely not a well known language but was chosen due to it emphasising ‘pure’ functions (functions that always give the same result for the same input). The creator of Cardano says this means that it gives a larger degree of certainty that implemented code is correct. However, like Ethereum, it also means the barrier for entry into building on the blockchain is higher as most coders will need to learn the language first.

    Polkadot, the blockchain which has the second highest number of active developers, also uses Solidity. The creator of DOT was also a co-founder of Ethereum and inventor of the Solidity language. It’s likely that developers who build on Ethereum also develop on DOT as it runs on the same language so skills are transferrable between the 2 blockchains.


    While most of these competitors have been around for multiple years, they still pale in comparison to the number of developers releasing on the Ethereum chain.

    Learning to make smart contracts

    If you’re looking to get into learning to code blockchain smart contracts there are plenty of resources and a lot of demand for developers, so its a great time to get involved. For starters, Ethereum with its EVM (Ethereum Virtual Machine) is the best to learn on as it has the most resources and support available. Once you learn Solidity you can then reasonably easily transfer your knowledge to other chains such as Solana, BSC, Polygon and more (mostly because they run the same EVM as Ethereum!). 

    CryptoZombies offers a great starter kit to begin learning about how smart contracts work, in particular with NFT creation. The site runs you through a number of different lessons so that you can build and deploy your own CryptoZombies smart contract. From there you can then use other resources like the official solidity documentation, SpeedRun Ethereum and the Ethereum website .

    Investing in smart contracts

    So you might be asking yourself how can I invest in smart contracts and this new technology? Well there are many ways to do so, primarily most investors tend to buy the blockchains token (such as ETH, SOL, Matic etc.) however this isn’t the only way. You can also support certain creators of protocols who produce smart contracts, by doing so you help fund their work and provide them with the opportunity to expand their projects. There are a number of stocks that are also either heavily into the blockchain/smart contract sector or have plans to begin offering services within the space. 

    By simply using smart contract compatible blockchains you’re helping to grow the ecosystem, so utilising new products which you believe have good potential is also a way that users invest into smart contracts. There is no lack in projects which utilise smart contracts either, from simple projects offering DeFi services all the way to ones selling houses.

    Interacting with smart contracts directly

    Smart contracts are at the core of blockchains like Ethereum, and for the most part projects tend to set up an interface which you can use to interact with the smart contract. This means that you can simply click a button on a website to use a smart contract, making it a very simple process.

    However sometimes you’ll need to interact more directly with smart contracts, such as if the website you are using has gone offline. While the website may be down the smart contract won’t be and you’ll still be able to interact with it.

    On the Ethereum blockchain you may do this by going to Etherscan and searching for the smart contract address. If you’re unable to find it that way you can also search for your own wallet and then find the smart contract through your past transactions. An example 

    Once on the smart contracts page (such as this one for Uniswap’s UNI token) you can click on ‘Contract’ and then ‘Write contract’. This will give you a list of options showing how you can interact with the contract. From there, just click the top right ‘connect to Web3’ – connect your wallet and then use the function that you want to. For the UNI smart contract it even details what the function does.

    Final thoughts

    In conclusion, smart contract compatible blockchains are here to stay. They offer huge potential in many areas to excel advancement, from transparent financial operations to buying a house. The current use cases of smart contracts is likely just a fraction of what will be available in the future.

    Next Article