Skip to content

Add __str__ method to src/tokens/custom_royalty_fee.py #971

@exploreriii

Description

@exploreriii

PROBLEM
Currently when trying to log details about custom royalty fee, we need to manually extract each instance we want.
This is burdensome and also unreliable.

SOLUTION
add a string method in src/tokens/custom_royalty_fee, similar to what was done in CustomFractionalFee:

    def __str__(self) -> str:
        """Return a string representation of the CustomFractionalFee."""
        max_len = max(len(k.replace('_', ' ').title()) for k in self.__dict__)
        return f"{self.__class__.__name__}:\n" + "".join(
            f"    {key.replace('_', ' ').title():<{max_len}} = {value}\n"
            for key, value in self.__dict__.items()
            )

Once that is done, refactor examples/tokens/custom_royalty_fee.py

    print("\nCustomRoyaltyFee:")
    print(f"Numerator: {royalty_fee.numerator}")
    print(f"Denominator: {royalty_fee.denominator}")
    print(f"Fallback Fee Amount: {royalty_fee.fallback_fee.amount if royalty_fee.fallback_fee is not None else 'None'}")
    print(f"Fallback Fee Denominating Token ID: {royalty_fee.fallback_fee.denominating_token_id if royalty_fee.fallback_fee is not None else 'None'}")
    print(f"Fee Collector Account ID: {royalty_fee.fee_collector_account_id}")
    print(f"All Collectors Exempt: {royalty_fee.all_collectors_are_exempt}")

so that we can use the string method, thus the example is simpler and more thorough.

NOTES
Be sure to update
tests/unit/test_custom_fee.py

With a test to verify the string method is working correctly and fields are extracted as expected

REMINDERS
New starters, please read:
docs/CONTRIBUTING.md
docs/sdk_developers/training/setup
and
docs/sdk_developers/training/workflow

All commits must be: git commit -S -s -m "...." with a GPG key set up

Thank you

Metadata

Metadata

Assignees

Labels

Good First IssueIssues which are ideal for a first time or new project contributor.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions