-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
174 lines (150 loc) · 5.25 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
from web3 import Web3
from typing import ClassVar, Dict, List
from dataclasses import dataclass
w3 = Web3()
@dataclass(frozen=True)
class Contracts:
HELPER_CONTRACT: ClassVar[str] = "0x3FF0041A614A9E6Bf392cbB961C97DA214E9CB31"
USDC_WETH_POOL: ClassVar[str] = "0xf08d4dea369c456d26a3168ff0024b904f2d8b91"
@dataclass(frozen=True)
class Tokens:
USDC: ClassVar[str] = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48".lower()
WETH: ClassVar[str] = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".lower()
@dataclass(frozen=True)
class BCowPool:
ADDRESS: str
def checksum(self) -> str:
return w3.to_checksum_address(self.ADDRESS)
@dataclass(frozen=True)
class CoWAmmOrderData:
sellToken: str
buyToken: str
receiver: str
sellAmount: int
buyAmount: int
validTo: int
appData: bytes
feeAmount: int
kind: bytes
partiallyFillable: bool
signature: bytes
usdc_amount: int
weth_amount: int
usdc_price: int
weth_price: int
@staticmethod
def from_order_response(prices: List[str], resp):
# TODO: Fix sell token & buy token
if resp[0].lower() == Tokens.USDC:
usdc_amount = resp[3]
elif resp[0].lower() == Tokens.WETH:
weth_amount = resp[3]
if resp[1].lower() == Tokens.USDC:
usdc_amount = resp[4]
elif resp[1].lower() == Tokens.WETH:
weth_amount = resp[4]
return CoWAmmOrderData(
sellToken=resp[0].lower(),
buyToken=resp[1].lower(),
receiver=resp[2],
sellAmount=resp[3],
buyAmount=resp[4],
validTo=resp[5],
appData=resp[6],
feeAmount=resp[7],
kind=resp[8],
partiallyFillable=resp[9],
signature=resp[10],
usdc_amount=usdc_amount,
weth_amount=weth_amount,
usdc_price=int(prices[0]),
weth_price=int(prices[1])
)
@dataclass(frozen=True)
class UCP:
prices: Dict[str, int]
def __getitem__(self, address: str) -> int:
return self.prices[address]
@classmethod
def from_lists(cls, tokens: List[str], prices: List[int]) -> 'UCP':
if len(tokens) != len(prices):
raise ValueError("Cannot zip different lengths")
prices = [int(price) for price in prices]
tokens_prices = dict()
for token, price in zip(tokens, prices):
# The first occurence of a price in list is the clearing price
if token not in tokens_prices:
tokens_prices[token] = price
return cls(prices=tokens_prices)
@dataclass(frozen=True)
class Trade:
BUY_TOKEN: str
SELL_TOKEN: str
BUY_AMOUNT: int
SELL_AMOUNT: int
BUY_PRICE: int
SELL_PRICE: int
IS_USDC_WETH: bool
IS_WETH_USDC: bool
def get_limit_price_in_usd(self):
if self.IS_USDC_WETH:
return 1e18 * self.BUY_PRICE / self.SELL_PRICE * 1e-6
elif self.IS_WETH_USDC:
return 1e18 * self.SELL_PRICE / self.BUY_PRICE * 1e-6
else:
return None
def get_limit_price_in_eth(self):
if self.IS_USDC_WETH:
return 1e6 * self.SELL_PRICE / self.BUY_PRICE * 1e-18
elif self.IS_WETH_USDC:
return 1e6 * self.BUY_PRICE / self.SELL_PRICE * 1e-18
else:
return None
def get_limit_price_in_wei(self):
if self.IS_USDC_WETH:
return 1e6 * self.SELL_PRICE / self.BUY_PRICE
elif self.IS_WETH_USDC:
return 1e6 * self.BUY_PRICE / self.SELL_PRICE
else:
return None
@dataclass(frozen=True)
class Trades:
trades: List[Trade]
isUsdcWeth: bool
isWethUsdc: bool
@classmethod
def from_lists(cls, tokens: str, prices: str, trades: str) -> 'Trades':
trades = json.loads(trades.replace(' ', ','))
tokens = tokens.strip('[]').split()
prices = prices.strip('[]').split()
trades_processed = []
isUsdcWeth = False
for trade in trades:
buy_token = tokens[int(trade['buyTokenIndex'])].lower()
sell_token = tokens[int(trade['sellTokenIndex'])].lower()
buy_amount = int(trade['buyAmount'])
sell_amount = int(trade['sellAmount'])
buy_price = int(prices[int(trade['buyTokenIndex'])])
sell_price = int(prices[int(trade['sellTokenIndex'])])
isUsdcWeth = False
isWethUsdc = False
if buy_token == Tokens.USDC and sell_token == Tokens.WETH:
isUsdcWeth = False
isWethUsdc = True
elif buy_token == Tokens.WETH and sell_token == Tokens.USDC:
isUsdcWeth = True
isWethUsdc = False
trades_processed.append(Trade(
BUY_TOKEN = buy_token,
SELL_TOKEN = sell_token,
BUY_AMOUNT = buy_amount,
SELL_AMOUNT = sell_amount,
BUY_PRICE = buy_price,
SELL_PRICE = sell_price,
IS_USDC_WETH = isUsdcWeth,
IS_WETH_USDC = isWethUsdc
))
return cls(trades=trades_processed, isUsdcWeth=isUsdcWeth, isWethUsdc=isWethUsdc)
def __iter__(self):
return iter(self.trades)