【Smart Contract】Smart Contract Development Guide

Table of contents

foreword

From the underlying blockchain infrastructure to the smart contract ecology of the application layer built on top of it, the Web3 field has developed quite fast. Based on smart contracts, we can build various dApps (decentralized applications),
such as DeFi, GameFi, NFT, GameFi and even SocialFi applications that have emerged in the past two years. Each of the listed dApps shines in their respective fields, creating a booming and prosperous scene in the entire Web3 field.

When you are familiar with the development of smart contracts, no matter what kind of dApp you want to develop (listed above), you already have 50% of the basic advantages, and the remaining 50% needs to be familiar with the business knowledge of the corresponding dApp field, as follows Let's take a look at the learning route of smart contract development.

It should be noted that this article focuses on practicality, and will not introduce too much history of related tools. Readers can inquire about this aspect by themselves.

1. Select the smart contract language

Contract programming language is the most basic part of smart contract developers, and you should master at least one to write smart contracts. The contract language is usually not a traditional programming language (such as C/C++, Python, etc.), in order to ensure that the contract code can get a definite execution result on any blockchain node, it must be required that the contract cannot access the outside world (such as
HTTP Connect or manipulate files), early blockchain developers designed programming languages ​​specifically for writing contracts, such as Solidity, Vyper, and later
some new contract languages ​​such as Move or Rust (but with some usage restrictions) . The good news is that these languages ​​have borrowed heavily from the syntax of traditional programming languages, such as Solidity from JavaScript and Vyper from Python, which makes the learning curve of becoming a contract developer a bit flatter.

1.1 Solidity

First of all, an option that does not require too much thinking is Solidity, because 90% of smart contracts are currently written in Solidity. Solidity is the earliest and most popular object-oriented high-level static language. It was originally designed for Ethereum EVM in 2014. Later,
many blockchain platforms compatible with Ethereum EVM (such as Ethereum, Avalanche, Moonbeam, Polygon , BSC), so now Solidity can also run on other EVM-compatible blockchains. Nine of the top ten Defi projects
currently use Solidity as their main programming language.

Solidity has been developed for almost ten years, and there are already a large number of excellent development tools available in the ecosystem, including third-party libraries and IDEs (described later). In addition, the more native language than Solidity that runs on the EVM is the assembly language Yul. When you advance Solidity, you will learn about applications in which Solidity and Yul interact inline to improve performance.

It should be noted that Solidity has some defects in syntax design. Of course, it has been continuously improved over the years, and its ease of use and security have been greatly improved.

For the study of Solidity, I strongly recommend the books Smart Contract Technology and Development listed on the homepage of this warehouse , and the author's code notes
for the book are also stored in this warehouse .

1.2 Vyper

Vyper is another contract language that is compatible with EVM (compilable to EVM bytecode like Solidity) and pays more attention to safety than Solidity. It is very similar to Python's syntax, but compared to Python, many unnecessary Features (such as class inheritance, function overloading, operator overloading, etc.),
reducing features can simplify the language and reduce the chance of errors.

Additionally, Vyper is designed to make it as difficult as possible for anyone to write misleading code. Simplicity for readers (i.e. reviewers) is more important than simplicity for writers (i.e. developers). This way, it will be easier to identify malicious code in smart contracts or decentralized applications (dApps).

It is important to note that Vyper is not a full replacement for Solidity, but a language to use when the highest level of security is required. Examples of projects written in Vyper include Uniswap v1, Curve.fi, and the first ETH 2.0 deposit contract.

1.3 Move

Created in 2019, Move is a contract language that is more difficult to master than Solidity and Vyper. It was rewritten based on Rust and was originally developed for Meta's Diem blockchain project. After the Diem project was disbanded, its founding team Aptos
and Sui, which were founded separately , also use Move as the core programming language.

The main features of Move are asset-oriented programming (resources are first-class citizens), security (inheriting many security features of Rust), and modularity (modules can be iterated).

Relatively speaking, the Move language is still very young and lacks large-scale engineering verification, and its development chain is not yet perfect, and the contract specification has not yet been formed, so it is recommended to only learn about it for interest.

1.4 Rust

Originally designed and released by Mozilla employee Graydon Hoare in 2006, Rust is a language designed for performance and security, especially safe concurrency, and is syntactically similar to C++. Rust
was not designed for smart contracts from the beginning, but exists as a traditional high-level language that strives for high security. Because its advantages in security are very suitable for the application scenarios of smart contracts, people choose to directly It is introduced into the field of blockchain.

