Coder Social home page Coder Social logo

gmixoulis / smart-contract-security Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haripandey21/smart-contract-security

0.0 0.0 0.0 455 KB

A collection of resources, best practices, and tools for improving the security of smart contracts.

JavaScript 27.57% Solidity 72.43%

smart-contract-security's Introduction

Smart Contract Security

A collection of resources, best practices, and tools for improving the security of smart contracts.

Tools i'm using to analyze any vulnerabilities :-

  • slither
  • mythril

Remember these factors for securing your contract.

  • Never never use tx.origin(vulnerable for reentrancy attack) so, prefer msg.sender over tx.origin
  • Avoid such time manipulating things :
  1. function which is manipulating the timestamp or,
  2. any input field for timestamp (in epoch) in etherscan, as you can manipulate the time by making it zero or any time by over/under flowing the time. So,if you wanna be safe from it..consider using Safemath from Openzeppelin.
  • if there is any function which is sending amount like this : payable(msg.sender).transfer(balanceOf[msg.sender]); such functions are vulnerable to Reentrancy attack, as we can call that function again & again, before the contract has finished executing a previous call. So, Use Modifier nonReentrant of ReentrancyGuard.sol from Openzeppelin.

Gas Optimization

  • Payable functions costs less gas fee than non-payable. (as Non-payable function need to revert transaction if anyone sedn ether to it,so that extra checking costs more gas fee.)

  • uint8 a=9 is more expensive than uint256 a=9. (in (256-8)bits it puts zero so,it consumes more gas fee rather than just allocating space like in uint256)

  • Do apply Variable packings Don't do this : uint128 a; uint256 b; uint128 c; Do this : uint128 a; uint128 b; uint256 c;

  • Utilize uncheck if you are confident of avoiding over/underflow. like in tokenids,counters,etc

  • keccak256 is cheapest has function.

  • prefer bytes32 over string/bytes.

  • use of global variable constants storage costs : 20000 gas constant(stored in bytecode) costs : 200 gas only /-

  • avoid updating storage variable in every loop. Declare local variable,use it & after all computation update state variable to local variable.

  • don't initialize variables, if they have default values Don't do this : uint256 counter=0; bool status= false;

  • Declare values explicitly if you know it's value. Don't use Solidity to derive that data

  • Use bool for existence of value,variable,addreesses than running loops in modifiers.

  • Avoid repetative check while using safemath No need to do this : require(balance>=amount) as balance=balance.sub(amount) also check that required condition.

  • use single line swap, (a,b)=(b,a)

  • bool is a uint8,its using 8 bits but it only needs 1 bit (as it only stores either true or false).so wastage of 7 bits.

  • struct & array datas always starts with a new slot. (not mapping does)

  • in Solidity,maximum size of contract is restricted to 24 KB.

  • use of internal calls

Here are some best practices for writing secure smart contracts:

  • Avoid using inline assembly in your code as much as possible, as it can be difficult to understand and audit.
  • Use libraries or packages with well-tested code instead of writing your own code for common functionality.
  • Test your smart contracts thoroughly and extensively, including edge cases and unexpected scenarios.
  • Regularly review and update your smart contracts to address any security vulnerabilities that are discovered.

Checklist

Here is a checklist for ensuring the security of your smart contracts:

  • Verify that all the inputs and outputs of the smart contract are properly validated.
  • Verify that the smart contract has sufficient access controls to prevent unauthorized access.
  • Verify that the smart contract does not have any infinite loops or reentrancy vulnerabilities.
  • Verify that the smart contract does not have any integer overflow or underflow vulnerabilities.
  • Verify that the smart contract does not have any vulnerabilities related to time manipulation.

smart-contract-security's People

Contributors

haripandey21 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.