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. EVM contracts

Calling EVM contracts from C++

How an EVM contract calls a C++ contract in AppLayer.

To invoke EVM contract functions from C++, we leverage a templated approach that mimics the contract's functions in a C++ class. This method provides a type-safe way to interact with contracts written in Solidity or other EVM-compatible languages.

First, we define a proxy C++ class that represents the EVM contract. This class will include stubs of the contract's functions, which do not contain actual logic but serve to match the contract's interface in the blockchain:

class SolMyContract {
  public:
    uint256_t myFunction(const uint256_t& arg1, const uint256_t& arg2) const {};
    static void registerContract() {
      ContractReflectionInterface::registerContractMethods<SolMyContract>(
        std::vector<std::string>{},  // List of dependencies or related artifacts if any
        std::make_tuple("myFunction", &SolMyContract::myFunction, FunctionTypes::View, std::vector<std::string>{"arg1", "arg2"})
      );
    }
};

Then, we ensure that the proxy class is registered within the blockchain before any calls are made. Typically, this registration is done once, often in the constructor of the calling C++ contract, to set up the reflection system used for method invocation:

uint256_t AnotherContract::callMyFunction(const Address& targetAddr, const uint256_t& arg1, const uint256_t& arg2) const {
  SolMyContract::registerContract();  // Ensure the EVM contract's methods are registered (Can be done only a single time in the constructor)
  return this->callContractViewFunction<SolMyContract>(this, targetAddr, &SolMyContract::myFunction, arg1, arg2);
}
PreviousExecuting contract calls via EVMCNextCalling C++ contracts from EVM

Last updated 1 year ago