Skip to content

Commit b7b2c20

Browse files
committed
5to workshop: Almacenamiento en los contratos
1 parent c6d838f commit b7b2c20

File tree

19 files changed

+280
-12
lines changed

19 files changed

+280
-12
lines changed
Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,62 @@
1-
use crate::interfaces::contract::RentACarContractTrait;
2-
use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env, Symbol};
1+
use crate::{
2+
interfaces::contract::RentACarContractTrait,
3+
storage::{
4+
admin::{read_admin, write_admin},
5+
car::{read_car, remove_car, write_car},
6+
rental::write_rental,
7+
structs::car::Car,
8+
structs::rental::Rental,
9+
token::write_token,
10+
types::car_status::CarStatus,
11+
},
12+
};
13+
14+
use soroban_sdk::{contract, contractimpl, Address, Env};
315

416
#[contract]
517
pub struct RentACarContract;
618

7-
pub const ADMIN_KEY: &Symbol = &symbol_short!("ADMIN");
8-
pub const TOKEN_KEY: &Symbol = &symbol_short!("TOKEN");
9-
1019
#[contractimpl]
1120
impl RentACarContractTrait for RentACarContract {
1221
fn __constructor(env: &Env, admin: Address, token: Address) {
13-
env.storage().instance().set(ADMIN_KEY, &admin);
14-
env.storage().instance().set(TOKEN_KEY, &token);
22+
write_admin(env, &admin);
23+
write_token(env, &token);
1524
}
1625

1726
fn get_admin(env: &Env) -> Address {
18-
env.storage()
19-
.instance()
20-
.get::<Symbol, Address>(ADMIN_KEY)
21-
.unwrap()
27+
read_admin(env)
28+
}
29+
30+
fn add_car(env: &Env, owner: Address, price_per_day: i128) {
31+
let car = Car {
32+
price_per_day,
33+
car_status: CarStatus::Available,
34+
};
35+
36+
write_car(env, &owner, &car);
37+
}
38+
39+
fn get_car_status(env: &Env, owner: Address) -> CarStatus {
40+
let car = read_car(env, &owner);
41+
42+
car.car_status
43+
}
44+
45+
fn rental(env: &Env, renter: Address, owner: Address, total_days_to_rent: u32, amount: i128) {
46+
let mut car = read_car(env, &owner);
47+
48+
car.car_status = CarStatus::Rented;
49+
50+
let rental = Rental {
51+
total_days_to_rent,
52+
amount,
53+
};
54+
55+
write_car(env, &owner, &car);
56+
write_rental(env, &renter, &owner, &rental);
57+
}
58+
59+
fn remove_car(env: &Env, owner: Address) {
60+
remove_car(env, &owner);
2261
}
2362
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
use soroban_sdk::{Address, Env};
22

3+
use crate::storage::types::car_status::CarStatus;
4+
35
pub trait RentACarContractTrait {
46
fn __constructor(env: &Env, admin: Address, token: Address);
57
fn get_admin(env: &Env) -> Address;
8+
fn add_car(env: &Env, owner: Address, price_per_day: i128);
9+
fn get_car_status(env: &Env, owner: Address) -> CarStatus;
10+
fn rental(env: &Env, renter: Address, owner: Address, total_days_to_rent: u32, amount: i128);
11+
fn remove_car(env: &Env, owner: Address);
612
}

contracts/rent-a-car/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![no_std]
2+
23
mod contract;
3-
pub mod interfaces;
4+
mod interfaces;
5+
mod storage;
46
mod tests;
57

68
pub use contract::RentACarContract;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use soroban_sdk::{Address, Env};
2+
3+
use super::types::storage::DataKey;
4+
5+
pub(crate) fn has_admin(env: &Env) -> bool {
6+
let key = DataKey::Admin;
7+
8+
env.storage().instance().has(&key)
9+
}
10+
11+
pub(crate) fn read_admin(env: &Env) -> Address {
12+
let key = DataKey::Admin;
13+
14+
env.storage().instance().get(&key).unwrap()
15+
}
16+
17+
pub(crate) fn write_admin(env: &Env, admin: &Address) {
18+
let key = DataKey::Admin;
19+
20+
env.storage().instance().set(&key, admin);
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use soroban_sdk::{Address, Env};
2+
3+
use crate::storage::{structs::car::Car, types::storage::DataKey};
4+
5+
pub(crate) fn has_car(env: &Env, owner: &Address) -> bool {
6+
env.storage().instance().has(&DataKey::Car(owner.clone()))
7+
}
8+
9+
pub(crate) fn read_car(env: &Env, owner: &Address) -> Car {
10+
env.storage()
11+
.instance()
12+
.get(&DataKey::Car(owner.clone()))
13+
.unwrap()
14+
}
15+
16+
pub(crate) fn write_car(env: &Env, owner: &Address, car: &Car) {
17+
env.storage()
18+
.instance()
19+
.set(&DataKey::Car(owner.clone()), car);
20+
}
21+
22+
pub(crate) fn remove_car(env: &Env, owner: &Address) {
23+
env.storage()
24+
.instance()
25+
.remove(&DataKey::Car(owner.clone()));
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub mod admin;
2+
pub mod car;
3+
pub mod rental;
4+
pub mod structs;
5+
pub mod token;
6+
pub mod types;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use soroban_sdk::{Address, Env};
2+
3+
use crate::storage::{structs::rental::Rental, types::storage::DataKey};
4+
5+
pub(crate) fn has_rental(env: &Env, renter: &Address, car_owner: &Address) -> bool {
6+
let key: DataKey = DataKey::Rental(renter.clone(), car_owner.clone());
7+
8+
env.storage().instance().has(&key)
9+
}
10+
11+
pub(crate) fn write_rental(env: &Env, renter: &Address, car_owner: &Address, rental: &Rental) {
12+
env.storage()
13+
.instance()
14+
.set(&DataKey::Rental(renter.clone(), car_owner.clone()), rental);
15+
}
16+
17+
pub(crate) fn read_rental(env: &Env, renter: &Address, car_owner: &Address) -> Rental {
18+
env.storage()
19+
.instance()
20+
.get(&DataKey::Rental(renter.clone(), car_owner.clone()))
21+
.unwrap()
22+
}
23+
24+
pub(crate) fn remove_rental(env: &Env, renter: &Address, car_owner: &Address) {
25+
env.storage()
26+
.instance()
27+
.remove(&DataKey::Rental(renter.clone(), car_owner.clone()));
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use soroban_sdk::contracttype;
2+
3+
use crate::storage::types::car_status::CarStatus;
4+
5+
#[derive(Clone)]
6+
#[contracttype]
7+
pub struct Car {
8+
pub price_per_day: i128,
9+
pub car_status: CarStatus,
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod car;
2+
pub mod rental;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use soroban_sdk::contracttype;
2+
3+
#[derive(Clone)]
4+
#[contracttype]
5+
pub struct Rental {
6+
pub total_days_to_rent: u32,
7+
pub amount: i128,
8+
}

0 commit comments

Comments
 (0)