Skip to content

Conversation

@emmanuelist
Copy link
Owner

Overview

This PR introduces a comprehensive smart contract for real-time mempool monitoring and transaction management on the Stacks blockchain. The contract provides robust functionality for tracking transactions, managing fee statistics, and monitoring mempool metrics.

Key Features

  • Real-time transaction tracking and status management
  • User-specific transaction watchlists
  • Dynamic fee statistics and recommendations
  • Mempool metrics monitoring
  • Administrative controls for fee thresholds and ownership

Technical Implementation

  • Implemented using Clarity smart contract language
  • Comprehensive data structures for storing transaction data, watchlists, and metrics
  • Built-in validation and security checks
  • Error handling with specific error codes
  • Efficient storage management using maps

Changes Include

Data Structures

  • Added tracked-transactions map for transaction details
  • Implemented user-watchlists map for user-specific watchlists
  • Created fee-stats map for block height-based fee statistics
  • Added mempool-metrics map for real-time mempool monitoring

Core Functionality

  • Transaction tracking and status management
  • Fee rate validation and priority calculation
  • Confirmation time estimation
  • User watchlist management
  • Fee statistics updates and retrieval
  • Mempool metrics monitoring

Administrative Features

  • Contract ownership management
  • Minimum fee threshold configuration
  • Security checks and authorization

Error Handling

  • Added comprehensive error codes (u1000-u1013)
  • Implemented validation functions for all inputs
  • Added safety checks for critical operations

Testing

  • All functions have been tested for correct operation
  • Error conditions have been verified
  • Authorization checks confirmed working
  • Data structure operations validated

Security Considerations

  • Owner-only access for critical functions
  • Input validation for all public functions
  • Rate limiting and size restrictions
  • Transaction ID format verification
  • Fee threshold enforcement

Documentation

  • Added comprehensive README
  • Included technical specifications
  • Documented all public functions
  • Added error code reference
  • Provided usage instructions

Deployment Steps

  1. Deploy smart contract to Stacks blockchain
  2. Set initial minimum fee threshold
  3. Transfer ownership if needed
  4. Begin monitoring transactions

Checklist

  • Code follows project style guidelines
  • All functions are properly documented
  • Error handling is implemented
  • Security checks are in place
  • Input validation is comprehensive
  • README is updated
  • No breaking changes

- Added error code constants for various error scenarios:
  - ERR-NOT-AUTHORIZED (u1000)
  - ERR-INVALID-PARAMS (u1001)
  - ERR-NOT-FOUND (u1002)
  - ERR-ALREADY-EXISTS (u1003)
  - ERR-INVALID-FEE (u1004)
- Included the use of ft-trait from sip-010-trait-ft-standard
- Defined `tracked-transactions` map to store transaction details including tx-id, fee-rate, size, priority, timestamp, confirmed status, category, and prediction.
- Added error code constants for various error scenarios.
- Defined `tracked-transactions` map to store transaction details.
- Defined `user-watchlists` map to store user-specific watchlist details.
- Defined `fee-stats` map to store fee statistics per block height:
  - avg-fee: uint
  - min-fee: uint
  - max-fee: uint
  - recommended-low: uint
  - recommended-medium: uint
  - recommended-high: uint
  - total-tx-count: uint
- Defined `mempool-metrics` map to store metrics related to the mempool:
  - timestamp: uint
  - size: uint
  - tx-count: uint
  - avg-confirmation-time: uint
  - congestion-level: uint
- Defined data variables:
  - contract-owner: principal
  - last-update: uint
  - total-tracked-tx: uint
  - min-fee-threshold: uint (in sats/byte)
- Defined private function `is-contract-owner` to verify if the transaction sender is the contract owner.
- Defined private function `validate-fee-rate` to check if the fee rate meets the minimum fee threshold.
- Defined private function `calculate-priority` to determine the priority of a transaction based on fee rate and size:
  - High priority: priority-score >= 100000
  - Medium priority: priority-score >= 50000
  - Low priority: priority-score < 50000
- Defined private function `estimate-confirmation-time` to calculate the estimated confirmation time based on fee rate and congestion level:
  - base-time: 10 minutes (600 seconds)
  - congestion-multiplier: 1 + (congestion / 100)
- Defined public function `track-transaction` to track a new transaction:
  - Calculates priority based on fee rate and size.
  - Retrieves current block time.
  - Validates fee rate and checks if the transaction already exists.
  - Stores transaction details in `tracked-transactions` map.
  - Updates total tracked transactions count.
- Defined public function `update-transaction-status` to update the confirmation status of a transaction:
  - Checks if the caller is the contract owner.
  - Retrieves the transaction data from `tracked-transactions` map.
  - Updates the `confirmed` status of the transaction.
- Defined public function `update-fee-statistics` to update fee statistics for a given block height:
  - Checks if the caller is the contract owner.
  - Updates the `fee-stats` map with the provided statistics.
- Defined read-only function `get-transaction-details` to retrieve details of a specific transaction:
  - Returns the transaction details from the `tracked-transactions` map.
- Defined read-only function `get-user-watchlist` to retrieve a user's watchlist:
  - Returns the watchlist details from the `user-watchlists` map.
- Defined read-only function `get-fee-statistics` to retrieve fee statistics for a given block height:
  - Returns the fee statistics from the `fee-stats` map.
- Defined private function `get-congestion-level` to retrieve the current congestion level from mempool metrics:
  - Uses the last update timestamp to fetch the metrics.
  - Returns the congestion level from the `mempool-metrics` map.
- Defined public function `update-mempool-metrics` to update mempool metrics:
  - Checks if the caller is the contract owner.
  - Retrieves the current block time.
  - Updates the `mempool-metrics` map with the provided metrics.
  - Sets the `last-update` variable to the current time.
- Defined public function `set-min-fee-threshold` to update the minimum fee threshold:
  - Checks if the caller is the contract owner.
  - Sets the `min-fee-threshold` variable to the new threshold value.
- Defined public function `transfer-ownership` to transfer contract ownership:
  - Checks if the caller is the contract owner.
  - Sets the `contract-owner` variable to the new owner.
- Added `use-trait ft-trait` for SIP-010 trait compliance.
- Updated error codes to include only relevant ones.
- Refactored `validate-fee-rate` to use `if` statement.
- Adjusted `calculate-priority` to use `cond` for clarity.
- Added validation checks in `track-transaction` and `update-transaction-status`.
- Included `validate-user` and `validate-height` functions for user and height validation.
- Added `validate-new-owner` function for ownership transfer validation.
- Ensured all public functions have appropriate validation and authorization checks.
- Added detailed overview of the smart contract features.
- Included sections for transaction tracking, user watchlists, fee statistics, and mempool metrics.
- Provided technical specifications including constants and data structures.
- Listed public functions for transaction management, watchlist management, statistics, and metrics.
- Documented administrative functions for setting fee thresholds and transferring ownership.
- Added error codes and their descriptions.
- Highlighted security features and considerations.
- Provided installation and usage instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants