analyse_trade_by_receipt_generic

Documentation for eth_defi.velvet.analysis.analyse_trade_by_receipt_generic function.

analyse_trade_by_receipt_generic(web3, tx_hash, tx_receipt, intent_based=True)[source]

Analyse of any trade based on ERC-20 transfer events.

Figure out

  • The success of the trade

  • Actual realised price (no idea of planned price w/slippage)

Use only ERC-20 Transfer event and do not peek into underlying DEX details.

  • Assume first Transfer() event is tokens going into trade

  • Assume last Transfer() event is tokens coming out of the trade

Example:

# Build tx using Velvet API
tx_data = vault.prepare_swap_with_enso(
    token_in="0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
    token_out="0x6921B130D297cc43754afba22e5EAc0FBf8Db75b",
    swap_amount=1_000_000,  # 1 USDC
    slippage=slippage,
    remaining_tokens=universe.spot_token_addresses,
    swap_all=False,
    from_=vault_owner,
)

# Perform swap
tx_hash = web3.eth.send_transaction(tx_data)
assert_transaction_success_with_explanation(web3, tx_hash)

receipt = web3.eth.get_transaction_receipt(tx_hash)

analysis = analyse_trade_by_receipt_generic(
    web3,
    tx_hash,
    receipt,
)

assert isinstance(analysis, TradeSuccess)
assert analysis.intent_based
assert analysis.token0.symbol == "USDC"
assert analysis.token1.symbol == "doginme"
assert analysis.amount_in == 1 * 10**6
assert analysis.amount_out > 0
# https://www.coingecko.com/en/coins/doginme
price = analysis.get_human_price(reverse_token_order=True)
assert 0 < price < 0.01
Returns

TradeSuccess or TradeFail instance.

For TradeSuccess, unknown fields we cannot figure out without DEX details are set to None.

Parameters
  • web3 (web3.main.Web3) –

  • tx_hash (str | bytes) –

  • tx_receipt (dict) –

Return type

eth_defi.trade.TradeSuccess | eth_defi.trade.TradeFail