solidity合约访问其他合约的mapping

2020年8月23日20:48:20BCOS评论518阅读模式
摘要

solidity合约访问其他合约的mapping的方式

错误代码

被访问合约

contract BagUserAdmin {
    mapping(bytes32 => RecordBean) bagRecordOf;
}

访问合约

contract UserGiveLikes{

    BagUserAdmin internal bagUserAdmin;
	constructor(address BagUserAdminAddress){
        bagUserAdmin = BagUserAdmin(BagUserAdminAddress);
    }
	// 直接去访问该合约对象的mapping
	bagRecordBean = bagUserAdmin.bagRecordOf[TypeConvertUtil.stringToBytes32(_forBagid)];

}

正确引用:

contract BagUserAdmin is TableDefTools{

    mapping(bytes32 => RecordBean) bagRecordOf;
	function getBagValues(string _bagid) public view returns(string []){
        return bagRecordOf[TypeConvertUtil.stringToBytes32(_bagid)].values;
    }
}
contract UserGiveLikes{

    BagUserAdmin internal bagUserAdmin;
	constructor(address BagUserAdminAddress){
        bagUserAdmin = BagUserAdmin(BagUserAdminAddress);
    }
	// 通过getter方法去访问该合约对象的mapping
	bagRecordValues = bagUserAdmin.getBagValues(_forBagid);

}

BCOS最后更新:2020-11-6