At present, Rust has been widely used in various fields of blockchain, such as blockchain infrastructure construction (Layer 1), contract programming (Layer 2), etc. At present, Polkadot, Solana, and Near use the Rust language as the core development language.

It should be noted that Rust's syntax is notoriously complex, and its learning curve is steep enough that its learning difficulty is often daunting. However, the 2024 update roadmap of the Rust language announced by the Rust language design team (Lang Team) on the official blog shows that reducing the difficulty of learning is the future development
direction of the Rust language. Rust is closer to high-performance languages ​​such as C/C++ in terms of language design, so developers familiar with C/C++ will have a slight advantage.

2. Deploy and test the framework

2.1 Overview

This section introduces some framework tools to assist in deploying and testing contract code. After continuous development in this field, various contract frameworks or tools are available today.

2.2 Remix

The first is Remix, which is not a framework itself but an IDE mainly based on browsers (also supports desktops), which can provide online smart contract compilation, testing and deployment functions based on Ethereum . Because it is a browser-based tool, it does not Care about the operating system, directly out of the box.
The code written in the Remix browser version will be saved in the browser cache, so accidentally clearing the cache will cause your workspace (workspace) to be emptied. This is a disadvantage, but Remix later also supports connecting to the local computer. work area.

Remix is ​​the earliest Solidity development tool, and almost all contract developers start to learn from Remix. But when developers integrate more complex logic in contracts (larger contract projects), they need to choose a framework with a higher degree of automation to develop, test and deploy contracts.

2.3 Truffle

Truffle is the earliest framework for writing Ethereum contracts, created by Consensys in 2016, and it is written based on JavaScript. The official introduction to it is: a framework for building, testing and deploying Ethereum network applications.
The entire framework can be regarded as a suite consisting of three tools: Truffle (development and testing environment), Ganache (rapid deployment of local EVM blockchain via desktop version or command line) and Drizzle (rich front-end UI library for building dApps).

Truffle has the greatest historical influence among all frameworks. You can see their impact on the industry. Many frameworks have adopted Truffle's practices. You'll see most smart contract engineer jobs require mastery of this framework.

2.4 Today

Hardhat is a JavaScript-based Ethereum contract development environment developed by Nomic Labs, which can help users build, test and deploy Ethereum network applications. Some key highlights of Hardhat include Solidity debugging, error messages for failed transactions, and explicit stack traces.
The built-in Hardhat network and CLI are also great additions to Hardhat's functionality. The Hardhat Network is a development-focused native Ethereum network, while the CLI (command line) is dedicated to flexible interaction with Hardhat's core functionality.

Although Truffle was created earlier, Hardhat is gradually encroaching on the market of Truffle, because Hardhat has better flexibility and lower cost of use in terms of test environment, error management and TypeScript integration.

The following are recommended resources:
Hardhat Official Getting Started Tutorial_English official document, continuously updated
Hardhat Official Getting Started Tutorial_Chinese translation translated in 2020, some information is outdated, but it does not affect getting started (it is mentioned in the deployment to online network part Use the ropsten test network, but this test network has been offline and needs to be changed to other test networks, you can check the English version for the latest steps)

Hardhat uses the official template, including a simple project example

2.5 Other frameworks

1. Brownie

In addition to the JavaScript-based frameworks introduced above, there is also Brownie , a Python-based contract development framework used by projects such as Curve.fi, yearn.finance, and Badger. It supports both solidity and vyper.
But the main highlight of this framework is that it is based on Python, and Brownie was born because many Python engineers despised working in JavaScript and wanted a framework for Python.
In addition, most traditional fintech fields use python instead of javascript, so with the creation of Brownie, it is also easier to move from the fintech field to Defi.

2. DappTools

This is an application built in Haskell. But don't worry, you don't need to know Haskell to use it. Primarily used by the MakerDAO team (the team behind DAI), DappTools is inspired by the Unix philosophy: "Write programs that do one thing, and do it well
. )”

DappTools is a tool focused on the command line, here, you can use the command line/shell tools you are already familiar with, such as bash or zsh, instead of python, javascript or other high-level programming languages ​​​​to help you develop. This effectively reduces one technique you have to be familiar with and forces you to get better at shell scripting!
It comes with a suite of tools such as dapp, seth, ethsign, and hevm, each of which is specifically designed as an essential part of a smart contract developer's daily work.

If you don't want to learn another language like JavaScript or Python, and want to use as few tools as possible in your setup, then give this framework a look.

3. Foundry

