solidity 中调用外部合约如何拿到结构体

solidity 0.8.0版本现已允许函数直接返回结构体,所以尝试在其他合约中调用

被调合约:

// SPDX-License-Identifier: MIT

pragma solidity 0.8.0;

contract Deployed {
    Nft[] public a;

    struct Nft {
        uint256 a;
        uint256 b;
    }

    function setA(uint256 v) public {
        a.push(Nft({a: v, b: v}));
    }

    function ge(uint256 _index) public view returns (Nft memory) {
        return a[_index];
    }
}

调用合约:

// SPDX-License-Identifier: MIT

pragma solidity 0.8.0;

//Deployed合约的接口
interface Deployed {
    struct Nft {
        uint256 a;
        uint256 b;
    }

    function setA(uint256) external;

    function a(uint256) external view returns (Nft memory);

    function ge(uint256 _index) external view returns (Nft memory);
}

contract Existing {
    Deployed dc;
    struct Nft {
        uint256 a;
        uint256 b;
    }

    constructor(address _address) {
        dc = Deployed(_address);
    }

    function getA(uint256 _index) public view returns (Nft memory) {
        Nft memory c = dc.a(_index);
        return c;
    }

    function setA(uint256 _v) public {
        dc.setA(_v);
    }
}

调用合约中getA方法报错:Return argument type struct Deployed.Nft memory is not implicitly convertible to expected type (type of first return variable) struct Existing.Nft memory.

如何才能在调用合约中拿到被调合约返回的结构体

请先 登录 后评论

最佳答案 2021-06-26 08:29

类型不匹配:一个是 Existing.Nft 一个Deployed.Nft

你把

struct Nft {
        uint256 a;
        uint256 b;
    }

放到合约或接口的外面 试试。

请先 登录 后评论

其它 1 个回答

Harlan
请先 登录 后评论
  • 2 关注
  • 0 收藏,3422 浏览
  • ANYFC 提出于 2021-06-18 18:36

相似问题