To reduce the amount of gas used, we will create a withdraw function that lets the owner of the contract withdraw funds that have been added when they want rather than withdraw with every transaction.
Your smart contract is ready to be used, since you have confidence in the code after testing it.
But there are a few things that you can add to improve the behavior and security of it.
Let's start by adding events.
Similar to any other programming language Events in solidity are a way to inform the calling application about the current state of the contract. Like notifying the javascript client about a change made to the contract allowing the client application to react to it.
Events are defined within the contract as a global and are called from inside a function. To declare an event you use the event
keyword, followed by an identifier and the parameter list. The values passed to the parameters are then used to log the information that is saved as part of the transaction inside the block.
To send the event you'll use the emit
keyword from anywhere inside the contract by passing the name of the event and the corresponding parameters.
For this contract you want to let the client application to know when a tip was successfully sent. To do that you'll create a new event named NewTip
like this
event NewTip(address indexed from, string message, string name, uint256 amount);
It will accept an address to identify who sent the tip, the message of the tip and also the name and the amount of eth sent,
And then, it will be emitted from the sendTip
function
// send the event
emit NewTip(msg.sender, _message, _name, msg.value);
This event will use the same parameters that the rest of the sendTip
operation.
msg.sender
to get the address of who sent the tip_message
the string with the message sent_name
the name of the sendermsg.value
the amount of eth
sent.