DEV Community

Denis Angell
Denis Angell

Posted on

Smart Contracts on XRPL's AlphaNet

The XRP Ledger Smart Contracts feature is now available for developers to explore and test on AlphaNet, a dedicated development network. This groundbreaking implementation brings native Layer 1 smart contract capabilities to the XRPL, combining the familiar design patterns of EVM-style contracts with the power of XRPL's native features and transactions.

Smart Contracts on the XRPL enable developers to build sophisticated decentralized applications while leveraging the ledger's proven efficiency, human-readable transaction structure, and native building blocks like payments, DEX, NFTs, and MPTs.

What Are XRPL Smart Contracts?

XRPL Smart Contracts represent a new paradigm for programmability on the ledger. Unlike traditional approaches, this design provides:

  • Permissionless deployment - No UNL approval required to deploy contracts
  • Native XRPL integration - Direct access to all XRPL features and primitives
  • Familiar development patterns - EVM-inspired design with functions and parameters
  • WebAssembly (WASM) runtime - Efficient execution with support for multiple programming languages On-chain ABIs - Human-readable interface definitions stored directly on the ledger
  • Event emission - Subscribe to contract events for real-time updates

Smart contracts live on pseudo-accounts and are triggered via the new ContractCall transaction type. Contracts can maintain their own state, interact with other XRPL features by submitting transactions, and emit events that applications can subscribe to.

Connect to AlphaNet

Developers can now connect to AlphaNet to begin testing Smart Contracts:

Network Details:

Replit

An example testing the base on the contract on alphanet can be seen in this replit: https://replit.com/@DenisAngell/SmartContracts. Keep coming back for more updates.

Key Features

New Transaction Types

  • ContractCreate - Deploy a new smart contract with optional initialization (Transfer Funds, Transfer NFT, Authorize)
  • ContractCall - Execute a contract function with specified parameters
  • ContractModify - Update contract properties (if permitted)
  • ContractDelete - Remove a contract from the ledger
  • ContractUserDelete - (NOT IMPLEMENTED) Allow users to reclaim reserves from contract data
  • ContractClawback - (NOT IMPLEMENTED) Enable token issuers to claw back from contracts

New Ledger Entries

  • ContractSource - Stores contract bytecode (deduplicated across deployments)
  • Contract - Represents a deployed contract instance
  • ContractData - Stores contract-specific state data

Developer Tools

  • Web IDE - Available at https://xrpl-hooks-ide-eight.vercel.app/develop for writing and deploying contracts
  • RPC Methods - New contract_info and event_history endpoints
  • Event Subscriptions - Subscribe to eventEmitted for real-time contract events

Sample Use Cases

Smart Contracts on the XRPL unlock numerous possibilities:

  • Cross-chain bridges - Integration with new bridging protocols
  • DeFi protocols - Derivatives, perpetuals, and advanced trading systems
  • Token utilities - Staking rewards for issued tokens
  • Governance systems - On-chain voting and proposal mechanisms
  • Gaming - Decentralized game logic and asset management
  • Marketplace logic - Custom rules for NFT marketplaces

Contract Examples:

https://github.com/Transia-RnD/craft-toolkit-ts/tree/main/contracts

Architecture Overview

Smart Contracts follow a clear architectural model:

  1. Deploy - Use ContractCreate to deploy WASM bytecode to a pseudo-account
  2. Call - Trigger contract functions via ContractCall transactions
  3. Interact - Contracts emit XRPL transactions to interact with native features
  4. Monitor - Subscribe to events or query contract history via RPC

The pseudo-account model ensures contracts cannot be controlled by any private key, providing security and predictability.

Core Smart Contract Components

Smart Contracts on the XRPL are built around four fundamental components that provide flexibility and power to developers:

1. Parameters: Instance & Function

Instance Parameters
Instance parameters act like environment variables for your contract. They allow you to configure contract behavior without modifying the source code, making contracts more reusable and flexible.

  • Defined at the ContractSource level
  • Set during contract deployment via ContractCreate
  • Analogous to environment variables or configuration settings
  • Allow the same contract code to be deployed multiple times with different configurations
  • Example: API endpoints, fee percentages, admin addresses

Function Parameters
Function parameters define the inputs for each callable contract function, creating a strongly-typed interface.

  • All parameter types must be valid XRPL serialized types (STAmount, STAccount, STUInt32, etc.)
  • Maximum of 4 parameters per function (expandable in future versions)
  • All parameters are required - no optional parameters or overloading
  • Parameters (Instance & Function) can have special flags for moving funds:
    • tfSendAmount - Automatically transfers the specified amount from caller to contract
    • tfSendNFToken - Transfers an NFToken to the contract
    • tfAuthorizeToken - Provides token authorization

