Nic*_* M. 7 ethereum solidity ethers.js
我目前正在学习 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 ethers.getContractFactory('Test')
const test = await factory.deploy();
console.log("Result from var:");
console.log(await test.d());
console.log("Result from getter:");
console.log(await test.getD());
})
});
Run Code Online (Sandbox Code Playgroud)
控制台结果:
Result from var:
[
'HELLO WORLD',
'0x5FbDB2315678afecb367f032d93F642f64180aa3',
foo: 'HELLO WORLD',
ctrt: '0x5FbDB2315678afecb367f032d93F642f64180aa3'
]
Result from getter:
[
'HELLO WORLD',
[ '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' ],
'0x5FbDB2315678afecb367f032d93F642f64180aa3',
foo: 'HELLO WORLD',
bar: [ '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266' ],
ctrt: '0x5FbDB2315678afecb367f032d93F642f64180aa3'
]
Run Code Online (Sandbox Code Playgroud)
如果数据的某些部分不可见,那么明确地说变量是公共的有什么意义呢?