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 @   apeNote  阅读(391)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示