addr.transfer(1 ether) 的可靠性错误
solidity error with addr.transfer(1 ether)
错误信息:
transact to Payment.deposit errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
contract Payment{
address Account2;
address Owner;
constructor() public{
Account2 = 0x583031D1113aD414F02576BD6afaBfb302140225;
Owner = msg.sender;
}
function deposit() payable public{
address(uint160(Account2)).transfer(1 ether);
}
它在我的混音 VM 上正常运行。
所以请确保:
合约余额大于1以太币
如果Account2
是一个合约地址,那么它需要有一个fallback函数来接收ether
如果您不想使用msg.value,那么您的合约必须有足够的余额。
因此,您可以创建一个回退函数,以便先将足够的以太币存入您的合约,然后您可以调用 deposit() 函数。
添加回退功能后的合约
pragma solidity ^0.5.1;
contract Payment{
address Account2;
address Owner;
constructor() public{
Account2 = 0x583031D1113aD414F02576BD6afaBfb302140225;
Owner = msg.sender;
}
function () payable external{}
function deposit() payable public{
address(uint160(Account2)).transfer(1 ether);
}
function getContractBalance() public view returns(uint) {
return address(this).balance;
}
}
错误信息:
transact to Payment.deposit errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
contract Payment{
address Account2;
address Owner;
constructor() public{
Account2 = 0x583031D1113aD414F02576BD6afaBfb302140225;
Owner = msg.sender;
}
function deposit() payable public{
address(uint160(Account2)).transfer(1 ether);
}
它在我的混音 VM 上正常运行。
所以请确保:
合约余额大于1以太币
如果
Account2
是一个合约地址,那么它需要有一个fallback函数来接收ether
如果您不想使用msg.value,那么您的合约必须有足够的余额。 因此,您可以创建一个回退函数,以便先将足够的以太币存入您的合约,然后您可以调用 deposit() 函数。
添加回退功能后的合约
pragma solidity ^0.5.1;
contract Payment{
address Account2;
address Owner;
constructor() public{
Account2 = 0x583031D1113aD414F02576BD6afaBfb302140225;
Owner = msg.sender;
}
function () payable external{}
function deposit() payable public{
address(uint160(Account2)).transfer(1 ether);
}
function getContractBalance() public view returns(uint) {
return address(this).balance;
}
}