credmark.cmf.types.token_erc20.Token

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

Bases: Contract

Fungible Token that conforms to ERC20 standards. You could create a token with the following

t = Token(symbol=’CMK’)

t = Token(address=’0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48’)

t = Token(‘CMK’)

t = Token(‘0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48’)

Show JSON schema
{
   "title": "Token",
   "description": "Fungible Token that conforms to ERC20 standards.\nYou could create a token with the following\n\n    t = Token(symbol='CMK')\n\n    t = Token(address='0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')\n\n    t = Token('CMK')\n\n    t = Token('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')",
   "type": "object",
   "properties": {
      "address": {
         "title": "Address",
         "type": "string",
         "pattern": "^0x[a-fA-F0-9]{40}$",
         "format": "evm-address"
      }
   },
   "required": [
      "address"
   ],
   "examples": [
      {
         "address": "0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9"
      },
      {
         "symbol": "AAVE"
      },
      {
         "address": "0x1F98431c8aD98523631AE4a59f267346ea31F984"
      },
      {
         "address": "0x1F98431c8aD98523631AE4a59f267346ea31F984",
         "abi": "(Optional) contract abi JSON string or list"
      }
   ]
}

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

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)

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[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
DTO class TokenMetadata(*, 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, symbol=None, name=None, decimals=None, total_supply=None, wrapped=None, set_loaded=False)[source]

Bases: ContractMetaData

Show JSON schema
{
   "title": "TokenMetadata",
   "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": {
         "$ref": "#/definitions/Contract"
      },
      "deployed_block_number": {
         "title": "Deployed Block Number",
         "type": "integer"
      },
      "symbol": {
         "title": "Symbol",
         "type": "string"
      },
      "name": {
         "title": "Name",
         "type": "string"
      },
      "decimals": {
         "title": "Decimals",
         "type": "integer"
      },
      "total_supply": {
         "title": "Total Supply",
         "type": "integer"
      },
      "wrapped": {
         "title": "Wrapped",
         "type": "string",
         "pattern": "^0x[a-fA-F0-9]{40}$",
         "format": "evm-address"
      },
      "set_loaded": {
         "title": "Set Loaded",
         "default": false,
         "type": "boolean"
      }
   },
   "definitions": {
      "Contract": {
         "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"
            }
         ]
      }
   }
}

Fields
  • abi (Optional[ABI])

  • abi_hash (Optional[str])

  • constructor_args (Optional[str])

  • contract_name (Optional[str])

  • decimals (Optional[int])

  • deploy_tx_hash (Optional[str])

  • deployed_block_number (Optional[BlockNumber])

  • is_transparent_proxy (Optional[bool])

  • name (Optional[str])

  • proxy_implementation (Optional['Contract'])

  • set_loaded (bool)

  • symbol (Optional[str])

  • total_supply (Optional[int])

  • wrapped (Optional[credmark.cmf.types.address.Address])

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 decimals: Optional[int] = None
field deploy_tx_hash: Optional[str] = None
field deployed_block_number: Optional[BlockNumber] = None
field is_transparent_proxy: Optional[bool] = None
field name: Optional[str] = None
field proxy_implementation: Optional['Contract'] = None
field set_loaded: bool = False
field symbol: Optional[str] = None
field total_supply: Optional[int] = None
field wrapped: Optional[Address] = None
Constraints
  • type = string

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

  • format = evm-address

get_cache(field, chain_id, block_number)[source]
update_cache(field, chain_id, block_number, value)[source]
as_erc20(set_loaded=False, use_alt=False)[source]
balance_of(address)[source]
Parameters

address (Union[ChecksumAddress, str, bytes, Address]) –

Return type

int

balance_of_scaled(address)[source]
Parameters

address (Union[ChecksumAddress, str, bytes, Address]) –

Return type

float

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)

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
scaled(value)[source]
Return type

float

set_abi(abi, set_loaded=False)

Set the ABI for the contract

Parameters

abi (Union[List, str]) –

to_accounts()
try_erc20_property(prop_name)[source]
unscaled(value)[source]
Return type

float

classmethod validate(value)[source]
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 decimals: int
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 fiat: bool
property functions

A web3 ContractFunctions instance for the contract.

property info
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 name: str
property proxy_for

A proxy implementation if available

property symbol: str
property total_supply: int
property total_supply_scaled: float