About Smart Contract

Nhan Cao
5 min readAug 22, 2021

If you are a smart contract developer, you will need to consider something about the contract:

  • Clean: No unused code, nonsense comment,
  • Well document: Comment to all functions
  • Formatted: Reformat, reformat, reformat
  • Unit test all functions
  • Migration for testnet and mainnet
  • Avoid duplication code, avoid using too many unnecessary modifiers to reduce contract size
  • Upgradable

*Reusable, Replaceable, Upgradable, Testable => Easier maintenance*

— — —

About some checklist in contract function

  • Consider using nonReentrant modifier in paying function to avoid duplicate calls
  • Use safe math to check about substract and div operators. Get revert if you div zero, or assign a negative number to uint.
  • About percent calculation should use the rate nominator approach. MUL first, DIV last
uint internal constant RATE_NOMINATOR = 10000;
uint tax = 100; // 1%
uint taxAmount = (amount * tax) / RATE_NOMINATOR;
  • Do not loop more than 500 in an array, use pagination for long array

— — —

About how to write code and unit tests

  • You need to clear the requirement first
  • Define test cases in your note

--

--