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

feat: RPC call for amount_to_liquidity #3835

Merged
merged 9 commits into from
Aug 17, 2023
Merged

Conversation

syan095
Copy link
Contributor

@syan095 syan095 commented Aug 14, 2023

Pull Request

Closes: PRO-111

Checklist

Please conduct a thorough self-review before opening the PR.

  • I am confident that the code works.
  • I have updated documentation where appropriate.

Summary

Added RPC call for getting the amount to liquidity

@syan095 syan095 requested a review from a team as a code owner August 14, 2023 05:05
@syan095 syan095 requested review from niklasnatter and acdibble and removed request for a team August 14, 2023 05:05
@linear
Copy link

linear bot commented Aug 14, 2023

PRO-111 Feature: RPC call for Amm:: liquidity_to_amounts()

Should expose liquidity_to_amounts() in the AMM as a rpc call, so front end can convert liquidity into asset amounts

@syan095 syan095 requested a review from dandanlen August 14, 2023 05:05
@codecov
Copy link

codecov bot commented Aug 14, 2023

Codecov Report

Merging #3835 (ff623e7) into main (f929770) will decrease coverage by 0%.
The diff coverage is 66%.

@@          Coverage Diff           @@
##            main   #3835    +/-   ##
======================================
- Coverage     71%     71%    -0%     
======================================
  Files        363     363            
  Lines      55659   55763   +104     
  Branches   55659   55763   +104     
======================================
+ Hits       39397   39467    +70     
- Misses     14303   14327    +24     
- Partials    1959    1969    +10     
Files Changed Coverage Δ
state-chain/amm/src/lib.rs 65% <ø> (ø)
state-chain/amm/src/range_orders.rs 89% <0%> (-<1%) ⬇️
state-chain/custom-rpc/src/lib.rs 0% <0%> (ø)
state-chain/runtime/src/lib.rs 40% <10%> (-1%) ⬇️
state-chain/pallets/cf-pools/src/lib.rs 64% <65%> (+<1%) ⬆️
state-chain/pallets/cf-pools/src/tests.rs 96% <100%> (+1%) ⬆️
state-chain/runtime/src/runtime_apis.rs 80% <100%> (+3%) ⬆️

... and 4 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

)
.unwrap(),
1u128,
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Having said all of the above, i'm not sure this test is really necessary. We don't need to ensure the accuracy of the conversion (this is, or should be tested in the amm crate).

What we really care about here is whether the rpc works as expected, for example it shouldn't panic if there is no pool, or if the user submits an invalid tick range.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test here only verifies that the pub function worked as intended, I'm sure the inner workings works, as it was already used and tested in the AMM.

I've tested the RPC with local testnet to ensure the RPC works and wouldn't crash when the input was invalid.

Copy link
Collaborator

@dandanlen dandanlen Aug 15, 2023

Choose a reason for hiding this comment

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

What I mean is that the exact value of the output is not really relevant to this test: we don't actually require that the error is within 1 unit. 1 is an arbitary limit (why not 2 or 10?).

This test is checking the inner details of the amm crate instead of testing this 'layer' of the rpc call. What this layer actually adds is: checking that the asset is valid, and passing the args correctly to the amm, passing the result (ok/err) back up the rpc call chain. If anything we should test this.

I've tested the RPC with local testnet

Even better if we can enforce this with a unit test :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I think i understand now.
Removed the test for the AMM function itself, and instead test the correct error is returned when pool doesn't exist and when tick range isn't valid

upper,
SideMap::from_array([unstable_amount.into(), stable_amount.into()]),
)
.ok()
Copy link
Collaborator

Choose a reason for hiding this comment

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

We just swallow the error here. We could instead return something useful. It looks like there are two possible errors, either the pool doesn't exist, or the tick range is invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found it difficult to return the error from run time to RPC call, I tried anyhow::error, &'static str and DispatchError, none of the was very nice to use.
I ended up having a custom error message when "None" was returned from the pallet call, so it provides better information to the RPC user.

Copy link
Collaborator

@dandanlen dandanlen Aug 15, 2023

Choose a reason for hiding this comment

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

We already have Error variants for all possible amm errors. I'll show you what I mean.

Ok I tried using DispatchError and I can see it's annoying! I think returning the pallet's Error<T> type might be an option, but I played around and I think a small dedicated error enum is the simplest to work with.

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 using a new error enum is definitely the best approach!

@syan095 syan095 requested a review from dandanlen August 15, 2023 22:47
Copy link
Collaborator

@dandanlen dandanlen left a comment

Choose a reason for hiding this comment

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

👍 Thanks @syan095

