-
Notifications
You must be signed in to change notification settings - Fork 51
feat: improve add liquidity logic #725
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
Closed
Mobile-Crest
wants to merge
4
commits into
opentensor:staging
from
Mobile-Crest:feat/improve_add_liquidity
+874
−245
Closed
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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,211 @@ | ||
| from typing import TYPE_CHECKING, Optional | ||
|
|
||
| from async_substrate_interface import AsyncExtrinsicReceipt | ||
|
|
||
| from bittensor_cli.src.bittensor.utils import unlock_key | ||
| from bittensor_cli.src.bittensor.balances import Balance | ||
| from bittensor_cli.src.commands.liquidity.utils import price_to_tick | ||
|
|
||
| if TYPE_CHECKING: | ||
| from bittensor_wallet import Wallet | ||
| from bittensor_cli.src.bittensor.subtensor_interface import SubtensorInterface | ||
|
|
||
|
|
||
| async def add_liquidity_extrinsic( | ||
| subtensor: "SubtensorInterface", | ||
| wallet: "Wallet", | ||
| hotkey_ss58: str, | ||
| netuid: int, | ||
| liquidity: Balance, | ||
| price_low: Balance, | ||
| price_high: Balance, | ||
| wait_for_inclusion: bool = True, | ||
| wait_for_finalization: bool = False, | ||
| ) -> tuple[bool, str, Optional[AsyncExtrinsicReceipt]]: | ||
| """ | ||
| Adds liquidity to the specified price range. | ||
|
|
||
| Arguments: | ||
| subtensor: The Subtensor client instance used for blockchain interaction. | ||
| wallet: The wallet used to sign the extrinsic (must be unlocked). | ||
| hotkey_ss58: the SS58 of the hotkey to use for this transaction. | ||
| netuid: The UID of the target subnet for which the call is being initiated. | ||
| liquidity: The amount of liquidity to be added. | ||
| price_low: The lower bound of the price tick range. | ||
| price_high: The upper bound of the price tick range. | ||
| wait_for_inclusion: Whether to wait for the extrinsic to be included in a block. Defaults to True. | ||
| wait_for_finalization: Whether to wait for finalization of the extrinsic. Defaults to False. | ||
|
|
||
| Returns: | ||
| Tuple[bool, str]: | ||
| - True and a success message if the extrinsic is successfully submitted or processed. | ||
| - False and an error message if the submission fails or the wallet cannot be unlocked. | ||
|
|
||
| Note: Adding is allowed even when user liquidity is enabled in specified subnet. Call | ||
| `toggle_user_liquidity_extrinsic` to enable/disable user liquidity. | ||
| """ | ||
| if not (unlock := unlock_key(wallet)).success: | ||
| return False, unlock.message, None | ||
|
|
||
| tick_low = price_to_tick(price_low.tao) | ||
| tick_high = price_to_tick(price_high.tao) | ||
|
|
||
| call = await subtensor.substrate.compose_call( | ||
| call_module="Swap", | ||
| call_function="add_liquidity", | ||
| call_params={ | ||
| "hotkey": hotkey_ss58, | ||
| "netuid": netuid, | ||
| "tick_low": tick_low, | ||
| "tick_high": tick_high, | ||
| "liquidity": liquidity.rao, | ||
| }, | ||
| ) | ||
|
|
||
| return await subtensor.sign_and_send_extrinsic( | ||
| call=call, | ||
| wallet=wallet, | ||
| wait_for_inclusion=wait_for_inclusion, | ||
| wait_for_finalization=wait_for_finalization, | ||
| ) | ||
|
|
||
|
|
||
| async def modify_liquidity_extrinsic( | ||
| subtensor: "SubtensorInterface", | ||
| wallet: "Wallet", | ||
| hotkey_ss58: str, | ||
| netuid: int, | ||
| position_id: int, | ||
| liquidity_delta: Balance, | ||
| wait_for_inclusion: bool = True, | ||
| wait_for_finalization: bool = False, | ||
| ) -> tuple[bool, str, Optional[AsyncExtrinsicReceipt]]: | ||
| """Modifies liquidity in liquidity position by adding or removing liquidity from it. | ||
|
|
||
| Arguments: | ||
| subtensor: The Subtensor client instance used for blockchain interaction. | ||
| wallet: The wallet used to sign the extrinsic (must be unlocked). | ||
| hotkey_ss58: the SS58 of the hotkey to use for this transaction. | ||
| netuid: The UID of the target subnet for which the call is being initiated. | ||
| position_id: The id of the position record in the pool. | ||
| liquidity_delta: The amount of liquidity to be added or removed (add if positive or remove if negative). | ||
| wait_for_inclusion: Whether to wait for the extrinsic to be included in a block. Defaults to True. | ||
| wait_for_finalization: Whether to wait for finalization of the extrinsic. Defaults to False. | ||
|
|
||
| Returns: | ||
| Tuple[bool, str]: | ||
| - True and a success message if the extrinsic is successfully submitted or processed. | ||
| - False and an error message if the submission fails or the wallet cannot be unlocked. | ||
|
|
||
| Note: Modifying is allowed even when user liquidity is enabled in specified subnet. | ||
| Call `toggle_user_liquidity_extrinsic` to enable/disable user liquidity. | ||
| """ | ||
| if not (unlock := unlock_key(wallet)).success: | ||
| return False, unlock.message, None | ||
|
|
||
| call = await subtensor.substrate.compose_call( | ||
| call_module="Swap", | ||
| call_function="modify_position", | ||
| call_params={ | ||
| "hotkey": hotkey_ss58, | ||
| "netuid": netuid, | ||
| "position_id": position_id, | ||
| "liquidity_delta": liquidity_delta.rao, | ||
| }, | ||
| ) | ||
|
|
||
| return await subtensor.sign_and_send_extrinsic( | ||
| call=call, | ||
| wallet=wallet, | ||
| wait_for_inclusion=wait_for_inclusion, | ||
| wait_for_finalization=wait_for_finalization, | ||
| ) | ||
|
|
||
|
|
||
| async def remove_liquidity_extrinsic( | ||
| subtensor: "SubtensorInterface", | ||
| wallet: "Wallet", | ||
| hotkey_ss58: str, | ||
| netuid: int, | ||
| position_id: int, | ||
| wait_for_inclusion: bool = True, | ||
| wait_for_finalization: bool = False, | ||
| ) -> tuple[bool, str, Optional[AsyncExtrinsicReceipt]]: | ||
| """Remove liquidity and credit balances back to wallet's hotkey stake. | ||
|
|
||
| Arguments: | ||
| subtensor: The Subtensor client instance used for blockchain interaction. | ||
| wallet: The wallet used to sign the extrinsic (must be unlocked). | ||
| hotkey_ss58: the SS58 of the hotkey to use for this transaction. | ||
| netuid: The UID of the target subnet for which the call is being initiated. | ||
| position_id: The id of the position record in the pool. | ||
| wait_for_inclusion: Whether to wait for the extrinsic to be included in a block. Defaults to True. | ||
| wait_for_finalization: Whether to wait for finalization of the extrinsic. Defaults to False. | ||
|
|
||
| Returns: | ||
| Tuple[bool, str]: | ||
| - True and a success message if the extrinsic is successfully submitted or processed. | ||
| - False and an error message if the submission fails or the wallet cannot be unlocked. | ||
|
|
||
| Note: Adding is allowed even when user liquidity is enabled in specified subnet. | ||
| Call `toggle_user_liquidity_extrinsic` to enable/disable user liquidity. | ||
| """ | ||
| if not (unlock := unlock_key(wallet)).success: | ||
| return False, unlock.message, None | ||
|
|
||
| call = await subtensor.substrate.compose_call( | ||
| call_module="Swap", | ||
| call_function="remove_liquidity", | ||
| call_params={ | ||
| "hotkey": hotkey_ss58, | ||
| "netuid": netuid, | ||
| "position_id": position_id, | ||
| }, | ||
| ) | ||
|
|
||
| return await subtensor.sign_and_send_extrinsic( | ||
| call=call, | ||
| wallet=wallet, | ||
| wait_for_inclusion=wait_for_inclusion, | ||
| wait_for_finalization=wait_for_finalization, | ||
| ) | ||
|
|
||
|
|
||
| async def toggle_user_liquidity_extrinsic( | ||
| subtensor: "SubtensorInterface", | ||
| wallet: "Wallet", | ||
| netuid: int, | ||
| enable: bool, | ||
| wait_for_inclusion: bool = True, | ||
| wait_for_finalization: bool = False, | ||
| ) -> tuple[bool, str, Optional[AsyncExtrinsicReceipt]]: | ||
| """Allow to toggle user liquidity for specified subnet. | ||
|
|
||
| Arguments: | ||
| subtensor: The Subtensor client instance used for blockchain interaction. | ||
| wallet: The wallet used to sign the extrinsic (must be unlocked). | ||
| netuid: The UID of the target subnet for which the call is being initiated. | ||
| enable: Boolean indicating whether to enable user liquidity. | ||
| wait_for_inclusion: Whether to wait for the extrinsic to be included in a block. Defaults to True. | ||
| wait_for_finalization: Whether to wait for finalization of the extrinsic. Defaults to False. | ||
|
|
||
| Returns: | ||
| Tuple[bool, str]: | ||
| - True and a success message if the extrinsic is successfully submitted or processed. | ||
| - False and an error message if the submission fails or the wallet cannot be unlocked. | ||
| """ | ||
| if not (unlock := unlock_key(wallet)).success: | ||
| return False, unlock.message, None | ||
|
|
||
| call = await subtensor.substrate.compose_call( | ||
| call_module="Swap", | ||
| call_function="toggle_user_liquidity", | ||
| call_params={"netuid": netuid, "enable": enable}, | ||
| ) | ||
|
|
||
| return await subtensor.sign_and_send_extrinsic( | ||
| call=call, | ||
| wallet=wallet, | ||
| wait_for_inclusion=wait_for_inclusion, | ||
| wait_for_finalization=wait_for_finalization, | ||
| ) |
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.
Still no respect for
--no-promptThere 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.
I think extrinsic function doesn't accept prompt, let me know if I am wrong
btcli/bittensor_cli/src/commands/liquidity/liquidity.py
Lines 31 to 32 in f48829d