Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_admin function #933

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions soroban-env-host/src/native_contract/token/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub trait TokenTrait {

fn set_admin(e: &Host, new_admin: Address) -> Result<(), HostError>;

fn admin(e: &Host) -> Result<Address, HostError>;

fn decimals(e: &Host) -> Result<u32, HostError>;

fn name(e: &Host) -> Result<String, HostError>;
Expand Down Expand Up @@ -329,6 +331,10 @@ impl TokenTrait for Token {
Ok(())
}

fn admin(e: &Host) -> Result<Address, HostError> {
read_administrator(e)
}

fn decimals(_e: &Host) -> Result<u32, HostError> {
// no need to load metadata since this is fixed for all SAC tokens
Ok(DECIMAL)
Expand Down
10 changes: 10 additions & 0 deletions soroban-env-host/src/native_contract/token/test_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ impl<'a> TestToken<'a> {
self.call_with_single_signer(admin, "set_admin", host_vec![self.host, new_admin])
}

pub(crate) fn admin(&self) -> Result<Address, HostError> {
self.host
.call(
self.address.clone().into(),
Symbol::try_from_val(self.host, &"admin")?,
host_vec![self.host].into(),
)?
.try_into_val(self.host)
}

pub(crate) fn decimals(&self) -> Result<u32, HostError> {
Ok(self
.host
Expand Down
11 changes: 11 additions & 0 deletions soroban-env-host/src/test/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ fn test_native_token_smart_roundtrip() {

// Also can't set a new admin (and there is no admin in the first place).
assert!(token.set_admin(&user, user.address(&test.host)).is_err());
assert!(token.admin().is_err());

assert_eq!(test.get_native_balance(&account_id), 100_000_000);
assert_eq!(
Expand Down Expand Up @@ -1166,11 +1167,21 @@ fn test_set_admin() {
test.create_default_account(&user);
test.create_default_trustline(&user);

assert_eq!(
token.admin().unwrap().to_sc_address().unwrap(),
admin.address(&test.host).to_sc_address().unwrap()
);

// Give admin rights to the new admin.
token
.set_admin(&admin, new_admin.address(&test.host))
.unwrap();

assert_eq!(
token.admin().unwrap().to_sc_address().unwrap(),
new_admin.address(&test.host).to_sc_address().unwrap()
);

// Make sure admin functions are unavailable to the old admin.
assert_eq!(
token
Expand Down