credmark.cmf.types.contract.Contract

DTO class Contract(*args, address)[source]

Bases: Account

Contract object to make web3 call smart contract functions. You could create a contract with the following

c = Contract(address=’0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2’)

c = Contract(‘0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2’)

Show JSON schema
{
   "title": "Contract",
   "description": "Contract object to make web3 call smart contract functions.\nYou could create a contract with the following\n\n    c = Contract(address='0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2')\n\n    c = Contract('0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2')",
   "type": "object",
   "properties": {
      "address": {
         "title": "Address",
         "type": "string",
         "pattern": "^0x[a-fA-F0-9]{40}$",
         "format": "evm-address"
      }
   },
   "required": [
      "address"
   ],
   "examples": [
      {
         "address": "0x1F98431c8aD98523631AE4a59f267346ea31F984"
      },
      {
         "address": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
         "abi": "(Optional) contract abi JSON string or list"
      }
   ]
}

Config
  • arbitrary_types_allowed: bool = True

  • schema_extra: dict = {‘examples’: [{‘address’: ‘0x1F98431c8aD98523631AE4a59f267346ea31F984’}, {‘address’: ‘0x1F98431c8aD98523631AE4a59f267346ea31F984’, ‘abi’: ‘(Optional) contract abi JSON string or list’}]}

  • underscore_attrs_are_private: bool = True

Fields
Parameters

address (Address) –

Return type

None

field address: Address [Required]
Constraints
  • type = string

  • pattern = ^0x[a-fA-F0-9]{40}$

  • format = evm-address

DTO class ContractMetaData(*, contract_name=None, deploy_tx_hash=None, constructor_args=None, abi_hash=None, abi=None, is_transparent_proxy=None, proxy_implementation=None, deployed_block_number=None)[source]

Bases: BaseModel

Show JSON schema
{
   "title": "ContractMetaData",
   "type": "object",
   "properties": {
      "contract_name": {
         "title": "Contract Name",
         "type": "string"
      },
      "deploy_tx_hash": {
         "title": "Deploy Tx Hash",
         "type": "string"
      },
      "constructor_args": {
         "title": "Constructor Args",
         "type": "string"
      },
      "abi_hash": {
         "title": "Abi Hash",
         "type": "string"
      },
      "abi": {
         "title": "Abi",
         "type": "array",
         "items": {}
      },
      "is_transparent_proxy": {
         "title": "Is Transparent Proxy",
         "type": "boolean"
      },
      "proxy_implementation": {
         "title": "Proxy Implementation"
      },
      "deployed_block_number": {
         "title": "Deployed Block Number",
         "type": "integer"
      }
   }
}

Fields
  • abi (Optional[credmark.cmf.types.abi.ABI])

  • abi_hash (Optional[str])

  • constructor_args (Optional[str])

  • contract_name (Optional[str])

  • deploy_tx_hash (Optional[str])

  • deployed_block_number (Optional[credmark.cmf.types.block_number.BlockNumber])

  • is_transparent_proxy (Optional[bool])

  • proxy_implementation (Optional[credmark.cmf.types.contract.Contract])

Parameters
Return type

None

field abi: Optional[ABI] = None
field abi_hash: Optional[str] = None
field constructor_args: Optional[str] = None
field contract_name: Optional[str] = None
field deploy_tx_hash: Optional[str] = None
field deployed_block_number: Optional[BlockNumber] = None
field is_transparent_proxy: Optional[bool] = None
field proxy_implementation: Optional[Contract] = None
fetch_events(event, argument_filters=None, from_block=None, to_block=None, address=None, topics=None, contract_address=None, argument_names=None, by_range=None, use_async=False, async_worker=10)[source]

contract_address is by default set to the event’s address.

For proxy contract, the event could be from the proxy contract and contract_address could be overridden with the contract’s address.

For example,

tok = Token(‘AAVE’) list(tok.fetch_events(

tok.proxy_for.events.Transfer, from_block=16_000_000, to_block=16_010_000, contract_address=tok.address))

For non-proxy contract, we may use as following

tok = Token(‘CRV’) df = pd.DataFrame(tok.fetch_events(

tok.events.Transfer, from_block=16_000_000, to_block=16_001_000, topics=[‘0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef’]))

df.loc[:, [‘blockNumber’, ‘_from’, ‘_to’, ‘_value’]]

Parameters
set_abi(abi, set_loaded=False)[source]

Set the ABI for the contract

Parameters

abi (Union[List, str]) –

to_accounts()
classmethod validate(value)
property abi

The ABI for the contract, if it’s available, otherwise None.

property constructor_args

Constructor args, if any, otherwise None.

property contract_name

Name of the contract, if available, otherwise None.

property deploy_tx_hash

The deploy transaction hash, if available, otherwise None.

property deployed_block_number
property events

A web3 ContractEvents instance for the contract.

property functions

A web3 ContractFunctions instance for the contract.

property info

A credmark.cmf.types.contract.ContractInfo instance for the contract.

property instance: Contract

A web3 Web3Contract instance or raises a ModelDataError if no ABI is available.

property is_transparent_proxy

True if is a transparent proxy. Otherwise False or None.

property ledger: ContractLedger

A ContractLedger instance which can be used to query the ledger for a contract’s functions or events.

To run a query, call a property of contract.ledger.functions.{NameOfFunction} or contract.ledger.events.{NameOfEvent}. The name of the function or event can be auto-completed by pressing TAB after the .. Alternatively, you could looked up from contract.abi.functions or contract.abi.events.

Functions example:

contract = Contract(address='0x3a3a65aab0dd2a17e3f1947ba16138cd37d08c04')
with contract.ledger.functions.approve as q:
    ret = q.select(
            columns=[q.spender')],
            aggregates=[(f'MAX({q.value})', 'max_value')],
            group_by=q.spender,
            order_by='"max_value" desc',
            limit=5)

    top_approvals = []
    for row in ret.data:
        top_approvals.append({
            "spender": row[q.spender],
            "value": row['max_value']
        })

Events example:

with contract.ledger.events.Transfer as q:
    ret = q.select(
        columns=[q.EVT_BLOCK_NUMBER,
                 q.from,
                 q.to,
                 q.value],
        order_by=q.EVT_BLOCK_NUMBER.desc(),
        limit=4)

See LedgerQueryContractFunctions or LedgerQueryContractEvents for more details.

property models
property proxy_for

A proxy implementation if available