Introduction
Molpha is a decentralized oracle network that provides reliable and secure data feeds for various assets. This guide will walk you through the process of integrating with Molpha oracles on the Solana blockchain, allowing your programs to access real-time data.Program Information
- Program ID:
moLfaMTgKNysiLevoQZk8igxektjJj4LtxSiUdfKHY3 - Serialization Format: Borsh
- Network: Solana Mainnet/Devnet
Key Concepts
The Molpha oracle program is built around a few key accounts:The Feed Account
The Feed account is the primary account you will interact with. It stores all the information about a specific data feed, including:
name_hash: A 32-byte unique identifier for the feed (hash of the feed name).authority: The public key of the account that can manage the feed.feed_type: The type of feed (Public or Personal).latest_answer: The most recent data point published to the feed.answer_history: A vector containing the history of recent answers.subscription_due_time: The Unix timestamp (i64) when the feed subscription expires.
Feed account matches the Molpha program’s IDL:
The Answer Struct
The Answer struct contains the actual data value and the timestamp when it was recorded.
value is a 32-byte array, which can represent different data types depending on the feed. For example, for a price feed, it could be a scaled integer encoded in the bytes. You’ll need to decode this based on your feed’s metadata or documentation.
On-chain Integration
To use Molpha oracle data in your Solana program, you need to:- Define the
FeedandAnswerstructs matching the Molpha program’s structure. - Pass the
Feedaccount to your instruction with proper owner verification. - Access the
latest_answerto get the current value. - Verify the feed subscription is still active.
1. Define the Account Structures
First, you need to define the account structures that match the Molpha program. These should be defined in your program:2. Define the Instruction Context
Define the accounts required for your instruction. Important: You must verify that the feed account is owned by the Molpha program using theowner constraint.
3. Implement the Instruction Logic
In your instruction logic, you can access the deserializedmolpha_feed account and read its latest_answer. Always verify the subscription is active before using the data.
4. Complete Example
Here is a complete example of a Solana program that reads from a Molpha oracle:Important Notes
- Owner Verification: Always use the
ownerconstraint to verify the feed account belongs to the Molpha program. This prevents malicious accounts from being passed as feeds. - Subscription Check: Always verify that
subscription_due_time > current_timestampbefore using feed data. - Value Decoding: The
valuefield is a 32-byte array. You need to decode it according to your feed’s data format (e.g., u64, i64, f64, etc.). - Account Discriminator: Anchor automatically handles the 8-byte discriminator when using
Account<'info, Feed>, so you don’t need to manually skip it.
Off-chain Usage with TypeScript
You can also fetch and use Molpha oracle data from off-chain applications using TypeScript. You’ll need the Molpha program IDL to properly deserialize the account data.Using Anchor SDK
Using Raw Account Data (Without Anchor)
If you don’t have the Anchor IDL, you can deserialize the account data manually using Borsh:Finding Feed Addresses
Feed addresses are PDAs derived from:- Seed:
"feed" - Authority: The feed’s authority public key
- Name hash: The 32-byte hash of the feed name
- Feed type: The feed type enum value
findProgramAddress or by querying the Molpha indexer/API if available.
Best Practices
- Always Verify Ownership: Use the
ownerconstraint to ensure the feed account belongs to the Molpha program. - Check Subscription Status: Always verify that
subscription_due_time > current_timestampbefore using feed data. - Handle Stale Data: Check the
timestampof the latest answer to ensure the data is recent enough for your use case. - Error Handling: Implement proper error handling for cases where the feed account doesn’t exist or subscription has expired.
- Value Decoding: Understand your feed’s data format. The 32-byte
valuearray may represent different types (u64, i64, f64, etc.) depending on the feed configuration. - Account Validation: Consider validating other feed properties like
min_signatures_thresholdif your use case requires a minimum number of signatures.
Common Issues
Account Discriminator Mismatch
If you get an “AccountDiscriminatorMismatch” error, ensure:- Your
Feedstruct exactly matches the Molpha program’s structure - You’re using
Account<'info, Feed>with theownerconstraint - The account data hasn’t been corrupted
Subscription Expired
Always checksubscription_due_time before using feed data. If the subscription has expired, the feed may not be updated anymore.
Value Decoding
Thevalue field is a 32-byte array. You need to know the encoding format:
- For u64:
u64::from_le_bytes(value[0..8].try_into().unwrap()) - For i64:
i64::from_le_bytes(value[0..8].try_into().unwrap()) - For f64: Use appropriate floating-point decoding
- Check the feed’s IPFS metadata or documentation for the exact format
Next Steps
- Smart Contracts Guide — Learn about contract architecture
- API Reference — Complete SDK documentation
- EVM Integration — Integrate on EVM chains
Resources
- Anchor Documentation
- Solana Cookbook
- Molpha Program IDL — Program interface definition
- Borsh Specification — Serialization format used by Molpha