Skip to content

Conversation

@saeedix
Copy link
Contributor

@saeedix saeedix commented Mar 22, 2025

Dear Fenguoz,
I hope this message finds you well. I've been using your excellent BSC-PHP library and would like to contribute an improvement that could benefit the community.

What's Proposed:
A new Helper class that provides:

  1. Simplified OOP interface for common operations
  2. Encapsulated configuration management
  3. Type-safe methods with PHP 7.4+ support
  4. Enhanced security practices (config validation, sanitization)
  5. Backward-compatible implementation

Key Benefits:
✅ Reduces boilerplate code for common tasks
✅ Implements best practices for key management
✅ Adds documentation-ready methods
✅ Maintains full compatibility with existing APIs
✅ Includes comprehensive usage examples

Best regards,
H0Z3YN

@Fenguoz
Copy link
Owner

Fenguoz commented Mar 25, 2025

Hello bro, I'm glad you submitted the PR. I have carefully checked it. It can be concise and improve efficiency, and can meet the needs of some people, but the design principle of a class is to achieve single responsibility and scalability as much as possible.

@saeedix
Copy link
Contributor Author

saeedix commented Mar 25, 2025

Hi @Fenguoz,

Thank you for your thoughtful review!

I’ve refined the implementation to better align with the principles you highlighted:

Key Adjustments:

  1. Single Responsibility

    • Removed the monolithic Helper class
    • Introduced a dedicated Network class solely for service initialization and validation
    • Separated concerns: Network handles service creation, while Bnb/BEP20 manage blockchain logic
  2. Scalability

    • Added NetworkServiceFactoryInterface to standardize service creation
    • Decoupled validation logic (contract address format, decimal ranges) for easy extension
    • Enabled dependency injection for API connectors (supports Node API/Bscscan API interchangeably)
  3. Safety & Compliance

    • Integrated parameter validation using Utils::isAddress()
    • Enforced decimal constraints (1-18) to prevent invalid token configurations

The new Network class simplifies usage while retaining flexibility:

// Initialize 
$network = new Network(new NodeApi('RPC_URL')); 

// Get validated services 
$bnb = $network->getBnbService(); 
$usdt = $network->getBEP20Service('0x...', 18); 

Would love your thoughts on this approach! Happy to iterate further if needed.

Best,
H0Z3YN

@Fenguoz
Copy link
Owner

Fenguoz commented Mar 26, 2025

Hi bro, I am glad to receive your PR again. I have carefully checked it and found some useful suggestions. For example, the verification of contract address and decimal. But I think adding Network may not be suitable. The subject called by the user is the token (bnb/bep20), and the proxy network is just the way it is used, so it is more appropriate to use dependency injection.

@saeedix
Copy link
Contributor Author

saeedix commented Mar 26, 2025

Hi @Fenguoz,

Thank you for your continued guidance and thoughtful feedback! 🙏 I deeply respect your design philosophy and approach to maintaining this project.

Regarding Your Notes:

  1. About the Network Class
    While I understand your preference for direct dependency injection, the Network class was proposed to:

    • Centralize validation logic (avoid code duplication)
    • Simplify multi-token management (e.g., projects using 10+ tokens)
    • Provide a standardized entry point for future extensions (NFTs, cross-chain, etc.)
      However, I fully defer to your architectural vision!
  2. Implemented Improvements
    As suggested, I've added direct validation in BEP20::__construct()

    // Contract address validation
    if (!Utils::isAddress($config['contract_address'])) {
        throw new InvalidArgumentException('Invalid BEP20 contract address');
    }
    
    // Decimal range validation (1-18)
    if ($config['decimals'] <= 0 || $config['decimals'] > 18) {
        throw new InvalidArgumentException('Decimals must be between 1-18');
    }

Benefits:

  • Prevents misconfigured tokens at instantiation
  • No performance impact (validation occurs once during initialization)
  • Backward-compatible and optional for users

I'm happy to remove the Network class entirely if it doesn't align with the project's direction.
Your insights are invaluable – I'm open to any other suggestions for improvement.

@Fenguoz
Copy link
Owner

Fenguoz commented Apr 1, 2025

Hi @Fenguoz,

Thank you for your continued guidance and thoughtful feedback! 🙏 I deeply respect your design philosophy and approach to maintaining this project.

Regarding Your Notes:

  1. About the Network Class
    While I understand your preference for direct dependency injection, the Network class was proposed to:

    • Centralize validation logic (avoid code duplication)
    • Simplify multi-token management (e.g., projects using 10+ tokens)
    • Provide a standardized entry point for future extensions (NFTs, cross-chain, etc.)
      However, I fully defer to your architectural vision!
  2. Implemented Improvements
    As suggested, I've added direct validation in BEP20::__construct()

    // Contract address validation
    if (!Utils::isAddress($config['contract_address'])) {
        throw new InvalidArgumentException('Invalid BEP20 contract address');
    }
    
    // Decimal range validation (1-18)
    if ($config['decimals'] <= 0 || $config['decimals'] > 18) {
        throw new InvalidArgumentException('Decimals must be between 1-18');
    }
    

Benefits:

  • Prevents misconfigured tokens at instantiation
  • No performance impact (validation occurs once during initialization)
  • Backward-compatible and optional for users

I'm happy to remove the Network class entirely if it doesn't align with the project's direction. Your insights are invaluable – I'm open to any other suggestions for improvement.

A new PR needs to be submitted, otherwise all historical codes will be merged😊

@saeedix
Copy link
Contributor Author

saeedix commented Apr 1, 2025

Hi @Fenguoz,

Thank you for the clarification! 🙌 I've submitted a new PR with the agreed-upon changes

Changes Included:

  1. BEP20 Validation

    • Contract address format check (Utils::isAddress())
    • Decimal range validation (1-18)
    • Backward-compatible & zero breaking changes
  2. Removed Network Class

    • Kept only the core validation logic you approved
    • Users interact directly with Bnb/BEP20 as intended

Let me know if any adjustments are needed! 😊

@Fenguoz
Copy link
Owner

Fenguoz commented Apr 2, 2025

Good job.
Tips: Next time you submit, try to submit the key code and check for redundant code, space code, etc.

@Fenguoz Fenguoz merged commit 002f945 into Fenguoz:master Apr 2, 2025
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