4

用户给A合约转账继续转账给B合约问题

 2 years ago
source link: https://learnblockchain.cn/question/2554
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
用户给A合约转账继续转账给B合约问题 | 登链社区 | 技术问答

用户给A合约转账继续转账给B合约问题

大概逻辑是:用户调用函数转账给A合约,函数内继续把余额转账给B合约。
A合约代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract test2{
	uint public value;
	address public seller;
	address public test1Adder;

	constructor(address _add) public {
		test1Adder = _add;
	}

	function tr() public payable {
		seller = msg.sender;
		value = msg.value;
		payable(test1Adder).transfer(value);
	}

	// 查看合约内金额
	function getBalance() public view returns(uint) {
		return payable(address(this)).balance;
	}

}

B合约代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract test1{
    
    // 查看合约内金额
    function getBalance() public view returns(uint) {
         return payable(address(this)).balance;
    }

}

执行tr方法结果:

image.png

如果把B合约的地址换成用户地址是可以转成功的,求大哥调教-.-

663 11小时前

合约要接受没有calldata的以太币转账时,需要在合约里加上receive() external payable{}或者fallback() external payable,所以在B合约里最后加上应该就行了

您需要登录后才可以回答问题,登录 或者 注册

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK