Skip to content

solidity-evm-knowledgebase/solidity-best-practices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 

Repository files navigation

solidity-best-practices

Contract Layout

  • version
  • imports
  • errors
  • interfaces, libraries, contracts
  • Type declarations
  • State variables
  • Events
  • Modifiers
  • Functions

Layout of Functions

  • constructor
  • receive function (if exists)
  • fallback function (if exists)
  • external functions
  • public functions
  • internal functions
  • private functions
  • getter functions (view & pure functions)

Function Layout

CEI: Checks, Effects, Interactions Pattern

Example:

    function pickWinner() external {
        // Checks
        if (block.timestamp - s_lastTimestamp < i_interval) {
            revert Raffle__RaffleNotOver();
        }
        
        // Effects (Internal Contract State)
        s_raffleState = RaffleState.CALCULATING;

        // Interactions (External Contract interactions)
        uint256 requestId = s_vrfCoordinator.requestRandomWords(
            VRFV2PlusClient.RandomWordsRequest({
                keyHash: i_keyHash,
                subId: i_subscriptionId,
                requestConfirmations: REQUEST_CONFIRMATIONS,
                callbackGasLimit: i_callbackGasLimit,
                numWords: NUM_WORDS,
                extraArgs: VRFV2PlusClient._argsToBytes(
                    // Set nativePayment to true to pay for VRF requests with Sepolia ETH instead of LINK
                    VRFV2PlusClient.ExtraArgsV1({nativePayment: false})
                )
            })
        );
    }

Layout of Function in Tests

  • Arrange
  • Act
  • Assert

Example:

    function testRaffleRevertsIfNotEnoughEthToEnter() public {
        // Arrange
        vm.prank(PLAYER);
        // Act / Assert
        vm.expectRevert(Raffle.Raffle__NotEnoughEthToEnterRaffle.selector);
        raffle.enterRaffle();
    }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published