Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(simulation-py): make price() function optional #126

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions tycho_simulation_py/python/tycho_simulation_py/evm/pool_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,53 @@ def _set_engine(self):
self._engine = engine

def _set_marginal_prices(self):
"""Set the spot prices for this pool.
"""Set the spot prices for this pool."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add more docs on this? There are important design decisions that led to this implementation

if Capability.PriceFunction in self.capabilities:
for t0, t1 in itertools.permutations(self.tokens, 2):
sell_amount = t0.to_onchain_amount(
self.get_sell_amount_limit(t0, t1) * Decimal("0.01")
)
frac = self._adapter_contract.price(
cast(HexStr, self.id_),
t0,
t1,
[sell_amount],
block=self.block,
overwrites=self._get_overwrites(t0, t1),
)[0]
if Capability.ScaledPrices in self.capabilities:
self.marginal_prices[(t0, t1)] = frac_to_decimal(frac)
else:
scaled = frac * Fraction(10**t0.decimals, 10**t1.decimals)
self.marginal_prices[(t0, t1)] = frac_to_decimal(scaled)
else:

We currently require the price function capability for now.
"""
self._ensure_capability(Capability.PriceFunction)
for t0, t1 in itertools.permutations(self.tokens, 2):
sell_amount = t0.to_onchain_amount(
self.get_sell_amount_limit(t0, t1) * Decimal("0.01")
)
frac = self._adapter_contract.price(
cast(HexStr, self.id_),
t0,
t1,
[sell_amount],
block=self.block,
overwrites=self._get_overwrites(t0,t1),
)[0]
if Capability.ScaledPrices in self.capabilities:
self.marginal_prices[(t0, t1)] = frac_to_decimal(frac)
else:
scaled = frac * Fraction(10**t0.decimals, 10**t1.decimals)
self.marginal_prices[(t0, t1)] = frac_to_decimal(scaled)
def swap(
sell_token: EthereumToken,
sell_amount: Decimal,
buy_token: EthereumToken,
) -> Decimal:
overwrites = self._get_overwrites(sell_token, buy_token)
trade, _ = self._adapter_contract.swap(
cast(HexStr, self.id_),
sell_token,
buy_token,
False,
sell_token.to_onchain_amount(sell_amount),
block=self.block,
overwrites=overwrites,
)

buy_amount = buy_token.from_onchain_amount(trade.received_amount)

return buy_amount

for t0, t1 in itertools.permutations(self.tokens, 2):
x1 = self.get_sell_amount_limit(t0, t1) * Decimal("0.01")
x2 = x1 + (x1 / 100)
y1 = swap(t0, x1, t1)
y2 = swap(t0, x2, t1)
self.marginal_prices[(t0, t1)] = (y2 - y1) / (x2 - x1)

def _ensure_capability(self, capability: Capability):
"""Ensures the protocol/adapter implement a certain capability."""
Expand Down
Loading