Skip to content

Commit

Permalink
feat: make origin pay burn on agent registration (#63)
Browse files Browse the repository at this point in the history
# Pull Request Checklist

Before submitting this PR, please make sure:

- [ ] You have run `cargo clippy` and addressed any warnings
- [ ] You have added appropriate tests (if applicable)
- [ ] You have updated the documentation (if applicable)
- [ ] You have reviewed your own code
- [ ] You have updated changelog (if applicable)

## Description

Please provide a brief description of the changes in this PR.

## Related Issues

Please link any related issues here
  • Loading branch information
devwckd authored Jan 11, 2025
1 parent e049489 commit cb03bf7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions pallets/governance/tests/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn register(account: AccountId, _unused: u16, module: AccountId, stake: u128) {
let _ = pallet_governance::whitelist::add_to_whitelist::<Test>(module);

assert_ok!(pallet_torus0::agent::register::<Test>(
module,
module,
b"agent".to_vec(),
b"url".to_vec(),
Expand Down
4 changes: 2 additions & 2 deletions pallets/torus0/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Agent<T: crate::Config> {
}

pub fn register<T: crate::Config>(
payer: AccountIdOf<T>,
agent_key: AccountIdOf<T>,
name: Vec<u8>,
url: Vec<u8>,
Expand All @@ -46,7 +47,6 @@ pub fn register<T: crate::Config>(
crate::Error::<T>::AgentAlreadyRegistered
);

// TODO: Take pruning scores into consideration
ensure!(
crate::Agents::<T>::iter().count() < crate::MaxAllowedAgents::<T>::get() as usize,
crate::Error::<T>::MaxAllowedAgents
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn register<T: crate::Config>(
let burn = crate::Burn::<T>::get();

let _ = <T as crate::Config>::Currency::withdraw(
&agent_key,
&payer,
burn,
WithdrawReasons::except(WithdrawReasons::TIP),
ExistenceRequirement::AllowDeath,
Expand Down
4 changes: 2 additions & 2 deletions pallets/torus0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ pub mod pallet {
url: Vec<u8>,
metadata: Vec<u8>,
) -> DispatchResult {
ensure_signed(origin)?;
agent::register::<T>(agent_key, name, url, metadata)
let payer = ensure_signed(origin)?;
agent::register::<T>(payer, agent_key, name, url, metadata)
}

#[pallet::call_index(4)]
Expand Down
21 changes: 20 additions & 1 deletion pallets/torus0/tests/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn register_correctly() {
));

assert_ok!(pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -39,6 +40,7 @@ fn register_without_being_whitelisted() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand Down Expand Up @@ -67,6 +69,7 @@ fn register_without_enough_balance() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -92,6 +95,7 @@ fn register_fail_name_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
"".as_bytes().to_vec(),
url.clone(),
Expand All @@ -102,6 +106,7 @@ fn register_fail_name_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
" ".repeat(pallet_torus0::MaxNameLength::<Test>::get() as usize + 1)
.as_bytes()
Expand All @@ -114,6 +119,7 @@ fn register_fail_name_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
vec![249u8, 9u8, 42u8],
url.clone(),
Expand All @@ -137,6 +143,7 @@ fn register_fail_url_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
"".as_bytes().to_vec(),
Expand All @@ -147,6 +154,7 @@ fn register_fail_url_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
" ".repeat(pallet_torus0::MaxNameLength::<Test>::get() as usize + 1)
Expand All @@ -159,6 +167,7 @@ fn register_fail_url_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
vec![249u8, 9u8, 42u8],
Expand All @@ -182,6 +191,7 @@ fn register_fail_metadata_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -196,6 +206,7 @@ fn register_fail_metadata_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -206,6 +217,7 @@ fn register_fail_metadata_validation() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -232,6 +244,7 @@ fn register_more_than_max_allowed_agents() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand Down Expand Up @@ -260,6 +273,7 @@ fn register_more_than_allowed_registrations_per_block() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand Down Expand Up @@ -290,6 +304,7 @@ fn register_more_than_registrations_per_interval() {

assert_err!(
pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -315,6 +330,7 @@ fn unregister_correctly() {
));

assert_ok!(pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand All @@ -340,6 +356,7 @@ fn unregister_twice() {
));

assert_ok!(pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand Down Expand Up @@ -369,7 +386,7 @@ fn update_correctly() {
));

assert_ok!(pallet_torus0::agent::register::<Test>(
agent, name, url, metadata,
agent, agent, name, url, metadata,
));

let new_name = "new-agent".as_bytes().to_vec();
Expand Down Expand Up @@ -412,6 +429,7 @@ fn update_with_zero_staking_fee() {
));

assert_ok!(pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand Down Expand Up @@ -461,6 +479,7 @@ fn update_with_zero_weight_control_fee() {
));

assert_ok!(pallet_torus0::agent::register::<Test>(
agent,
agent,
name.clone(),
url.clone(),
Expand Down
4 changes: 4 additions & 0 deletions pallets/torus0/tests/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn add_stake_correctly() {
assert_ok!(pallet_governance::whitelist::add_to_whitelist::<Test>(to));

assert_ok!(pallet_torus0::agent::register::<Test>(
to,
to,
"to".as_bytes().to_vec(),
"to://idk".as_bytes().to_vec(),
Expand Down Expand Up @@ -99,6 +100,7 @@ fn remove_stake_correctly() {
assert_ok!(pallet_governance::whitelist::add_to_whitelist::<Test>(to));

assert_ok!(pallet_torus0::agent::register::<Test>(
to,
to,
"to".as_bytes().to_vec(),
"to://idk".as_bytes().to_vec(),
Expand Down Expand Up @@ -137,6 +139,7 @@ fn remove_stake_with_less_than_required_amount() {

assert_ok!(pallet_governance::whitelist::add_to_whitelist::<Test>(to));
assert_ok!(pallet_torus0::agent::register::<Test>(
to,
to,
"to".as_bytes().to_vec(),
"to://idk".as_bytes().to_vec(),
Expand Down Expand Up @@ -180,6 +183,7 @@ fn remove_stake_with_unregistered_agent() {

assert_ok!(pallet_governance::whitelist::add_to_whitelist::<Test>(to));
assert_ok!(pallet_torus0::agent::register::<Test>(
to,
to,
"to".as_bytes().to_vec(),
"to://idk".as_bytes().to_vec(),
Expand Down

0 comments on commit cb03bf7

Please sign in to comment.