enzyme.events

Documentation for eth_defi.enzyme.events Python module.

Enzyme protocol event reader.

  • High level interface for Enzyme deposit and withdrawal events, with unit conversion and token data look up

  • Read different events from Enzyme vaults that are necessary for managing the available trading capital

Functions

fetch_vault_balance_events(vault, ...)

Get the deposits to Enzyme vault in a specific time range.

fetch_vault_balances(vault[, block_identifier])

Get the live balances of the vault tokens at a specific block.

Classes

Deposit

Enzyme deposit event wrapper.

EnzymeBalanceEvent

Enzyme deposit/redeem event wrapper.

LiveBalance

Current balance of a position in Enzyme vault.

Redemption

Enzyme deposit event wrapper.

class EnzymeBalanceEvent

Bases: object

Enzyme deposit/redeem event wrapper.

Wrap the underlying raw JSON-RPC eth_getLogs data to something more manageable.

vault: eth_defi.enzyme.vault.Vault

Enzyme vault instance

event_data: dict

Underlying EVM JSON-RPC log data

static wrap(vault, event_data)

Parse Solidity events to the wrapped format.

Parameters
  • event_data (dict) –

    Raw JSON-RPC event data.

    Example:

    {'address': '0xbeaafda2e17fc95e69dc06878039d274e0d2b21a',
     'blockHash': '0x5eee3d7d2f32034955f2db9c2e84c8dfabb89a4001d32d4e01bdae540f5a0c06',
     'blockNumber': 65,
     'chunk_id': 62,
     'context': None,
     'data': '0x000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000001dcd6500',
     'event': <class 'web3._utils.datatypes.SharesBought'>,
     'logIndex': '0x4',
     'removed': False,
     'timestamp': 1679394381,
     'topics': ['0x849165c18b9d0fb161bcb145e4ab523d350e5c98f1dbbb1960331e7ee3ca6767',
                '0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'],
     'transactionHash': '0xb430a5546dd43042e3d36526fbd71ebc38c8598f6ee354f17839d3cdddf74530',
     'transactionIndex': '0x0',
     'transactionLogIndex': '0x4'}
    

  • vault (eth_defi.enzyme.vault.Vault) –

Return type

eth_defi.enzyme.events.EnzymeBalanceEvent

property timestamp: datetime.datetime

Return the block mined at timestamp.

property web3: web3.main.Web3

Our web3 connection.

property arguments: List[bytes]

Access the non-indexed Solidity event arguments.

property denomination_token: eth_defi.token.TokenDetails

Get the denominator token for withdrawal/deposit.

Read the token on-chain details.

Returns

Usually ERC-20 details for USDC

property shares_token: eth_defi.token.TokenDetails

Get the shares token for withdrawal/deposit.

Read the token on-chain details.

Returns

ERC-20 details for a token with the fund name/symbol and 18 decimals.

__init__(vault, event_data)
Parameters
Return type

None

class Deposit

Bases: eth_defi.enzyme.events.EnzymeBalanceEvent

Enzyme deposit event wrapper.

  • Wraps SharesBought event

  • See ComptrollerLib.sol

The solidity event:

event SharesBought(
    address indexed buyer,
    uint256 investmentAmount,
    uint256 sharesIssued,
    uint256 sharesReceived
);
property investment_amount: decimal.Decimal

Amount of deposit/withdrawal in the denominator token.

property shares_issued: decimal.Decimal

Amount of deposit/withdrawal in the denominator token.

property receiver: eth_typing.evm.HexAddress

Address of the user who received the bought shares.

__init__(vault, event_data)
Parameters
Return type

None

property arguments: List[bytes]

Access the non-indexed Solidity event arguments.

property denomination_token: eth_defi.token.TokenDetails

Get the denominator token for withdrawal/deposit.

Read the token on-chain details.

Returns

Usually ERC-20 details for USDC

property shares_token: eth_defi.token.TokenDetails

Get the shares token for withdrawal/deposit.

Read the token on-chain details.

Returns

ERC-20 details for a token with the fund name/symbol and 18 decimals.

property timestamp: datetime.datetime

Return the block mined at timestamp.

property web3: web3.main.Web3

Our web3 connection.

static wrap(vault, event_data)

Parse Solidity events to the wrapped format.

Parameters
  • event_data (dict) –

    Raw JSON-RPC event data.

    Example:

    {'address': '0xbeaafda2e17fc95e69dc06878039d274e0d2b21a',
     'blockHash': '0x5eee3d7d2f32034955f2db9c2e84c8dfabb89a4001d32d4e01bdae540f5a0c06',
     'blockNumber': 65,
     'chunk_id': 62,
     'context': None,
     'data': '0x000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000001dcd6500',
     'event': <class 'web3._utils.datatypes.SharesBought'>,
     'logIndex': '0x4',
     'removed': False,
     'timestamp': 1679394381,
     'topics': ['0x849165c18b9d0fb161bcb145e4ab523d350e5c98f1dbbb1960331e7ee3ca6767',
                '0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'],
     'transactionHash': '0xb430a5546dd43042e3d36526fbd71ebc38c8598f6ee354f17839d3cdddf74530',
     'transactionIndex': '0x0',
     'transactionLogIndex': '0x4'}
    

  • vault (eth_defi.enzyme.vault.Vault) –

