Skip to content

Workshop Code #4

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
68 changes: 10 additions & 58 deletions src/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import argparse
import decimal
import logging

import uvicorn

from dotenv import load_dotenv
from fastapi import FastAPI, Request
from pydantic import BaseSettings

from src.models.batch_auction import BatchAuction
from src.models.solver_args import SolverArgs
from src.util.numbers import decimal_to_str
from src.util.schema import (
BatchAuctionModel,
SettledBatchAuctionModel,
Expand Down Expand Up @@ -65,68 +66,19 @@ async def solve(problem: BatchAuctionModel, request: Request): # type: ignore
print("Parameters Supplied", solver_args)

# 1. Solve BatchAuction: update batch_auction with
# batch.solve()
batch.solve()

sample_output = {
"ref_token": batch.ref_token.value,
"orders": {order.order_id: order.as_dict() for order in batch.orders},
"prices": {
"0x4e3fbd56cd56c3e72c1403e103b45db9da5b9d2b": 10658174560450550,
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": 1000000000000000000,
"0xdac17f958d2ee523a2206206994597c13d831ec7": 314968423380884,
"orders": {
order.order_id: order.as_dict()
for order in batch.orders
if order.is_executed()
},
"amms": {
"6": {
"kind": "ConstantProduct",
"reserves": {
"0x4e3fbd56cd56c3e72c1403e103b45db9da5b9d2b": "151047198637918194794625",
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": "1615731604381456130940",
},
"fee": "0.003",
"cost": {
"amount": "1668264347574664",
"token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
},
"mandatory": False,
"id": "6",
"execution": [
{
"buy_token": "0x4e3fbd56cd56c3e72c1403e103b45db9da5b9d2b",
"exec_buy_amount": 4416092913242591886,
"sell_token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"exec_sell_amount": 47095265163205056,
"exec_sell_amount_nfee": 47236971948467672,
"liquidity_fee": 1.3248278739727776e16,
"exec_plan": {"position": 0, "sequence": 0},
}
],
},
"3": {
"kind": "ConstantProduct",
"reserves": {
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": "10052659476364989570713",
"0xdac17f958d2ee523a2206206994597c13d831ec7": "31939939882438",
},
"fee": "0.003",
"cost": {
"amount": "1668264347574664",
"token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
},
"mandatory": False,
"id": "3",
"execution": [
{
"buy_token": "0xdac17f958d2ee523a2206206994597c13d831ec7",
"exec_buy_amount": 55272822,
"sell_token": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"exec_sell_amount": 17344146155103392,
"exec_sell_amount_nfee": 17396335070270996,
"liquidity_fee": 165818.46600000001,
"exec_plan": {"position": 1, "sequence": 0},
}
],
},
"prices": {
str(key): decimal_to_str(value) for key, value in batch.prices.items()
},
"amms": {},
}
return sample_output

Expand Down
24 changes: 23 additions & 1 deletion src/models/batch_auction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from decimal import Decimal
from typing import Any, Optional

from src.models.order import Order, OrdersSerializedType
from src.models.order import Order, OrderMatchType, OrdersSerializedType
from src.models.token import (
Token,
TokenInfo,
Expand Down Expand Up @@ -154,6 +154,28 @@ def default_ref_token_price(self) -> Decimal:

def solve(self) -> None:
"""Solve Batch"""
orders = self.orders
for i in range(len(orders) - 1):
for j in range(i + 1, len(orders)):
order_i, order_j = orders[i], orders[j]
if order_i.match_type(order_j) == OrderMatchType.BOTH_FILLED:
order_i.execute(
buy_amount_value=order_j.sell_amount,
sell_amount_value=order_i.sell_amount,
)
order_j.execute(
buy_amount_value=order_i.sell_amount,
sell_amount_value=order_j.sell_amount,
)
# For sell Orders:
# executedBuyAmount = executedSellAmount.mul(sellPrice).ceilDiv(buyPrice)
token_a = self.token_info(order_i.sell_token)
token_b = self.token_info(order_i.buy_token)
# This is the sellPrice for order i
self.prices[token_a.token] = order_j.sell_amount
# This is the buyPrice of order i
self.prices[token_b.token] = order_i.sell_amount
return

#################################
# SOLUTION PROCESSING METHODS #
Expand Down