美文网首页
Solidity中的 struct与mapping可访问性

Solidity中的 struct与mapping可访问性

作者: 已不再更新_转移到qiita | 来源:发表于2018-10-31 14:54 被阅读24次

    solidity contract

    contract Game {
    
      struct Land {  
        address user;  
        mapping(address=>uint) businessman;
      }
    
      Land[9] public lands; //九州
    
      function getBusinessman(uint landInx, address _user) public view returns(uint) {
        return lands[landInx].businessman[_user];
      }
    
    }
    

    Bulk access to lists/arrays/etc is painful in Solidity. You rarely see it in contracts. In your case, a possible solution is to provide a function to access one item, using its index, and to let the caller loops from 0 to id.

    访问批量数据在Solidity中是很痛苦的

    js script

    let addr = "0x583031d1113ad414f02576bd6afabfb302140225";
    let landInx=1;
    let land = await contract.methods.lands(landInx).call();
    land.user; // 可获取数据
    land.businessman[addr]; // 获取不到数据
    
    let res = await contract.methods.getBusinessman(landInx, addr).call();
    
    console.log(res); //可获取数据
    

    参考:

    https://stackoverflow.com/a/37643972
    https://github.com/ethereum/solidity/issues/40
    https://medium.com/coinmonks/solidity-tutorial-returning-structs-from-public-functions-e78e48efb378

    相关文章

      网友评论

          本文标题:Solidity中的 struct与mapping可访问性

          本文链接:https://www.haomeiwen.com/subject/rltuzftx.html