solidity基础-合约调用

A合约调用B合约

合约B

contract B {

  uint public x;
  uint public value;

  function setX(uint _x) public returns (uint){
    x = _x;   
    return x;
  }

  function setXandSendEther(uint _x) public payable returns(uint, uint256){
    x = _x;
    value = msg.value;
    return(x, value);
  }
}

合约A

contract A{

  uint public num;
  uint public value;

  function callSetX(B _b, uint256 _x) public {
    uint256 x = _b.setX(_x);
  }

  function callSetXAddr(address _addr, uint256 _x) public {
    B b = B(_addr);
    b.setX(_x);
  }

  function callSetXSendEther(address _addr, uint256 _x)public payable{
    B b = B(_addr);
    (uint256 x, uint256 value) = b.setXandSendEther{value: msg.value}(_x);
  }

}

部署合约,先调用合约A,输入B 的合约地址和 值,

 并且输入token的值,再运行

 合约执行成功后,看合约B, 可以看到

 

posted @ 2022-06-08 23:29  apeNote  阅读(383)  评论(0编辑  收藏  举报