Catatan Seekor Solidity

Solidity adalah bahasa pemrograman berorientasi objek yang dirancang untuk mengimplementasikan smart contract pada blockchain Ethereum.

Fundamental

Contract Structure

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyContract {
    // State variables
    uint public myNumber;
    string public myString;
    
    // Constructor
    constructor(uint _number, string memory _string) {
        myNumber = _number;
        myString = _string;
    }
    
    // Functions
    function setNumber(uint _number) public {
        myNumber = _number;
    }
    
    function getNumber() public view returns (uint) {
        return myNumber;
    }
}

Data Types

Function Modifiers

Events

Smart Contract Examples

Simple Token Contract

Best Practices

Security Considerations

  • Always validate inputs

  • Use SafeMath library (though not needed in Solidity 0.8+)

  • Implement access control

  • Be careful with external calls

  • Use reentrancy guards

Gas Optimization

  • Use appropriate data types

  • Pack structs efficiently

  • Minimize storage operations

  • Use events instead of storage for logs

Function Visibility

  • Use external for functions that are only called externally

  • Use public for functions that need to be called both externally and internally

  • Use internal for functions that are only called within the contract

  • Use private for functions that are only called within the current contract

References

Stack Exchange

Articles

Tools

Development Environment

  • Remix IDE (browser-based)

  • Hardhat (development framework)

  • Truffle (development framework)

  • Foundry (testing framework)

Testing

  • Hardhat Test

  • Truffle Test

  • Foundry Test

  • OpenZeppelin Test Helpers

Deployment

  • Hardhat Deploy

  • OpenZeppelin Defender

  • Etherscan API

  • Infura/Alchemy for RPC

Last updated