Skip to content
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

Ctez2 #1

Open
wants to merge 98 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
0863e9a
fix: check undefined instead of zero
shekhar-shubhendu May 31, 2021
2c4e29f
New difference equations on token_to_cash and cash_to_token.
dhsorens Aug 5, 2021
603c0b8
Reorganize code for and add comments for readability.
dhsorens Aug 6, 2021
485ef41
Add entrypoint to cfmm contract to update target, which affects the e…
dhsorens Aug 9, 2021
9dba0a9
Fix price and difference equations
dhsorens Aug 9, 2021
d47229a
Add transaction fees to token_to_cash and cash_to_token.
dhsorens Aug 9, 2021
60a5d62
Correct price calculation
dhsorens Aug 9, 2021
4adca10
Update get_target function to be compatible with the ctez_target entr…
dhsorens Aug 9, 2021
36d7943
get_target security
dhsorens Aug 9, 2021
d5758a4
Cleanup.
dhsorens Aug 10, 2021
a9260f6
Fix recursive notation.
dhsorens Aug 10, 2021
fb0c8bb
Fix the get_target function
dhsorens Aug 10, 2021
70b7cc4
New iterative approximating function for dy given dx.
dhsorens Aug 10, 2021
d0caa68
Minor debug
dhsorens Aug 10, 2021
0f78d11
After experimental results showed newton descent to be more consisten…
dhsorens Aug 13, 2021
72786fc
todo for division by zero
dhsorens Aug 13, 2021
13dbcfe
Bug fixes.
dhsorens Aug 13, 2021
94fff03
Added new curve to token_to_token transfers.
dhsorens Aug 13, 2021
23ef607
Bound Newton to 4 rounds of computation.
dhsorens Aug 13, 2021
f3147d9
Add capability for choosing number of rounds in difference equation c…
dhsorens Aug 13, 2021
99edc35
Use option type to make optional arg for convergence of the diff equa…
dhsorens Aug 16, 2021
6d921e0
Remove optional args; UX can do that.
dhsorens Aug 16, 2021
1d3a081
Correct target in ctez in response to comment.
dhsorens Aug 17, 2021
68d47d1
Formulae -> formula
dhsorens Aug 17, 2021
80aebda
Price variation which offloads computation to cfmm
dhsorens Aug 17, 2021
1be5163
Improve Newton accuracy, keeping underestimates.
dhsorens Aug 17, 2021
0435bb4
Remove redundant check.
dhsorens Aug 17, 2021
68233b3
Adding first iteration of tests.
Aug 24, 2021
0ad5ed6
moved test.mligo into the correct directory
Aug 24, 2021
91ae651
temporarily move test to the root folder
Aug 25, 2021
b8652fc
Generic setup function which allows for mutation testing and a divers…
Aug 25, 2021
6f45587
Merge pull request #1 from differentialderek/price-variation
dhsorens Aug 25, 2021
01bbfe5
Changed target to nat, instead of nat * nat, in keeping with old conv…
Aug 25, 2021
cf7007f
Modify test so that target is a nat.
Aug 25, 2021
244e8bc
dy_approx starts at 0n
Aug 26, 2021
d71d812
Change to int
Aug 26, 2021
456755c
adjust is positive, rounded down, via a double negative.
Aug 26, 2021
aeaf2e2
Deleting dead code, clarifying comments
Aug 26, 2021
91146a2
Optimise newton in response to Arthur's comment.
Aug 26, 2021
82e46e7
Change multiplication/division by b to bitwise shifts
Aug 26, 2021
238f72d
Some bug fixes to compile correctly.
Aug 26, 2021
fde3ccf
Confused left shift with right shift
Aug 26, 2021
fb4b808
Remove unused vars
Aug 26, 2021
743d4c6
Correct default init values for target and drift
Aug 26, 2021
3578365
Keep everything positive in the newton calculation
Aug 26, 2021
67452af
Resolve some TODOs in the code
Aug 27, 2021
cdc3970
Fix txn test
Aug 27, 2021
22bbb67
Updating tests.
Sep 1, 2021
88b1466
Adding parameterized trades
Sep 1, 2021
265f94a
Merge remote-tracking branch 'upstream/main'
shekhar-shubhendu Sep 2, 2021
2de0183
Compilation with #if ORACLE directive
Sep 3, 2021
c6658a4
test params update
Sep 3, 2021
905350a
Syntax standardization
Sep 3, 2021
0b0a68b
Each time the target is updated, it feeds it to the cfmm.
Sep 3, 2021
aea9e18
Adds test for drift
Sep 3, 2021
d964e11
Migrate tests to tests folder
Sep 7, 2021
d4d3ac2
Remove fee from storage.
Sep 8, 2021
33c7de4
cleanup the ctez cfmm by separating it from the general one
Sep 8, 2021
0861a48
remove tez / baker option for cfmm
Sep 8, 2021
1dd4640
fix division by 2^48
Sep 9, 2021
9e25cd0
optimize newton for gas
Sep 9, 2021
b3ed41d
simplify newton function call
Sep 9, 2021
f6cffc8
fix inverted price
Sep 9, 2021
7ecadda
fix price oracle reporting
Sep 13, 2021
7ba3a83
Merge branch 'tezos-checker:next' into next
shekhar-shubhendu Sep 13, 2021
28d7c7d
feat: update frontend to match new cfmm
shekhar-shubhendu Sep 13, 2021
c8c1c39
feat: update cfmm storage for deployment
shekhar-shubhendu Sep 13, 2021
c4c3f15
feat: update add liquidity and conversion
shekhar-shubhendu Sep 14, 2021
549610a
fix: remove console.log
shekhar-shubhendu Sep 14, 2021
4639f1c
feat: fix price
shekhar-shubhendu Sep 14, 2021
ab600d9
cleanup
shekhar-shubhendu Sep 14, 2021
d6a7274
fix: use same value as ctez
shekhar-shubhendu Sep 14, 2021
f484443
add the old preprocessed version of the tez ctez cfmm for easy compar…
Sep 16, 2021
937c6df
add information about the old preprocessed version of the tez ctez cfmm
Sep 16, 2021
7032290
fix: remove var with incorrect name
shekhar-shubhendu Sep 16, 2021
4d78307
feat: add loading oven
shekhar-shubhendu Sep 16, 2021
91ef9ac
Merge branch 'tezos-checker:next' into next
shekhar-shubhendu Sep 16, 2021
f086b54
revert: deploy.sh
shekhar-shubhendu Sep 16, 2021
b24efd3
Merge pull request #28 from tzConnectBerlin/next
murbard Sep 20, 2021
561f439
fix tezSold in tez_to_token
Sep 21, 2021
5e60bb5
Add entrypoint GetMarginalPrice
gkaracha Oct 15, 2021
39fb98a
Merge pull request #30 from gkaracha/next
murbard Nov 15, 2021
fe3e883
Update README.md
murbard Nov 17, 2021
ceabe74
use a flatter cuve
Nov 26, 2021
7b2955a
remove dead code
Nov 26, 2021
3b41b4f
add a short description of ctez mechanisms
Dec 2, 2021
8c581d3
add support for liquidity fee
murbard Nov 27, 2022
0bc3b62
contracts compile, modernize deprecated instructions
murbard Nov 27, 2022
1a2114b
fix bug in fee_r computation
murbard Feb 4, 2023
921c033
add a few todos
Dec 30, 2023
e31e748
add todo line
Dec 30, 2023
404e7c6
limit dex
Feb 28, 2024
febdfe9
limit dex
Feb 28, 2024
8a04dd5
add some code for newton descent
Feb 28, 2024
33bd648
add a note about how to compute withdraw
Feb 29, 2024
9986fc1
wip
Mar 26, 2024
eb5d062
WIP merging of contracts
May 1, 2024
6d0b720
separate half dex into parameterised module
May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ligoproject
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ Each time the CFMM pushes its rate to the ctez contract, the drift, and the targ

