如何将结构从合约 A 传递到合约 B?最佳实践 [英] How to pass struct from contract A to contract B? Best practice

查看:46
本文介绍了如何将结构从合约 A 传递到合约 B?最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了这样一种方式,当创建一个带结构的通用接口,然后契约 A 和 B 继承带结构的接口.

I found such way, when one general interface with structure is created and then contract A and B inherit the interface with structure.

但我想知道是否还有其他方法?

But I'm wondering if there are other ways?

是否存在可以更新具有结构的合同的情况?

And could there be a case where a contract with a structure can be updated?

pragma experimental ABIEncoderV2;
pragma solidity ^0.6.0;
 
interface params {
     struct  structTest {
        uint256 data;
    }
}

contract contractA is params{
    function testCall(structTest calldata _structParams) public pure returns (uint256){
        return _structParams.data;
    }
}

contract contractB is params{
    contractA aContractInstance;
    
    constructor (address _a) public {
        aContractInstance = contractA(_a);
    }
    
    function test(structTest calldata _structParams) public view returns(uint256){
        // call contract A from B and pass structure
        return aContractInstance.testCall(_structParams);
    }
}

推荐答案

interface IContractA {

    struct User {
        address addr;
    }
    function getUser(aaddress addr) external view returns (User memory user);
}

contract contractB{

    function getUserFromContractA(address addr) public view
        returns (IContractA.User memory user)
    {
      ContractA = IContractA(addrContractA);
      user = ContractA.getUser(addr);
    }
}

这篇关于如何将结构从合约 A 传递到合约 B?最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