Skip to content

Commit b54cdac

Browse files
authored
Merge pull request #1703 from opentensor/feat/uniswapv3-lp
Enable liquidity providers
2 parents ea97e4c + d9022c0 commit b54cdac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+11809
-3075
lines changed

Cargo.lock

Lines changed: 1784 additions & 1123 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,35 @@ members = [
3333
]
3434
resolver = "2"
3535

36+
[workspace.package]
37+
edition = "2024"
38+
3639
[workspace.lints.clippy]
37-
indexing-slicing = "deny"
3840
arithmetic-side-effects = "deny"
39-
type_complexity = "allow"
40-
unwrap-used = "deny"
41+
indexing-slicing = "deny"
4142
manual_inspect = "allow"
4243
result_large_err = "allow"
44+
type_complexity = "allow"
45+
unwrap-used = "deny"
4346
useless_conversion = "allow" # until polkadot is patched
4447

4548
[workspace.dependencies]
49+
node-subtensor-runtime = { default-features = false, path = "runtime" }
4650
pallet-admin-utils = { default-features = false, path = "pallets/admin-utils" }
4751
pallet-collective = { default-features = false, path = "pallets/collective" }
4852
pallet-commitments = { default-features = false, path = "pallets/commitments" }
4953
pallet-registry = { default-features = false, path = "pallets/registry" }
5054
pallet-crowdloan = { default-features = false, path = "pallets/crowdloan" }
5155
pallet-subtensor = { default-features = false, path = "pallets/subtensor" }
56+
pallet-subtensor-swap = { default-features = false, path = "pallets/swap" }
57+
pallet-subtensor-swap-runtime-api = { default-features = false, path = "pallets/swap/runtime-api" }
58+
pallet-subtensor-swap-rpc = { default-features = false, path = "pallets/swap/rpc" }
59+
safe-math = { default-features = false, path = "primitives/safe-math" }
5260
subtensor-custom-rpc = { default-features = false, path = "pallets/subtensor/rpc" }
5361
subtensor-custom-rpc-runtime-api = { default-features = false, path = "pallets/subtensor/runtime-api" }
5462
subtensor-precompiles = { default-features = false, path = "precompiles" }
5563
subtensor-runtime-common = { default-features = false, path = "common" }
56-
node-subtensor-runtime = { default-features = false, path = "runtime" }
64+
subtensor-swap-interface = { default-features = false, path = "pallets/swap-interface" }
5765

5866
async-trait = "0.1"
5967
cargo-husky = { version = "1", default-features = false }
@@ -98,6 +106,7 @@ proc-macro2 = { version = "1", features = ["span-locations"] }
98106
thiserror = "1.0"
99107
walkdir = "2"
100108
approx = "0.5"
109+
alloy-primitives = { version = "0.8.23", default-features = false }
101110

102111
subtensor-macros = { path = "support/macros" }
103112

