Skip to content

Commit 66076ad

Browse files
authored
Merge pull request #1529 from opentensor/sasha/feat/uniswapv3-lp
Adapt stake_into_subnet to swap v3
2 parents 9f0b542 + 00b7044 commit 66076ad

File tree

6 files changed

+68
-70
lines changed

6 files changed

+68
-70
lines changed

pallets/subtensor/src/macros/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod events {
1414
/// a network is removed.
1515
NetworkRemoved(u16),
1616
/// stake has been transferred from the a coldkey account onto the hotkey staking account.
17-
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16, u64),
17+
StakeAdded(T::AccountId, T::AccountId, u64, u64, u16),
1818
/// stake has been removed from the hotkey staking account onto the coldkey account.
1919
StakeRemoved(T::AccountId, T::AccountId, u64, u64, u16, u64),
2020
/// stake has been moved from origin (hotkey, subnet ID) to destination (hotkey, subnet ID) of this amount (in TAO).

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<T: Config> Pallet<T> {
7070
netuid,
7171
tao_staked.saturating_to_num::<u64>(),
7272
T::SwapInterface::max_price(),
73-
);
73+
)?;
7474

7575
// Ok and return.
7676
Ok(())
@@ -159,15 +159,13 @@ impl<T: Config> Pallet<T> {
159159

160160
// 6. Swap the stake into alpha on the subnet and increase counters.
161161
// Emit the staking event.
162-
let fee = DefaultStakingFee::<T>::get();
163162
Self::stake_into_subnet(
164163
&hotkey,
165164
&coldkey,
166165
netuid,
167166
tao_staked.saturating_to_num::<u64>(),
168167
limit_price,
169-
fee,
170-
);
168+
)?;
171169

172170
// Ok and return.
173171
Ok(())

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<T: Config> Pallet<T> {
309309
maybe_limit_price: Option<u64>,
310310
maybe_allow_partial: Option<bool>,
311311
check_transfer_toggle: bool,
312-
) -> Result<u64, Error<T>> {
312+
) -> Result<u64, DispatchError> {
313313
// Calculate the maximum amount that can be executed
314314
let max_amount = if let Some(limit_price) = maybe_limit_price {
315315
Self::get_max_amount_move(origin_netuid, destination_netuid, limit_price)
@@ -370,9 +370,8 @@ impl<T: Config> Pallet<T> {
370370
destination_coldkey,
371371
destination_netuid,
372372
tao_unstaked,
373-
T::SwapInterface::max_price(),
374-
fee,
375-
);
373+
T::SwapInterface::max_price(),
374+
)?;
376375
}
377376

378377
Ok(tao_unstaked.saturating_sub(fee))

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl<T: Config> Pallet<T> {
271271
Self::get_root_netuid(),
272272
total_tao_unstaked,
273273
T::SwapInterface::max_price(),
274-
);
274+
)?;
275275

276276
// 5. Done and ok.
277277
Ok(())

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use safe_math::*;
33
use share_pool::{SharePool, SharePoolDataOperations};
44
use sp_std::ops::Neg;
55
use substrate_fixed::types::{I64F64, I96F32, U64F64, U96F32, U110F18};
6+
use subtensor_swap_interface::{OrderType, SwapHandler};
67

