Solidity tips and tricks

We end this chapter with some recommendations and best practices you can adopt while you write your smart contracts:

  • There is no size limit to the data sent along your transaction. You will be only limited by how much ether you have and the block gas limit.
  • Constants are not stored in contract storage. A constant state such as uint256 constant x = block.timestamp; means that contract will re-evaluate x every time it is used.
  • Remember that calls to external functions can fail, therefore always check the return value.
  • For transferring funds, it's preferable to use transfer() over send() as transfer() is equivalent to require(send()) and will throw if it doesn't execute successfully.
  • Be aware that the block's timestamp can be influenced a little bit by miners. It's safe to use block.timestamp if your contract function can tolerate a 30-second drift in time, since the tolerated NTP drift is 10 seconds, as defined in https://github.com/ethereum/go-ethereum/blob/master/p2p/discover/udp.go#L57. Don't be confused if you read in the yellow paper (the old version) that a tolerable drift interval of up to 900 seconds (15 minutes) is allowed. This is outdated information.
  • There's a limit in Solidity to how many variables you can define in a function (including parameters and return variables). The limit is 16 variables, otherwise you get the StackTooDeepException error.
  • Contracts can't trigger themselves but they need an external poke (for example, a contract can't automatically do something at specific time, like a Cron job, unless it receives an external signal).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset