Skip to the content.

← Back to Homepage

Security : This doc helps readers know about security vulnerabilities in smart contracts and also developer practices in the end. This doc by no means covers all the vulnerabilities as smart contract security is an evergoing learning path.

1. Ether Balance

Never try to calculate the balance of the contract using manual counters. A vulnerable approach is that everytime the contract receives ether, based on the functions implemented, people update state variables as a counter/balance received. This is wrong as coinbase transactions and selfdestruct destination transactions can Force Ether in!. They do not trigger any functions and don’t care if you have payable functions or fallback functions. The ether balance increases. Check out GridLock’s Bug.

Solution :

Always use balance(this) to get the balance of contract. This also is NOT good. See below.

2. Outliers :

EVM defines 0**0 as 1.

Because of wrapping, assert(x == -x) becomes true when x = type(int).min as positive overflows and becomes int.MIN again.

Right and left shift results are capped at the type extremes.

Division by zero and Modulo by zero can’t be suppresed with even unchecked{} as they are panic errors.

Calling a function after using delete on it causes a panic error.

3. Overflow/Underflow

Prior to 0.8, solidity used to underflow/overflow without any errors.

After v0.8, an error is thrown. Use something like SafeMath library if Solidity version is below v0.8.

4. Selfdestruct :

A contract can self destruct itself and force it’s ether into a victim contract. This makes the victim not aware of balance increase as no function, not even fallback/receive is called for a self destruct. So using address(this).balance might break when someone does the selfdestruct attack.

Contrast this with above, we have 2 choices.

5. The blockchain is transparent :

6. Randomness:

7.require is also treated as a function :

8. Gas DoS

9. DAG (C3) Abuse :

10. Dynamic array/mapping backdoor :

11. Writing critical code yourself :

12. Replay Attacks :

13. Timestamps :

14. msg.sender vs tx.origin:

15. Use the withdrawal pattern over forwarding:

16. Untrusted Delegatecall :

17. Storage pointers left uninitialized :

18. Reentrancy

19. Loose access to selfdestruct()

20. Always check return values :

21. Expect the worst and have circuit breakers:

A few things to consider when developing smart contracts :

  1. Testing is more important. Using Remix to quickly prototype or just build something for fun is good. In fact Remix is awesome to learn solidity syntax.
  2. But, when you want to build something serious for even for your portfolio or create a Dapp/contract for contributing to a community, testing is important.
  3. Always use industry standard libraries like openzeppelin. And still write tests, their library is tested but your extension code is not.
  4. Learn Waffle testing. Hardhat users, have a look at the hardhat-waffle library here.
  5. Use the solidity-coverage plugin. Good tests cover most of the code and cases. Use this plugin.
  6. Run slither and check if your code has any known vulns. Check out slither here. Check out Mythril from consensys here for similar functionality with also traces of transactions.
  7. If you want even more confidence, use a graphing package like this or this. They help show you how the control flow is happening in your contract.
  8. When you’re sure that testing is done and coverage % is good, test on local fork. Hardhat can fork current mainnet state. And now simulate if everything works good.
  9. Then deploy to testnet. Developers using block.<property> and anything block related should decide which testnet to deploy as many vary from correctly replicating but unstable testnets to fast blocks but not so close testnets.
  10. Most importantly, always be in the game. Sign up for Week in Ethereum News or immunefi or anything that helps keep you updated. You’ll mostly come across any new developements from them. And in the best case, you find out something yourself . Also follow @samczsun on twitter and read his blog as he explains many vulns and what he did.