Skip to content

Commit

Permalink
Add previous custom types contract back in as simple_custom_types (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
sisuresh authored Aug 9, 2024
1 parent 75dcdad commit f42ae73
Show file tree
Hide file tree
Showing 7 changed files with 1,855 additions and 191 deletions.
216 changes: 28 additions & 188 deletions custom_types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,202 +1,42 @@
#![no_std]
use soroban_sdk::{
contract, contracterror, contractimpl, contracttype, symbol_short, vec, Address, Bytes, BytesN,
Env, Map, String, Symbol, Val, Vec, I256, U256,
};

const COUNTER: Symbol = symbol_short!("COUNTER");

#[contract]
pub struct CustomTypesContract;

/// This is from the rust doc above the struct Test
#[contracttype]
pub struct Test {
pub a: u32,
pub b: bool,
pub c: Symbol,
}

#[contracttype]
pub enum SimpleEnum {
First,
Second,
Third,
}
use soroban_sdk::{contract, contractimpl, contracttype, symbol_short, Env, Symbol};

#[contracttype]
#[derive(Clone, Copy)]
// The `repr` attribute is here to specify the memory alignment for this type
#[repr(u32)]
pub enum RoyalCard {
// TODO: create the fields here for your `RoyalCard` type
Jack = 11, // delete this
Queen = 12, // delete this
King = 13, // delete this
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct State {
pub count: u32,
pub last_incr: u32,
}

#[contracttype]
pub struct TupleStruct(Test, SimpleEnum);
const STATE: Symbol = symbol_short!("STATE");

#[contracttype]
pub enum ComplexEnum {
Struct(Test),
Tuple(TupleStruct),
Enum(SimpleEnum),
Asset(Address, i128),
Void,
}
#[contract]
pub struct IncrementContract;

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
/// Please provide an odd number
NumberMustBeOdd = 1,
}
#[contractimpl]
impl CustomTypesContract {
pub fn hello(_env: Env, hello: Symbol) -> Symbol {
hello
}

pub fn auth(env: Env, addr: Address, world: Symbol) -> Address {
addr.require_auth();
// Emit test event
env.events().publish(("auth",), world);

addr
}

// get current count
pub fn get_count(env: Env) -> u32 {
env.storage().persistent().get(&COUNTER).unwrap_or(0)
}

// increment count and return new one
pub fn inc(env: Env) -> u32 {
let mut count: u32 = env.storage().persistent().get(&COUNTER).unwrap_or(0); // Panic if the value of COUNTER is not u32.
count += 1;
env.storage().persistent().set(&COUNTER, &count);
count
}

pub fn woid(_env: Env) {
// do nothing
}

pub fn val(_env: Env) -> Val {
Val::default()
}

pub fn u32_fail_on_even(_env: Env, u32_: u32) -> Result<u32, Error> {
if u32_ % 2 == 1 {
Ok(u32_)
} else {
Err(Error::NumberMustBeOdd)
}
}

pub fn u32_(_env: Env, u32_: u32) -> u32 {
u32_
}

pub fn i32_(_env: Env, i32_: i32) -> i32 {
i32_
}

pub fn i64_(_env: Env, i64_: i64) -> i64 {
i64_
}

/// Example contract method which takes a struct
pub fn strukt_hel(env: Env, strukt: Test) -> Vec<Symbol> {
vec![&env, symbol_short!("Hello"), strukt.c]
}
impl IncrementContract {
/// Increment increments an internal counter, and returns the value.
pub fn increment(env: Env, incr: u32) -> u32 {
// Get the current count.
let mut state = Self::get_state(env.clone());

pub fn strukt(_env: Env, strukt: Test) -> Test {
strukt
}

pub fn simple(_env: Env, simple: SimpleEnum) -> SimpleEnum {
simple
}

pub fn complex(_env: Env, complex: ComplexEnum) -> ComplexEnum {
complex
}

pub fn addresse(_env: Env, addresse: Address) -> Address {
addresse
}

pub fn bytes(_env: Env, bytes: Bytes) -> Bytes {
bytes
}

pub fn bytes_n(_env: Env, bytes_n: BytesN<9>) -> BytesN<9> {
bytes_n
}

pub fn card(_env: Env, card: RoyalCard) -> RoyalCard {
card
}

pub fn boolean(_: Env, boolean: bool) -> bool {
boolean
}

/// Negates a boolean value
pub fn not(_env: Env, boolean: bool) -> bool {
!boolean
}

pub fn i128(_env: Env, i128: i128) -> i128 {
i128
}

pub fn u128(_env: Env, u128: u128) -> u128 {
u128
}

pub fn multi_args(_env: Env, a: u32, b: bool) -> u32 {
if b {
a
} else {
0
}
}

pub fn map(_env: Env, map: Map<u32, bool>) -> Map<u32, bool> {
map
}

pub fn vec(_env: Env, vec: Vec<u32>) -> Vec<u32> {
vec
}
// Increment the count.
state.count += incr;
state.last_incr = incr;

pub fn tuple(_env: Env, tuple: (Symbol, u32)) -> (Symbol, u32) {
tuple
}
// Save the count.
env.storage().instance().set(&STATE, &state);

/// Example of an optional argument
pub fn option(_env: Env, option: Option<u32>) -> Option<u32> {
option
// Return the count to the caller.
state.count
}

pub fn u256(_env: Env, u256: U256) -> U256 {
u256
}

pub fn i256(_env: Env, i256: I256) -> I256 {
i256
}

pub fn string(_env: Env, string: String) -> String {
string
}

pub fn tuple_strukt(_env: Env, tuple_strukt: TupleStruct) -> TupleStruct {
tuple_strukt
/// Return the current state.
pub fn get_state(env: Env) -> State {
env.storage().instance().get(&STATE).unwrap_or(State {
count: 0,
last_incr: 0,
}) // If no value set, assume 0.
}
}

mod test;
15 changes: 12 additions & 3 deletions custom_types/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ use soroban_sdk::Env;
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, CustomTypesContract);
let client = CustomTypesContractClient::new(&env, &contract_id);
assert_eq!(client.u32_fail_on_even(&1), 1);
let contract_id = env.register_contract(None, IncrementContract);
let client = IncrementContractClient::new(&env, &contract_id);

assert_eq!(client.increment(&1), 1);
assert_eq!(client.increment(&10), 11);
assert_eq!(
client.get_state(),
State {
count: 11,
last_incr: 10
}
);
}
Loading

0 comments on commit f42ae73

Please sign in to comment.