Skip to content

Commit

Permalink
Update soroban-sdk (stellar#131)
Browse files Browse the repository at this point in the history
* Update soroban-sdk

* typo
  • Loading branch information
leighmcculloch authored Oct 6, 2022
1 parent dda4f40 commit 62432ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ codegen-units = 1
lto = true

[patch.crates-io]
soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" }
soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" }
soroban-auth = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "864a309b" }
soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "2c1e802c" }
soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "2c1e802c" }
soroban-auth = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "2c1e802c" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "2c1e802c" }
soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" }
soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" }
soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "fb8a384e" }
Expand Down
20 changes: 10 additions & 10 deletions auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,51 @@

mod test;

use soroban_sdk::{contractimpl, contracttype, BigInt, Env, Invoker};
use soroban_sdk::{contractimpl, contracttype, Address, BigInt, Env};

#[contracttype]
pub enum DataKey {
SavedNum(Invoker),
SavedNum(Address),
Admin,
}

pub struct ExampleContract;

#[contractimpl]
impl ExampleContract {
/// Set the admin invoker.
/// Set the admin Address.
///
/// May be called only once unauthenticated, and
/// then only by current admin.
pub fn set_admin(env: Env, new_admin: Invoker) {
pub fn set_admin(env: Env, new_admin: Address) {
let admin = Self::admin(&env);
if let Some(admin) = admin {
assert_eq!(env.invoker(), admin);
}
env.data().set(DataKey::Admin, new_admin);
}

/// Set the number for an authenticated invoker.
/// Set the number for an authenticated address.
pub fn set_num(env: Env, num: BigInt) {
let id = env.invoker();
env.data().set(DataKey::SavedNum(id), num);
}

/// Get the number for an invoker.
pub fn num(env: Env, id: Invoker) -> Option<BigInt> {
/// Get the number for an Address.
pub fn num(env: Env, id: Address) -> Option<BigInt> {
env.data().get(DataKey::SavedNum(id)).map(Result::unwrap)
}

/// Overwrite any number for an invoker.
/// Overwrite any number for an Address.
/// Callable only by admin.
pub fn overwrite(env: Env, id: Invoker, num: BigInt) {
pub fn overwrite(env: Env, id: Address, num: BigInt) {
let admin = Self::admin(&env);
assert_eq!(Some(env.invoker()), admin);

env.data().set(DataKey::SavedNum(id), num);
}

fn admin(env: &Env) -> Option<Invoker> {
fn admin(env: &Env) -> Option<Address> {
env.data().get(DataKey::Admin).map(Result::unwrap)
}
}
16 changes: 8 additions & 8 deletions auth/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::*;

use soroban_sdk::{testutils::Accounts, BytesN, Env, Invoker};
use soroban_sdk::{testutils::Accounts, Address, BytesN, Env};

#[test]
fn test() {
Expand All @@ -13,23 +13,23 @@ fn test() {

// Initialize contract by setting the admin.
let admin = env.accounts().generate();
let admin_invoker = &Invoker::Account(admin.clone());
client.set_admin(admin_invoker);
let admin_address = &Address::Account(admin.clone());
client.set_admin(admin_address);

// Check if user 1 has a num, it doesn't yet.
let user1 = env.accounts().generate();
let user1_invoker = &Invoker::Account(user1.clone());
assert_eq!(client.num(user1_invoker), None);
let user1_address = &Address::Account(user1.clone());
assert_eq!(client.num(user1_address), None);

// Have user 1 set a num for themselves.
let five = BigInt::from_u32(&env, 5);
client.with_source_account(&user1).set_num(&five);
assert_eq!(client.num(user1_invoker), Some(five));
assert_eq!(client.num(user1_address), Some(five));

// Have admin overwrite user 1's num.
let ten = BigInt::from_u32(&env, 10);
client
.with_source_account(&admin)
.overwrite(user1_invoker, &ten);
assert_eq!(client.num(user1_invoker), Some(ten));
.overwrite(user1_address, &ten);
assert_eq!(client.num(user1_address), Some(ten));
}

0 comments on commit 62432ca

Please sign in to comment.