0
点赞
收藏
分享

微信扫一扫

solidity 0.8.0 以上版本:冲提ht、bnb

Java架构领域 2022-09-09 阅读 112


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

contract ContractBalanceTest {
address public owner;

constructor() public payable {
owner = msg.sender;
}

modifier onlyOwner () {
require(msg.sender == owner, "This can only be called by the contract owner!");
_;
}

function deposit() payable public {
}

function depositAmount(uint256 amount) payable public {
require(msg.value == amount);
}

function withdraw() payable onlyOwner public {
payable(msg.sender).transfer(address(this).balance);
}

function withdrawAmount(uint256 amount) onlyOwner payable public {
require(msg.value == amount);
require(amount <= getBalance());
payable(msg.sender).transfer(amount); //this not work
//msg.sender.transfer(getBalance()); // this ok
}

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

 

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

contract ContractBalanceTest {
address public owner;

constructor() public payable {
owner = msg.sender;
}

modifier onlyOwner () {
require(msg.sender == owner, "This can only be called by the contract owner!");
_;
}

function deposit() payable public {
}

function depositAmount(uint256 amount) payable public {
require(msg.value == amount);
}

function withdraw(uint256 amount) onlyOwner public {
//payable(msg.sender).transfer(address(this).balance);
(bool success, ) = msg.sender.call{value: amount}(new bytes(0));
}

function withdrawAmount(uint256 amount) payable public {
//require(msg.value == amount);
require(amount <= getBalance());
//payable(msg.sender).transfer(amount); //this not work
(bool success, ) = msg.sender.call{value: amount}(new bytes(0));
//msg.sender.transfer(getBalance()); // this ok
}

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

举报

相关推荐

0 条评论