@@ -152,6 +161,7 @@ sc-transaction-pool = { git = "https://github.com/paritytech/polkadot-sdk.git",
152161
sc-transaction-pool-api = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2412-6" }
153162

154163
sp-api = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2412-6", default-features = false }
164+
sp-arithmetic = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2412-6", default-features = false }
155165
sp-block-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2412-6", default-features = false }
156166
sp-blockchain = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2412-6", default-features = false }
157167
sp-consensus = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2412-6" }

common/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ targets = ["x86_64-unknown-linux-gnu"]
1313
[dependencies]
1414
codec = { workspace = true }
1515
frame-support = { workspace = true }
16-
precompile-utils = { workspace = true }
1716
scale-info = { workspace = true }
1817
serde = { workspace = true }
1918
sp-core = { workspace = true }
@@ -29,7 +28,6 @@ fast-blocks = []
2928
std = [
3029
"codec/std",
3130
"frame-support/std",
32-
"precompile-utils/std",
3331
"scale-info/std",
3432
"serde/std",
3533
"sp-core/std",

common/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,37 @@ impl Default for ProxyType {
148148
}
149149
}
150150

151+
pub trait SubnetInfo<AccountId> {
152+
fn tao_reserve(netuid: NetUid) -> u64;
153+
fn alpha_reserve(netuid: NetUid) -> u64;
154+
fn exists(netuid: NetUid) -> bool;
155+
fn mechanism(netuid: NetUid) -> u16;
156+
fn is_owner(account_id: &AccountId, netuid: NetUid) -> bool;
157+
}
158+
159+
pub trait BalanceOps<AccountId> {
160+
fn tao_balance(account_id: &AccountId) -> u64;
161+
fn alpha_balance(netuid: NetUid, coldkey: &AccountId, hotkey: &AccountId) -> u64;
162+
fn increase_balance(coldkey: &AccountId, tao: u64);
163+
fn decrease_balance(coldkey: &AccountId, tao: u64) -> Result<u64, DispatchError>;
164+
fn increase_stake(
165+
coldkey: &AccountId,
166+
hotkey: &AccountId,
167+
netuid: NetUid,
168+
alpha: u64,
169+
) -> Result<(), DispatchError>;
170+
fn decrease_stake(
171+
coldkey: &AccountId,
172+
hotkey: &AccountId,
173+
netuid: NetUid,
174+
alpha: u64,
175+
) -> Result<u64, DispatchError>;
176+
fn increase_provided_tao_reserve(netuid: NetUid, tao: u64);
177+
fn decrease_provided_tao_reserve(netuid: NetUid, tao: u64);
178+
fn increase_provided_alpha_reserve(netuid: NetUid, alpha: u64);
179+
fn decrease_provided_alpha_reserve(netuid: NetUid, alpha: u64);
180+
}
181+
151182
pub mod time {
152183
use super::*;
153184

node/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ node-subtensor-runtime = { workspace = true, features = ["std"] }
111111
subtensor-runtime-common = { workspace = true, features = ["std"] }
112112
subtensor-custom-rpc = { workspace = true, features = ["std"] }
113113
subtensor-custom-rpc-runtime-api = { workspace = true, features = ["std"] }
114+
pallet-subtensor-swap-rpc = { workspace = true, features = ["std"] }
115+
pallet-subtensor-swap-runtime-api = { workspace = true, features = ["std"] }
114116

115117
[build-dependencies]
116118
substrate-build-script-utils = { workspace = true }

node/src/rpc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ where
115115
CIDP: CreateInherentDataProviders<Block, ()> + Send + Clone + 'static,
116116
CT: fp_rpc::ConvertTransaction<<Block as BlockT>::Extrinsic> + Send + Sync + Clone + 'static,
117117
{
118+
use pallet_subtensor_swap_rpc::{Swap, SwapRpcApiServer};
118119
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer};
119120
use sc_consensus_manual_seal::rpc::{ManualSeal, ManualSealApiServer};
120121
use substrate_frame_rpc_system::{System, SystemApiServer};
@@ -131,6 +132,9 @@ where
131132
// Custom RPC methods for Paratensor
132133
module.merge(SubtensorCustom::new(client.clone()).into_rpc())?;
133134

135+
// Swap RPC
136+
module.merge(Swap::new(client.clone()).into_rpc())?;
137+
134138
module.merge(System::new(client.clone(), pool.clone()).into_rpc())?;
135139
module.merge(TransactionPayment::new(client).into_rpc())?;
136140

pallets/admin-utils/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ substrate-fixed = { workspace = true }
3232
pallet-evm-chain-id = { workspace = true }
3333
pallet-drand = { workspace = true, default-features = false }
3434
sp-consensus-grandpa = { workspace = true }
35+
subtensor-swap-interface = { workspace = true }
3536
subtensor-runtime-common = { workspace = true }
3637

3738
[dev-dependencies]
@@ -43,6 +44,7 @@ pallet-balances = { workspace = true, features = ["std"] }
4344
pallet-scheduler = { workspace = true }
4445
pallet-grandpa = { workspace = true }
4546
sp-std = { workspace = true }
47+
pallet-subtensor-swap = { workspace = true }
4648

4749
[features]
4850
default = ["std"]
@@ -57,6 +59,7 @@ std = [
5759
"pallet-evm-chain-id/std",
5860
"pallet-grandpa/std",
5961
"pallet-scheduler/std",
62+
"pallet-subtensor-swap/std",
6063
"pallet-subtensor/std",
6164
"scale-info/std",
6265
"sp-consensus-aura/std",
@@ -68,6 +71,7 @@ std = [
6871
"sp-tracing/std",
6972
"sp-weights/std",
7073
"substrate-fixed/std",
74+
"subtensor-swap-interface/std",
7175
"subtensor-runtime-common/std"
7276
]
7377
runtime-benchmarks = [
@@ -79,6 +83,7 @@ runtime-benchmarks = [
7983
"pallet-grandpa/runtime-benchmarks",
8084
"pallet-scheduler/runtime-benchmarks",
8185
"pallet-subtensor/runtime-benchmarks",
86+
"pallet-subtensor-swap/runtime-benchmarks",
8287
"sp-runtime/runtime-benchmarks",
8388
]
8489
try-runtime = [

pallets/admin-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ pub mod pallet {
10881088
log::trace!("Setting minimum stake to: {}", min_stake);
10891089
pallet_subtensor::Pallet::<T>::set_nominator_min_required_stake(min_stake);
10901090
if min_stake > prev_min_stake {
1091-
log::trace!("Clearing small nominations");
1091+
log::trace!("Clearing small nominations if possible");
10921092
pallet_subtensor::Pallet::<T>::clear_small_nominations();
10931093
log::trace!("Small nominations cleared");
10941094
}

pallets/admin-utils/src/tests/mock.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#![allow(clippy::arithmetic_side_effects, clippy::unwrap_used)]
22

3+
use core::num::NonZeroU64;
4+
35
use frame_support::{
4-
assert_ok, derive_impl, parameter_types,
6+
PalletId, assert_ok, derive_impl, parameter_types,
57
traits::{Everything, Hooks, InherentBuilder, PrivilegeCmp},
68
};
79
use frame_system::{self as system, offchain::CreateTransactionBase};
@@ -32,6 +34,7 @@ frame_support::construct_runtime!(
3234
Drand: pallet_drand::{Pallet, Call, Storage, Event<T>} = 6,
3335
Grandpa: pallet_grandpa = 7,
3436
EVMChainId: pallet_evm_chain_id = 8,
37+
Swap: pallet_subtensor_swap::{Pallet, Call, Storage, Event<T>} = 9,
3538
}
3639
);
3740

@@ -213,6 +216,7 @@ impl pallet_subtensor::Config for Test {
213216
type InitialTaoWeight = InitialTaoWeight;
214217
type InitialEmaPriceHalvingPeriod = InitialEmaPriceHalvingPeriod;
215218
type DurationOfStartCall = DurationOfStartCall;
219+
type SwapInterface = Swap;
216220
type KeySwapOnSubnetCost = InitialKeySwapOnSubnetCost;
217221
type HotkeySwapOnSubnetInterval = HotkeySwapOnSubnetInterval;
218222
}
@@ -273,6 +277,27 @@ impl pallet_balances::Config for Test {
273277
type RuntimeHoldReason = ();
274278
}
275279

280+
// Swap-related parameter types
281+
parameter_types! {
282+
pub const SwapProtocolId: PalletId = PalletId(*b"ten/swap");
283+
pub const SwapMaxFeeRate: u16 = 10000; // 15.26%
284+
pub const SwapMaxPositions: u32 = 100;
285+
pub const SwapMinimumLiquidity: u64 = 1_000;
286+
pub const SwapMinimumReserve: NonZeroU64 = NonZeroU64::new(1_000_000).unwrap();
287+
}
288+
289+
impl pallet_subtensor_swap::Config for Test {
290+
type RuntimeEvent = RuntimeEvent;
291+
type SubnetInfo = SubtensorModule;
292+
type BalanceOps = SubtensorModule;
293+
type ProtocolId = SwapProtocolId;
294+
type MaxFeeRate = SwapMaxFeeRate;
295+
type MaxPositions = SwapMaxPositions;
296+
type MinimumLiquidity = SwapMinimumLiquidity;
297+
type MinimumReserve = SwapMinimumReserve;
298+
type WeightInfo = ();
299+
}
300+
276301
pub struct OriginPrivilegeCmp;
277302

278303
impl PrivilegeCmp<OriginCaller> for OriginPrivilegeCmp {

pallets/subtensor/Cargo.toml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ hex = { workspace = true }
4444
share-pool = { default-features = false, path = "../../primitives/share-pool" }
4545
safe-math = { default-features = false, path = "../../primitives/safe-math" }
4646
approx = { workspace = true }
47+
subtensor-swap-interface = { workspace = true }
48+
subtensor-runtime-common = { workspace = true }
4749

4850
pallet-collective = { version = "4.0.0-dev", default-features = false, path = "../collective" }
4951
pallet-drand = { path = "../drand", default-features = false }
@@ -58,11 +60,11 @@ ark-serialize = { workspace = true, default-features = false }
5860
w3f-bls = { workspace = true, default-features = false }
5961
sha2 = { workspace = true }
6062
rand_chacha = { workspace = true }
61-
subtensor-runtime-common = { workspace = true }
6263

6364
[dev-dependencies]
6465
pallet-balances = { workspace = true, features = ["std"] }
6566
pallet-scheduler = { workspace = true }
67+
pallet-subtensor-swap = { workspace = true }
6668
sp-version = { workspace = true }
6769
# Substrate
6870
sp-tracing = { workspace = true }
@@ -91,6 +93,7 @@ std = [
9193
"pallet-membership/std",
9294
"pallet-preimage/std",
9395
"pallet-scheduler/std",
96+
"pallet-subtensor-swap/std",
9497
"pallet-transaction-payment/std",
9598
"pallet-utility/std",
9699
"rand_chacha/std",
@@ -111,6 +114,7 @@ std = [
111114
"substrate-fixed/std",
112115
"substrate-fixed/std",
113116
"subtensor-runtime-common/std",
117+
"subtensor-swap-interface/std",
114118
"tle/std",
115119
"w3f-bls/std",
116120
]
@@ -120,14 +124,15 @@ runtime-benchmarks = [
120124
"frame-support/runtime-benchmarks",
121125
"frame-system/runtime-benchmarks",
122126
"pallet-balances/runtime-benchmarks",
123-
"pallet-membership/runtime-benchmarks",
124-
"pallet-utility/runtime-benchmarks",
125-
"sp-runtime/runtime-benchmarks",
126127
"pallet-collective/runtime-benchmarks",
128+
"pallet-drand/runtime-benchmarks",
129+
"pallet-membership/runtime-benchmarks",
127130
"pallet-preimage/runtime-benchmarks",
128131
"pallet-scheduler/runtime-benchmarks",
129-
"pallet-drand/runtime-benchmarks",
132+
"pallet-subtensor-swap/runtime-benchmarks",
130133
"pallet-transaction-payment/runtime-benchmarks",
134+
"pallet-utility/runtime-benchmarks",
135+
"sp-runtime/runtime-benchmarks",
131136
]
132137
try-runtime = [
133138
"frame-support/try-runtime",

0 commit comments

Comments
 (0)