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
Describe the result of a failed Uniswap swap. |
|
A base class for Success/Fail trade result. |
|
Describe the result of a successful Uniswap swap. |
- class TradeResult
Bases:
objectA base class for Success/Fail trade result.
- 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
- class TradeSuccess
Bases:
eth_defi.trade.TradeResultDescribe 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.
- 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()
- 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
Noneif 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
- 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
gas_used (int) –
effective_gas_price (int) –
path (Optional[List[eth_typing.evm.HexAddress]]) –
amount_in (int) –
amount_out_min (int | None) –
amount_out (int) –
price (decimal.Decimal) –
amount_in_decimals (int) –
amount_out_decimals (int) –
token0 (eth_defi.token.TokenDetails | None) –
token1 (eth_defi.token.TokenDetails | None) –
lp_fee_paid (float | None) –
intent_based (bool | None) –
untaxed_amount_out (int | None) –
transfer_event_count (int) –
- 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
- class TradeFail
Bases:
eth_defi.trade.TradeResultDescribe the result of a failed Uniswap swap.
The transaction reverted for a reason or another.
- __init__(gas_used, effective_gas_price, revert_reason=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