From 800a4f300d26c4072d3ff609d416d767bd3f31fe Mon Sep 17 00:00:00 2001 From: Alex Mootz Date: Thu, 9 Nov 2023 08:52:33 -0500 Subject: [PATCH] feat: add set_admin function --- pool/src/contract.rs | 22 +++++++++++++++- test-suites/tests/test_pool.rs | 46 ++++++++++++++++++++++++++++------ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/pool/src/contract.rs b/pool/src/contract.rs index e804d1c0..25b8cbc5 100644 --- a/pool/src/contract.rs +++ b/pool/src/contract.rs @@ -39,6 +39,15 @@ 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 @@ -46,7 +55,7 @@ pub trait Pool { /// /// ### 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 /// @@ -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); diff --git a/test-suites/tests/test_pool.rs b/test-suites/tests/test_pool.rs index 5f13fc6f..4a3ec7d2 100644 --- a/test-suites/tests/test_pool.rs +++ b/test-suites/tests/test_pool.rs @@ -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]; @@ -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(), @@ -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) ) ] @@ -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(),