CLV Documentations
Portal
Portal
  • Welcome to Clover
  • Useful Links
  • CLV Token
  • Quick Start
    • Clover Networks
    • Sakura Networks
    • Using Local Node
    • Using Testnet
      • Create an account
      • Faucet
      • Run a Testnet Node
      • Connect to Testnet
  • Development Guide
    • Introduction
      • Prerequisites
      • Setup environment
    • Using MetaMask
    • Using Remix
    • Using Web3.js
      • Query Balance
      • Send Transaction
    • Counter Tutorial
      • Setup dapp project
      • Setup truffle
      • The Counter Contract
      • Deploy Contract
      • Counter Webapp
  • Clover Wallets & Dapps
    • Clover Extension Wallet
      • Getting Started
      • Switch Networks
      • Add Tokens
      • Send Tokens
      • Cross-Chain Transfer
      • View Seed Phrase
      • Import Account
      • dApp Integration
      • Substrate dApp Integration
      • Solana-dApp-Integration
      • dApp Interaction Protocol
      • Wallet Integration QA
    • Clover Mobile Wallet
    • Clover Web Wallet
      • dApp Integration
    • Clover Assets Explorer
    • Clover Cross-Chain Explorer
  • Maintain
    • Running a validator on Clover Network
    • Running a RPC node
    • Staking on Clover
      • Staking via Apps
      • Staking via Clover Wallet
  • Technical Documentation
    • Web3 compatibility
      • eth_protocolVersion
      • eth_syncing
      • eth_hashrate
      • eth_coinbase
      • eth_mining
      • eth_chainId
      • eth_gasPrice
      • eth_accounts
      • eth_blockNumber
      • eth_getBalance
      • eth_getStorageAt
      • eth_getBlock
      • eth_getTransactionCount
      • eth_getBlockTransactionCount
      • eth_getBlockUncleCount
      • eth_getCode
      • eth_sendTransaction
      • eth_sendSignedTransaction
      • eth_call
      • eth_estimateGas
      • eth_getTransaction
      • eth_getTransactionByBlockHashAndIndex
      • eth_getTransactionByBlockNumberAndIndex
      • eth_getTransactionReceipt
      • eth_getUncle
      • eth_getLogs
      • eth_getWork
      • eth_submitWork
      • eth_submitHashrate
      • eth_subscribe
      • eth_unsubscribe
      • net_version
      • net_peerCount
      • net_listening
      • web3_clientVersion
      • web3_sha3
    • Clover Test Cases
    • Clover EVM
    • Clover Accounts Binding
    • Query Balance
    • Transaction Finality
  • Clover Eco Incentive Program
    • Introduction
    • Clover Developer Incentive Program
    • Virtual Ethereum address binding
    • Clover User Incentive Program
  • Parachain Auction
    • About Polkadot Parachain Auction
Powered by GitBook
On this page
  • The Code
  • Explain
  • Test out
  1. Development Guide
  2. Counter Tutorial

The Counter Contract

PreviousSetup truffleNextDeploy Contract

Last updated 4 years ago

The Code

Smart contracts are put at the contracts folder. Create the contract folder using:

mkdir contracts

Placing smart contracts in the contracts folder is a convention of truffle. You can specify a different directory by modifying truffle configuration. Checkout the section in truffles document.

Once created the contracts folder , create the Counter.sol file with contents below:

Counter.sol
pragma solidity >=0.5.0 <0.7.0;

contract Counter {
  uint32 public current_value;

  function inc() public {
    require(current_value < 10000, "Counter: max value");
    current_value = current_value + 1;
  }

  function dec() public {
    require(current_value > 0, "Counter: min value");
    current_value = current_value - 1;
  }
}

The contract code is quite self explain:

  • A state variable current_value which is an unsigned integer.

  • inc() method, which increase the current_value by one.

  • dec() method, which decrease the current_value by one.

  • current_value has a bound of [0, 10000] which was checked in inc and dec methods.

Run command truffle compile, it will find and compiles the Counter contract. you should looks outputs like:

Compiling your contracts...
===========================
> Compiling ./contracts/Counter.sol
> Artifacts written to ~/counter-dapp/build/contracts
> Compiled successfully using:
   - solc: 0.5.2+commit.1df8f40c.Emscripten.clang

Truffle command will download solidity compiler at the first time. There could be some messages related to the compiler setup. It's normal.

Explain

Test out

🌟
🚗
📝
conracts_directory