Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "p
w3f-bls = { git = "https://github.com/opentensor/bls", branch = "fix-no-std", default-features = false }
ark-crypto-primitives = { version = "0.4.0", default-features = false }
ark-scale = { version = "0.0.11", default-features = false }
sp-ark-bls12-381 = { git = "https://github.com/paritytech/arkworks-substrate", package = "sp-ark-bls12-381", default-features = false }
ark-bls12-381 = { version = "0.4.0", default-features = false }
ark-serialize = { version = "0.4.0", default-features = false }
ark-ff = { version = "0.4.0", default-features = false }
Expand Down
6 changes: 3 additions & 3 deletions pallets/drand/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ scale-info = { workspace = true, features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
log.workspace = true
hex = { workspace = true, features = ["serde"] }
hex = { workspace = true, features = ["serde", "alloc"] }
sha2.workspace = true
anyhow.workspace = true
# frame deps
Expand All @@ -29,7 +29,7 @@ sp-io.workspace = true
sp-runtime.workspace = true
sp-std.workspace = true
# arkworks dependencies
sp-ark-bls12-381.workspace = true
sp-crypto-ec-utils = { workspace = true, features = ["bls12-381"] }
ark-bls12-381 = { workspace = true, features = ["curve"] }
ark-serialize = { workspace = true, features = ["derive"] }
ark-ff.workspace = true
Expand Down Expand Up @@ -64,7 +64,7 @@ std = [
"serde/std",
"serde_json/std",
"hex/std",
"sp-ark-bls12-381/std",
"sp-crypto-ec-utils/std",
"ark-bls12-381/std",
"ark-serialize/std",
"ark-ff/std",
Expand Down
2 changes: 1 addition & 1 deletion pallets/drand/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use ark_ec::pairing::Pairing;
use ark_std::{Zero, ops::Neg};
use sp_ark_bls12_381::{
use sp_crypto_ec_utils::bls12_381::{
Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G2Affine as G2AffineOpt,
};

Expand Down
2 changes: 1 addition & 1 deletion pallets/drand/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use ark_ec::{AffineRepr, hashing::HashToCurve};
use ark_serialize::CanonicalSerialize;
use codec::Decode;
use sha2::{Digest, Sha256};
use sp_ark_bls12_381::{G1Affine as G1AffineOpt, G2Affine as G2AffineOpt};
use sp_crypto_ec_utils::bls12_381::{G1Affine as G1AffineOpt, G2Affine as G2AffineOpt};
use tle::curves::drand::TinyBLS381;
use w3f_bls::engine::EngineBLS;

Expand Down
25 changes: 2 additions & 23 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,6 @@ impl<T: Config> Pallet<T> {
price_limit: TaoCurrency,
drop_fees: bool,
) -> Result<TaoCurrency, DispatchError> {
// Record the protocol TAO before the swap.
let protocol_tao = Self::get_protocol_tao(netuid);

// Decrease alpha on subnet
let actual_alpha_decrease =
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha);
Expand All @@ -705,13 +702,6 @@ impl<T: Config> Pallet<T> {
let swap_result =
Self::swap_alpha_for_tao(netuid, actual_alpha_decrease, price_limit, drop_fees)?;

// Record the protocol TAO after the swap.
let protocol_tao_after = Self::get_protocol_tao(netuid);
// This should decrease as we are removing TAO from the protocol.
let protocol_tao_delta: TaoCurrency = protocol_tao.saturating_sub(protocol_tao_after);
// Use max to overstate the TAO flow from the protocol.
let tao_flow = protocol_tao_delta.max(swap_result.amount_paid_out.into());

// Refund the unused alpha (in case if limit price is hit)
let refund = actual_alpha_decrease.saturating_sub(
swap_result
Expand All @@ -738,7 +728,7 @@ impl<T: Config> Pallet<T> {
// }

// Record TAO outflow
Self::record_tao_outflow(netuid, tao_flow);
Self::record_tao_outflow(netuid, swap_result.amount_paid_out.into());

LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());

Expand Down Expand Up @@ -777,20 +767,9 @@ impl<T: Config> Pallet<T> {
set_limit: bool,
drop_fees: bool,
) -> Result<AlphaCurrency, DispatchError> {
// Record the protocol TAO before the swap.
let protocol_tao = Self::get_protocol_tao(netuid);

// Swap the tao to alpha.
let swap_result = Self::swap_tao_for_alpha(netuid, tao, price_limit, drop_fees)?;

// Record the protocol TAO after the swap.
let protocol_tao_after = Self::get_protocol_tao(netuid);

// This should increase as we are adding TAO to the protocol.
let protocol_tao_delta: TaoCurrency = protocol_tao_after.saturating_sub(protocol_tao);
// Use min to understate the TAO flow into the protocol.
let tao_flow = protocol_tao_delta.min(tao);

ensure!(
!swap_result.amount_paid_out.is_zero(),
Error::<T>::AmountTooLow
Expand Down Expand Up @@ -826,7 +805,7 @@ impl<T: Config> Pallet<T> {
}

// Record TAO inflow
Self::record_tao_inflow(netuid, tao_flow);
Self::record_tao_inflow(netuid, swap_result.amount_paid_in.into());

LastColdkeyHotkeyStakeBlock::<T>::insert(coldkey, hotkey, Self::get_current_block_as_u64());

Expand Down
18 changes: 10 additions & 8 deletions pallets/swap/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,10 +880,12 @@ impl<T: Config> Pallet<T> {
// ---------------- USER: refund τ and convert α → stake ----------------

// 1) Refund τ principal directly.
if rm.tao > TaoCurrency::ZERO {
T::BalanceOps::increase_balance(&owner, rm.tao);
user_refunded_tao = user_refunded_tao.saturating_add(rm.tao);
T::TaoReserve::decrease_provided(netuid, rm.tao);
let tao_total_from_pool: TaoCurrency = rm.tao.saturating_add(rm.fee_tao);
if tao_total_from_pool > TaoCurrency::ZERO {
T::BalanceOps::increase_balance(&owner, tao_total_from_pool);
user_refunded_tao =
user_refunded_tao.saturating_add(tao_total_from_pool);
T::TaoReserve::decrease_provided(netuid, tao_total_from_pool);
}

// 2) Stake ALL withdrawn α (principal + fees) to the best permitted validator.
Expand Down Expand Up @@ -967,17 +969,17 @@ impl<T: Config> Pallet<T> {
Ok(rm) => {
let alpha_total_from_pool: AlphaCurrency =
rm.alpha.saturating_add(rm.fee_alpha);
let tao = rm.tao;
let tao_total_from_pool: TaoCurrency = rm.tao.saturating_add(rm.fee_tao);

if tao > TaoCurrency::ZERO {
burned_tao = burned_tao.saturating_add(tao);
if tao_total_from_pool > TaoCurrency::ZERO {
burned_tao = burned_tao.saturating_add(tao_total_from_pool);
}
if alpha_total_from_pool > AlphaCurrency::ZERO {
burned_alpha = burned_alpha.saturating_add(alpha_total_from_pool);
}

log::debug!(
"clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao:?}, α_total={alpha_total_from_pool:?}"
"clear_protocol_liquidity: burned protocol pos: netuid={netuid:?}, pos_id={pos_id:?}, τ={tao_total_from_pool:?}, α_total={alpha_total_from_pool:?}"
);
}
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 360,
spec_version: 361,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
Loading