小编Nic*_* M.的帖子

Solidity - 为什么公共 Struct 变量的默认 getter 不返回 Struct 内的每个变量

我目前正在学习 Solidity 语言,我注意到当我尝试在 JS 代码中获取 Struct 的值时,Solidity 返回每个没有数组的变量。我必须创建自定义 getter 来访问结构内的所有数据。

我制作了一个非常简单的合约示例,其中的 Struct 在构造函数内初始化。

我正在使用自定义 getter 访问该变量,并在 JS 代码中生成一个变量。

测试溶液

pragma solidity ^0.8.4;

contract Test {

    struct Data {
        string foo;
        address[] bar;
        address ctrt;
    }

    Data public d;

    constructor() {
        d.foo = "HELLO WORLD";
        d.bar.push(msg.sender);
        d.ctrt = address(this);
    }

    function getD() public view returns (Data memory) {
        return d;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试.js

const {ethers} = require('hardhat');

describe('Test', function () {
  it('should test something', async function() {
    const factory = await …
Run Code Online (Sandbox Code Playgroud)

ethereum solidity ethers.js

7
推荐指数
1
解决办法
3733
查看次数

标签 统计

ethereum ×1

ethers.js ×1

solidity ×1