credmark.cmf.model.context.ModelContext
- class ModelContext(chain_id, block_number, web3_registry)[source]
Bases:
object
Model contexts class. It holds the current context (chain id and block number) as well as helpers for getting ledger data, running other models both individually and historically over a series of blocks, looking up contracts, and accessing a web3 node.
Current context uses contextvars to ensure setting and getting context is safe when using concurrent code. It can be used with either threads or asyncio.
You can access an instance of this class from a model as
self.context
.Methods
Get the current context and raise a ModelNoContextError exception if there is no current context.
The
context.forked
method can be use to create a temporary context.Get the current context, which could be None.
Reset the context to the value it had before ModelContext.set_current_context was called.
Run a model by slug and optional version.
Set the current context, which could be None.
Attributes
Context block number.
Context chain id as an integer
A
HistoricalUtil
instance which can be used to run a model over a series of blocks based on time or block intervals.A
Ledger
instance which can be used to query the ledger for data.The
context.models
attribute can be used to run models with a method call, with any-
in the model slug replaced with_
.Context chain id as an integer
A configured web3 instance
A configured web3 instance
A
Web3Multicall
instance which can be used to batch query web3 using multicall.- Parameters
chain_id (int) –
block_number (BlockNumber) –
web3_registry (Web3Registry) –
- property block_number: BlockNumber
Context block number. A credmark.cmf.types.BlockNumber instance.
- classmethod current_context()[source]
Get the current context and raise a ModelNoContextError exception if there is no current context.
- Return type
- abstract fork(*, chain_id=None, block_number=None)[source]
The
context.forked
method can be use to create a temporary context. It inherits all the properties from the current_context. Some properties like block_number and chain_id can be changed.- Parameters
- Return type
Generator[ModelContext, Any, None]
- classmethod get_current_context()[source]
Get the current context, which could be None. Normally you should use current_context() instead.
- Return type
- property historical: HistoricalUtil
A
HistoricalUtil
instance which can be used to run a model over a series of blocks based on time or block intervals.
- property models: Models
The
context.models
attribute can be used to run models with a method call, with any-
in the model slug replaced with_
.For example:
context.run_model('example.model')
becomescontext.models.example.model()
context.run_model('example.ledger-blocks')
becomescontext.models.example.ledger_blocks()
context.run_model('var-model')
becomescontext.models.var_model()
The input that you pass to
context.run_model()
can be passed to the method call as keyword (named) args, for example:output = context.models.rpc.get_blocknumber(timestamp=1438270017)
Or as an input
DTO
or dict:output = context.models.example.model(input_dto)
You can use the run output to create an output DTO:
output = EchoDto(**context.models.example.model(input_dto))
You can run a model with a context of a block number (it must be lower than the block number of the current context) by calling the
models
instance with ablock_number
arg:output = context.models(block_number=123).example.model(input_dto)
- classmethod reset_current_context(token)[source]
Reset the context to the value it had before ModelContext.set_current_context was called.
Pass the token returned by ModelContext.set_current_context method
- Parameters
token (Token[Optional[ModelContext]]) –
- abstract run_model(slug: str, input: Union[dict, BaseModel, IntDTO, StrDTO, FloatDTO], return_type: Type[DTOT], block_number: Optional[int] = None, version: Optional[str] = None, local: bool = False) DTOT [source]
- abstract run_model(slug: str, input: Union[dict, BaseModel, IntDTO, StrDTO, FloatDTO], return_type: Optional[Type[dict]] = None, block_number: Optional[int] = None, version: Optional[str] = None, local: bool = False) dict
Run a model by slug and optional version.
In order for models to be consistently deterministic, the ONLY type of error a model should catch and handle from a call to
run_model()
is aModelDataError
, which is considered a permanent error for the given context. All other errors are considered transient, coding errors, or conditions that may change in the future.- Parameters
slug (str) – the slug of the model
input (dict | DTO) – an optional dictionary or DTO instance of input data that will be passed to the model when it is run.
block_number (int | None) – optional block number to use as context. If None, the block_number of the current context will be used.
version (str | None) – optional version of the model. If version is None, the latest version of the model is used. Use of this parameter is NOT recommended.
return_type (DTO Type | None) – optional class to use for the returned output data. If not specified, returned value is a dict. If a DTO specified, the returned value will be an instance of that class if the output data is compatible with it. If its not, an exception will be raised.
- Returns
The output returned by the model’s run() method as a dict or a DTO instance if return_type is specified.
- Raises
ModelDataError – A catch-able permanent error.
ModelRunError – A non-permanent run error. Should not be caught.
ModelNotFoundError – Requested model was not found. Should not be caught.
ModelBaseError – other subclasses of
ModelBaseError
that should not be caught.
- classmethod set_current_context(context)[source]
Set the current context, which could be None. Normally you should not use this method.
- Parameters
context (Optional[ModelContext]) –
- Return type
Token[Optional[ModelContext]]
- property web3: Web3
A configured web3 instance
- property web3_async: AsyncWeb3
A configured web3 instance
- property web3_batch: Web3Batch
A
Web3Multicall
instance which can be used to batch query web3 using multicall.