Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: src-01 and sro-01 #1745

Closed
wants to merge 1 commit into from
Closed

chore: src-01 and sro-01 #1745

wants to merge 1 commit into from

Conversation

claytonneal
Copy link
Member

@claytonneal claytonneal commented Jan 24, 2025

Description

Audit findings SRC-01 and SRO-01

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] This change requires a documentation update
  • Minor technical debt

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Core package unit tests

Test Configuration:

  • Node.js Version: 18.18.0

Checklist:

  • My code follows the coding standards of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • New and existing integration tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have not added any vulnerable dependencies to my code

Summary by CodeRabbit

  • New Features

    • Introduced FungibleToken class to support flexible token handling
    • Enhanced VTHO token implementation with improved decimal management
  • Refactor

    • Updated Clause transaction method to support generic fungible tokens
    • Refactored VTHO class to extend FungibleToken
  • Tests

    • Added comprehensive unit tests for FungibleToken and VTHO classes
  • Documentation

    • Minor comment improvements in provider and signer classes

@claytonneal claytonneal requested a review from a team as a code owner January 24, 2025 14:47
Copy link

coderabbitai bot commented Jan 24, 2025

Walkthrough

This pull request introduces a new FungibleToken class in the core package, which extends the Coin class and provides a more flexible approach to handling token transactions. The changes modify the VTHO class to inherit from FungibleToken, update the transferToken method in the Clause class to accept FungibleToken instead of VTHO, and add corresponding unit tests to validate the new implementation. The modifications aim to generalize token handling and provide more robust decimal place management for different types of fungible tokens.

Changes

File Change Summary
packages/core/src/transaction/Clause.ts Updated transferToken method to accept FungibleToken instead of VTHO
packages/core/src/vcdm/currency/FungibleToken.ts New class introduced with support for custom decimal places and token representation
packages/core/src/vcdm/currency/VTHO.ts Refactored to extend FungibleToken instead of Coin
packages/core/tests/vcdm/currency/FungibleToken.unit.test.ts Added unit tests for FungibleToken class functionality
packages/core/tests/vcdm/currency/VTHO.unit.test.ts Added test for wei property validation
packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts Minor comment corrections
packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts Minor comment language refinements

Sequence Diagram

sequenceDiagram
    participant User
    participant Clause
    participant FungibleToken
    participant Transaction

    User->>Clause: transferToken(tokenAddress, recipientAddress, amount)
    Clause->>FungibleToken: Validate token amount
    FungibleToken-->>Clause: Validated token
    Clause->>Transaction: Create transaction with token transfer
    Transaction-->>User: Transaction confirmation
Loading

Possibly related PRs

Suggested reviewers

  • lucanicoladebiasi
  • fabiorigam

Poem

🐰 Tokens dance with decimal grace,
Fungible friends in coding's embrace,
VTHO evolves, a flexible might,
Transactions leap to a new height!
CodeRabbit's magic, tokens take flight! 🚀

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
packages/core/src/vcdm/currency/FungibleToken.ts (1)

1-39: Consider adding input validation for the decimal parameter.
Currently, there's no explicit check to ensure decimals is non-negative (or within a sensible range). Passing an unexpected or negative value might cause unintended behavior.

 protected constructor(
     symbol: Txt,
     value: FixedPointNumber,
-    decimals: bigint = FungibleToken.WEI_DEFAULT
+    decimals: bigint = FungibleToken.WEI_DEFAULT,
 ) {
+    if (decimals < 0n) {
+        throw new Error('Decimal places cannot be negative');
+    }
     super(symbol, value);
     this._decimals = decimals;
     this.wei = value.dp(this._decimals).scaledValue;
 }
packages/core/tests/vcdm/currency/FungibleToken.unit.test.ts (1)

1-47: Test boundary conditions for decimals.
These tests validate typical scenarios (custom and default decimals). Consider adding boundary or extreme test cases (e.g., zero decimals, very large decimals, negative attempts, etc.) to further strengthen the reliability of this class.

packages/core/src/transaction/Clause.ts (2)

225-225: Update JSDoc reference.
The current documentation references "VTHO" via @see VTHO.transferTokenTo, but the parameter now accepts a general FungibleToken. Consider updating the reference to avoid confusion if you intend for any VIP-180 token to be transferred.

- * @see VTHO.transferTokenTo
+ * @see FungibleToken.transferTo

225-225: Clarify integer vs. decimal amounts in JSDoc.
The JSDoc states "Throws an error if the amount is not a positive integer," but the check is isFinite() and isPositive(). If decimals are allowed, update the documentation for consistency.

- * @throws {InvalidDataType} Throws an error if the amount is not a positive integer.
+ * @throws {InvalidDataType} Throws an error if the amount is not a finite positive number.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8537acc and d5f7523.

