chore(testing): Remove hard sell limit flag.
The setting is now inferred from the adapters capabilities.
This commit is contained in:
@@ -51,7 +51,7 @@ class EthereumToken(BaseModel):
|
|||||||
return int(amount)
|
return int(amount)
|
||||||
|
|
||||||
def from_onchain_amount(
|
def from_onchain_amount(
|
||||||
self, onchain_amount: Union[int, Fraction], quantize: bool = True
|
self, onchain_amount: Union[int, Fraction], quantize: bool = True
|
||||||
) -> Decimal:
|
) -> Decimal:
|
||||||
"""Converts an Integer to a quantized decimal, by shifting left by the token's
|
"""Converts an Integer to a quantized decimal, by shifting left by the token's
|
||||||
maximum amount of decimals (e.g.: 1000000 becomes 1.000000 for a 6-decimal token
|
maximum amount of decimals (e.g.: 1000000 becomes 1.000000 for a 6-decimal token
|
||||||
@@ -66,14 +66,14 @@ class EthereumToken(BaseModel):
|
|||||||
with localcontext(Context(rounding=ROUND_FLOOR, prec=256)):
|
with localcontext(Context(rounding=ROUND_FLOOR, prec=256)):
|
||||||
if isinstance(onchain_amount, Fraction):
|
if isinstance(onchain_amount, Fraction):
|
||||||
return (
|
return (
|
||||||
Decimal(onchain_amount.numerator)
|
Decimal(onchain_amount.numerator)
|
||||||
/ Decimal(onchain_amount.denominator)
|
/ Decimal(onchain_amount.denominator)
|
||||||
/ Decimal(10 ** self.decimals)
|
/ Decimal(10 ** self.decimals)
|
||||||
).quantize(Decimal(f"{1 / 10 ** self.decimals}"))
|
).quantize(Decimal(f"{1 / 10 ** self.decimals}"))
|
||||||
if quantize is True:
|
if quantize is True:
|
||||||
try:
|
try:
|
||||||
amount = (
|
amount = (
|
||||||
Decimal(str(onchain_amount)) / 10 ** self.decimals
|
Decimal(str(onchain_amount)) / 10 ** self.decimals
|
||||||
).quantize(Decimal(f"{1 / 10 ** self.decimals}"))
|
).quantize(Decimal(f"{1 / 10 ** self.decimals}"))
|
||||||
except InvalidOperation:
|
except InvalidOperation:
|
||||||
amount = Decimal(str(onchain_amount)) / Decimal(10 ** self.decimals)
|
amount = Decimal(str(onchain_amount)) / Decimal(10 ** self.decimals)
|
||||||
@@ -114,6 +114,8 @@ class Capability(IntEnum):
|
|||||||
ConstantPrice = auto()
|
ConstantPrice = auto()
|
||||||
TokenBalanceIndependent = auto()
|
TokenBalanceIndependent = auto()
|
||||||
ScaledPrice = auto()
|
ScaledPrice = auto()
|
||||||
|
HardLimits = auto()
|
||||||
|
MarginalPrice = auto()
|
||||||
|
|
||||||
|
|
||||||
class SynchronizerState(Enum):
|
class SynchronizerState(Enum):
|
||||||
|
|||||||
@@ -64,13 +64,6 @@ class ThirdPartyPool(BaseModel):
|
|||||||
|
|
||||||
trace: bool = False
|
trace: bool = False
|
||||||
|
|
||||||
hard_sell_limit: bool = False
|
|
||||||
"""
|
|
||||||
Whether the pool will revert if you attempt to sell more than the limit. Defaults to
|
|
||||||
False where it is assumed that exceeding the limit will provide a bad price but will
|
|
||||||
still succeed.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, **data):
|
def __init__(self, **data):
|
||||||
super().__init__(**data)
|
super().__init__(**data)
|
||||||
self._set_engine(data.get("engine", None))
|
self._set_engine(data.get("engine", None))
|
||||||
@@ -172,14 +165,14 @@ class ThirdPartyPool(BaseModel):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def get_amount_out(
|
def get_amount_out(
|
||||||
self: TPoolState,
|
self: TPoolState,
|
||||||
sell_token: EthereumToken,
|
sell_token: EthereumToken,
|
||||||
sell_amount: Decimal,
|
sell_amount: Decimal,
|
||||||
buy_token: EthereumToken,
|
buy_token: EthereumToken,
|
||||||
) -> tuple[Decimal, int, TPoolState]:
|
) -> tuple[Decimal, int, TPoolState]:
|
||||||
# if the pool has a hard limit and the sell amount exceeds that, simulate and
|
# if the pool has a hard limit and the sell amount exceeds that, simulate and
|
||||||
# raise a partial trade
|
# raise a partial trade
|
||||||
if self.hard_sell_limit:
|
if Capability.HardLimits in self.capabilities:
|
||||||
sell_limit = self.get_sell_amount_limit(sell_token, buy_token)
|
sell_limit = self.get_sell_amount_limit(sell_token, buy_token)
|
||||||
if sell_amount > sell_limit:
|
if sell_amount > sell_limit:
|
||||||
partial_trade = self._get_amount_out(sell_token, sell_limit, buy_token)
|
partial_trade = self._get_amount_out(sell_token, sell_limit, buy_token)
|
||||||
@@ -192,10 +185,10 @@ class ThirdPartyPool(BaseModel):
|
|||||||
return self._get_amount_out(sell_token, sell_amount, buy_token)
|
return self._get_amount_out(sell_token, sell_amount, buy_token)
|
||||||
|
|
||||||
def _get_amount_out(
|
def _get_amount_out(
|
||||||
self: TPoolState,
|
self: TPoolState,
|
||||||
sell_token: EthereumToken,
|
sell_token: EthereumToken,
|
||||||
sell_amount: Decimal,
|
sell_amount: Decimal,
|
||||||
buy_token: EthereumToken,
|
buy_token: EthereumToken,
|
||||||
) -> tuple[Decimal, int, TPoolState]:
|
) -> tuple[Decimal, int, TPoolState]:
|
||||||
trade, state_changes = self._adapter_contract.swap(
|
trade, state_changes = self._adapter_contract.swap(
|
||||||
cast(HexStr, self.id_),
|
cast(HexStr, self.id_),
|
||||||
@@ -223,7 +216,7 @@ class ThirdPartyPool(BaseModel):
|
|||||||
return buy_amount, trade.gas_used, new_state
|
return buy_amount, trade.gas_used, new_state
|
||||||
|
|
||||||
def _get_overwrites(
|
def _get_overwrites(
|
||||||
self, sell_token: EthereumToken, buy_token: EthereumToken, **kwargs
|
self, sell_token: EthereumToken, buy_token: EthereumToken, **kwargs
|
||||||
) -> dict[Address, dict[int, int]]:
|
) -> dict[Address, dict[int, int]]:
|
||||||
"""Get an overwrites dictionary to use in a simulation.
|
"""Get an overwrites dictionary to use in a simulation.
|
||||||
|
|
||||||
@@ -234,7 +227,7 @@ class ThirdPartyPool(BaseModel):
|
|||||||
return _merge(self.block_lasting_overwrites, token_overwrites)
|
return _merge(self.block_lasting_overwrites, token_overwrites)
|
||||||
|
|
||||||
def _get_token_overwrites(
|
def _get_token_overwrites(
|
||||||
self, sell_token: EthereumToken, buy_token: EthereumToken, max_amount=None
|
self, sell_token: EthereumToken, buy_token: EthereumToken, max_amount=None
|
||||||
) -> dict[Address, dict[int, int]]:
|
) -> dict[Address, dict[int, int]]:
|
||||||
"""Creates overwrites for a token.
|
"""Creates overwrites for a token.
|
||||||
|
|
||||||
@@ -297,13 +290,12 @@ class ThirdPartyPool(BaseModel):
|
|||||||
engine=self._engine,
|
engine=self._engine,
|
||||||
balances=self.balances,
|
balances=self.balances,
|
||||||
minimum_gas=self.minimum_gas,
|
minimum_gas=self.minimum_gas,
|
||||||
hard_sell_limit=self.hard_sell_limit,
|
|
||||||
balance_owner=self.balance_owner,
|
balance_owner=self.balance_owner,
|
||||||
stateless_contracts=self.stateless_contracts,
|
stateless_contracts=self.stateless_contracts,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_sell_amount_limit(
|
def get_sell_amount_limit(
|
||||||
self, sell_token: EthereumToken, buy_token: EthereumToken
|
self, sell_token: EthereumToken, buy_token: EthereumToken
|
||||||
) -> Decimal:
|
) -> Decimal:
|
||||||
"""
|
"""
|
||||||
Retrieves the sell amount of the given token.
|
Retrieves the sell amount of the given token.
|
||||||
|
|||||||
Reference in New Issue
Block a user