78
impl<T: Config> Pallet<T> {
89
/// Retrieves the total alpha issuance for a given subnet.
@@ -829,55 +830,58 @@ impl<T: Config> Pallet<T> {
829830
coldkey: &T::AccountId,
830831
netuid: u16,
831832
tao: u64,
832-
price_limit: u64,
833-
) -> u64 {
834-
// Step 2. Swap the tao to alpha.
835-
let alpha = Self::swap_tao_for_alpha(netuid, tao_staked);
836-
let mut actual_alpha = 0;
837-
if (tao_staked > 0) && (alpha > 0) {
838-
// Step 3: Increase the alpha on the hotkey account.
839-
actual_alpha = Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
840-
hotkey, coldkey, netuid, alpha,
841-
);
833+
price_limit: u64,
834+
) -> Result<u64, DispatchError> {
835+
// Swap the tao to alpha.
836+
let swap_result = T::SwapInterface::swap(netuid, OrderType::Buy, tao, price_limit)?;
842837

843-
// Step 4: Update the list of hotkeys staking for this coldkey
844-
let mut staking_hotkeys = StakingHotkeys::<T>::get(coldkey);
845-
if !staking_hotkeys.contains(hotkey) {
846-
staking_hotkeys.push(hotkey.clone());
847-
StakingHotkeys::<T>::insert(coldkey, staking_hotkeys.clone());
848-
}
838+
// Increase the alpha on the hotkey account.
839+
if Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
840+
hotkey,
841+
coldkey,
842+
netuid,
843+
swap_result.amount_paid_out,
844+
) == 0
845+
|| swap_result.amount_paid_out == 0
846+
{
847+
return Ok(0);
848+
}
849+
850+
// Step 4: Update the list of hotkeys staking for this coldkey
851+
let mut staking_hotkeys = StakingHotkeys::<T>::get(coldkey);
852+
if !staking_hotkeys.contains(hotkey) {
853+
staking_hotkeys.push(hotkey.clone());
854+
StakingHotkeys::<T>::insert(coldkey, staking_hotkeys.clone());
849855
}
850856

851-
// Step 5. Increase Tao reserves by the fee amount.
857+
// Update TAO reserves
852858
SubnetTAO::<T>::mutate(netuid, |total| {
853-
*total = total.saturating_add(actual_fee);
859+
*total = swap_result.new_tao_reserve;
854860
});
855-
TotalStake::<T>::mutate(|total| {
856-
*total = total.saturating_add(actual_fee);
861+
SubnetAlphaIn::<T>::mutate(netuid, |total| {
862+
*total = swap_result.new_alpha_reserve;
857863
});
858864
LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());
859865

860-
// Step 6. Deposit and log the staking event.
866+
// Deposit and log the staking event.
861867
Self::deposit_event(Event::StakeAdded(
862868
coldkey.clone(),
863869
hotkey.clone(),
864-
tao_staked,
865-
actual_alpha,
870+
tao,
871+
swap_result.amount_paid_out,
866872
netuid,
867-
actual_fee,
868873
));
874+
869875
log::debug!(
870-
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?}, fee: {:?} )",
876+
"StakeAdded( coldkey: {:?}, hotkey:{:?}, tao: {:?}, alpha:{:?}, netuid: {:?} )",
871877
coldkey.clone(),
872878
hotkey.clone(),
873-
tao_staked,
874-
actual_alpha,
879+
tao,
880+
swap_result.amount_paid_out,
875881
netuid,
876-
actual_fee
877882
);
878883

879-
// Step 7: Return the amount of alpha staked
880-
actual_alpha
884+
Ok(swap_result.amount_paid_out)
881885
}
882886

