Add our own DTO

Although you can use dictionaries for model input and output data in your python code, Credmark highly recommends the use of DTOs (Data Transfer Objects.) To create a DTO, simply subclass the DTO base class and use DTOFields to annotate your properties. The CMF uses the pydantic python module (DTO is simply an alias for pydantic BaseModel and DTOField an alias for Field), so almost anything that works with pydantic will work for your DTO.

We will define the input DTO to take in the block number. For this import DTO from credmark.dto by replacing the previous EmptyInput import (line #2) and model input in @Model.describe and run().

In this model, we define one field in the DTO to specify how many blocks to look back from the current block. The DTO can be simply set with the name and type of the field.

class ApeCountInput(DTO):
    block_number_count: int

DTOField object helps to set the default value, and constraints and description. So we have the below in the example.

from credmark.cmf.model import Model
from credmark.dto import DTO

class ApeCountInput(DTO):
    block_number_count: int = DTOField(1, gt=0,
                                    description='Number of blocks to look back')


@Model.describe(slug='contrib.ape-count',
                version='1.0',
                display_name='Count how many transactions in the one block',
                description="Use ledger and some counts",
                input=ApeCountInput,
                output=dict)
class ContribModel(Model):
    def run(self, input: ApeCountInput) -> dict:
        return {}

Last updated