其他分享
首页 > 其他分享> > solidity基础-recieve和send

solidity基础-recieve和send

作者:互联网

recieve 接收token函数

pragma solidity >=0.7.0 <0.9.0;

contract ReceiveTest {

    receive() external payable{
    }

    fallback() external payable{       
    }

    function getBalance() public view returns(uint256){
        return address(this).balance;
    }
    
}

  

send 发送token函数

pragma solidity >=0.7.0 <0.9.0;
contract SendTest{

    function transfer(address payable _to) public payable{
        _to.transfer(msg.value);
    }

    function send(address payable _to) public payable{
       bool sent = _to.send(msg.value);
       require(sent, "Failed to send eth.");
    }

    function call(address payable _to) public payable{
        (bool sent, bytes memory data) = _to.call{value: msg.value}("test");
        require(sent, "Failed to send eth.");
    }
}

 

部署合约

先填写 token 数量

 

调用 “发送合约”, 复制粘贴地址

    

调用“接收合约”, 点击 getBalance, 可以看到获取到 3 个 token。 

    

 

但是, 一般使用 call, 其他两个 send 和 transfer 有安全问题。

 

标签:solidity,0.7,recieve,token,send,pragma
来源: https://www.cnblogs.com/apenote/p/16348324.html