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

Support for replicating tokens on chain #311

Merged
merged 27 commits into from
Dec 17, 2021
Merged

Conversation

gkaracha
Copy link
Contributor

@gkaracha gkaracha commented Dec 10, 2021

Closes #263.

  • Implement a mock cfmm oracle to use in tests. To ensure that our tests can catch forgotten changes (i.e., avoid mixing it up with the usual mock oracle), the name of the price entrypoint is %getCfmmPrice (as opposed to %getPrice which is the one we use for the mock oracle). This is not very descriptive and does not indicate the units we expect the fraction to have (tok/ctok), but it's only a placeholder for now. We might want to change this in the future, or even perhaps get it from the yaml file at build time.
    Also: currently both mock oracles (index and cfmm) have an update entrypoint but since this is not part of Checker's spec I saw no good reason to deviate on the name of this one too.
  • Extend the yaml specification with another field (tracking_type) which can take one of two values: index or token.
  • Generalize the type of the (index-)oracle to a pair of nats (CHF in TEZ, (TEZ/CHF)). This reduces the number of differences between token-based and index-based instances quite some.
  • Turn the calculation of the target into jinja templates. The different price calculation does manifest itself in the drift adjustment (see Generalize checker to be able to replicate tokens on chain #263 (comment)), but indirectly, through the target. Hence the target became a jinja template and not the drift derivative.
  • Add docs/prices.md with some notes about price calculations, taken from Generalize checker to be able to replicate tokens on chain #263 (comment).
  • Extend CI to run all the tests with all the three possible configurations: checker-index-tez.yaml, checker-index-fa2.yaml, and checker-token-fa2.yaml.

@github-actions

This comment has been minimized.

@gkaracha gkaracha force-pushed the gkaracha/cfmm-price branch from 0704155 to 1923412 Compare December 15, 2021 16:05
@github-actions

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Dec 16, 2021

Gas costs 74f9e3d 6662564 Diff
checker%touch 42390 42430 40
checker%create_burrow 22343 22344 1
checker%add_liquidity 12599 12600 1
checker%set_burrow_delegate 18365 18366 1
checker%mint_kit 7131 7132 1
checker%touch_burrow 6102 6103 1
checker%transfer 6311 6312 1
checker%burn_kit 7359 7360 1
Entrypoint sizes 74f9e3d 6662564 Diff
touch 56504 56687 183
receive_price 287 289 2
Test coverage 74f9e3d 6662564 Diff
targetCalculation.ml None 100 100
getOracleEntrypoint.ml None 75 75
checkerTypes.ml 77.78 78.57 0.789999999999992
checker.ml 94.97 94.98 0.010000000000005116
TOTAL 81.9 81.91 0.009999999999990905

@gkaracha gkaracha marked this pull request as ready for review December 16, 2021 15:02
@gkaracha
Copy link
Contributor Author

gkaracha commented Dec 16, 2021

OK, I think this one is finally ready for review @dorranh! The description is also now up to date.

After merging this I think we'll be at the point where the README should have separate build/deployment/testing instructions per configuration. Hopefully the API won't really change after this PR so we can start looking at #118 soon after.

I had to deactivate three tests that are configuration-specific for CI to pass, but we should be able to renable those tests once #284 is addressed (partially or completely; I might be able to fix that rather easily, being on the OCaml side). Either way, I'd go ahead and merge this PR without them for now.

@gkaracha gkaracha requested a review from dorranh December 16, 2021 15:03
Copy link
Contributor

@dorranh dorranh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me! I just had one question regarding units of measure. If the comment is not actually an issue then I am happy merging 🙂

src/targetCalculation.mli Show resolved Hide resolved
(** Calculate the current target based on the current quantity, and the current
price of kit in ctok (as given by the CFMM).
{[
target_{i+1} = FLOOR (q_{i+1} / kit_in_ctok_{i+1})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the target still dimensionless if we've removed index from this equation? To me this reads: [1/kit] / [ctok/kit] => [1/ctok]. Perhaps q has different units in the case of a token-based checker? Not sure if this has implications for the system though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just re-read the new document covering the units of measure and am still a bit confused about which units q is supposed to have. Perhaps we could discuss this offline as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's discuss this more offline. For reference, here's how I've been thinking about the two cases:

  • index-tracking CHF
    index  : tok/CHF
    p_cfmm : tok/kit
    => (index/p_cfmm) : kit/CHF
    
  • token-tracking tzCHF
    p_cfmm : tzCHF/kit
    => (1/p_cfmm) : kit/tzCHF
    

let price = fixedpoint_of_ratio_floor (make_ratio (Ligo.int idx) tok_scaling_factor_int) in
let price =
let (num, den) = oracle_price in
fixedpoint_of_ratio_floor (make_ratio (Ligo.int num) (Ligo.int den)) in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a comment here to make it extra obvious that we are losing some precision?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I added a small note in a9d6a88 👍

@github-actions
Copy link

github-actions bot commented Dec 17, 2021

Gas costs 74f9e3d a9d6a88 Diff
checker%touch 42390 42430 40
checker%touch_burrow 6102 6103 1
checker%add_liquidity 12599 12600 1
checker%mint_kit 7131 7132 1
checker%transfer 6311 6312 1
checker%set_burrow_delegate 18365 18366 1
checker%create_burrow 22343 22344 1
checker%burn_kit 7359 7360 1
checker%update_operators 5589 5590 1
Entrypoint sizes 74f9e3d a9d6a88 Diff
touch 56504 56675 171
receive_price 287 289 2
Test coverage 74f9e3d a9d6a88 Diff
targetCalculation.ml None 100 100
getOracleEntrypoint.ml None 75 75
checkerTypes.ml 77.78 78.57 0.789999999999992
checker.ml 94.97 94.98 0.010000000000005116
TOTAL 81.9 81.89 -0.010000000000005116

@gkaracha
Copy link
Contributor Author

@dorranh shall I go ahead and merge this?

@dorranh
Copy link
Contributor

dorranh commented Dec 17, 2021

@gkaracha, yep I think it's good to go following our separate discussion of the target calculation.

@gkaracha gkaracha merged commit 89b06df into master Dec 17, 2021
@gkaracha gkaracha deleted the gkaracha/cfmm-price branch December 17, 2021 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generalize checker to be able to replicate tokens on chain
2 participants