Skip to content

Commit

Permalink
feat : update lst precompiles and add icon to pool metadata (#812)
Browse files Browse the repository at this point in the history
* add new function to balances precompile

* fix consesus data provider

* update lst precompile

* precompile working

* add icons

* update types

* update types

* fmt

* update tests

* fix tests

* fix clippy
  • Loading branch information
1xstj authored Nov 8, 2024
1 parent 6364c16 commit 88635cd
Show file tree
Hide file tree
Showing 31 changed files with 8,540 additions and 2,883 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ members = [
"precompiles/verify-bls381-signature",
"precompiles/multi-asset-delegation",
"precompiles/services",
"precompiles/tangle-lst",
"tangle-subxt",
"evm-tracer",
]
Expand Down Expand Up @@ -315,6 +316,7 @@ pallet-evm-precompile-verify-ecdsa-stark-signature = { path = "precompiles/verif
pallet-evm-precompile-verify-schnorr-signatures = { path = "precompiles/verify-schnorr-signatures", default-features = false }
pallet-evm-precompile-verify-bls381-signature = { path = "precompiles/verify-bls381-signature", default-features = false }
pallet-evm-precompile-services = { path = "precompiles/services", default-features = false }
pallet-evm-precompile-tangle-lst = { path = "precompiles/tangle-lst", default-features = false }

# Precompiles utils
postcard = { version = "1", default-features = false }
Expand Down
20 changes: 15 additions & 5 deletions pallets/tangle-lst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ pub mod pallet {
#[pallet::constant]
type MaxNameLength: Get<u32> + Clone;

/// The maximum length of a pool icon.
#[pallet::constant]
type MaxIconLength: Get<u32> + Clone;

/// Infallible method for converting `Currency::Balance` to `U256`.
type BalanceToU256: Convert<BalanceOf<Self>, U256>;

Expand Down Expand Up @@ -956,7 +960,8 @@ pub mod pallet {
root: AccountIdLookupOf<T>,
nominator: AccountIdLookupOf<T>,
bouncer: AccountIdLookupOf<T>,
name: BoundedVec<u8, T::MaxNameLength>,
name: Option<BoundedVec<u8, T::MaxNameLength>>,
icon: Option<BoundedVec<u8, T::MaxIconLength>>,
) -> DispatchResult {
let depositor = ensure_signed(origin)?;

Expand All @@ -965,7 +970,7 @@ pub mod pallet {
Ok(*id)
})?;

Self::do_create(depositor, amount, root, nominator, bouncer, pool_id, name)
Self::do_create(depositor, amount, root, nominator, bouncer, pool_id, name, icon)
}

/// Create a new delegation pool with a previously used pool id
Expand All @@ -976,21 +981,23 @@ pub mod pallet {
/// * `pool_id` - `A valid PoolId.
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::create())]
#[allow(clippy::too_many_arguments)]
pub fn create_with_pool_id(
origin: OriginFor<T>,
#[pallet::compact] amount: BalanceOf<T>,
root: AccountIdLookupOf<T>,
nominator: AccountIdLookupOf<T>,
bouncer: AccountIdLookupOf<T>,
pool_id: PoolId,
name: BoundedVec<u8, T::MaxNameLength>,
name: Option<BoundedVec<u8, T::MaxNameLength>>,
icon: Option<BoundedVec<u8, T::MaxIconLength>>,
) -> DispatchResult {
let depositor = ensure_signed(origin)?;

ensure!(!BondedPools::<T>::contains_key(pool_id), Error::<T>::PoolIdInUse);
ensure!(pool_id < LastPoolId::<T>::get(), Error::<T>::InvalidPoolId);

Self::do_create(depositor, amount, root, nominator, bouncer, pool_id, name)
Self::do_create(depositor, amount, root, nominator, bouncer, pool_id, name, icon)
}

/// Nominate on behalf of the pool.
Expand Down Expand Up @@ -1508,14 +1515,16 @@ impl<T: Config> Pallet<T> {
)
}

#[allow(clippy::too_many_arguments)]
fn do_create(
who: T::AccountId,
amount: BalanceOf<T>,
root: AccountIdLookupOf<T>,
nominator: AccountIdLookupOf<T>,
bouncer: AccountIdLookupOf<T>,
pool_id: PoolId,
name: BoundedVec<u8, T::MaxNameLength>,
name: Option<BoundedVec<u8, T::MaxNameLength>>,
icon: Option<BoundedVec<u8, T::MaxIconLength>>,
) -> DispatchResult {
let root = T::Lookup::lookup(root)?;
let nominator = T::Lookup::lookup(nominator)?;
Expand Down Expand Up @@ -1545,6 +1554,7 @@ impl<T: Config> Pallet<T> {
depositor: who.clone(),
},
name,
icon,
);

bonded_pool.try_bond_funds(&who, amount, BondType::Create)?;
Expand Down
3 changes: 3 additions & 0 deletions pallets/tangle-lst/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ parameter_types! {
pub static StakingMinBond: Balance = 10;
pub storage Nominations: Option<Vec<AccountId>> = None;
}

pub struct StakingMock;

impl StakingMock {
Expand Down Expand Up @@ -293,6 +294,7 @@ impl pallet_lst::Config for Runtime {
type MaxMetadataLen = MaxMetadataLen;
type MaxUnbonding = MaxUnbonding;
type MaxNameLength = ConstU32<50>;
type MaxIconLength = ConstU32<50>;
type Fungibles = Assets;
type AssetId = AssetId;
type PoolId = PoolId;
Expand Down Expand Up @@ -423,6 +425,7 @@ impl ExtBuilder {
900,
901,
902,
Default::default(),
Default::default()
));
assert_ok!(Lst::set_metadata(RuntimeOrigin::signed(900), 1, vec![1, 1]));
Expand Down
6 changes: 3 additions & 3 deletions pallets/tangle-lst/src/tests/bonded_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn balance_to_point_works() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
metadata: PoolMetadata { name: Default::default(), icon: Default::default() },
},
};

Expand Down Expand Up @@ -121,7 +121,7 @@ fn points_to_balance_works() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
metadata: PoolMetadata { name: Default::default(), icon: Default::default() },
},
};

Expand Down Expand Up @@ -170,7 +170,7 @@ fn ok_to_join_with_works() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
metadata: PoolMetadata { name: Default::default(), icon: Default::default() },
},
};

Expand Down
36 changes: 32 additions & 4 deletions pallets/tangle-lst/src/tests/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn create_works() {
123,
456,
789,
Default::default(),
Default::default()
));

Expand Down Expand Up @@ -71,7 +72,15 @@ fn create_errors_correctly() {

// Then
assert_noop!(
Lst::create(RuntimeOrigin::signed(11), 9, 123, 456, 789, Default::default()),
Lst::create(
RuntimeOrigin::signed(11),
9,
123,
456,
789,
Default::default(),
Default::default()
),
Error::<Runtime>::MinimumBondNotMet
);

Expand All @@ -80,7 +89,15 @@ fn create_errors_correctly() {

// Then
assert_noop!(
Lst::create(RuntimeOrigin::signed(11), 19, 123, 456, 789, Default::default()),
Lst::create(
RuntimeOrigin::signed(11),
19,
123,
456,
789,
Default::default(),
Default::default()
),
Error::<Runtime>::MinimumBondNotMet
);

Expand All @@ -91,7 +108,7 @@ fn create_errors_correctly() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
metadata: PoolMetadata { name: Default::default(), icon: Default::default() },
},
}
.put();
Expand All @@ -100,7 +117,15 @@ fn create_errors_correctly() {

// Then
assert_noop!(
Lst::create(RuntimeOrigin::signed(11), 20, 123, 456, 789, Default::default()),
Lst::create(
RuntimeOrigin::signed(11),
20,
123,
456,
789,
Default::default(),
Default::default()
),
Error::<Runtime>::MaxPools
);
});
Expand All @@ -118,6 +143,7 @@ fn create_with_pool_id_works() {
123,
456,
789,
Default::default(),
Default::default()
));

Expand All @@ -132,6 +158,7 @@ fn create_with_pool_id_works() {
654,
783,
1,
Default::default(),
Default::default()
),
Error::<Runtime>::PoolIdInUse
Expand All @@ -145,6 +172,7 @@ fn create_with_pool_id_works() {
654,
783,
3,
Default::default(),
Default::default()
),
Error::<Runtime>::InvalidPoolId
Expand Down
4 changes: 2 additions & 2 deletions pallets/tangle-lst/src/tests/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn join_errors_correctly() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
metadata: PoolMetadata { name: Default::default(), icon: Default::default() },
},
}
.put();
Expand Down Expand Up @@ -136,7 +136,7 @@ fn join_panics_when_reward_pool_not_found() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
metadata: PoolMetadata { name: Default::default(), icon: Default::default() },
},
}
.put();
Expand Down
9 changes: 6 additions & 3 deletions pallets/tangle-lst/src/types/bonded_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ pub struct BondedPoolInner<T: Config> {
#[scale_info(skip_type_params(T))]
pub struct PoolMetadata<T: Config> {
/// pool name
pub name: BoundedVec<u8, T::MaxNameLength>,
pub name: Option<BoundedVec<u8, T::MaxNameLength>>,
/// pool icon
pub icon: Option<BoundedVec<u8, T::MaxIconLength>>,
}

/// A wrapper for bonded pools, with utility functions.
Expand Down Expand Up @@ -54,15 +56,16 @@ impl<T: Config> BondedPool<T> {
pub fn new(
id: PoolId,
roles: PoolRoles<T::AccountId>,
name: BoundedVec<u8, T::MaxNameLength>,
name: Option<BoundedVec<u8, T::MaxNameLength>>,
icon: Option<BoundedVec<u8, T::MaxIconLength>>,
) -> Self {
Self {
id,
inner: BondedPoolInner {
commission: Commission::default(),
roles,
state: PoolState::Open,
metadata: PoolMetadata { name },
metadata: PoolMetadata { name, icon },
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions precompiles/tangle-lst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ pallet-tangle-lst = { workspace = true }
tangle-primitives = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }
derive_more = { workspace = true, features = ["full"] }
hex-literal = { workspace = true }
serde = { workspace = true }
sha3 = { workspace = true }
sp-staking = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true, features = ["std", "testing"] }
Expand All @@ -39,7 +40,7 @@ precompile-utils = { workspace = true, features = ["std", "testing"] }
pallet-balances = { workspace = true, features = ["std"] }
pallet-assets = { workspace = true, features = ["std"] }
pallet-timestamp = { workspace = true, features = ["std"] }
scale-info = { workspace = true, features = ["derive", "std"] }
scale-info = { workspace = true }
sp-io = { workspace = true, features = ["std"] }

[features]
Expand Down
19 changes: 10 additions & 9 deletions precompiles/tangle-lst/TangleLst.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ interface TangleLst {
/// @param root The root account of the pool.
/// @param nominator The nominator account of the pool.
/// @param bouncer The bouncer account of the pool.
function create(uint256 amount, bytes32 root, bytes32 nominator, bytes32 bouncer) external returns (uint8);

/// @dev Create a new pool with a specific pool ID.
/// @param amount The initial amount to create the pool with.
/// @param root The root account of the pool.
/// @param nominator The nominator account of the pool.
/// @param bouncer The bouncer account of the pool.
/// @param poolId The desired pool ID.
function createWithPoolId(uint256 amount, bytes32 root, bytes32 nominator, bytes32 bouncer, uint256 poolId) external returns (uint8);
/// @param name The name of the pool.
/// @param icon The icon of the pool.
function create(
uint256 amount,
bytes32 root,
bytes32 nominator,
bytes32 bouncer,
bytes calldata name,
bytes calldata icon
) external returns (uint8);

/// @dev Nominate validators for a pool.
/// @param poolId The ID of the pool.
Expand Down
Loading

0 comments on commit 88635cd

Please sign in to comment.