883887
pub fn get_alpha_share_pool(

pallets/swap/src/pallet/impls.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<T: Config> SwapStep<T> {
130130
} else {
131131
// Stop at limit price (refund needed)
132132
self.action = SwapStepAction::StopIn;
133-
self.delta_in = Pallet::<T>::delta_in(
133+
self.delta_in = Self::delta_in(
134134
self.order_type,
135135
self.current_liquidity,
136136
self.current_price,
@@ -145,7 +145,7 @@ impl<T: Config> SwapStep<T> {
145145
if self.edge_quantity < self.lim_quantity {
146146
// Cross at edge price
147147
self.action = SwapStepAction::Crossing;
148-
self.delta_in = Pallet::<T>::delta_in(
148+
self.delta_in = Self::delta_in(
149149
self.order_type,
150150
self.current_liquidity,
151151
self.current_price,
@@ -156,7 +156,7 @@ impl<T: Config> SwapStep<T> {
156156
} else if self.edge_quantity > self.lim_quantity {
157157
// Stop at limit price (refund needed)
158158
self.action = SwapStepAction::StopIn;
159-
self.delta_in = Pallet::<T>::delta_in(
159+
self.delta_in = Self::delta_in(
160160
self.order_type,
161161
self.current_liquidity,
162162
self.current_price,
@@ -167,7 +167,7 @@ impl<T: Config> SwapStep<T> {
167167
} else {
168168
// Stop on edge (refund needed)
169169
self.action = SwapStepAction::StopOn;
170-
self.delta_in = Pallet::<T>::delta_in(
170+
self.delta_in = Self::delta_in(
171171
self.order_type,
172172
self.current_liquidity,
173173
self.current_price,
@@ -181,7 +181,7 @@ impl<T: Config> SwapStep<T> {
181181
else {
182182
if self.target_quantity <= self.lim_quantity {
183183
// Stop on edge price
184-
self.delta_in = Pallet::<T>::delta_in(
184+
self.delta_in = Self::delta_in(
185185
self.order_type,
186186
self.current_liquidity,
187187
self.current_price,
@@ -197,7 +197,7 @@ impl<T: Config> SwapStep<T> {
197197
} else {
198198
// Stop at limit price (refund needed)
199199
self.action = SwapStepAction::StopIn;
200-
self.delta_in = Pallet::<T>::delta_in(
200+
self.delta_in = Self::delta_in(
201201
self.order_type,
202202
self.current_liquidity,
203203
self.current_price,
@@ -226,9 +226,6 @@ impl<T: Config> SwapStep<T> {
226226
Pallet::<T>::add_fees(self.netuid, self.order_type, fee);
227227
let delta_out = Pallet::<T>::convert_deltas(self.netuid, self.order_type, self.delta_in);
228228

229-
// TODO (look inside method)
230-
// Self::update_reserves(netuid, order_type, self.delta_in, delta_out);
231-
232229
// Get current tick
233230
let current_tick_index = TickIndex::current_bounded::<T>(self.netuid);
234231

@@ -284,6 +281,27 @@ impl<T: Config> SwapStep<T> {
284281
delta_out,
285282
})
286283
}
284+
285+
/// Get the input amount needed to reach the target price
286+
fn delta_in(
287+
order_type: OrderType,
288+
liquidity_curr: U64F64,
289+
sqrt_price_curr: U64F64,
290+
sqrt_price_target: SqrtPrice,
291+
) -> u64 {
292+
let one = U64F64::saturating_from_num(1);
293+
294+
(match order_type {
295+
OrderType::Sell => liquidity_curr.saturating_mul(
296+
one.safe_div(sqrt_price_target.into())
297+
.saturating_sub(one.safe_div(sqrt_price_curr)),
298+
),
299+
OrderType::Buy => {
300+
liquidity_curr.saturating_mul(sqrt_price_target.saturating_sub(sqrt_price_curr))
301+
}
302+
})
303+
.saturating_to_num::<u64>()
304+
}
287305
}
288306

289307
impl<T: Config> Pallet<T> {
@@ -573,27 +591,6 @@ impl<T: Config> Pallet<T> {
573591
}
574592
}
575593

576-
/// Get the input amount needed to reach the target price
577-
fn delta_in(
578-
order_type: OrderType,
579-
liquidity_curr: U64F64,
580-
sqrt_price_curr: U64F64,
581-
sqrt_price_target: SqrtPrice,
582-
) -> u64 {
583-
let one = U64F64::saturating_from_num(1);
584-
585-
(match order_type {
586-
OrderType::Sell => liquidity_curr.saturating_mul(
587-
one.safe_div(sqrt_price_target.into())
588-
.saturating_sub(one.safe_div(sqrt_price_curr)),
589-
),
590-
OrderType::Buy => {
591-
liquidity_curr.saturating_mul(sqrt_price_target.saturating_sub(sqrt_price_curr))
592-
}
593-
})
594-
.saturating_to_num::<u64>()
595-
}
596-
597594
/// Update liquidity when crossing a tick
598595
fn update_liquidity_at_crossing(netuid: NetUid, order_type: OrderType) -> Result<(), Error<T>> {
599596
let mut liquidity_curr = CurrentLiquidity::<T>::get(netuid);

0 commit comments

Comments
 (0)