Foundry is a rewritten version of Paradigm's DappTools using Rust, so it is also a command-line-based toolkit. It mainly consists of three components:

  • Forge: Ethereum testing framework;
  • Cast: A swiss army knife for interacting with EVM smart contracts, sending transactions and fetching chain data;
  • Anvil: local blockchain node, similar to Ganache, Hardhat network.

With the blessing of Rust, the compilation performance of Foundry's contract code is much better than that of DappTools.

3. Front-end tools

Building dApp requires front-end tools. In addition to basic frameworks such as React and Vue, there are also some web3-specific third-party libraries for us to use quickly.

3.1 Getting Started

create-eth-app
is a scaffolding for rapid development of Ethereum applications based on CLI. It can help us initialize a project environment with a clear directory structure through a single command, and has multiple front-end frameworks (React/Vue) and project templates (default /Compound/aave/maker etc) optional.
It is a very suitable tool for beginners to use, allowing us to quickly understand the basic architecture knowledge of web3 app project development.

3.2 Web3.js/Ethers.js

Both are currently popular JS libraries for interacting with the Ethereum network. They can perform similar functions, such as connecting to the Layer1 network, querying wallet balances, monitoring network changes, etc. For some more specific introductions, Li Liubai is recommended The article: Library comparison: WEB3JS vs. ETHERSJS .

3.3 Scaffolding

UseDapp and Drizzle are React-based web3 project development scaffolding, which provide some basic plug-ins and hooks, which are also commonly used.

3.4 Moral

This platform not only provides SDKs in different programming languages ​​that interact with various blockchain networks, but also exists as a web3 infrastructure, that is, it provides Layer1 network node services of various public chains, IPFS node connections, and so on.

In addition, it also provides dApp layer APIs, such as NFT API, DeFi API, ENS analysis API, etc. Compared with Web3.js/Ethers.js, Moralis has richer, more comprehensive and free functions, and is recommended for use in projects.

Moral EVM API

4. wallet

The testing, deployment and online interaction of smart contracts are inseparable from the wallet. The wallet not only stores our account, but also an intermediate tool for connecting the account and the contract. We need to choose which wallet to use according to the actual situation.

4.1 Little Fox Wallet (MetaMask)

It is the most widely used Ethereum wallet in this field, and it is also a browser wallet, which is very convenient to test in the browser. It is a wallet type that every dApp developer must master.

But note that the Fox Wallet is a hot wallet, it will always be connected to the Internet, so it is more vulnerable to network attacks, attackers can steal your wallet private key through the network, you need to be very careful about its use, remember not to use it on a small Use a large fund account on Fox Wallet .

4.2 Multi-signature wallet

Fox Wallet is a "single-signature wallet". Transactions on the blockchain only require a single wallet signature, and there is a single point of failure of private key leakage . The multi-signature wallet is easy to understand. It requires multiple wallets (owned by multiple people) to provide signatures to initiate a transaction.
Compared with single-signature wallets, the security is much improved. Multi-signature wallets are usually used on accounts held by multiple people, such as a DAO or an organization.

How multi-sig wallets work and how to use them

4.3 Cold wallet

For example, Ledger and Trezor, the difference between cold wallets is that it is more troublesome to use. It usually uses a hardware device isolated from the network to keep the private key. When the wallet needs to be used, it is connected to the computer via USB/Bluetooth. Used, more often, to keep large sums of money.

5. Block explorer

The block browser is a website used to browse the transaction details of the mainnet/testnet blockchain ledger, and supports searching by attributes such as addresses, transaction hashes, and Tokens. In addition, it usually also provides the function of interacting with the contract (if the blockchain supports it).

6. Testnet and Faucets

Almost every public chain has its test network, on which developers can deploy contract projects at almost no cost for various tests. Deploying contracts on the testnet requires the corresponding native tokens. You can find the faucet website corresponding to the blockchain testnet by searching for " blockchain name + faucet " , and you can get free tokens for the testnet on the faucet website.

7. Web3 infrastructure

This refers to services such as blockchain node public gateways and decentralized storage (IPFS), which we need to use during deployment and development. Fortunately, there are already some companies in the market that provide these services for free.

  • Alchemy: a well-known blockchain node provider and API provider;
  • Infura: Provide Layer1 access and use of IPFS SDK;
  • Moralis: includes the above services, as well as a richer SDK, recommended;
  • QuickNode: support for richer blockchain network types;

reference

Guess you like

Origin blog.csdn.net/sc_lilei/article/details/129251942