I have such function to transfer tokens from contract and i get error where shoudl i add payable and i dont really understand if withdraw will withdraw ethers or tokens?
function withdrawBalances() public nonReentrant {
uint share = _Balances[msg.sender];
_Balances[msg.sender] = 0;
msg.sender.transfer(share);
}
.withdrawBalances 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.
This error occurs when you try to send some Ether when calling a function that isn't payable. Set Ether value to 0 to avoid this. You can also allow a function to accept Ether by using the payable keyword.
function withdrawBalances() public payable {
...
}
Related
I'm new to Solidity and i have this function to send value from the user account to the contract. It increments the balance of the user and that info is stored in a mapping.
function sendMoney() public payable {
payable(address(this)).transfer(msg.value);
balance_received[msg.sender] += msg.value;
}
It compiles and i can deploy but when calling the function i get this message:
"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."
I have funds in the user account and i state in the function that it's payable. What am I missing?
Thanks
I have a contract defined in solidity and I want to make it so that when a specific function is called, the overall cost of the contract increases by 1 ether. I am a little fuzzy on how to use ether in practice. Would I just use a normal int for this? Where does the keyword ether come into play?
As you probably know, 1 ether == 1000000000000000000 (or 10^18) wei.
You can access the transaction value in a global variable msg.value that returns amount of wei sent with the transaction.
So you can make a simple validation that checks whether the transaction calling your function has a value of 1 ETH.
function myFunc() external payable {
require(msg.value == 1 ether, 'Need to send 1 ETH');
}
It's the same as comparing to 10^18 wei
function myFunc() external payable {
require(msg.value == 1000000000000000000, 'Need to send 1 ETH');
}
function myFunc() external payable {
require(msg.value == 1e18, 'Need to send 1 ETH');
}
There's also a short paragraph on the Solidity docs that shows more examples: https://docs.soliditylang.org/en/v0.8.2/units-and-global-variables.html#ether-units
I get the following error every time I try to deploy a program with a wei value greater than zero:
Code:
pragma solidity ^0.5.1;
contract testContract {
uint value;
constructor (uint _p) public {
value = _p;
}
function setP(uint _n) payable public {
value = _n;
}
function setNP(uint _n) public {
value = _n;
}
function get () view public returns (uint) {
return value;
}
}
Output:
creation of testContract 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.
creation of testContract pending...
0 [vm] from: OxAb8...35cb2 to: testContract.(constructor) value: 1 wei data: 0x608...00001 logs: 0 hash: 0x344...07156
creation of testContract 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.
Perhaps you need to specify a "payable" specifier in the smart contract constructor - like for "setP" function.
This happens because you are sending Wei with the contract deployment transaction which is not required.
The transaction can get reverted because of the following reasons:
Transaction done is not for a payable function
The amount you are sending is less than you current balance
I try to transfer Ether from the contract to an address but it gives the error that the transaction is out of gas. I think it's a small problem but I can't find it. I have to specifically use solidity version 0.4.24.
The warning from Remix
The error from MetaMask
I have tried different methods, like:
address.transfer(amount);
address.send(amount);
address.call.value(amount)( );
All methods will give the same out of gas exception. and the send and call method will also give a warning that it's outdated and that I should use the transfer method.
I also tried to adjust the gas and it didn't work, I also tried the needed 2,300 for the transfer listed on the docs.
The code:
pragma solidity ^0.4.24;
contract TestContract {
function payAddress(address _address) external payable {
_address.transfer(msg.value);
}
}
If the problem is that the contract doesn't have any Ether to transfer, can it use the Ether I send with the function call? Or is the problem something else?
Thank you for reading.
edit:
I have tried to send Ether to my Contract and that works, I do have Ether on my contract now, but the function still gives the same error as before. So the problem is something else.
Current code:
pragma solidity ^0.4.24;
contract TestContract {
function() external payable { }
function payContract() public payable {}
function paySomeone(address _address, uint256 _amount) external {
_address.transfer(_amount);
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
The balance of the contract
The parameters I use
Same MetaMask error as before
As you can see here the balance of the contract is 10 wei, but when i try to send 9 wei it still gives the same out of gas error. I also still get the same error from Remix as before.
I also post the issue on the Stack exchange and got an answer there. The issue was my
Ganache version. I switched to the Robsten test network and it worked. I'll link the post here.
Yes. In order to send ether from contract to another address, first you must send some ether to the contract address. Take a look at this and this.
This is my code in Solidity
pragma solidity ^0.4.17;
contract WithdrawalContract {
mapping(address => uint) buyers;
function buy()public payable {
require(msg.value > 0);
buyers[msg.sender] = msg.value;
}
function withdraw()public {
uint amount = buyers[msg.sender];
require(amount > 0);
buyers[msg.sender] = 0;
require(msg.sender.send(amount));
}
}
Below is the error i am getting. Not sure why it is asking for constructor
transact to WithdrawalContract.buy errored: VM error: revert.
revert The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value. Debug the transaction to get more information.
That's just part of the error message Remix shows when a transaction reverts, presumably because that's a common mistake people make.
In your case, that's not the issue. My guess would be that you're calling buy and not attaching any ether, so the require(msg.value > 0) is causing the transaction to revert. In Remix, make sure you have a non-zero "value" in the upper right.