Return type

eth_defi.enzyme.events.EnzymeBalanceEvent

vault: eth_defi.enzyme.vault.Vault

Enzyme vault instance

event_data: dict

Underlying EVM JSON-RPC log data

class Redemption

Bases: eth_defi.enzyme.events.EnzymeBalanceEvent

Enzyme deposit event wrapper.

Currently only supports redeemSharesInKind withdrawal method. This means we get the tokens of the undetlying positions directly to the investor wallet without sellign them.

  • Wraps SharesRedeemed event

  • See ComptrollerLib.sol

  • See redeemSharesInKind()

The solidity event:

event SharesRedeemed(
    address indexed redeemer,
    address indexed recipient,
    uint256 sharesAmount,
    address[] receivedAssets,
    uint256[] receivedAssetAmounts
);
property redeem_amount: decimal.Decimal

Amount of withdrawal in the number of shares.

property redeemed_assets: List[Tuple[eth_defi.token.TokenDetails, int]]

Get the list of assets in this withdrawal.

Returns

List of (redeemed token, raw token amount) tuples

property receiver: eth_typing.evm.HexAddress

Address of the user who received the assets.

Can be different from the redeemer.

property redeemer: eth_typing.evm.HexAddress

Address of the user who did the redemption transaction.

Can be different from the receiver.

__init__(vault, event_data)
Parameters
Return type

None

property arguments: List[bytes]

Access the non-indexed Solidity event arguments.

property denomination_token: eth_defi.token.TokenDetails

Get the denominator token for withdrawal/deposit.

Read the token on-chain details.

Returns

Usually ERC-20 details for USDC

property shares_token: eth_defi.token.TokenDetails

Get the shares token for withdrawal/deposit.

Read the token on-chain details.

Returns

ERC-20 details for a token with the fund name/symbol and 18 decimals.

property timestamp: datetime.datetime

Return the block mined at timestamp.

property web3: web3.main.Web3

Our web3 connection.

static wrap(vault, event_data)

Parse Solidity events to the wrapped format.

Parameters
  • event_data (dict) –

    Raw JSON-RPC event data.

    Example:

    {'address': '0xbeaafda2e17fc95e69dc06878039d274e0d2b21a',
     'blockHash': '0x5eee3d7d2f32034955f2db9c2e84c8dfabb89a4001d32d4e01bdae540f5a0c06',
     'blockNumber': 65,
     'chunk_id': 62,
     'context': None,
     'data': '0x000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000001dcd6500000000000000000000000000000000000000000000000000000000001dcd6500',
     'event': <class 'web3._utils.datatypes.SharesBought'>,
     'logIndex': '0x4',
     'removed': False,
     'timestamp': 1679394381,
     'topics': ['0x849165c18b9d0fb161bcb145e4ab523d350e5c98f1dbbb1960331e7ee3ca6767',
                '0x00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c8'],
     'transactionHash': '0xb430a5546dd43042e3d36526fbd71ebc38c8598f6ee354f17839d3cdddf74530',
     'transactionIndex': '0x0',
     'transactionLogIndex': '0x4'}
    

  • vault (eth_defi.enzyme.vault.Vault) –

Return type

eth_defi.enzyme.events.EnzymeBalanceEvent

vault: eth_defi.enzyme.vault.Vault

Enzyme vault instance

event_data: dict

Underlying EVM JSON-RPC log data

class LiveBalance

Bases: object

Current balance of a position in Enzyme vault.

See fetch_vault_balances() for details.

vault: eth_defi.enzyme.vault.Vault

Enzyme vault instance

token: eth_defi.token.TokenDetails

Which token is this event for

balance: decimal.Decimal

Underlying raw token balance, converted to decimal

__init__(vault, token, balance)
Parameters
Return type

None

fetch_vault_balance_events(vault, start_block, end_block, read_events)

Get the deposits to Enzyme vault in a specific time range.

  • Uses eth_getLogs ABI

  • Read both deposits and withdrawals in one go

  • Serial read

  • Slow over long block ranges

  • See ComptrollerLib.sol

Parameters
Return type

Iterable[eth_defi.enzyme.events.EnzymeBalanceEvent]

fetch_vault_balances(vault, block_identifier='latest')

Get the live balances of the vault tokens at a specific block.

Does EVM state based reading instead of event based reading.

  • Gets the total balances of positions held by vault

  • Does not get shares of individual investors

Warning

Enzyme returns positions with zero balance so you need to filter these out.

Example:

balance_map = {b.token.address: b for b in fetch_vault_balances(vault) if b.balance > 0}
assert len(balance_map) == 2
assert balance_map[usdc.address].balance == 1300
assert balance_map[weth.address].balance == pytest.approx(Decimal("0.124500872629987902"))
Parameters
Returns

The balances at the current or specific block

Return type

Iterable[eth_defi.enzyme.events.LiveBalance]