From 03274887346b68601257888193ffb8541a32494f Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Tue, 19 Apr 2022 16:31:31 +0200 Subject: [PATCH 1/3] add solve method from workshop --- src/_server.py | 64 +++---------------------------------- src/models/batch_auction.py | 24 +++++++++++++- 2 files changed, 28 insertions(+), 60 deletions(-) diff --git a/src/_server.py b/src/_server.py index e323c2a..2c8249a 100644 --- a/src/_server.py +++ b/src/_server.py @@ -6,6 +6,7 @@ import argparse import decimal import logging +from src.util.numbers import decimal_to_str import uvicorn from dotenv import load_dotenv @@ -65,68 +66,13 @@ 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, - }, - "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}, - } - ], - }, - }, + "orders": {order.order_id: order.as_dict() for order in batch.orders if order.is_executed()}, + "prices": {str(key): decimal_to_str(value) for key, value in batch.prices.items()}, + "amms": {}, } return sample_output diff --git a/src/models/batch_auction.py b/src/models/batch_auction.py index 8852afd..a5938dd 100644 --- a/src/models/batch_auction.py +++ b/src/models/batch_auction.py @@ -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, @@ -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_j.buy_amount + ) + order_j.execute( + buy_amount_value=order_i.sell_amount, + sell_amount_value=order_i.buy_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 # From fd6cc9595912948f37caf60f659ab49bb9a55f34 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Tue, 19 Apr 2022 16:36:56 +0200 Subject: [PATCH 2/3] revert accidental changes --- src/models/batch_auction.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models/batch_auction.py b/src/models/batch_auction.py index a5938dd..343f465 100644 --- a/src/models/batch_auction.py +++ b/src/models/batch_auction.py @@ -156,16 +156,16 @@ def solve(self) -> None: """Solve Batch""" orders = self.orders for i in range(len(orders) - 1): - for j in range(i+1, len(orders)): + 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_j.buy_amount + 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_i.buy_amount + sell_amount_value=order_j.sell_amount, ) # For sell Orders: # executedBuyAmount = executedSellAmount.mul(sellPrice).ceilDiv(buyPrice) From 4d0a1ebb1cee024a6e54e894eb8e5ada08f169b3 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Wed, 20 Apr 2022 15:39:34 +0200 Subject: [PATCH 3/3] fix import order --- .gitignore | 3 +++ src/_server.py | 14 ++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 560fc40..552f97b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ */__pycache__ venv/ .env + +.idea/ +.vscode/ diff --git a/src/_server.py b/src/_server.py index 2c8249a..4b877ce 100644 --- a/src/_server.py +++ b/src/_server.py @@ -6,15 +6,15 @@ import argparse import decimal import logging -from src.util.numbers import decimal_to_str - 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, @@ -70,8 +70,14 @@ async def solve(problem: BatchAuctionModel, request: Request): # type: ignore sample_output = { "ref_token": batch.ref_token.value, - "orders": {order.order_id: order.as_dict() for order in batch.orders if order.is_executed()}, - "prices": {str(key): decimal_to_str(value) for key, value in batch.prices.items()}, + "orders": { + order.order_id: order.as_dict() + for order in batch.orders + if order.is_executed() + }, + "prices": { + str(key): decimal_to_str(value) for key, value in batch.prices.items() + }, "amms": {}, } return sample_output