How the CloddsBot Trade Ledger Anchors Data On-Chain Across Solana, Polygon, and Base

The CloddsBot trade ledger creates a tamper-proof record of each trade decision by writing a deterministic hash to a blockchain through a modular Anchor Service that abstracts Solana, Polygon, and Base implementations behind a unified API.

The CloddsBot repository implements a robust trade ledger on-chain anchoring system that ensures immutable audit trails for automated trading decisions. By leveraging chain-specific transaction formats, the bot publishes cryptographic proofs to Solana, Polygon, or Base while maintaining a consistent interface. This article examines the implementation details found in src/ledger/anchor.ts to explain how the anchoring mechanism works across these three distinct blockchain architectures.

Architecture Overview and Configuration

The anchoring system centers on the AnchorConfig interface defined in src/ledger/anchor.ts, which specifies the target chain and required credentials. The configuration supports distinct RPC endpoints and private keys for Solana and EVM-compatible networks.

export interface AnchorConfig {
  chain: AnchorChain;
  solanaRpcUrl?: string;
  solanaPrivateKey?: string;
  evmRpcUrl?: string;
  evmPrivateKey?: string;
  batchSize?: number;
  batchMaxAgeMs?: number;
}

The factory function createAnchorService(config) initializes the service and returns an object exposing three methods: anchor(hash), anchorBatch(hashes), and getConfig(). When batchSize is configured, the service buffers incoming hashes and automatically flushes them after batchMaxAgeMs milliseconds or when the buffer reaches capacity.

Solana Anchoring via the Memo Program

For Solana, the anchorToSolana(hash, config) function constructs a transaction that writes the hash into the Memo program (MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr). This approach stores data immutably on-chain without deploying custom smart contracts.

The implementation loads the keypair via ../solana/wallet, signs the transaction, and waits for network confirmation. Upon success, it returns the transaction signature as txHash. The deterministic nature of the hash ensures that any alteration to the original trade data would produce a different anchor reference, providing cryptographic proof of the decision state at a specific point in time.

EVM Anchoring for Polygon and Base

Both Polygon and Base utilize the generic anchorToEvm(hash, chain, config) function, which handles all EVM-compatible chains uniformly. The implementation uses the ethers library to create a JSON-RPC provider and builds a wallet instance from the supplied private key.

Rather than deploying a custom contract, the service sends a minimal self-transfer transaction (zero value) with the calldata containing the formatted string clodds:ledger:<hash>. This method embeds the ledger hash directly into the transaction data field, making it permanently retrievable from the blockchain history. The transaction receipt hash serves as the anchor proof for both Polygon and Base networks.

Batch Anchoring Strategy

When processing high volumes of trade decisions, the batching mechanism optimizes gas costs and RPC usage. The flushBatch method inside createAnchorService concatenates pending hashes, computes a SHA-256 hash of the combined data, and anchors this single aggregated hash using the chain-specific routine (anchorHash).

This approach reduces on-chain transaction count while maintaining verifiability, as the batch hash cryptographically represents all contained entries. The service respects the batchMaxAgeMs parameter to ensure that delayed decisions do not remain unanchored indefinitely, providing configurable latency guarantees for audit requirements.

Verification and Integrity Checking

The verifyAnchor(txHash, expectedHash, chain) function enables third-party verification of anchored data without requiring access to the bot's internal state. For Solana, the function inspects parsed memo instructions to extract the embedded hash. For EVM chains (Polygon and Base), it decodes the tx.data field to UTF-8 and checks for the clodds:ledger:<expectedHash> pattern.

This verification layer ensures that the on-chain record matches the expected trade ledger hash, allowing auditors to confirm that specific trading decisions were recorded immutably at claimed timestamps.

Summary

  • Modular architecture: The createAnchorService factory provides a chain-agnostic API while handling Solana and EVM implementations separately under the hood.
  • Solana implementation: Uses the native Memo program to store hashes cost-effectively without smart contract deployment.
  • EVM implementation: Embeds hashes in transaction calldata for Polygon and Base using standard self-transfer transactions via ethers.
  • Batching support: Configurable batchSize and batchMaxAgeMs parameters aggregate multiple hashes into single on-chain anchors to optimize costs.
  • Verification: The verifyAnchor function supports independent audit by checking transaction memos or calldata against expected hashes.

Frequently Asked Questions

How does the trade ledger ensure data immutability once anchored?

The ledger generates a deterministic SHA-256 hash of the trade decision data before calling the anchor service. Because blockchains are append-only ledgers, once the anchorToSolana or anchorToEvm function writes this hash to a confirmed transaction, the data becomes tamper-proof. Any modification to the original trade data would produce a different hash that would fail verification against the on-chain record.

What is the difference between single and batch anchoring in CloddsBot?

Single anchoring calls anchor(hash) to immediately write one hash to the blockchain, providing real-time auditability. Batch anchoring accumulates hashes in memory until reaching the configured batchSize or batchMaxAgeMs threshold, then calls flushBatch to concatenate and hash the collection before anchoring once. This reduces transaction fees but introduces latency proportional to the batch configuration.

Can I verify an anchored trade decision without running the bot?

Yes. The verifyAnchor function exported from src/ledger/anchor.ts requires only the transaction hash (txHash), the expected ledger hash, and the chain identifier. For Solana, it queries the memo instruction data; for Polygon or Base, it decodes the transaction calldata. This allows external auditors to confirm anchoring independently using standard RPC endpoints.

Why does the EVM implementation use calldata instead of a smart contract?

The anchorToEvm function uses calldata in zero-value self-transactions because it provides the cheapest permanent storage on EVM chains. Storing data in transaction input data (calldata) costs significantly less gas than contract storage (SSTORE operations), making it economically viable to anchor frequent trade decisions on Polygon and Base without deploying or maintaining dedicated smart contract infrastructure.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →