Skip to content

Commit

Permalink
feat: add set_admin function
Browse files Browse the repository at this point in the history
  • Loading branch information
mootz12 committed Nov 9, 2023
1 parent 0dafadd commit 800a4f3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
22 changes: 21 additions & 1 deletion pool/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ pub trait Pool {
usdc_id: Address,
);

/// (Admin only) Set a new address as the admin of this pool
///
/// ### Arguments
/// * `new_admin` - The new admin address
///
/// ### Panics
/// If the caller is not the admin
fn set_admin(e: Env, new_admin: Address);

/// (Admin only) Update the pool
///
/// ### Arguments
/// * `backstop_take_rate` - The new take rate for the backstop
///
/// ### Panics
/// If the caller is not the admin
fn update_pool(e: Env, backstiop_take_rate: u64);
fn update_pool(e: Env, backstop_take_rate: u64);

/// (Admin only) Initialize a reserve in the pool
///
Expand Down Expand Up @@ -233,6 +242,17 @@ impl Pool for PoolContract {
);
}

fn set_admin(e: Env, new_admin: Address) {
storage::bump_instance(&e);
let admin = storage::get_admin(&e);
admin.require_auth();

storage::set_admin(&e, &new_admin);

e.events()
.publish((Symbol::new(&e, "set_admin"), admin), new_admin);
}

fn update_pool(e: Env, backstop_take_rate: u64) {
storage::bump_instance(&e);
let admin = storage::get_admin(&e);
Expand Down
46 changes: 38 additions & 8 deletions test-suites/tests/test_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ fn test_pool_user() {
/// Does not test internal state management of the lending pool, only external effects.
#[test]
fn test_pool_config() {
let fixture = create_fixture_with_data(true);
let fixture = create_fixture_with_data(false);

let pool_fixture = &fixture.pools[0];

Expand Down Expand Up @@ -682,12 +682,46 @@ fn test_pool_config() {
]
);

// Set admin (admin only)
let new_admin = Address::random(&fixture.env);
pool_fixture.pool.set_admin(&new_admin);
assert_eq!(
fixture.env.auths()[0],
(
fixture.bombadil.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
pool_fixture.pool.address.clone(),
Symbol::new(&fixture.env, "set_admin"),
vec![&fixture.env, new_admin.to_val(),]
)),
sub_invocations: std::vec![]
}
)
);
let event = vec![&fixture.env, fixture.env.events().all().last_unchecked()];
assert_eq!(
event,
vec![
&fixture.env,
(
pool_fixture.pool.address.clone(),
(
Symbol::new(&fixture.env, "set_admin"),
fixture.bombadil.clone()
)
.into_val(&fixture.env),
new_admin.into_val(&fixture.env)
)
]
);

// Set status (admin only)
pool_fixture.pool.set_status(&1);
assert_eq!(
fixture.env.auths()[0],
(
fixture.bombadil.clone(),
new_admin.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
pool_fixture.pool.address.clone(),
Expand All @@ -707,11 +741,7 @@ fn test_pool_config() {
&fixture.env,
(
pool_fixture.pool.address.clone(),
(
Symbol::new(&fixture.env, "set_status"),
fixture.bombadil.clone()
)
.into_val(&fixture.env),
(Symbol::new(&fixture.env, "set_status"), new_admin.clone()).into_val(&fixture.env),
1u32.into_val(&fixture.env)
)
]
Expand Down Expand Up @@ -758,7 +788,7 @@ fn test_pool_config() {
assert_eq!(
fixture.env.auths()[0],
(
fixture.bombadil.clone(),
new_admin.clone(),
AuthorizedInvocation {
function: AuthorizedFunction::Contract((
pool_fixture.pool.address.clone(),
Expand Down

0 comments on commit 800a4f3

Please sign in to comment.