Pundi
Pundi AIPundi X
  • 🤖Pundi AI overview
  • 🈁Pundi AI Data Platform
    • Data stakeholders
    • Data Labelling
    • Pundi AI Data Platform (Alpha Testnet) - User Guide
    • FAQ
  • 🦾Pundi AIFX Omnilayer
    • Pundi AIFX
    • Installation Pundi AIFX
    • Setup Node
      • Full node with Binaries
      • Full node with Docker
      • Snapshot Guide
      • Node Monitoring Device
    • Validators
      • Validator Overview
      • Setting Up a Validator for Pundi AIFX
      • Validator Recovery
      • Validator FAQ
      • Validator Security Notice
      • Migration Best Practices
      • Transfer Validator Permissions
    • Delegators
      • Delegators FAQ
      • Delegator CLI Guide
      • Delegator Security Notice
    • Pundi AIFX Tutorials
      • Pundi AIFX CLI Commands
      • Cloud Setup
      • Testnet faucet
      • Ledger Integration for fxcored
      • Sentry Nodes
      • Account Migration Guide (CLI)
    • Upgrade Instructions
      • Cosmovisor Upgrade Guide v8.5.x
        • Cosmovisor Integration - Binaries
        • Cosmovisor Integration - Docker
      • Manual Upgrade Guide v8.5.x
        • Binaries - Upgrading Your Node
        • Docker - Upgrading Your Node
      • Upgrade Versions
        • v2.2.0 Upgrade Instructions
        • v3.1.0 Upgrade Instructions
        • v4.2.1 Upgrade Instructions
        • v5.0.0 Upgrade Instructions
      • Upgrade FAQ
    • Deploying on Pundi AIFX EVM
      • Products
      • MetaMask
        • Download and Install MetaMask
        • Add Network
        • Configure Custom Tokens
        • Create and Import Accounts
      • Connect Wallet to Dapps
        • Connect using MetaMask
        • Connect using Pundi Wallet (Wallet Connect)
      • With Remix
      • With Truffle
      • With Hardhat
      • Cross-Chain Bridges
        • Bridging Tokens from other networks
        • Pundi Gravity Bridge
    • DApps and Infra
      • Margin X Swap
      • Pundi Wallet
      • PundiScan
      • Baklava Space
      • Safe Multisig
      • PortfolioX
      • Token Factory
    • Developers
      • Pundi AIFX Network
      • Pundi AIFX Modules
      • Pundi AIFX JSON RPC
      • Pundi AIFX REST API
      • Web3 JSON RPC
        • JSON RPC Server
        • Namespaces
        • JSON RPC Methods
        • Events
      • Support Leap
      • Third Party Price Oracles
      • Contract Deployments
      • Pundi AIFX Cross Chain
        • sendToFx
        • f(x)Core
        • ibc
        • Target
      • Precompiled Contracts
        • CrossChain Precompiled
        • Staking-V2 Precompiled
      • Pundi AIFX SDKs
        • Python SDK
        • JavaScript SDK
      • Contract Monitoring
    • Pundi X Chain (under Pundi X)
      • Getting Started
        • Install Pundi X (PundiXChain)
        • Setup Node
          • Full node with Binaries
          • Full node with Docker
          • Snapshot Guide
          • Node Monitoring Device
          • Node Peers
      • Validators
        • Validator Overview
        • Setting Up a Validator for PundiXChain
        • Validator Recovery
        • Validator FAQ
        • Validator Security
        • Sentry Nodes
      • Delegators
        • Delegator FAQ
        • Delegator Overview
        • Delegator CLI Guide
      • Pundi X Tutorials
        • Pundi X CLI Guide
        • Ledger Integration for pundixd
        • Testnet Faucet
        • Cloud Setup
      • Upgrade Instructions
        • Cosmovisor Integration - Binaries
        • Cosmovisor Integration - Docker
        • Support keplr
      • Developers
        • Pundi X Network
        • Pundi X Protobuf
        • Pundi X gRPC & REST
        • Pundi X JSON RPC
        • Pundi X Cross Chain
          • ibc
  • 👛PURSE+
    • Purse Box
      • How many NFTs can I mint?
      • Minting (Etherscan)
      • Minting (Pursetoken.com)
    • PURSE+
      • Links
    • Bridging
      • BSC -> Ethereum
    • Protocols
      • LP Restaking Farms
        • How to Use Farms
        • Contract on BscScan
      • PURSE Staking
      • PURSE Staking Rewards
        • Campaigns
      • PURSE Retroactive Rewards
        • Campaigns
    • Contracts
      • Ethereum
      • Binance
      • Function X
    • IBC
      • PUNDIX Chain
    • PURSE Token
      • Background
      • ERC404 intro
      • Accounting of Balances
      • Minting PURSE NFT(s)
      • Transferring PURSE
      • Maintaining PURSE NFT(s)
      • NFTs in Queue
      • Token IDs
  • 🎡Pundi AI MM Agent
    • Why Market Making
    • Traditional MM vs. AMM
    • AI MM Agent: Bridging the Gap
    • Use Cases
    • Train & Launch (Step 1 & 2)
    • Flywheel (Step 3)
    • How the AI MM Agent Works in Detail
    • Why This is Powerful
  • 🛍️Pundi AI Data Marketplace (soon)
  • 🥇PUNDIAI Token Overview
    • Revenue generation flywheel
    • vePUNDIAI
      • how to bribe with vePUNDIAI
    • Protocol pool
  • Governance
    • Governance Proposal Information
      • EGF Info
      • EGF Grants Program
      • Application Template
      • Successful Grant Applicants
      • Launching an EGF Proposal
  • Links
