第30节:传递结构体

小白入门:https://github.com/dukedaily/solidity-expert ,欢迎star转发,文末加V入群。

职场进阶: https://dukeweb3.com

如何正确传值:

案例一:

https://ethereum.stackexchange.com/questions/72435/how-to-pass-struct-params-in-remix-ide

pragma solidity ^0.7;
pragma experimental ABIEncoderV2;

contract TestStruct {

      struct User {
        string name;
        uint256 age;
     }

    mapping (bytes32 => User) users;

    function addUsers (User [] memory _users) public {

        for (uint i = 0; i < _users.length; i++) {

           bytes32 hash = keccak256(abi.encode(_users[i].name));
           users[hash] = _users[i];

        }
    }

    function getUser (string memory username) public view returns (User memory) {

        bytes32 hash = keccak256(abi.encode(username));

        return users[hash];
    }
}

输入参数:

[["Duke", 20], ["Linda", 21]]

测试成功!

案例二:

// SPDX-License-Identifier: MIT
pragma solidity =0.8.10;
pragma abicoder v2;

contract StructTest {
    event Info(uint8 _type, address _addr);

    struct Token {
        uint8 tokenType;
        address tokenAddress;
    }

    function call(Token [] memory tokens) external {
        for (uint8 i = 0; i< tokens.length; i++) {
            (uint8 t, address addr) = (tokens[i].tokenType, tokens[i].tokenAddress);
            emit Info(t, addr);
        }
    }
}

输入参数:

[[0,0x5B38Da6a701c568545dCfcB03FcB875f56beddC4],[1,0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2]]

但是一直失败!

原因:地址需要使用双引号包裹传递,正确参数:

[[0,"0x5B38Da6a701c568545dCfcB03FcB875f56beddC4"],[1,"0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2"]]

当结构体中有数组

正确做法:

  1. 先使用storage创建空的数组对象
  2. 然后拼装对象,push到数组中

错误做法:

  1. 先new一个数组,拼装数据
  2. 然后赋值到结构体中