Huione Docs
White Paper
Developers
Developers
  • Learn
    • Introduction to Huione Chain
    • Getting started with Huione Chain
  • Architecture
    • What is a Huione Chain Cluster?
    • Clusters
      • Huione Chain Clusters
      • RPC Endpoints
      • Benchmark a Cluster
      • Performance Metrics
    • Consensus
      • Synchronization
      • Leader Rotation
      • Fork Generation
      • Managing Forks
      • Turbine Block Propagation
      • Commitment Status
      • Secure Vote Signing
      • Stake Delegation and Rewards
    • Validators
      • Overview
      • TPU
      • TVU
      • Blockstore
      • Gossip Service
      • The Runtime
    • Dex & Swap
      • Trade on dex
      • Trade on swap
  • CLI
    • Command-line Guide
    • Install the Huione Tool Suite
    • Command-line Wallets
      • Command Line Wallets
      • Paper Wallet
      • File System Wallet
      • Support / Troubleshooting
    • Using Huione Chain CLI
    • Connecting to a Cluster
    • Send and Receive Tokens
    • Staking
    • Deploy a Program
    • Offline Transaction Signing
    • Durable Transaction Nonces
    • CLI Usage Reference
  • Developers
    • Get Started
      • Hello World
      • Local development
      • Rust program
    • Core Concepts
      • Accounts
      • Transactions
        • Overview
        • Versioned Transactions
        • Address Lookup Tables
      • Programs
      • Rent
      • Calling between programs
      • Runtime
    • Clients
      • JSON RPC API -1
      • JSON RPC API -2
      • JSON RPC API -3
      • Web3 JavaScript API
      • Web3 API Reference
      • Rust API
    • Writing Programs
      • Overview
      • Developing with Rust
      • Deploying
      • Debugging
      • Program Examples
      • FAQ
    • Native Programs
      • Overview
      • Sysvar Cluster Data
    • Local Development
      • Huione Test Validator
    • Backward Compatibility Policy
    • Anchor Book
      • Introduction
        • What is Anchor
        • Anchor Documentation
        • Prerequisites
      • Getting Started
        • Installation
        • Hello, Anchor!
      • Anchor Programs In-Depth
        • Essentials
          • High-level Overview
          • The Accounts Struct
          • The Program Module
          • Errors
          • Milestone Project - Tic-Tac-Toe
        • Intermediate
          • Cross-Program Invocations
          • PDAs
          • Events
      • Anchor BTS
        • The Discriminator
      • Anchor References
        • Space Reference
        • Javascript Anchor Types Reference
        • CLI Reference
        • Code References
  • Validators
    • Running a Validator
    • Getting Started
      • Validator Requirements
    • Voting Setup
      • Starting a Validator
      • Vote Account Management
      • Staking
      • Monitoring a Validator
      • Publishing Validator Info
      • Failover Setup
      • Troubleshooting
    • Geyser
      • Geyser Plugins
  • Staking
    • Staking on Huione
    • Stake Account Structure
  • Integrations
    • Add Huione to Your Exchange
    • Retrying Transactions
  • Library
    • Introduction
    • Token Program
    • Associated Token Account Program
    • Memo Program
    • Name Service
    • Feature Proposal Program
    • NFT Program
      • Overview
      • Interface
      • Usage Guidelines
        • Create a new NFT-Mint
        • Cast NFT
        • Transfer an NFT
        • Change account status
        • Permission settings
        • Query Interface
        • Continuous casting
        • Change the Mint attribute
      • Operation Overview
        • Create a new NFT-Mint
        • Transfer NFT
        • Destroy
        • Freeze NFT accounts
        • Update
    • Huione multi-sign program
      • Overview
      • Interface
      • Usage Guidelines
        • Create a multi-signature account
        • Create a proposal account
        • Vote proposal
        • Verify Proposal
        • Add-singer
        • Remove-signer
      • Operation Overview
        • Create a multi-signature account
        • Create a proposal account
        • Vote
        • Verify
        • Add-singer
        • Remove-signer
Powered by GitBook
On this page
  • Events
  • Table of Contents
  • Introduction to Events
  • Defining Events
  • Emitting Events
  • Subscribing to Events
  • Unsubscribing from Events

Was this helpful?

  1. Developers
  2. Anchor Book
  3. Anchor Programs In-Depth
  4. Intermediate

Events

Events

Events in Anchor provide a powerful mechanism for notifying and communicating between different components of a Huione dApp. They allow for the emission and tracking of occurrences within the program's execution. This documentation will cover the concept of events in Anchor and how to use them in your program development.

Table of Contents

  • Introduction to Events

  • Defining Events

  • Emitting Events

  • Subscribing to Events

  • Unsubscribing from Events

Introduction to Events

An event is a structured piece of data that holds information about a specific occurrence in a program. Events can be used to provide transparency, traceability, and synchronization in decentralized applications.

There is no native support for events in Huione. Because of this, Anchor events depends on logging in order to emit events. Programs log base64 encoded event data and clients parse the logs of the transaction to interpret the events.

Defining Events

Events are defined using the #[event] attribute macro. This macro allows you to specify the fields that an event should contain. Events can include various data types, making them versatile for different use cases.

#[event]
pub struct TransferEvent {
    from: Pubkey,
    to: Pubkey,
    amount: u64,
}

In this example, we define an event named TransferEvent with three fields: from (sender's address), to (receiver's address), and amount (the transferred amount).

Emitting Events

To emit an event within your Anchor program, you can use the emit! macro:

#[program]
pub mod my_program {
    use super::*;

    pub fn transfer(ctx: Context<TransferContext>, amount: u64) -> Result<()>  {
        // Perform transfer logic

        // Emit the TransferEvent
        emit!(TransferEvent {
            from: *ctx.accounts.from.key,
            to: *ctx.accounts.to.key,
            amount,
        });

        Ok(())
    }
}

In this example, when the transfer function is called, a TransferEvent is emitted using the emit! macro. The relevant data is populated into the event fields.

Subscribing to Events

Anyone can subscribe to events emitted by your program using Anchor's event subscription mechanisms.

const subscriptionId = program.addEventListener("TransferEvent", (event) => {
  // Handle event...
});

Unsubscribing from Events

The event listener should be removed once it's no longer required:

program.removeEventListener(subscriptionId);
PreviousPDAsNextAnchor BTS

Last updated 6 months ago

Was this helpful?

You can subscribe to events using Anchor TS library():

@xoneorg/huione-anchor