Web3 writes its first solidity smart contract in the Truffle project

Well, the above Web3 builds the truffle smart contract development environment locally. We set up a Truffle project locally
, and then we explain the purpose of the files in the project.

Then we still use ganache to simulate a local blockchain environment
insert image description here
and then open the Truffle project we built.
First of all, the first thing we need to look at is truffle-config.js. We
must first configure the connection environment. Here we directly connect to the local

We open it and find networks under module.exports.
There is a development under it.
insert image description here
Remove the comment of this content and release
insert image description here
it. Obviously, the above information 127.0.0.1 ip 8545 port points to a local service started by our ganache

So in the future, as soon as we deploy, we will publish it to our local blockchain

Then we will find some more advanced configurations below.
insert image description hereHere, the gas can be configured to limit the amount of business we process each time.
insert image description here
Then it is the following from. As we said before deploying smart contracts to the chain, there will also be absenteeism. Then which account will deduct the gas? Its role
If you don’t set it, it will read the first account in your environment by default,
insert image description here
and then it also gives us a remote address.
insert image description here
Here, we are only doing a development environment, so let’s ignore it
and then we have another one here. Optimizing the configuration
insert image description here
We just release it directly to this location.
insert image description here
Then we import the first account provided by ganache in MetaMask.
We should pay attention to the changes of his ETH.
insert image description here
After the configuration file is written, it will come to our core function and create it under the contracts directory. A file called StudentStorage.sol
Let's write a simple function first.
We write two interfaces here. One can store a student's name and age, and the other interface reads the information we stored
, and then our first sentence of solidity is directly Copy
the reference code as follows

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

First of all, if your VS Code does not have solidity tools installed, it will be black and white.
insert image description here
We can find solidity management tools in this plug-in library,
insert image description here
so that our code is full of colors.
insert image description here
Then let’s take a look at the meaning of this code.
The first sentence It means what kind of protocol our entire solidity development follows, is
it the GPL or the MIT protocol
? For example, when we upload code to open source tools, we still need to write a protocol,
because smart contracts are open and transparent, so we have to make it clear that others can do it directly. It can be used after others have changed it.
insert image description here
The second sentence is limited to the version of the solidity editor,
insert image description here
because the solidity language itself is also growing. For example, if you run the code of vue3 on IE, it will disappear immediately,
so it is equivalent to a version. Compatible with

Solidity combines JavaScript, python, and C language, but it is generally closer to js or similar to TypeScript,
so it can’t be done directly. It is relatively friendly to js users, but there must still be a lot to learn.

Then our StudentStorage.sol code is written as follows

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract StudentStorage{
    
    
    // 创建两个变量
    //用户年龄
    uint userAge;
    //用户名
    string userName;
    
    function setData(uint age,string memory name) public {
    
    
        userAge = age;
        userName = name;
    }
    /*
      view:视图函数  只访问数据 不修改数据
      pure:既不访问数据也不修改数据
    */
    function getData() public view returns (uint,string memory) {
    
    
        return (userAge,userName);
    }
}

I won’t say much about the first two lines, and the global name of the contract package must be the same as the name of our file. For example, I call it StudentStorage here, and
we define two variables userAge and userName. You can check the variable type of user name and user name yourself or You remember
the uint number
string string string
,
and then we defined a function setData,
where our name parameter involves a keyword memory, which is mainly the storage location of our value. First, the basic type, such as our uint number type, does not need to declare the
specific The explanation is as shown in the figure below.
insert image description here
First of all, we need to know that if we want to put it on the chain, it will definitely consume fuel.

Then there is a public at the back, which is mainly to declare the scope of the method. Many back-end data management languages ​​​​will involve this concept,
insert image description here
and our getData involves two concepts. The role of view has been commented above, and you can reduce it by declaring it. Fuel consumption
and returns are the same as many back-end languages. We declare a method to declare its return type clearly.
Here we declare to return two, a uint number type and a string memory string type,
corresponding to our userAge userName

Some people may wonder what is the use of pure.
First, maybe for example, we write a function parameter to pass XY and then calculate the business trip. Then our function is just a calculation variable. All the variables are given by the outside and returned to the outside. This is neither modified nor accessed. Our internal data is naturally very suitable for pure, which reduces unnecessary fuel consumption

All our operations in the smart contract will consume gas, so we still need to write more standardized

Then we run the project terminal as an administrator
, then we can execute it in the project terminal after we finish writing

truffle compile

Our project environment is up, and
insert image description here
there will be an obvious extra directory here.
insert image description here
Well, at least now the project is running. If we test it, we will continue below.

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/131520630