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

Generate Python types for all internal Solidity contract types. #847

Merged
merged 13 commits into from
Aug 23, 2023

Conversation

sentilesdal
Copy link
Contributor

@sentilesdal sentilesdal commented Aug 22, 2023

These are not the best commits, but not the worst. Probably best to look at the total files.

This PR creates a new file like IHyperdriveTypes.py that has all the internal types of a contract. There are also improvements to the formatting of the function parameters. I've also added a very basic test for the contract jinja template. This will be mostly replaced with better testing that uses a custom abi that i'll create to make sure we are testing all the crazy structs / nested tuples etc that we'll see returned from a contract. Next steps will be to add output types to all functions and then events!

Example output:

IHyperdriveTypes.py

"""Dataclasses for all structs in the IHyperdrive contract."""

from dataclasses import dataclass


@dataclass
class Checkpoint:
    """Checkpoint struct."""

    sharePrice: int
    longSharePrice: int
    shortBaseVolume: int

@dataclass
class Fees:
    """Fees struct."""

    curve: int
    flat: int
    governance: int


@dataclass
class PoolConfig:
    """PoolConfig struct."""

    baseToken: str
    initialSharePrice: int
    minimumShareReserves: int
    positionDuration: int
    checkpointDuration: int
    timeStretch: int
    governance: str
    feeCollector: str
    Fees: Fees
    oracleSize: int
    updateGap: int

# more types below

Example contracts output now:

IHyperdriveContract.py

class IHyperdriveAddLiquidityContractFunction(ContractFunction):
    """ContractFunction for the addLiquidity method."""

    # pylint: disable=arguments-differ
    def __call__(
        self, _contribution: int, _minApr: int, _maxApr: int, _destination: str, _asUnderlying: bool
    ) -> "IHyperdriveAddLiquidityContractFunction":
        super().__call__(_contribution, _minApr, _maxApr, _destination, _asUnderlying)
        return self

    # TODO: add call def so we can get return types for the calls
    # def call()
    
    class IHyperdriveContractFunctions(ContractFunctions):
    """ContractFunctions for the IHyperdrive contract."""

    addLiquidity: IHyperdriveAddLiquidityContractFunction
    # more below
    
    class IHyperdriveContract(Contract):
    """A web3.py Contract class for the IHyperdrive contract."""

    def __init__(self, address: ChecksumAddress | None = None, abi=Any) -> None:
        self.abi = abi
        # TODO: make this better, shouldn't initialize to the zero address, but the Contract's init
        # function requires an address.
        self.address = address if address else cast(ChecksumAddress, "0x0000000000000000000000000000000000000000")

        try:
            # Initialize parent Contract class
            super().__init__(address=address)

        except FallbackNotFound:
            print("Fallback function not found. Continuing...")

    functions: IHyperdriveContractFunctions

@vercel
Copy link

vercel bot commented Aug 22, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
elf-simulations ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 23, 2023 8:33pm

Copy link
Member

@dpaiton dpaiton left a comment

Choose a reason for hiding this comment

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

a few typos & such, otherwise good

@sentilesdal sentilesdal merged commit cb9ab5a into main Aug 23, 2023
4 checks passed
@sentilesdal sentilesdal deleted the matt-add-tests branch August 23, 2023 20:46
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