pleasemarkdarkly / solidity-examples

Examples of various contracts using Solidity ^0.8.0

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Solidity Template

Solidity Hardhat Typescript Waffle Graphic

Includes:

  • Hardhat: compile and run the smart contracts on a local development network
  • TypeChain: generate TypeScript types for smart contracts
  • Ethers: renowned Ethereum library and wallet implementation
  • Waffle: tooling for writing comprehensive smart contract tests
  • Solhint: linter
  • Prettier Plugin Solidity: code formatter

This is a GitHub template, which means you can reuse it as many times as you want. You can do that by clicking the "Use this template" button at the top of the page.

Usage

Prerequisites

Set up your .env with the respective values:

cp -v .env.example .env

Alchemy requires the API URL and ETH Private Key, Infura requires the API KEY and Mnemonic

Before running any command, make sure to install dependencies:

yarn

Prettier

yarn lint:prettier

Linting

If you want to re-initialize Solhint's configuration file with all the default rules enabled:

yarn && solhint --init

Or replace the existing file with:

{
  "extends": "solhint:default"
}

Lint all the files inside the contracts directory:

solhint 'contracts/**/*.sol'

Or use the included:

yarn lint:sol

Or both Prettier and Solhint:

yarn lint

Compile

Compile the smart contracts with Hardhat:

yarn compile

TypeChain

Compile the smart contracts and generate TypeChain artifacts:

yarn build

Test

Run the Mocha tests:

yarn test

Deploy contract to network

(requires Mnemonic and Infura API key)

npx hardhat run --network rinkeby ./scripts/deploy.ts

Or Deploy contract to Alchemy network

(requires Alchemy API URL and Ethereum private key) The API URL is for the Ropsten network.

npx hardhat run scripts/deploy.ts --network alchemy

Validate a contract with Etherscan

(requires Etherscan API key)

npx hardhat verify --network <network> <DEPLOYED_CONTRACT_ADDRESS> "Constructor argument 1"

For example:

npx hardhat verify --network ropsten 0x4ed4DDd7981e347b673f697DC821965A3EB64b9c

Returns:

Creating Typechain artifacts in directory typechain for target ethers-v5
Successfully generated Typechain artifacts!
Compiling 1 file with 0.6.12
Successfully submitted source code for contract
contracts/TestToken.sol:TestToken at 0x4ed4DDd7981e347b673f697DC821965A3EB64b9c
for verification on Etherscan. Waiting for verification result...

Successfully verified contract TestToken on Etherscan.
https://ropsten.etherscan.io/address/0x4ed4DDd7981e347b673f697DC821965A3EB64b9c#code

0x4ed4DDd7981e347b673f697DC821965A3EB64b9c

Network

To verify your various test, hardhat, and Alchemy networks are configured properly, npx hardhat networks will return such details.

Network settings =>
Hardhat Runtime Environment =>
[
  'config',
  'hardhatArguments',
  'tasks',
  'run',
  'artifacts',
  'network',
  '_extenders',
  'ethers',
  'waffle',
  'upgrades'
]
Alchemy =>
{
  accounts: [
    '0xREDACTED_PRIVATE_KEY'
  ],
  gas: 'auto',
  gasPrice: 'auto',
  gasMultiplier: 1,
  httpHeaders: {},
  timeout: 20000,
  url: 'https://eth-ropsten.alchemyapi.io/v2/REDACTED_ALCHEMY_API_KEY'
}
Ropsten =>
{
  accounts: {
    initialIndex: 0,
    count: 10,
    path: "m/44'/60'/0'/0",
    mnemonic: 'REDACTED_MNEMONIC_WORDS'
  },
  gas: 'auto',
  gasPrice: 'auto',
  gasMultiplier: 1,
  httpHeaders: {},
  timeout: 20000,
  chainId: 3,
  url: 'https://ropsten.infura.io/v3/REDACTED_INFURA_API_KEY'
}

Added plugins

Other

Contracts

There are several token examples. The first includes an ICO contract, the Interview Token and ChuckNorris Token are both derived from OpenZeppelin. The ChuckNorris Token includes a deployment script where the Token description is updated with Chuck Norris jokes fetched from an API.

Prettier

npx prettier --write 'contracts/**/*.sol'

Solhint

To disable all validations in the line following a comment:

 // solhint-disable-next-line
 bytes32 public constant MINTER_PANTS = keccak256("MINTER_PANTS");

Current line:

bytes32 public constant MINTER_PANTS = keccak256("MINTER_PANTS"); // solhint-disable-line

Block of code:

 /* solhint-disable */
  contract Forwarder { 
    address public destinationAddress;

    function Forwarder() public {
      destinationAddress = msg.sender;
    }

    function() payable public {
          destinationAddress.transfer(msg.value);
    }

    function flush() public {
      destinationAddress.transfer(this.balance);
    }
  }
  /* solhint-enable */

Additional Solidity Resources

Branding

Attribution

1The Sale contract was updated tp ^0.8.0 from HunterLong's ^0.4.21 Solidity contract.

2The project template was hacked from PaulRBerg's template.

About

Examples of various contracts using Solidity ^0.8.0

License:MIT License


Languages

Language:TypeScript 68.3%Language:Solidity 29.8%Language:JavaScript 1.1%Language:Shell 0.8%