@dandanlen dandanlen changed the title Feature: RPC call for amount_to_liquidity feat: RPC call for amount_to_liquidity Aug 17, 2023
@dandanlen dandanlen enabled auto-merge (squash) August 17, 2023 16:10
@dandanlen dandanlen merged commit dbfe96a into main Aug 17, 2023
@dandanlen dandanlen deleted the feat/rpc-liquidity-to-amounts branch August 17, 2023 16:43
marcellorigotti pushed a commit that referenced this pull request Aug 22, 2023
Co-authored-by: Daniel <daniel@chainflip.io>
Co-authored-by: dandanlen <3168260+dandanlen@users.noreply.github.com>
AlastairHolmes pushed a commit that referenced this pull request Aug 23, 2023
AlastairHolmes pushed a commit that referenced this pull request Aug 23, 2023
AlastairHolmes pushed a commit that referenced this pull request Aug 24, 2023
AlastairHolmes pushed a commit that referenced this pull request Aug 25, 2023
AlastairHolmes added a commit that referenced this pull request Sep 5, 2023
* Revert "feat: RPC call for amount_to_liquidity (#3835)"

This reverts commit dbfe96a.

* rename collected structures

* Add PositionInfo

squash

* cut out inner_liquidity_to_amounts


squash


squash

* cut out inner_amounts_to_liquidity

* amm range orders mint and burn by Size

* output operation liquidity delta

* remove unneeded trait impls

* move sqrt_price_to_price

* current_price rpc

* add nicer swap and position functions

* remove pool rpcs

* remove deser and ser impls

* amm pool new function

* current_price doesn't use traits

* make private/hide unneeded interfaces

* pub amount in PositionInfo

* cutout error translation functions

* desired -> maximum

* rename RangeOrderSize to OldRangeOrderSize

* new interfaces

* new implementations

* handle order ids

* spelling and add safe mode checks

* update bouncer for new lp-interface

* bouncer linting

* fix range_order bouncer command

* bouncer linting

* lp api returns

* required asset ratio rpc

* lp rpc

* Use "order" not "position"

* use u256

* utilities

squash

squash

* use new utilities

* pools function

pool_info and pools functions

* pool_orders

* pool_range_order_liquidity_value

* rpcs

* _asset

* fix

* move debit down

* bought_amount

* LiquidityTooLarge

* remove old test

* remove range_order iszero check

* basic comments

---------

Co-authored-by: Roy Yang <roy@chainflip.io>
Co-authored-by: Martin Rieke <martin@chainflip.io>
dandanlen pushed a commit that referenced this pull request Sep 6, 2023
* Revert "feat: RPC call for amount_to_liquidity (#3835)"

This reverts commit dbfe96a.

* rename collected structures

* Add PositionInfo

squash

* cut out inner_liquidity_to_amounts

squash

squash

* cut out inner_amounts_to_liquidity

* amm range orders mint and burn by Size

* output operation liquidity delta

* remove unneeded trait impls

* move sqrt_price_to_price

* current_price rpc

* add nicer swap and position functions

* remove pool rpcs

* remove deser and ser impls

* amm pool new function

* current_price doesn't use traits

* make private/hide unneeded interfaces

* pub amount in PositionInfo

* cutout error translation functions

* desired -> maximum

* rename RangeOrderSize to OldRangeOrderSize

* new interfaces

* new implementations

* handle order ids

* spelling and add safe mode checks

* update bouncer for new lp-interface

* bouncer linting

* fix range_order bouncer command

* bouncer linting

* lp api returns

* required asset ratio rpc

* lp rpc

* Use "order" not "position"

* use u256

* utilities

squash

squash

* use new utilities

* pools function

pool_info and pools functions

* pool_orders

* pool_range_order_liquidity_value

* rpcs

* _asset

* fix

* move debit down

* bought_amount

* LiquidityTooLarge

* remove old test

* remove range_order iszero check

* basic comments

---------

Co-authored-by: Roy Yang <roy@chainflip.io>
Co-authored-by: Martin Rieke <martin@chainflip.io>
dandanlen pushed a commit that referenced this pull request Sep 6, 2023
* Revert "feat: RPC call for amount_to_liquidity (#3835)"

This reverts commit dbfe96a.

* rename collected structures

* Add PositionInfo

squash

* cut out inner_liquidity_to_amounts

squash

squash

* cut out inner_amounts_to_liquidity

* amm range orders mint and burn by Size

* output operation liquidity delta

* remove unneeded trait impls

* move sqrt_price_to_price

* current_price rpc

* add nicer swap and position functions

* remove pool rpcs

* remove deser and ser impls

* amm pool new function

* current_price doesn't use traits

* make private/hide unneeded interfaces

* pub amount in PositionInfo

* cutout error translation functions

* desired -> maximum

* rename RangeOrderSize to OldRangeOrderSize

* new interfaces

* new implementations

* handle order ids

* spelling and add safe mode checks

* update bouncer for new lp-interface

* bouncer linting

* fix range_order bouncer command

* bouncer linting

* lp api returns

* required asset ratio rpc

* lp rpc

* Use "order" not "position"

* use u256

* utilities

squash

squash

* use new utilities

* pools function

pool_info and pools functions

* pool_orders

* pool_range_order_liquidity_value

* rpcs

* _asset

* fix

* move debit down

* bought_amount

* LiquidityTooLarge

* remove old test

* remove range_order iszero check

* basic comments

---------

Co-authored-by: Roy Yang <roy@chainflip.io>
Co-authored-by: Martin Rieke <martin@chainflip.io>
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.

2 participants