Web3 smart contracts store more student data in arrays

In the previous article, Web3 wrote its first solidity smart contract in the Truffle project and took everyone to write a relatively leaky smart contract
insert image description here
, so that every time we set it, the original data will be overwritten.

Then some people may think that we can make an array and add all the new data to the array, isn't it all right?
This idea is actually very good. We can transform our smart contracts based on this idea.

We change the entire StudentStorage.sol code as follows

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

contract StudentStorage{
    
    
    //struct是一个结构体
    struct userData {
    
    
        uint id;
        string name;
        uint age;
    }
    //基于结构体创建一个动态数组
    userData[] public userList;
    
    function addtList(string memory name,uint age) public returns (uint) {
    
    
        uint cont = userList.length;
        uint index = cont+1;
        userList.push(userData(index,name,age));
        return userList.length;
    }

    function queryList() public view returns (userData[] memory) {
    
    
        return userList;
    }
}

We first defined a struct structure, which is an object object, which has three fields of numeric type id, string type name, and numeric type age.
Then we defined an array userList whose type is limited to userData,
which is every time you add it. A subscript must be an object of id name age in this format.
Here we define that it is a dynamic array without limiting its length.
You can do the same

下标类型[10] public 数组名称;

In this way, it becomes a static data set fixed at ten pieces of data

Then we addtList receives two parameters: name of string type and age of number type,
and then we still have one less id
, so here we do an auto-increment logic to get the length of our userList through the array.length and then add one to it as index Pass it to userData
so that our three parameters are complete, call push to insert the data into the end of the array,
after processing, we return the length of the array
, and then our queryList simply returns the processed userList array

Then we can try it out and execute it on the terminal first.

truffle migrate

Deploy the contract we wrote to the blockchain

Then we create a folder called scripts in the project root directory and create a test.js
reference content as follows

const Contacts = artifacts.require("StudentStorage.sol")

module.exports = async function(callback) {
    
    
    const studentStorage = await Contacts.deployed();
    await studentStorage.addtList("天山派掌门",11);
    await studentStorage.addtList("小猫猫",15);
    await studentStorage.addtList("大猫猫",25);
    const list = await studentStorage.queryList();
    console.log(list);
    callback()
}

We imported StudentStorage
here without specifying contracts,
it will automatically find this file in contracts for you,
then we Contacts.deployed to initialize the object of this contract,
then we called addtList through this object, added three pieces of data in a row
, and then called queryList to get this array
and output it go up

Then we execute the terminal

truffle exec .\scripts\test.js

Test code
insert image description here
Obviously our operation is successful and the data has been added

And then our most magical operation here
we run again

truffle exec .\scripts\test.js

insert image description here
He becomes six because you run the first time he adds three and the second run he adds three more

Because our blockchain itself also has storage capacity, of course, every time our smart contract executes an operation, including adding data, it will consume fuel.

Then we add a line of code to test.js under scripts in the root directory

console.log(await studentStorage.userList(1))

insert image description here
Find the data whose id is 1
Execute again

truffle exec .\scripts\test.js

The result is output
insert image description here

Guess you like

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