-
Notifications
You must be signed in to change notification settings - Fork 10
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
add features and tests #2
Merged
Merged
Changes from 61 commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
44a7bf8
bump gauge version
bout3fiddy 42bff31
feat: add exchange_received_split experimental idea; fix: upkeep xcp …
bout3fiddy b0aecd0
add docstrings to exchange_received_split
bout3fiddy be077a2
add distinction between split_in and split_out
bout3fiddy d3a6282
fix: isqrt precision issues; tests
bout3fiddy 58ff178
fix extra accounting of dx received
bout3fiddy d879e4c
add cicd
bout3fiddy ca4317c
update boa
bout3fiddy 7797ad4
fix path
bout3fiddy 39963c8
update limits in tests
bout3fiddy d2e7fec
remove print; ignore flake8
bout3fiddy aea8d70
add fuzz
bout3fiddy 97a644b
begin stateful tests
bout3fiddy 6ab6f4a
fix: rearrange calcs to avoid overflow
bout3fiddy 3c3e29a
fix: ramp A gamma with old xp[i] value and not new one
bout3fiddy 4d76e11
claim admin virtual balances as well
bout3fiddy e601564
only proportion goes to admin lp virtual balance
bout3fiddy 71c58d6
introduce spot portfolio and account for portfolio balance in _transf…
bout3fiddy 05d0b4f
only msg.sender can deposit or withdraw from spot wallet
bout3fiddy f901e54
Change range of params for get_y unitary test.
3be3a0f
fix: error using cached price instead of uncached for price oracle
bout3fiddy 244144f
Merge branch 'feat/tests' of https://github.com/curvefi/twocrypto-ng …
bout3fiddy 5eceae4
change max limits for gamma and A (found by filipp after rigorous tes…
bout3fiddy 53a8e46
use pragma instead
bout3fiddy 4352c72
fix: redesign exchange_received_split to relax out amounts
bout3fiddy 026afab
fix: use correct amounts
bout3fiddy 5500457
goodbye spot wallet idea. you were good but not good enough
bout3fiddy 0e33774
fix limits and remove spot wallet tests
bout3fiddy d102eaf
remove ape
bout3fiddy 6a22fe4
remove mentions of tricrypto
bout3fiddy 70c89a4
remove k reference
bout3fiddy 4cabeeb
Fix get_y operation order for precision and overflow prevention.
b0079e3
add test for fixed precision issues
bout3fiddy 2f3f931
pack last timestamp into one uint256 and add tests
bout3fiddy 52c03cb
remove asserts because packed2 values are timestamps so no need to do…
bout3fiddy 407fd0f
claim fees before remove liq one
bout3fiddy 91122cd
Add multiple get_y test instances, uncomment asserts.
2ff7b80
Merge branch 'feat/tests' of github.com:curvefi/twocrypto-ng into fea…
92a0435
unsafe add nonce
bout3fiddy ef3491c
Merge branch 'feat/tests' of https://github.com/curvefi/twocrypto-ng …
bout3fiddy fe68f2c
reduce unnecessary storage reads in some conditions in _claim_admin_fees
bout3fiddy 6fce03c
use unsafe sub for +ve timedeltas
bout3fiddy f4ab108
gas: remove unnecessary re-read of storage in remove_liquidity
bout3fiddy eb4acb3
add fuzz to this test
bout3fiddy 616a278
claim fees before deposit
bout3fiddy ec5b122
remove double mul of precisions[0]
bout3fiddy 6d80da0
fix: remove double mul correctly :P
bout3fiddy ba1dc63
remove call to reduction coefficient
bout3fiddy a3388cd
fix: remove double precision division
bout3fiddy d500585
fix: use correct method_id
bout3fiddy 7cabdbc
use correct method
bout3fiddy fd2e505
call ma_time instead of ma_half_time'
bout3fiddy d8021ec
check A and gamma in implementation constructor and not in factory to…
bout3fiddy c81dbdc
use DynArrays and save a bunch of gas while deploying pool
bout3fiddy 5ae137d
use immutables
bout3fiddy f9958d3
log new approval event
bout3fiddy 4a95cc1
unsafe divs and muls in get_y
bout3fiddy 0692ba9
add one unsafe add
bout3fiddy ac588b0
gas: newton_D
bout3fiddy 7875cae
check nonzero math impl
bout3fiddy 9487057
add salt to eip712 tyephash
bout3fiddy adfed8d
add more newton_D checks
bout3fiddy 5a03822
fix: some errors; github workflow
bout3fiddy 359951f
fix: return int instead of array of Decimal types
bout3fiddy e931743
Update test_newton_D_ref.py
cf74f17
Fix newton_D_ref test params.
def5008
Fix newton_D_ref test param limits.
75c46c1
Merge branch 'feat/tests' of github.com:curvefi/twocrypto-ng into fea…
2b46e41
Update newton_D test.
4cc4b74
fix test MAX_A lim and fix order of unpacking in amm impl constructor
bout3fiddy ab2080b
remove admin fee auto claim in add_liquidity
bout3fiddy 7092010
pack precisions and name packed gamma and A var better
bout3fiddy 036a8e8
fix math tests
bout3fiddy 6246ad9
stateful tests and other tests
bout3fiddy 50b381b
fix: lp price isqrt input
bout3fiddy b8601ae
fix: get_p test
bout3fiddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,11 @@ | ||
name: pre-commit | ||
|
||
on: [pull_request, push] | ||
|
||
jobs: | ||
pre-commit: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-python@v4 | ||
- uses: pre-commit/[email protected] |
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,30 @@ | ||
name: unit-tests-boa | ||
|
||
on: ["push", "pull_request"] | ||
|
||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
jobs: | ||
boa-tests: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Cache Compiler Installations | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
~/.vvm | ||
key: compiler-cache | ||
|
||
- name: Setup Python 3.10.4 | ||
bout3fiddy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.10.4 | ||
|
||
- name: Install Requirements | ||
run: pip install -r requirements.txt | ||
|
||
- name: Run Tests | ||
run: python -m pytest tests/ -n auto |
This file was deleted.
Oops, something went wrong.
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,195 @@ | ||
# @version 0.3.10 | ||
|
||
|
||
N_COINS: constant(uint256) = 2 | ||
PRECISION: constant(uint256) = 10 ** 18 # The precision to convert to | ||
A_MULTIPLIER: constant(uint256) = 10000 | ||
|
||
MIN_GAMMA: constant(uint256) = 10**10 | ||
MAX_GAMMA: constant(uint256) = 2 * 10**15 | ||
|
||
MIN_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER / 10 | ||
MAX_A: constant(uint256) = N_COINS**N_COINS * A_MULTIPLIER * 1000 | ||
|
||
|
||
@internal | ||
@pure | ||
def _newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256: | ||
""" | ||
Calculating x[i] given other balances x[0..N_COINS-1] and invariant D | ||
ANN = A * N**N | ||
""" | ||
# Safety checks | ||
assert ANN > MIN_A - 1 and ANN < MAX_A + 1 # dev: unsafe values A | ||
assert gamma > MIN_GAMMA - 1 and gamma < MAX_GAMMA + 1 # dev: unsafe values gamma | ||
assert D > 10**17 - 1 and D < 10**15 * 10**18 + 1 # dev: unsafe values D | ||
|
||
x_j: uint256 = x[1 - i] | ||
y: uint256 = D**2 / (x_j * N_COINS**2) | ||
K0_i: uint256 = (10**18 * N_COINS) * x_j / D | ||
# S_i = x_j | ||
|
||
# frac = x_j * 1e18 / D => frac = K0_i / N_COINS | ||
# assert (K0_i > 10**16*N_COINS - 1) and (K0_i < 10**20*N_COINS + 1) # dev: unsafe values x[i] | ||
|
||
# x_sorted: uint256[N_COINS] = x | ||
# x_sorted[i] = 0 | ||
# x_sorted = self.sort(x_sorted) # From high to low | ||
# x[not i] instead of x_sorted since x_soted has only 1 element | ||
|
||
convergence_limit: uint256 = max(max(x_j / 10**14, D / 10**14), 100) | ||
|
||
for j in range(255): | ||
y_prev: uint256 = y | ||
|
||
K0: uint256 = K0_i * y * N_COINS / D | ||
S: uint256 = x_j + y | ||
|
||
_g1k0: uint256 = gamma + 10**18 | ||
if _g1k0 > K0: | ||
_g1k0 = _g1k0 - K0 + 1 | ||
else: | ||
_g1k0 = K0 - _g1k0 + 1 | ||
|
||
# D / (A * N**N) * _g1k0**2 / gamma**2 | ||
mul1: uint256 = 10**18 * D / gamma * _g1k0 / gamma * _g1k0 * A_MULTIPLIER / ANN | ||
|
||
# 2*K0 / _g1k0 | ||
mul2: uint256 = 10**18 + (2 * 10**18) * K0 / _g1k0 | ||
|
||
yfprime: uint256 = 10**18 * y + S * mul2 + mul1 | ||
_dyfprime: uint256 = D * mul2 | ||
if yfprime < _dyfprime: | ||
y = y_prev / 2 | ||
continue | ||
else: | ||
yfprime -= _dyfprime | ||
fprime: uint256 = yfprime / y | ||
|
||
# y -= f / f_prime; y = (y * fprime - f) / fprime | ||
# y = (yfprime + 10**18 * D - 10**18 * S) // fprime + mul1 // fprime * (10**18 - K0) // K0 | ||
y_minus: uint256 = mul1 / fprime | ||
y_plus: uint256 = (yfprime + 10**18 * D) / fprime + y_minus * 10**18 / K0 | ||
y_minus += 10**18 * S / fprime | ||
|
||
if y_plus < y_minus: | ||
y = y_prev / 2 | ||
else: | ||
y = y_plus - y_minus | ||
|
||
diff: uint256 = 0 | ||
if y > y_prev: | ||
diff = y - y_prev | ||
else: | ||
diff = y_prev - y | ||
if diff < max(convergence_limit, y / 10**14): | ||
frac: uint256 = y * 10**18 / D | ||
assert (frac > 10**16 - 1) and (frac < 10**20 + 1) # dev: unsafe value for y | ||
return y | ||
|
||
raise "Did not converge" | ||
|
||
@external | ||
@pure | ||
def newton_y(ANN: uint256, gamma: uint256, x: uint256[N_COINS], D: uint256, i: uint256) -> uint256: | ||
return self._newton_y(ANN, gamma, x, D, i) | ||
|
||
|
||
@internal | ||
@pure | ||
def geometric_mean(unsorted_x: uint256[N_COINS], sort: bool) -> uint256: | ||
""" | ||
(x[0] * x[1] * ...) ** (1/N) | ||
""" | ||
x: uint256[N_COINS] = unsorted_x | ||
if sort and x[0] < x[1]: | ||
x = [unsorted_x[1], unsorted_x[0]] | ||
D: uint256 = x[0] | ||
diff: uint256 = 0 | ||
for i in range(255): | ||
D_prev: uint256 = D | ||
# tmp: uint256 = 10**18 | ||
# for _x in x: | ||
# tmp = tmp * _x / D | ||
# D = D * ((N_COINS - 1) * 10**18 + tmp) / (N_COINS * 10**18) | ||
# line below makes it for 2 coins | ||
D = unsafe_div(D + x[0] * x[1] / D, N_COINS) | ||
if D > D_prev: | ||
diff = unsafe_sub(D, D_prev) | ||
else: | ||
diff = unsafe_sub(D_prev, D) | ||
if diff <= 1 or diff * 10**18 < D: | ||
return D | ||
raise "Did not converge" | ||
|
||
|
||
@external | ||
@view | ||
def newton_D(ANN: uint256, gamma: uint256, x_unsorted: uint256[N_COINS]) -> uint256: | ||
""" | ||
Finding the invariant using Newton method. | ||
ANN is higher by the factor A_MULTIPLIER | ||
ANN is already A * N**N | ||
|
||
Currently uses 60k gas | ||
""" | ||
|
||
# Initial value of invariant D is that for constant-product invariant | ||
x: uint256[N_COINS] = x_unsorted | ||
if x[0] < x[1]: | ||
x = [x_unsorted[1], x_unsorted[0]] | ||
|
||
D: uint256 = N_COINS * self.geometric_mean(x, False) | ||
S: uint256 = x[0] + x[1] | ||
__g1k0: uint256 = gamma + 10**18 | ||
|
||
for i in range(255): | ||
D_prev: uint256 = D | ||
assert D > 0 | ||
# Unsafe ivision by D is now safe | ||
|
||
# K0: uint256 = 10**18 | ||
# for _x in x: | ||
# K0 = K0 * _x * N_COINS / D | ||
# collapsed for 2 coins | ||
K0: uint256 = unsafe_div(unsafe_div((10**18 * N_COINS**2) * x[0], D) * x[1], D) | ||
|
||
_g1k0: uint256 = __g1k0 | ||
if _g1k0 > K0: | ||
_g1k0 = unsafe_sub(_g1k0, K0) + 1 # > 0 | ||
else: | ||
_g1k0 = unsafe_sub(K0, _g1k0) + 1 # > 0 | ||
|
||
# D / (A * N**N) * _g1k0**2 / gamma**2 | ||
mul1: uint256 = unsafe_div(unsafe_div(unsafe_div(10**18 * D, gamma) * _g1k0, gamma) * _g1k0 * A_MULTIPLIER, ANN) | ||
|
||
# 2*N*K0 / _g1k0 | ||
mul2: uint256 = unsafe_div(((2 * 10**18) * N_COINS) * K0, _g1k0) | ||
|
||
neg_fprime: uint256 = (S + unsafe_div(S * mul2, 10**18)) + mul1 * N_COINS / K0 - unsafe_div(mul2 * D, 10**18) | ||
|
||
# D -= f / fprime | ||
D_plus: uint256 = D * (neg_fprime + S) / neg_fprime | ||
D_minus: uint256 = D*D / neg_fprime | ||
if 10**18 > K0: | ||
D_minus += unsafe_div(D * (mul1 / neg_fprime), 10**18) * unsafe_sub(10**18, K0) / K0 | ||
else: | ||
D_minus -= unsafe_div(D * (mul1 / neg_fprime), 10**18) * unsafe_sub(K0, 10**18) / K0 | ||
|
||
if D_plus > D_minus: | ||
D = unsafe_sub(D_plus, D_minus) | ||
else: | ||
D = unsafe_div(unsafe_sub(D_minus, D_plus), 2) | ||
|
||
diff: uint256 = 0 | ||
if D > D_prev: | ||
diff = unsafe_sub(D, D_prev) | ||
else: | ||
diff = unsafe_sub(D_prev, D) | ||
if diff * 10**14 < max(10**16, D): # Could reduce precision for gas efficiency here | ||
# Test that we are safe with the next newton_y | ||
for _x in x: | ||
frac: uint256 = _x * 10**18 / D | ||
return D | ||
|
||
raise "Did not converge" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just pick either push or pull_request, using both generates twice the same job for every commit in a PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.