Powered by GitBook
On this page
  • Introduction
  • EVM Precompiled Contracts
  • Pundi AIFX Precompiled Contracts
  1. Pundi AIFX Omnilayer
  2. Developers

Precompiled Contracts

PreviousTargetNextCrossChain Precompiled

Last updated 3 months ago

Introduction

Precompiled contracts are specialized smart contracts designed to handle operations that are too complex or unsupported by Solidity, such as intricate mathematical or cryptographic functions. These contracts streamline certain processes by providing optimized implementations of such functions, making them more efficient and less resource-intensive.

In the Ethereum Virtual Machine (EVM), most precompiled contracts have to be called directly using assembly or through: precompiledAddress.staticcall(...) in Solidity; with the ecRecover precompile being the only exception. In this instance, the ecRecover precompile can be invoked directly using the ecrecover keyword thanks to the wrapper function implemented by Solidity which simplifies calling the precompile.

EVM precompiled contracts are also available on Pundi AIFX; and in addition to those, Pundi AIFX also features more advanced functionalities such as delegations and cross-chain support through additional precompiled contracts - of which will be explored in this article. Like most precompiles on the EVM, precompiles on Pundi AIFX can only be called through assembly or the staticcall call in Solidity.

EVM Precompiled Contracts

Precompiled contracts on the EVM exist at fixed addresses and can be called deterministically. The addresses start from 1, and increment for each precompile. One example of a precompile on the EVM is the ecRecover`precompile. The contract can be found at the address: 1 (0x01), and it is used for recovering an address from a hash and a digital signature for that hash. Solidity provides a built-in function that interfaces with the ecRecover precompile, it accepts a message along with the r, s and v parameters and returns the address that was used to sign the message.

function verify(
    address addressTest, 
    bytes32 msgHash,
    uint8 v,
    bytes32 r,
    bytes32 s
) public view returns (bool) {
    //Use ECRECOVER to verify address
    return (ecrecover(msgHash, v, r, s) == (addressTest));
}

The following shows an example of calling another precompile: SHA2-256, which does not have a wrapper function provided by Solidity for invocation; in this case, inline-assembly must be used.

function hashWithSha256(uint256 numberToHash) public view returns (bytes32) {
    assembly {
        mstore(0, numberToHash) // store arg in memory for `staticcall`
        let ok := staticcall(gas(), 2, 0, 32, 0, 32)
        if iszero(ok) {
            revert(0,0)
	}
	return(0, 32)
    }
}

Pundi AIFX Precompiled Contracts

To enable contract-based invocation of more advanced functionalities on the Pundi AIFX chain, such as delegation, cross-chain etc., support for precompiled contracts was added to the EVM module on Pundi AIFX.

Pundi AIFX is built based on the cosmos-sdk, and contract data in the EVM module is stored separately. Thus, to implement precompiled contracts, state data must be synchronized between the cosmos-sdk and EVM module.

After the implementation of the cross-chain precompiled contract, corresponding functions based on the precompiled contract only need to be added to ensure state synchronization. No matter how the call to the precompiled contract is made, atomicity can be guaranteed; and the precompiled contract can be directly called by other contracts or addresses, used in the same way as an ordinary contract.

The EVM module is a of the ethermint project from Evmos, and modifications, among others, include changes to ensure compatibility with Pundi AIFX. Data in the EVM module is completely isolated from other cosmos-sdk modules, and calls to contracts can be directly made within other modules through the EVM module. However, initiating calls to functions from other Pundi AIFX modules from within a contract are not allowed; hooks must be set in the EVM module and contract to passively invoke advanced functionalities on Pundi AIFX.

🦾
ecrecover
fork
BridgeCall Precompile
Staking V2 Precompile
CrossChain Precompile