如何 send() 或 transfer() 从合约地址稳定地发送到账户地址(我的意思是从 address(this).balance 中扣除)
How to send() or transfer() from the contract address to an account address in solidity (I mean to deduct from address(this).balance)
部署合约后,它有一个地址,我们可以通过 address(this) 调用它。我的合同可以从一个地址接收,这意味着我可以发送()或转移()到这个合同,但是如果我想从这个实际合同转移到任何账户,我该怎么做?
示例代码:
function submitTransaction(address _to,uint _value,string memory _desc) public
onlyOwner
{
require(_value <= address(this).balance,"This wallet does not have enough balace to send");
if(!_to.send(_value)){
revert("doposit fail");
}
}
但是我怎样才能从 address(this).balance 中扣除呢?
如果没有充分的理由,请不要使用 safe
,请改用 transfer
。 transfer
会在失败的情况下抛出错误,而 safe
方法只会 return false
,这意味着代码的下一行仍将被执行,并且如果您不处理错误,交易将不会被还原。
function submitTransaction(address _to,uint _value,string memory _desc) public
onlyOwner
{
require(_value <= address(this).balance,"This wallet does not have enough balace to send");
_to.transfer(_value);
}
您可以看到这个智能合约代码示例:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Bank {
address owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "You're not the smart contract owner!");
_;
}
event Deposited(address from, uint amount);
function depositMoney() public payable {
emit Deposited(msg.sender, msg.value);
}
// Use transfer method to withdraw an amount of money and for updating automatically the balance
function withdrawMoney(address _to, uint _value) public onlyOwner {
payable(_to).transfer(_value);
}
// Getter smart contract Balance
function getSmartContractBalance() external view returns(uint) {
return address(this).balance;
}
}
ADVICE:在这种情况下,如果您只想使用传输或发送,我建议您使用 transfer()
方法而不是 send()
,因为它如果传输无效则抛出失败。我建议阅读此 thread。相反,为了避免重入攻击,您必须使用 call
方法来转移以太币。
(bool success, ) = addressToTransfer.call{value: address(this).balance}("");
require(success, "Transfer failed.");
部署合约后,它有一个地址,我们可以通过 address(this) 调用它。我的合同可以从一个地址接收,这意味着我可以发送()或转移()到这个合同,但是如果我想从这个实际合同转移到任何账户,我该怎么做?
示例代码:
function submitTransaction(address _to,uint _value,string memory _desc) public
onlyOwner
{
require(_value <= address(this).balance,"This wallet does not have enough balace to send");
if(!_to.send(_value)){
revert("doposit fail");
}
}
但是我怎样才能从 address(this).balance 中扣除呢?
如果没有充分的理由,请不要使用 safe
,请改用 transfer
。 transfer
会在失败的情况下抛出错误,而 safe
方法只会 return false
,这意味着代码的下一行仍将被执行,并且如果您不处理错误,交易将不会被还原。
function submitTransaction(address _to,uint _value,string memory _desc) public
onlyOwner
{
require(_value <= address(this).balance,"This wallet does not have enough balace to send");
_to.transfer(_value);
}
您可以看到这个智能合约代码示例:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Bank {
address owner;
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "You're not the smart contract owner!");
_;
}
event Deposited(address from, uint amount);
function depositMoney() public payable {
emit Deposited(msg.sender, msg.value);
}
// Use transfer method to withdraw an amount of money and for updating automatically the balance
function withdrawMoney(address _to, uint _value) public onlyOwner {
payable(_to).transfer(_value);
}
// Getter smart contract Balance
function getSmartContractBalance() external view returns(uint) {
return address(this).balance;
}
}
ADVICE:在这种情况下,如果您只想使用传输或发送,我建议您使用 transfer()
方法而不是 send()
,因为它如果传输无效则抛出失败。我建议阅读此 thread。相反,为了避免重入攻击,您必须使用 call
方法来转移以太币。
(bool success, ) = addressToTransfer.call{value: address(this).balance}("");
require(success, "Transfer failed.");