If the price of ctez implied by the CFMM is below the target, the drift is *raised* by `max(1024 * (target / price - 1)^2, 1) * 2^(-48)` times the number of seconds since the last adjustment. If it is below, it is *lowered* by that amount. This corresponds roughly to a maximum adjustment of the annualized drift of one percentage point for every fractional day since the last adjustment. The adjustment saturates when the discrepancy exceeds one 32ndth. Note that, by a small miracle, `ln(1.01) / year / day ~ 1.027 * 2^(-48) / second^2` which we use to simplify the computation in the implementation.

### Curve

If `x` is the quantity of tez, and `y` the quantity of ctez, and `t` the target (in tez per ctez), then CFMM uses the constant formula `(x + y)^8 - (x - y)^8 = k`. The price is equal to the target when `y = x / t` and, on that point, all derivatives from the 2nd to the 7th vanish, meaning there is more liquidity there.
To give an example, if `t = 1`, `x = 100` and `y = 100`, and a user adds `dx = 63` to the pool, they receive `dy = 62.4`, that is less than `1%` slippage for taking nearly two thirds of the `y` pool!

The target is fed to the CFMM by the ctez contract.

## Rationale

If the price of ctez remains below its target, the drift will keep increasing and at some point, under a quadratically compounding rate vaults are forced into liquidation which may cause ctez to be bid up to claim the tez in the vaults.
Expand Down
5 changes: 5 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- check for bugs
- ensure last price of block is used or perhaps VWAP
- negative fees when restoring AMM towards target
- ensure drift is updated based on the price before the last update, not the last update
- lock LPs for a few blocks after they initiate withdrawal request
568 changes: 568 additions & 0 deletions attic/cfmm_tez_ctez.old.preprocessed.mligo

Large diffs are not rendered by default.

202 changes: 8 additions & 194 deletions cfmm.mligo

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions cfmm_initial_storage.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ tokenPool = 1n ;
cashPool = 1n ;
lqtTotal = 1n ;
pendingPoolUpdates = 0n ;
tokenAddress = ("FA12_CTEZ" : address) ;
lqtAddress = ("tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU" : address) ;
lastOracleUpdate = ("2021-01-01T00:00:00Z" : timestamp) ;
consumerEntrypoint = ("CTEZ_ADDRESS%cfmm_price" : address) ;
}

7 changes: 4 additions & 3 deletions cfmm_initial_storage.mligo
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{ tokenPool = 1n ;
{ tezPool = 1n ;
cashPool = 1n ;
target = Bitwise.shift_left 1n 48n;
lqtTotal = 1n ;
pendingPoolUpdates = 0n ;
tokenAddress = ("FA12_CTEZ" : address) ;
ctez_address = ("CTEZ_ADDRESS" : address) ;
cashAddress = ("FA12_CTEZ" : address) ;
lqtAddress = ("tz1Ke2h7sDdakHJQh8WX4Z372du1KChsksyU" : address) ;
lastOracleUpdate = ("2021-01-01T00:00:00Z" : timestamp) ;
consumerEntrypoint = ("CTEZ_ADDRESS%cfmm_price" : address) ;
Expand Down
5 changes: 5 additions & 0 deletions cfmm_tez_ctez.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define ORACLE
#define CASH_IS_TEZ
[@inline] let const_fee = 9995n (* 0.05% fee *)
[@inline] let const_fee_denom = 10000n
#include "cfmm.mligo"
Loading