-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from bancorprotocol/balancer2
Balancer2
- Loading branch information
Showing
15 changed files
with
2,247 additions
and
790 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
base class for representing a generic curve in the context of the optimizer* | ||
(c) Copyright Bprotocol foundation 2023. | ||
Licensed under MIT | ||
*whilst the name of the file ("cpc") alludes to constant product curves, this is for | ||
historical reasons only; this class deal with generic curves | ||
NOTE: this class is not part of the API of the Carbon protocol, and you must expect breaking | ||
changes even in minor version updates. Use at your own risk. | ||
""" | ||
from abc import ABC, abstractmethod | ||
class CurveBase(ABC): | ||
""" | ||
base class for representing a generic curve in the context of the optimizer | ||
""" | ||
|
||
@abstractmethod | ||
def dxvecfrompvec_f(self, pvec, *, ignorebounds=False): | ||
""" | ||
get token holding vector xvec from price vector pvec | ||
:pvec: a dict containing all prices; the dict must contain the keys | ||
for tknx and for tkny and the associated value must be the respective | ||
price in any numeraire (only the ratio is used) | ||
:returns: token difference amounts as dict {tknx: dx, tkny: dy} | ||
""" | ||
raise NotImplementedError("dxvecfrompvec_f must be implemented by subclass") | ||
|
||
@abstractmethod | ||
def xvecfrompvec_f(self, pvec, *, ignorebounds=False): | ||
""" | ||
get change in token holding vector xvec, dxvec, from price vector pvec | ||
:pvec: a dict containing all prices; the dict must contain the keys | ||
for tknx and for tkny and the associated value must be the respective | ||
price in any numeraire (only the ratio is used) | ||
:returns: token amounts as dict {tknx: x, tkny: y} | ||
""" | ||
raise NotImplementedError("dxvecfrompvec_f must be implemented by subclass") | ||
|
||
@abstractmethod | ||
def invariant(self, include_target=False): | ||
""" | ||
returns the actual invariant of the curve (eg x*y for constant product) | ||
:include_target: if True, the target invariant returned in addition to the actual invariant | ||
:returns: invariant, or (invariant, target) | ||
""" | ||
raise NotImplementedError("invariant must be implemented by subclass") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,8 @@ | |
This module is still subject to active research, and comments and suggestions are welcome. | ||
The corresponding author is Stefan Loesch <[email protected]> | ||
""" | ||
__VERSION__ = "5.0" | ||
__DATE__ = "26/Jul/2023" | ||
__VERSION__ = "5.1" | ||
__DATE__ = "25/Aug/2023" | ||
|
||
from dataclasses import dataclass, field, fields, asdict, astuple, InitVar | ||
import pandas as pd | ||
|
@@ -211,7 +211,13 @@ def dtknfromp_f(p, *, islog10=True, asdct=False, quiet=False): | |
print(f"\n[dtknfromp_f] =====================>>>") | ||
print(f"prices={p}") | ||
print(f"tokens={tokens_t}") | ||
|
||
|
||
# pvec is dict {tkn -> (log) price} for all tokens in p | ||
pvec = {tkn: p_ for tkn, p_ in zip(tokens_t, p)} | ||
pvec[targettkn] = 1 | ||
if P("debug") and not quiet: | ||
print(f"pvec={pvec}") | ||
|
||
sum_by_tkn = {t: 0 for t in alltokens_s} | ||
for pair, (tknb, tknq) in zip(pairs, pairs_t): | ||
if get(p, tokens_ix.get(tknq)) > 0: | ||
|
@@ -221,8 +227,14 @@ def dtknfromp_f(p, *, islog10=True, asdct=False, quiet=False): | |
price = 1 | ||
curves = curves_by_pair[pair] | ||
c0 = curves[0] | ||
dxdy = tuple(dxdy_f(c.dxdyfromp_f(price)) for c in curves) | ||
#dxdy = tuple(dxdy_f(c.dxdyfromp_f(price)) for c in curves) | ||
dxvecs = (c.dxvecfrompvec_f(pvec) for c in curves) | ||
|
||
if P("debug2") and not quiet: | ||
dxdy = tuple(dxdy_f(c.dxdyfromp_f(price)) for c in curves) | ||
# TODO: rewrite this using the dxvec | ||
# there is no need to extract dy dx; just iterate over dict | ||
# however not urgent because this is debug code | ||
print(f"\n{c0.pairp} --->>") | ||
print(f" price={price:,.4f}, 1/price={1/price:,.4f}") | ||
for r, c in zip(dxdy, curves): | ||
|
@@ -233,12 +245,16 @@ def dtknfromp_f(p, *, islog10=True, asdct=False, quiet=False): | |
print(s) | ||
print(f"<<--- {c0.pairp}") | ||
|
||
sumdx, sumdy = sum(dxdy) | ||
sum_by_tkn[tknq] += sumdy | ||
sum_by_tkn[tknb] += sumdx | ||
# old code from dxdy = tuple(dxdy_f(c.dxdyfromp_f(price)) for c in curves) | ||
# sumdx, sumdy = sum(dxdy) | ||
# sum_by_tkn[tknq] += sumdy | ||
# sum_by_tkn[tknb] += sumdx | ||
for dxvec in dxvecs: | ||
for tkn, dx_ in dxvec.items(): | ||
sum_by_tkn[tkn] += dx_ | ||
|
||
if P("debug") and not quiet: | ||
print(f"pair={c0.pairp}, {sumdy:,.4f} {tn(tknq)}, {sumdx:,.4f} {tn(tknb)}, price={price:,.4f} {tn(tknq)} per {tn(tknb)} [{len(curves)} funcs]") | ||
# if P("debug") and not quiet: | ||
# print(f"pair={c0.pairp}, {sumdy:,.4f} {tn(tknq)}, {sumdx:,.4f} {tn(tknb)}, price={price:,.4f} {tn(tknq)} per {tn(tknb)} [{len(curves)} funcs]") | ||
|
||
result = tuple(sum_by_tkn[t] for t in tokens_t) | ||
if P("debug") and not quiet: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.