The combination of instance and function parameters creates a clean, type-safe interface that's both human-readable and programmatically verifiable.

2. Data: Storage and State Management

Smart Contracts have powerful data storage capabilities with support for simple and complex data structures:

Basic Get/Set Operations
Store and retrieve simple key-value data for contract state management.

Nested Data Structures
Contracts can store and retrieve nested objects, enabling complex state management. For example, you can store user profiles with multiple nested properties, or maintain hierarchical data structures within your contract.

Array Operations
Full support for array-based storage with index-based access. Store ordered collections and access elements by their position, including support for nested arrays within your data structures.

Storage Types

  • Contract-level data - Global state accessible across all function calls
  • Per-user data - Isolated storage for each user interacting with the contract
  • Nested structures - Complex objects with multiple levels of nesting
  • Arrays - Ordered collections with index-based access

All data is stored using XRPL's native serialized types, ensuring efficiency and compatibility with the broader ledger ecosystem.

3. Emit: Transaction Emission

One of the most powerful features of XRPL Smart Contracts is the ability to emit XRPL transactions directly from contract code. This allows contracts to interact with all native XRPL features:

How It Works
Contracts can construct and submit transactions as if they were coming from the contract's pseudo-account. These transactions are validated and processed just like any other XRPL transaction.

Supported Transaction Types
Contracts can emit virtually any XRPL transaction:

  • Payment - Send XRP, tokens, or MPTs
  • TrustSet - Establish trust lines
  • OfferCreate - Create DEX orders
  • NFTokenMint - Create NFTs
  • EscrowCreate/EscrowFinish - Use escrow functionality
  • AccountSet - Modify account settings
  • And many more...

Example Use Cases

  • Automated Payments - Contract triggers payments based on conditions
  • DEX Integration - Create or cancel offers programmatically
  • Token Management - Mint or burn tokens in response to contract logic
  • Multi-step Operations - Chain multiple transactions together
  • Cross-feature Integration - Combine payments, escrows, and NFTs in complex workflows

Fee Handling
Fees for emitted transactions are included in the ContractCall transaction fee, making cost calculation transparent for users.

This emission model preserves the XRPL's human-readable transaction structure while giving contracts full access to the ledger's native capabilities.

4. Events: Event Emission and Subscriptions

Smart Contracts can emit custom events that applications and users can subscribe to, enabling real-time notifications and monitoring:

Event Emission
Contracts can emit events at any point during execution to signal important state changes or actions:

  • Custom event names and data payloads
  • Multiple events per contract call
  • Indexed for efficient querying
  • Stored in ledger history

Event Subscriptions
The new eventEmitted subscription allows applications to:

  • Listen for events from specific contracts
  • Filter by event type or parameters
  • Receive real-time notifications
  • Build reactive applications

Event History
The event_history RPC method provides:

  • Historical event queries
  • Filter by contract, time range, or event type
  • Pagination support for large result sets
  • Complete audit trail of contract activity

Common Use Cases

  • Price Updates - Oracles emit new price data
  • State Changes - Notify when important thresholds are crossed
  • User Actions - Track deposits, withdrawals, or trades
  • Governance - Signal proposal creation or voting completion
  • Monitoring - Alert systems can watch for specific conditions

Events make it easy to build responsive dApps that react to on-chain contract activity without constant polling, while maintaining a complete historical record.

Getting Started

  1. Set up your development environment

  2. Fund your test account

    • Use the AlphaNet faucet to receive test XRP
    • Reserve requirements apply for contract deployment
  3. Write your first contract

    • Define contract functions with typed parameters
    • Use the XRPL API to interact with ledger state
    • Emit transactions to modify the ledger
  4. Deploy and test

    • Submit a ContractCreate transaction
    • Call your contract functions via ContractCall
    • Monitor events and transaction results

Example: Basic Contract Structure

#![cfg_attr(target_arch = "wasm32", no_std)]

use xrpl_wasm_std::host::trace::trace;
use xrpl_wasm_std::core::data::codec::{get_data, set_data};

#[unsafe(no_mangle)]
pub extern "C" fn my_function() -> i32 {
    // Your contract logic here
    let _ = trace("Function called");

    0 // Success
}
Enter fullscreen mode Exit fullscreen mode

Resources and Support

Join the Future of XRPL Development

Smart Contracts represent a major evolution for the XRP Ledger, enabling developers to build the next generation of decentralized applications while preserving everything that makes XRPL special: efficiency, reliability, and simplicity.

Start building today on AlphaNet and help shape the future of programmability on the XRPL. Your feedback and contributions during this testing phase are invaluable to ensuring a robust, production-ready implementation.

Connect. Build. Innovate.


Questions, feedback, or need help getting started? Join us in the XRPL Developer Discord or reach out directly. Let's build something amazing together.

Top comments (0)