📒 Files selected for processing (7)
  • packages/core/src/transaction/Clause.ts (3 hunks)
  • packages/core/src/vcdm/currency/FungibleToken.ts (1 hunks)
  • packages/core/src/vcdm/currency/VTHO.ts (3 hunks)
  • packages/core/tests/vcdm/currency/FungibleToken.unit.test.ts (1 hunks)
  • packages/core/tests/vcdm/currency/VTHO.unit.test.ts (1 hunks)
  • packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts (1 hunks)
  • packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts
  • packages/network/src/provider/providers/hardhat-provider/hardhat-provider.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: rpc-proxy / test / test
  • GitHub Check: unit-integration-test-browser / Build & Lint (latest)
  • GitHub Check: unit-integration-test-browser / Build & Lint (lts/*)
  • GitHub Check: unit-integration-test / Build & Lint (latest)
  • GitHub Check: unit-integration-test-browser / Build & Lint (18)
  • GitHub Check: unit-integration-test / Build & Lint (lts/*)
  • GitHub Check: unit-integration-test / Build & Lint (18)
  • GitHub Check: Execute doc examples
🔇 Additional comments (3)
packages/core/tests/vcdm/currency/VTHO.unit.test.ts (1)

28-34: Test coverage looks solid!
This test correctly verifies the wei value using the default of 18 decimals for VTHO. No issues found here.

packages/core/src/transaction/Clause.ts (2)

235-235: Good extension to a broader token type.
Switching from VTHO to FungibleToken nicely broadens this method’s applicability to different VIP-180 tokens. This helps reduce code duplication and centralizes token handling.


18-18: Ensure the import path is valid.
This newly introduced import references a path that might not exist or might have been moved.

Below is a script to search for the FungibleToken definition and confirm that it’s located at ../vcdm/currency/FungibleToken:

✅ Verification successful

Import path is valid and correct
The file exists at packages/core/src/vcdm/currency/FungibleToken.ts and exports the FungibleToken symbol, making the import path ../vcdm/currency/FungibleToken valid from packages/core/src/transaction/Clause.ts.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify that the file FungibleToken.ts exists in the specified path and includes the export for FungibleToken.
fd FungibleToken.ts --exec cat {} | rg -A 10 -B 10 "export.*FungibleToken"

Length of output: 350


Script:

#!/bin/bash
# Show the full path of FungibleToken.ts
fd --full-path FungibleToken.ts

Length of output: 82

import { FixedPointNumber } from '../FixedPointNumber';
import { Txt } from '../Txt';
import { Units } from './Units';
import { FungibleToken } from './FungibleToken';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

The review comment is valid and the suggested fix is necessary
The Units enum defines valid values from 0 to 18, but there's no runtime validation to prevent invalid values that could cause the exponent calculation to fail. The suggested range check aligns with the codebase's unit system and prevents potential runtime errors.

  • The fix should be applied to the of method in packages/core/src/vcdm/currency/VTHO.ts
  • Additional test cases should be added to packages/core/tests/vcdm/currency/VTHO.unit.test.ts to verify the validation
🔗 Analysis chain

Validate unit range for the of method.
When choosing a Units value that is significantly larger than VTHO_DECIMALS, (VTHO.VTHO_DECIMALS - BigInt(unit)) could become negative, potentially risking incorrect or unexpected scaling. Add a check to ensure unit remains in a valid range.

public static of(
    value: bigint | number | string | FixedPointNumber,
    unit: Units = Units.ether
): VTHO {
    const fpn =
        value instanceof FixedPointNumber
            ? value
            : FixedPointNumber.of(value);

+   // Simple range check if unit is larger than 18 or negative.
+   if (BigInt(unit) > VTHO.VTHO_DECIMALS || BigInt(unit) < 0n) {
+       throw new Error('Invalid unit range for VTHO calculation');
+   }

    return new VTHO(
        fpn.div(
            FixedPointNumber.of(10n ** (VTHO.VTHO_DECIMALS - BigInt(unit)))
        )
    );
}

Also applies to: 11-11, 13-13, 26-26, 34-34, 56-58

🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Get the complete VTHO implementation and related token files
echo "=== VTHO.ts ==="
cat packages/core/src/vcdm/currency/VTHO.ts

echo -e "\n=== Looking for similar token implementations ==="
fd ".*Token.*\.ts$" packages/core/src/vcdm/currency

echo -e "\n=== Searching for VTHO unit tests ==="
fd ".*VTHO.*test.*" packages/core

Length of output: 2516


Script:

#!/bin/bash
# Check the base class, Units enum, and tests
echo "=== FungibleToken.ts ==="
cat packages/core/src/vcdm/currency/FungibleToken.ts

echo -e "\n=== Units.ts ==="
cat packages/core/src/vcdm/currency/Units.ts

echo -e "\n=== VTHO.unit.test.ts ==="
cat packages/core/tests/vcdm/currency/VTHO.unit.test.ts

Length of output: 7040

Copy link

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 98%
98.93% (4383/4430) 96.95% (1401/1445) 98.9% (907/917)
Title Tests Skipped Failures Errors Time
core 840 0 💤 0 ❌ 0 🔥 2m 29s ⏱️
network 731 0 💤 0 ❌ 0 🔥 4m 59s ⏱️
errors 40 0 💤 0 ❌ 0 🔥 17.966s ⏱️
logging 3 0 💤 0 ❌ 0 🔥 19.525s ⏱️
hardhat-plugin 19 0 💤 0 ❌ 0 🔥 1m 7s ⏱️
aws-kms-adapter 23 0 💤 0 ❌ 0 🔥 1m 28s ⏱️
ethers-adapter 5 0 💤 0 ❌ 0 🔥 1m 18s ⏱️
rpc-proxy 37 0 💤 0 ❌ 0 🔥 1m 6s ⏱️

@claytonneal
Copy link
Member Author

closed as will redo in new PR

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.

1 participant