Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit 78f731f

Browse files
authored
Merge pull request #315 from bancorprotocol/solidly_1
Solidly Analytics
2 parents 3402bbc + f62b3bc commit 78f731f

28 files changed

+20904
-3
lines changed

fastlane_bot/testing.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
66
from fastlane_bot.testing import *
77
"""
8-
__VERSION__ = "1.2"
9-
__DATE__ = "07/May/2023"
8+
__VERSION__ = "1.3"
9+
__DATE__ = "15/Jan/2024"
1010

1111
import math as m
1212
import numpy as np
@@ -112,6 +112,59 @@ def _require_version(rl, al):
112112
elif r < a:
113113
return True
114114
return True
115+
116+
class Timer():
117+
"""
118+
times functions calls; timer as arg, kwargs; timer1/2 have 1/2 args respectively
115119
116120
117-
print("imported m, np, pd, plt, os, sys, decimal; defined iseq, raises, require")
121+
USAGE
122+
Timer.t(func, *args, *kwargs, N=100_000)
123+
Timer.t1(func, args, N=100_000)
124+
Timer.t2(func, arg1, arg2, N=100_000)
125+
126+
Note: the default value for N can be changed by using a derived class, eg:
127+
128+
class MyTimer(Timer):
129+
N = 1_000_000
130+
MyTimer.t(func, *args, *kwargs)
131+
"""
132+
N = 1_000_000
133+
134+
@classmethod
135+
def timer(cls, func, *args, N=None, **kwargs):
136+
"""times the calls to func; func is called with args and kwargs; returns time in msec per 1m calls"""
137+
if N is None:
138+
N = cls.N
139+
start_time = time.time()
140+
for _ in range(N):
141+
func(*args, **kwargs)
142+
end_time = time.time()
143+
return (end_time - start_time)/N*1_000_000*1000
144+
145+
@classmethod
146+
def timer1(cls, func, arg, N=None):
147+
"""times the calls to func; func is called with arg; returns time in msec per 1m calls"""
148+
if N is None:
149+
N = cls.N
150+
start_time = time.time()
151+
for _ in range(N):
152+
func(arg)
153+
end_time = time.time()
154+
return (end_time - start_time)/N*1_000_000*1000
155+
156+
@classmethod
157+
def timer2(cls, func, arg1, arg2, N=None):
158+
"""times the calls to func; func is called with arg1, arg2; returns time in msec per 1m calls"""
159+
if N is None:
160+
N = cls.N
161+
start_time = time.time()
162+
for _ in range(N):
163+
func(arg1, arg2)
164+
end_time = time.time()
165+
return (end_time - start_time)/N*1_000_000*1000
166+
timer = Timer.timer
167+
timer1 = Timer.timer1
168+
timer2 = Timer.timer2
169+
170+
print("imported m, np, pd, plt, os, sys, decimal; defined iseq, raises, require, Timer")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
collection of tools for AMM invariant functions
3+
4+
(c) Copyright Bprotocol foundation 2024.
5+
Licensed under MIT
6+
"""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
object representing the Bancor (constant product) AMM invariant
3+
4+
(c) Copyright Bprotocol foundation 2024.
5+
Licensed under MIT
6+
"""
7+
__VERSION__ = '0.9'
8+
__DATE__ = "18/Jan/2024"
9+
10+
# import decimal as d
11+
# D = d.Decimal
12+
# import math as m
13+
14+
from .invariant import Invariant, dataclass
15+
from .functions import Function
16+
17+
@dataclass(frozen=True)
18+
class BancorSwapFunction(Function):
19+
"""represents the Bancor AMM swap function y(x,k)=k/x"""
20+
__VERSION__ = __VERSION__
21+
__DATE__ = __DATE__
22+
23+
k: float
24+
25+
def f(self, x):
26+
return self.k / x
27+
28+
@dataclass
29+
class BancorInvariant(Invariant):
30+
"""represents the Bancor invariant function"""
31+
__VERSION__ = __VERSION__
32+
__DATE__ = __DATE__
33+
34+
def __post_init__(self):
35+
self._y_Func_class = BancorSwapFunction
36+
37+
def k_func(self, x, y):
38+
"""Bancor invariant function k(x,y)=x*y"""
39+
return x*y
40+
41+
42+
43+
44+

0 commit comments

Comments
 (0)