trade

Documentation for eth_defi.trade Python module.

Trade outcome analysis.

  • Data structures for analysis Uniswap trade outcomes

  • Can be used with both Uniswap v2 and Uniswap v3 style DEXes

  • Determine and calculate factors like realised price, slippage, paid trading fee

For usage see

Classes

TradeFail

Describe the result of a failed Uniswap swap.

TradeResult

A base class for Success/Fail trade result.

TradeSuccess

Describe the result of a successful Uniswap swap.

class TradeResult

Bases: object

A base class for Success/Fail trade result.

gas_used: int

How many units of gas we burned

effective_gas_price: int

What as the gas price used in wei. Set to 0 if not available.

get_cost_of_gas()

This will return the gas cost of the transaction in blockchain’s native currency e.g. in ETH on Ethereum.

Return type

decimal.Decimal

__init__(gas_used, effective_gas_price)
Parameters
  • gas_used (int) –

  • effective_gas_price (int) –

Return type

None

class TradeSuccess

Bases: eth_defi.trade.TradeResult

Describe the result of a successful Uniswap swap.

See eth_defi.uniswap_v2.analysis.analyse_trade_by_receipt()

path: Optional[List[eth_typing.evm.HexAddress]]

Routing path that was used for this trade.

Should be lowercased.

amount_in: int

How much token swas swapped from

amount_out_min: int | None

What was the expected minimum output with slippage tolerance

amount_out: int

What was the actual output

price: decimal.Decimal

The price of the trade in some order.

  • Uniswap v2: Overall price paid as in token (first in the path) to out token (last in the path).

  • Uniswap v3: depends on ticks and order of token0 and token1 in the underlying pool smart contract

Price includes any fees paid during the order routing path.

Note that you get inverse price, if you route ETH-USD or USD-ETH e.g. are you doing buy or sell.

See also get_human_price()

amount_in_decimals: int

Token information bookkeeping

amount_out_decimals: int

Token information bookkeeping

token0: eth_defi.token.TokenDetails | None

Uniswap v3 pool token 0

Needed to calculate reverse token order.

token1: eth_defi.token.TokenDetails | None

Uniswap v3 pool token 1

Needed to calculate reverse token order.

lp_fee_paid: float | None

How much was the LP fee

Note: this is the raw amount in terms of the amount in token

intent_based: bool | None = None

Did we use a third party intent service for this swap.

E.g. Enso.

We might not be analyse fees and path directly.

untaxed_amount_out: int | None = None

For Uniswap v2 swaps were token tax applies.

Set to None if could not be determined.

transfer_event_count: int = 0

Did we generate some excessive transfer events during the swap.

Usually a sign of some rigging mechanism.

get_human_price(reverse_token_order=False)

Get the executed price of this trade in a human-readable form.

This depends on:

  • If we are on Uniswap v2 or v3

  • If we do buy or sell

  • If quote token is token0 or token1 in Uniswap v3 pool

Example:

# TODO
pass
Parameters

reverse_token_order

Base and quote token order.

Quote token should be natural quote token like USD or ETH based token of the trade. If reverse_token_order is set quote token is token0 of the pool, otherwise token1.

Return type

decimal.Decimal

get_tax()

Get Uniswap v2 style token tax.

Returns

Tax in bps. Always negative.

0 if no tax.

None if could not determined.

Return type

float | None

__init__(gas_used, effective_gas_price, path, amount_in, amount_out_min, amount_out, price, amount_in_decimals, amount_out_decimals, token0, token1, lp_fee_paid, intent_based=None, untaxed_amount_out=None, transfer_event_count=0)
Parameters
Return type

None

get_cost_of_gas()

This will return the gas cost of the transaction in blockchain’s native currency e.g. in ETH on Ethereum.

Return type

decimal.Decimal

gas_used: int

How many units of gas we burned

effective_gas_price: int

What as the gas price used in wei. Set to 0 if not available.

class TradeFail

Bases: eth_defi.trade.TradeResult

Describe the result of a failed Uniswap swap.

The transaction reverted for a reason or another.

revert_reason: Optional[str] = None

Revert reason if we managed to extract one

__init__(gas_used, effective_gas_price, revert_reason=None)
Parameters
Return type

None

get_cost_of_gas()

This will return the gas cost of the transaction in blockchain’s native currency e.g. in ETH on Ethereum.

Return type

decimal.Decimal

gas_used: int

How many units of gas we burned

effective_gas_price: int

What as the gas price used in wei. Set to 0 if not available.