坚固性将复杂对象保存在合同中



我有一个用例,我需要在我的合约中保留以下示例数据。

{
Linkage : {"4" : "1", "77" : "59", "5" : "64", "4" : "464", "455" : "364", "25" : "364", "25" : "164", "55" : "8684", "85" : "864"},
UserId : "Some Id",
}

字典显然是可扩展的(根和链接(。 我想发送数据并将其作为对象检索(c# 和 Java 样式(。因此,当我从 WEB3 进行通信时,我可以传递 json。 这可能吗?

这就是我卡住的地方...

pragma solidity ^0.4.13;
contract Test{         
struct UserProfile{       
string UserId;
}   
UserProfile public Profile;
function setProfile(UserProfile newProfile) public {
Profile  = newProfile;
}        
}

启动事务时(尚不能传递对象(。您只能通过internal函数调用传入/返回struct(请参阅 Solidity 常见问题解答(。

您必须使用基元类型传入数据并将它们添加到内部struct

pragma solidity ^0.4.13;
contract Test { 
struct UserProfile {
string userId;
mapping(uint256 => uint256) linkage;
}   
UserProfile public profile;
function addLinkage(uint256 id, uint256 value) public {
profile.linkage[id] = value;
}
}

请注意,如果要批量传入链接,可以使用addLinkage(uint256[] ids, uint256[] values).

您可以将对象序列化为字符串,solidity 没有对象原语,并且由于以太坊 vm 的性质,可能永远不会。

相关内容

最新更新