The contract should try to send its funds but it will need to have some to begin with.
You will have to send 1000000000 wei, or more when you deploy the deploy the contract. Any surplus will remain in the contract.
I can't think of any good reason to do this so I'll refrain from suggesting improvements.
Hope it helps.
UPDATED
WARN: I don't think anyone will be thrilled about wading through all that code perchance to work out why you would want to trade to tokens for ETH and then set fire to the ETH received. NO WARRANTY.
if(gweiToBurn>0){
address(0).transfer(gweiToBurn*conversion); // convert to wei
// address(0).transfer(gweiToBurn.mul(conversion)); // using SafeMath
}
For reference:
pragma solidity 0.5.2;
contract BurnEth {
function burnEth() public payable {
address(0).transfer(msg.value);
}
}
UPDATE 2
Instead, what I need is a solution that does not require anyone to set the msg.value. Setting the msg.value must be done within another contract
This is quite confused, so let's review the ground rules.
- A contract cannot send funds it doesn't have.
- A contract cannot reach out and spend anyone else's funds.
A contract can hold funds that belong to other people, for example an Escrow arrangement. In such a case, the contract has 1 balance which is the pool of all customer deposits and it is, therefore, responsible for accounting. We might have a balance in a contract with an arrangement like this:
mapping(address => uint) public userBalances;
That way the contract can track its liabilities. From the EVM's perspective, that is the contract's money, not the users.
I'm not sure which situation you are describing.
Instead, what I need is a solution that does not require anyone to set the msg.value
For the record, no one can set msg.value
. It is a property of a transaction and it is not writable. Either the transaction arrived with money, or it arrived with no money.
You don't want to use msg.value
so I will proceed on the basis that money is not coming with the transactions. It must, therefore, be money that is already in the contract. There is no other possibility.
This example is a very idiomatic representation of a withdraw pattern - a contract that holds other people's money.
- Users can deposit funds.
- Use different user accounts to deposit and check their balances.
- Users can withdrawn funds up to their account balance. They can't withdraw more than they put in.
- A very dangerous
burnSome()
function takes funds from a user account and destroys it. The user to rob and the amount to destroy are arbitrary, but the user has to have the money, or it can't be burnt.
It's a good practice to emit an event for each state change and it might help with debugging your testing methodology.
pragma solidity 0.5.2;
contract WeHaveMoneyToBurn {
mapping(address => uint) public userBalances;
event LogDeposit(address depositor, uint amount);
event LogWithdrawl(address withdrawer, uint amount);
event LogFundsDestroyed(address victim, uint amount);
// users can increase their balance by depositing funds
function deposit() public payable {
userBalances[msg.sender] += msg.value;
emit LogDeposit(msg.sender, msg.value);
}
// users can retrieve funds that were not burned
function withdraw(uint amount) public {
require(amount <= userBalances[msg.sender], "Insufficient Funds");
userBalances[msg.sender] -= amount;
emit LogWithdrawl(msg.sender, amount);
msg.sender.transfer(amount);
}
// anyone can burn funds from a user account
function burnSome(address burnFrom, uint amount) public {
require(amount <= userBalances[burnFrom], "User does have that much to burn");
userBalances[burnFrom] -= amount;
emit LogFundsDestroyed(burnFrom, amount);
address(0).transfer(amount);
}
}
BurnEth
contract get created as a new instance when OtherContract is deployed? Or does theBurnEth
contract already exist on the Blockchain (and has its own address)?