AppLayer
  • Welcome to AppLayer Docs
  • Introducing AppLayer
    • A Primer on Smart Contracts
    • The Problem With EVMs
    • What is AppLayer?
  • How AppLayer works
    • Validators
    • Sentinels
    • Application Chains
    • Bridging
      • AppLayer-to-AppLayer Data Bridging
      • AppLayer-to-AppLayer Token Bridging
      • AppLayer-to-External Bridging (Ethereum, Solana, etc.)
  • Understanding rdPoS
    • Blockchains overview
    • How rdPoS works
    • Validator implementations
    • Slashing
  • BDK implementation
    • The utils folder
    • The contract folder
    • The core folder
    • Transactions and Blocks
    • Database
    • Contract call handling
    • RLP (Recursive-Length Prefix)
    • P2P Overview
    • P2P Encoding
  • Understanding contracts
    • Solidity ABI
    • Internal and external contract calls
    • Setting up the development environment
    • Contract Tester
  • Precompiled contracts
    • Types of pre-compiled contracts
    • Dynamic and Protocol Contracts
    • SafeVariables and commit/revert logic
    • How to code a precompiled contract
    • Creating a Dynamic Contract (Simple)
      • Simple Contract Header
      • Simple Contract Source
      • Deploying and testing
    • Creating a Dynamic Contract (Advanced)
    • Creating a Protocol Contract (Advanced)
  • EVM contracts
    • State management and VM instance creation
    • Seamless C++/EVM integration
    • C++ to other contract calls
    • EVM to other contract calls
    • Executing contract calls via EVMC
    • Calling EVM contracts from C++
    • Calling C++ contracts from EVM
  • Getting started with AppLayer Testnet
  • Join our Community
  • Get in Touch
  • Glossary
Powered by GitBook
On this page
  1. Precompiled contracts

Creating a Dynamic Contract (Simple)

A walkthrough on the basics of a Dynamic Contract's structure.

Let's create a test contract that allows three variables - name, number and tuple - to be changed by the owner of the contract, as well as perform basic operations with structs/tuples and event emissions. We will call this contract SimpleContract (which just so happens to be already implemented by the project due to internal testing purposes, but you can do it yourself by hand - check the src/contract/templates/simplecontract.{h,cpp,sol} files for reference).

Creating the Files

First we need to create the contract's header (.h) and source (.cpp) files, as is customary in C++ development - the header will contain the definition of our contract class, and the source will contain the implementation details.

Go to your local testnet's root folder, into the src/contract/templates subfolder, and create two new files - simplecontract.h and simplecontract.cpp. Those files will contain the declaration and definition of your contract's logic, respectively.

Then, add both files to the CMakeLists.txt file in the same folder, so they can be compiled with the project:

set(CONTRACT_HEADERS
  # ...
  ${CMAKE_SOURCE_DIR}/src/contract/templates/simplecontract.h
  # ...
)
set(CONTRACT_SOURCES
  # ...
  ${CMAKE_SOURCE_DIR}/src/contract/templates/simplecontract.cpp
  # ...
)

In the following subchapters, we'll implement the header file first, then the source file, write some tests and finally deploy the contract alongside the blockchain.

PreviousHow to code a precompiled contractNextSimple Contract Header

Last updated 5 months ago