Skip to content

Commit

Permalink
remane variable for consistency and less confusion (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Mar 24, 2023
1 parent 61037ac commit cfca9e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
34 changes: 18 additions & 16 deletions src/fetch/payouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__( # pylint: disable=too-many-arguments
payment_eth: int,
secondary_reward_eth: int,
slippage_eth: int,
reward_cow: int,
primary_reward_cow: int,
secondary_reward_cow: int,
):
assert exec_cost >= 0, f"invalid execution cost {exec_cost}"
Expand All @@ -85,7 +85,7 @@ def __init__( # pylint: disable=too-many-arguments
self.exec_cost = exec_cost
self.payment_eth = payment_eth
self.slippage_eth = slippage_eth
self.reward_cow = reward_cow
self.primary_reward_cow = primary_reward_cow
self.secondary_reward_eth = secondary_reward_eth
self.secondary_reward_cow = secondary_reward_cow

Expand All @@ -109,7 +109,7 @@ def from_series(cls, frame: Series) -> RewardAndPenaltyDatum:
reward_target=Address(reward_target),
payment_eth=int(frame["payment_eth"]),
slippage_eth=slippage,
reward_cow=int(frame["reward_cow"]),
primary_reward_cow=int(frame["reward_cow"]),
exec_cost=int(frame["execution_cost_eth"]),
secondary_reward_eth=int(frame["secondary_reward_eth"]),
secondary_reward_cow=int(frame["secondary_reward_cow"]),
Expand All @@ -121,7 +121,7 @@ def total_outgoing_eth(self) -> int:

def total_cow_reward(self) -> int:
"""Total outgoing COW token reward"""
return self.reward_cow + self.secondary_reward_cow
return self.primary_reward_cow + self.secondary_reward_cow

def total_eth_reward(self) -> int:
"""Total outgoing ETH reward"""
Expand All @@ -140,21 +140,23 @@ def as_payouts(self) -> list[Transfer]:
"""
# We do not handle overdraft scenario here!
assert not self.is_overdraft()
reward_eth = int(self.total_eth_reward())
reward_cow = int(self.total_cow_reward())
total_eth_reward = int(self.total_eth_reward())
total_cow_reward = int(self.total_cow_reward())

reimbursement_eth = int(self.exec_cost + self.slippage_eth)
# We do not have access to token conversion here, but we do have other converted values
# x_eth:x_cow = y_eth:y_cow --> y_cow = y_eth * x_cow / x_eth
reimbursement_cow = (
(reimbursement_eth * reward_cow) // reward_eth if reward_eth != 0 else 0
(reimbursement_eth * total_cow_reward) // total_eth_reward
if total_eth_reward != 0
else 0
)

if reimbursement_eth > 0 > reward_cow:
if reimbursement_eth > 0 > total_cow_reward:
# If the total payment is positive but the total rewards are negative,
# pay the total payment in ETH. The total payment corresponds to reimbursement,
# reduced by the negative reward.
# That assertion was not necessary because:
# Note that;
# reimbursement_eth + reward_eth
# = self.total_eth_reward() + self.exec_cost + self.slippage_eth
# = self.payment_eth + self.secondary_reward_eth + self.slippage_eth
Expand All @@ -165,14 +167,14 @@ def as_payouts(self) -> list[Transfer]:
Transfer(
token=None,
recipient=self.solver,
amount_wei=reimbursement_eth + reward_eth,
amount_wei=reimbursement_eth + total_eth_reward,
)
]
if reimbursement_eth + reward_eth > 0
if reimbursement_eth + total_eth_reward > 0
else []
)

if reimbursement_eth < 0 < reward_cow:
if reimbursement_eth < 0 < total_cow_reward:
# If the total payment is positive but the total reimbursement is negative,
# pay the total payment in COW. The total payment corresponds to a payment of rewards,
# reduced by the negative reimbursement.
Expand All @@ -181,10 +183,10 @@ def as_payouts(self) -> list[Transfer]:
Transfer(
token=Token(COW_TOKEN_ADDRESS),
recipient=self.solver,
amount_wei=reimbursement_cow + reward_cow,
amount_wei=reimbursement_cow + total_cow_reward,
)
]
if reimbursement_cow + reward_cow > 0
if reimbursement_cow + total_cow_reward > 0
else []
)

Expand All @@ -206,12 +208,12 @@ def as_payouts(self) -> list[Transfer]:
Transfer(
token=Token(COW_TOKEN_ADDRESS),
recipient=self.reward_target,
amount_wei=reward_cow,
amount_wei=total_cow_reward,
)
)
except AssertionError:
logging.warning(
f"Invalid COW Transfer {self.solver} with amount={reward_cow}"
f"Invalid COW Transfer {self.solver} with amount={total_cow_reward}"
)

return result
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_payouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def sample_record(
reward_target=self.reward_target,
payment_eth=payment,
exec_cost=cost,
reward_cow=(payment - cost) * self.conversion_rate,
primary_reward_cow=(payment - cost) * self.conversion_rate,
secondary_reward_eth=participation,
secondary_reward_cow=participation * self.conversion_rate,
slippage_eth=slippage,
Expand Down

0 comments on commit cfca9e0

Please sign in to comment.