Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 11, 2022 06:39 pm GMT

11X ethereum truffle : Developing smart contracts

Setting up a Project

First contract

Compiling Solidity

Using OpenZeppelin Contracts

Importing OpenZeppelin Contracts

truffle Tutorials

Contact

Setting up a Project

The first step after creating a project is to install a development tool.

The most popular development framework for Ethereum is Hardhat, and we cover its most common use with ethers.js. The next most popular is Truffle which uses web3.js. Each has their strengths and it is useful to be comfortable using all of them.

In these guides we will show how to develop, test and deploy smart contracts using Truffle and Hardhat.

To get started with Truffle we will install it in our project directory.

$ npm install --save-dev truffle

Once installed, we can initialize Truffle. This will create an empty Truffle project in our project directory.

npx truffle initStarting init...================> Copying project files to /home/openzeppelin/learnInit successful, sweet!

First contract

We store our Solidity source files (.sol) in a contracts directory. This is equivalent to the src directory you may be familiar with from other languages.

We can now write our first simple smart contract, called Box: it will let people store a value that can be later retrieved.

We will save this file as contracts/Box.sol. Each .sol file should have the code for a single contract, and be named after it.

// contracts/Box.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;contract Box {    uint256 private _value;    // Emitted when the stored value changes    event ValueChanged(uint256 value);    // Stores a new value in the contract    function store(uint256 value) public {        _value = value;        emit ValueChanged(value);    }    // Reads the last stored value    function retrieve() public view returns (uint256) {        return _value;    }}

Compiling Solidity

The Ethereum Virtual Machine (EVM) cannot execute Solidity code directly: we first need to compile it into EVM bytecode.

Our Box.sol contract uses Solidity 0.8 so we need to first configure Truffle to use an appropriate solc version.

We specify a Solidity 0.8 solc version in our truffle-config.js.

// truffle-config.js  ...  // Configure your compilers  compilers: {    solc: {      version: "0.8.4",    // Fetch exact version from solc-bin (default: truffle's version)      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)      // settings: {          // See the solidity docs for advice about optimization and evmVersion      //  optimizer: {      //    enabled: false,      //    runs: 200      //  },      //  evmVersion: "byzantium"      // }    }  },  ...

Compiling can then be achieved by running a single compile command:

npx truffle compileCompiling your contracts...=========================== Fetching solc version list from solc-bin. Attempt #1 Downloading compiler. Attempt #1.> Compiling ./contracts/Box.sol> Compiling ./contracts/Migrations.sol> Artifacts written to /home/openzeppelin/learn/build/contracts> Compiled successfully using:   - solc: 0.8.4+commit.c7e474f2.Emscripten.clang

The compile command will automatically look for all contracts in the contracts directory, and compile them using the Solidity compiler using the configuration in truffle-config.js.

You will notice a build/contracts directory was created: it holds the compiled artifacts (bytecode and metadata), which are .json files. Its a good idea to add this directory to your .gitignore.

Using OpenZeppelin Contracts

Reusable modules and libraries are the cornerstone of great software. OpenZeppelin Contracts contains lots of useful building blocks for smart contracts to build on. And you can rest easy when building on them: theyve been the subject of multiple audits, with their security and correctness battle-tested.

Many of the contracts in the library are not standalone, that is, youre not expected to deploy them as-is. Instead, you will use them as a starting point to build your own contracts by adding features to them. Solidity provides multiple inheritance as a mechanism to achieve this: take a look at the Solidity documentation for more details.

For example, the Ownable contract marks the deployer account as the contracts owner, and provides a modifier called onlyOwner. When applied to a function, onlyOwner will cause all function calls that do not originate from the owner account to revert. Functions to transfer and renounce ownership are also available.

When used this way, inheritance becomes a powerful mechanism that allows for modularization, without forcing you to deploy and manage multiple contracts.

Importing OpenZeppelin Contracts

The latest published release of the OpenZeppelin Contracts library can be downloaded by running:

$ npm install @openzeppelin/contracts

You should always use the library from these published releases: copy-pasting library source code into your project is a dangerous practice that makes it very easy to introduce security vulnerabilities in your contracts.

To use one of the OpenZeppelin Contracts, import it by prefixing its path with @openzeppelin/contracts. For example, in order to replace our own Auth contract, we will import @openzeppelin/contracts/access/Ownable.sol to add access control to Box:

// contracts/Box.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;// Import Ownable from the OpenZeppelin Contracts libraryimport "@openzeppelin/contracts/access/Ownable.sol";// Make Box inherit from the Ownable contractcontract Box is Ownable {    uint256 private _value;    event ValueChanged(uint256 value);    // The onlyOwner modifier restricts who can call the store function    function store(uint256 value) public onlyOwner {        _value = value;        emit ValueChanged(value);    }    function retrieve() public view returns (uint256) {        return _value;    }}

The OpenZeppelin Contracts documentation is a great place to learn about developing secure smart contract systems. It features both guides and a detailed API reference: see for example the Access Control guide to know more about the Ownable contract used in the code sample above.

truffle Tutorials

CN Github truffle : github.com/565ee/truffle_CN

CN CSDN truffle : blog.csdn.net/wx468116118

EN Github truffle Tutorials : github.com/565ee/truffle_EN

Contact

Homepage : 565.ee

GitHub : github.com/565ee

Email : [email protected]

Facebook : facebook.com/565.ee

Twitter : twitter.com/565_eee

Telegram : t.me/ee_565


Original Link: https://dev.to/565ee/11x-ethereum-truffle-developing-smart-contracts-153m

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To