前言
Solidity是以太坊智能合约的编程语言,我们可以通过web3.js来与合约进行通信,并接收Solidity函数的返回值。不少人在接收struct类型的返回值时不知道怎么处理,本文展示一种解析方法,以供各位学习交流,如有更好的方法,欢迎讨论。
Solidity返回的struct首先看solidity代码,截取了关键部分,定义了一个结构体以及一个id到结构体的映射,该映射为public,意味着会自动生成getter函数。
struct Status{
string studentId; // 学籍号
string schoolId;// 学校
string majorId;// 专业
uint8 length;// 学制
uint8 eduType;// 学历类别
uint8 eduForm;// 学习形式
uint8 level;// 层次(专/本/硕/博)
uint8 state;// 学籍状态
uint16 class;// 班级
uint32 admissionDate;// 入学日期
uint32 departureDate;// 离校日期
}
mapping (uint32=>Status) public idToUndergraduate;// id到本科学籍的映射
js调用合约
this.StudentFactory = TruffleContract(StudentFactoryABI);
if (typeof web3 !== 'undefined') {
console.warn('Using web3 detected from external source.');
window.web3 = new Web3(window.web3.currentProvider)
} else {
$('body').html('没有检测到MetaMask插件,请安装MetaMask后刷新页面重试');
}
this.StudentFactory.setProvider(window.web3.currentProvider);
let studentFactory;
StudentFactory.at(address).then((instance) => {
studentFactory = instance;
return studentFactory.idToUndergraduate.call(id);
}).then((result) => {
console.log(result);// 打印结果
}).catch((e) => {
console.warn(e);
})
通过promise获取到result,将result输出到控制台显示。结果如下:
可以看到,整体上struct变成了一个数组,但是只有struct摆在前面的string类型数据是正常显示,而后面的uint都变成了形如**r {s: 1, e: 0, c: Array(1)}**的东西。
具体代码如下,通过map()和回调函数解析数据:
let studentFactory;
StudentFactory.at(address).then((instance) => {
studentFactory = instance;
return studentFactory.idToUndergraduate.call(id);
}).then((result) => {
result.map((v, i) => { // 遍历
console.log(v.valueOf()); // 打印结果
});
}).catch((e) => {
console.warn(e);
})
打印效果: