Skip to content

Commit

Permalink
Merge branch 'main' of github.com:webb-tools/tangle into services-pre…
Browse files Browse the repository at this point in the history
…compile
  • Loading branch information
1xstj committed Sep 10, 2024
2 parents 83deed7 + 2ad67b9 commit 39eb4ac
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 97 deletions.
2 changes: 1 addition & 1 deletion node/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Tangle. If not, see <http://www.gnu.org/licenses/>.
//! A collection of node-specific RPC methods.

#![allow(unused_imports)]
use jsonrpsee::RpcModule;
use std::sync::Arc;
// Substrate
Expand Down
12 changes: 10 additions & 2 deletions pallets/tangle-lst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ pub mod pallet {
#[pallet::constant]
type MaxUnbonding: Get<u32>;

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

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

Expand Down Expand Up @@ -950,6 +954,7 @@ pub mod pallet {
root: AccountIdLookupOf<T>,
nominator: AccountIdLookupOf<T>,
bouncer: AccountIdLookupOf<T>,
name: BoundedVec<u8, T::MaxNameLength>,
) -> DispatchResult {
let depositor = ensure_signed(origin)?;

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

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

/// Create a new delegation pool with a previously used pool id
Expand All @@ -976,13 +981,14 @@ pub mod pallet {
nominator: AccountIdLookupOf<T>,
bouncer: AccountIdLookupOf<T>,
pool_id: PoolId,
name: BoundedVec<u8, T::MaxNameLength>,
) -> 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)
Self::do_create(depositor, amount, root, nominator, bouncer, pool_id, name)
}

/// Nominate on behalf of the pool.
Expand Down Expand Up @@ -1507,6 +1513,7 @@ impl<T: Config> Pallet<T> {
nominator: AccountIdLookupOf<T>,
bouncer: AccountIdLookupOf<T>,
pool_id: PoolId,
name: BoundedVec<u8, T::MaxNameLength>,
) -> DispatchResult {
let root = T::Lookup::lookup(root)?;
let nominator = T::Lookup::lookup(nominator)?;
Expand Down Expand Up @@ -1535,6 +1542,7 @@ impl<T: Config> Pallet<T> {
bouncer: Some(bouncer),
depositor: who.clone(),
},
name,
);

bonded_pool.try_bond_funds(&who, amount, BondType::Create)?;
Expand Down
10 changes: 9 additions & 1 deletion pallets/tangle-lst/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ impl pallet_lst::Config for Runtime {
type PalletId = PoolsPalletId;
type MaxMetadataLen = MaxMetadataLen;
type MaxUnbonding = MaxUnbonding;
type MaxNameLength = ConstU32<50>;
type Fungibles = Assets;
type AssetId = AssetId;
type PoolId = PoolId;
Expand Down Expand Up @@ -404,7 +405,14 @@ impl ExtBuilder {
// make a pool
let amount_to_bond = Lst::depositor_min_bond();
<Runtime as Config>::Currency::make_free_balance_be(&10u32.into(), amount_to_bond * 5);
assert_ok!(Lst::create(RawOrigin::Signed(10).into(), amount_to_bond, 900, 901, 902));
assert_ok!(Lst::create(
RawOrigin::Signed(10).into(),
amount_to_bond,
900,
901,
902,
Default::default()
));
assert_ok!(Lst::set_metadata(RuntimeOrigin::signed(900), 1, vec![1, 1]));
let last_pool = LastPoolId::<Runtime>::get();
for (account_id, bonded) in self.members {
Expand Down
29 changes: 8 additions & 21 deletions pallets/tangle-lst/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,14 @@ fn test_setup_works() {
assert_eq!(TotalValueLocked::<T>::get(), 10);

let last_pool = LastPoolId::<Runtime>::get();
assert_eq!(
BondedPool::<Runtime>::get(last_pool).unwrap(),
BondedPool::<Runtime> {
id: last_pool,
inner: BondedPoolInner {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
},
}
);
assert_eq!(
RewardPools::<Runtime>::get(last_pool).unwrap(),
RewardPool::<Runtime> {
last_recorded_reward_counter: Zero::zero(),
last_recorded_total_payouts: 0,
total_rewards_claimed: 0,
total_commission_claimed: 0,
total_commission_pending: 0,
}
);
let bonded_pool = BondedPool::<Runtime>::get(last_pool).unwrap();

assert_eq!(bonded_pool.id, last_pool);

let inner = bonded_pool.inner;
assert_eq!(inner.commission, Commission::default());
assert_eq!(inner.roles, DEFAULT_ROLES);
assert_eq!(inner.state, PoolState::Open);

let bonded_account = Lst::create_bonded_account(last_pool);
let reward_account = Lst::create_reward_account(last_pool);
Expand Down
52 changes: 27 additions & 25 deletions pallets/tangle-lst/src/tests/bonded_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,49 @@ use frame_support::{assert_noop, assert_ok};
#[test]
fn test_setup_works() {
ExtBuilder::default().build_and_execute(|| {
// Check counts
assert_eq!(BondedPools::<Runtime>::count(), 1);
assert_eq!(RewardPools::<Runtime>::count(), 1);
assert_eq!(SubPoolsStorage::<Runtime>::count(), 0);
assert_eq!(UnbondingMembers::<Runtime>::count(), 0);

// Check staking duration
assert_eq!(StakingMock::bonding_duration(), 3);

// Check metadata
assert!(Metadata::<T>::contains_key(1));

// initial member.
// Check total value locked
assert_eq!(TotalValueLocked::<T>::get(), 10);

let last_pool = LastPoolId::<Runtime>::get();
assert_eq!(
BondedPool::<Runtime>::get(last_pool).unwrap(),
BondedPool::<Runtime> {
id: last_pool,
inner: BondedPoolInner {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
},
}
);
assert_eq!(
RewardPools::<Runtime>::get(last_pool).unwrap(),
RewardPool::<Runtime> {
last_recorded_reward_counter: Zero::zero(),
last_recorded_total_payouts: 0,
total_rewards_claimed: 0,
total_commission_claimed: 0,
total_commission_pending: 0,
}
);

// Check BondedPool
let bonded_pool = BondedPool::<Runtime>::get(last_pool).unwrap();
assert_eq!(bonded_pool.id, last_pool);
assert_eq!(bonded_pool.inner.commission, Commission::default());
assert_eq!(bonded_pool.inner.roles, DEFAULT_ROLES);
assert_eq!(bonded_pool.inner.state, PoolState::Open);

// Check RewardPool
let reward_pool = RewardPools::<Runtime>::get(last_pool).unwrap();
assert_eq!(reward_pool.last_recorded_reward_counter, Zero::zero());
assert_eq!(reward_pool.last_recorded_total_payouts, 0);
assert_eq!(reward_pool.total_rewards_claimed, 0);
assert_eq!(reward_pool.total_commission_claimed, 0);
assert_eq!(reward_pool.total_commission_pending, 0);

let bonded_account = Lst::create_bonded_account(last_pool);
let reward_account = Lst::create_reward_account(last_pool);

// the bonded_account should be bonded by the depositor's funds.
// Check bonded account stake
assert_eq!(StakingMock::active_stake(&bonded_account).unwrap(), 10);
assert_eq!(StakingMock::total_stake(&bonded_account).unwrap(), 10);

// but not nominating yet.
// Check nominations
assert!(Nominations::get().is_none());

// reward account should have an initial ED in it.
// Check reward account balance
assert_eq!(
Currency::free_balance(reward_account),
<Balances as CurrencyT<AccountId>>::minimum_balance()
Expand All @@ -66,6 +65,7 @@ fn balance_to_point_works() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
},
};

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

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

Expand Down
67 changes: 43 additions & 24 deletions pallets/tangle-lst/src/tests/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,29 @@ fn create_works() {
StakingMock::minimum_nominator_bond(),
123,
456,
789
789,
Default::default()
));

assert_eq!(TotalValueLocked::<T>::get(), 10 + StakingMock::minimum_nominator_bond());

assert_eq!(Currency::free_balance(11), 90);
assert_eq!(
BondedPool::<Runtime>::get(2).unwrap(),
BondedPool {
id: 2,
inner: BondedPoolInner {
commission: Commission::default(),
roles: PoolRoles {
depositor: 11,
root: Some(123),
nominator: Some(456),
bouncer: Some(789)
},
state: PoolState::Open,
}
}
);

let bonded_pool = BondedPool::<Runtime>::get(2).unwrap();

assert_eq!(bonded_pool.id, 2);

let inner = bonded_pool.inner;
assert_eq!(inner.commission, Commission::default());

let roles = inner.roles;
assert_eq!(roles.depositor, 11);
assert_eq!(roles.root, Some(123));
assert_eq!(roles.nominator, Some(456));
assert_eq!(roles.bouncer, Some(789));

assert_eq!(inner.state, PoolState::Open);

assert_eq!(
StakingMock::active_stake(&next_pool_stash).unwrap(),
StakingMock::minimum_nominator_bond()
Expand Down Expand Up @@ -69,7 +71,7 @@ fn create_errors_correctly() {

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

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

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

Expand All @@ -89,6 +91,7 @@ fn create_errors_correctly() {
commission: Commission::default(),
roles: DEFAULT_ROLES,
state: PoolState::Open,
metadata: PoolMetadata { name: BoundedVec::default() },
},
}
.put();
Expand All @@ -97,7 +100,7 @@ fn create_errors_correctly() {

// Then
assert_noop!(
Lst::create(RuntimeOrigin::signed(11), 20, 123, 456, 789),
Lst::create(RuntimeOrigin::signed(11), 20, 123, 456, 789, Default::default()),
Error::<Runtime>::MaxPools
);
});
Expand All @@ -114,24 +117,40 @@ fn create_with_pool_id_works() {
StakingMock::minimum_nominator_bond(),
123,
456,
789
789,
Default::default()
));

assert_eq!(Currency::free_balance(11), 90);
// delete the initial pool created, then pool_Id `1` will be free

assert_noop!(
Lst::create_with_pool_id(RuntimeOrigin::signed(12), 20, 234, 654, 783, 1),
Lst::create_with_pool_id(
RuntimeOrigin::signed(12),
20,
234,
654,
783,
1,
Default::default()
),
Error::<Runtime>::PoolIdInUse
);

assert_noop!(
Lst::create_with_pool_id(RuntimeOrigin::signed(12), 20, 234, 654, 783, 3),
Lst::create_with_pool_id(
RuntimeOrigin::signed(12),
20,
234,
654,
783,
3,
Default::default()
),
Error::<Runtime>::InvalidPoolId
);

// start dismantling the pool.
assert_ok!(Lst::set_state(RuntimeOrigin::signed(902), 1, PoolState::Destroying));
assert_ok!(fully_unbond_permissioned(10, 1));
});
}
Loading

0 comments on commit 39eb4ac

Please sign in to comment.