Skip to content

Commit 982bcd4

Browse files
committed
6to workshop: Deposito y retiro en los contratos
1 parent b7b2c20 commit 982bcd4

File tree

13 files changed

+130
-2
lines changed

13 files changed

+130
-2
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::{
22
interfaces::contract::RentACarContractTrait,
3+
methods::token::token::token_transfer,
34
storage::{
45
admin::{read_admin, write_admin},
56
car::{read_car, remove_car, write_car},
7+
contract_balance::{read_contract_balance, write_contract_balance},
68
rental::write_rental,
79
structs::car::Car,
810
structs::rental::Rental,
@@ -31,6 +33,7 @@ impl RentACarContractTrait for RentACarContract {
3133
let car = Car {
3234
price_per_day,
3335
car_status: CarStatus::Available,
36+
available_to_withdraw: 0,
3437
};
3538

3639
write_car(env, &owner, &car);
@@ -43,17 +46,41 @@ impl RentACarContractTrait for RentACarContract {
4346
}
4447

4548
fn rental(env: &Env, renter: Address, owner: Address, total_days_to_rent: u32, amount: i128) {
49+
renter.require_auth();
50+
4651
let mut car = read_car(env, &owner);
4752

4853
car.car_status = CarStatus::Rented;
54+
car.available_to_withdraw += amount;
4955

5056
let rental = Rental {
5157
total_days_to_rent,
5258
amount,
5359
};
5460

61+
let mut contract_balance = read_contract_balance(&env);
62+
contract_balance += amount;
63+
64+
write_contract_balance(&env, &contract_balance);
5565
write_car(env, &owner, &car);
5666
write_rental(env, &renter, &owner, &rental);
67+
68+
token_transfer(&env, &renter, &env.current_contract_address(), &amount);
69+
}
70+
71+
fn payout_owner(env: &Env, owner: Address, amount: i128) {
72+
owner.require_auth();
73+
74+
let mut car = read_car(&env, &owner);
75+
let mut contract_balance = read_contract_balance(&env);
76+
77+
car.available_to_withdraw -= amount;
78+
contract_balance -= amount;
79+
80+
write_car(&env, &owner, &car);
81+
write_contract_balance(&env, &contract_balance);
82+
83+
token_transfer(&env, &env.current_contract_address(), &owner, &amount);
5784
}
5885

5986
fn remove_car(env: &Env, owner: Address) {

contracts/rent-a-car/src/interfaces/contract.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pub trait RentACarContractTrait {
99
fn get_car_status(env: &Env, owner: Address) -> CarStatus;
1010
fn rental(env: &Env, renter: Address, owner: Address, total_days_to_rent: u32, amount: i128);
1111
fn remove_car(env: &Env, owner: Address);
12+
fn payout_owner(env: &Env, owner: Address, amount: i128);
1213
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
mod contract;
44
mod interfaces;
5+
mod methods;
56
mod storage;
67
mod tests;
78

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod token;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod token;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use soroban_sdk::{
2+
token::{self},
3+
Address, Env,
4+
};
5+
6+
use crate::storage::token::read_token;
7+
8+
pub fn token_transfer(env: &Env, from: &Address, to: &Address, amount: &i128) {
9+
let token_address = read_token(env);
10+
let token = token::TokenClient::new(env, &token_address);
11+
token.transfer(from, to, amount);
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use soroban_sdk::Env;
2+
3+
use crate::storage::types::storage::DataKey;
4+
5+
pub fn read_contract_balance(env: &Env) -> i128 {
6+
env.storage()
7+
.persistent()
8+
.get(&DataKey::ContractBalance)
9+
.unwrap_or(0)
10+
}
11+
12+
pub fn write_contract_balance(env: &Env, amount: &i128) {
13+
env.storage()
14+
.persistent()
15+
.set(&DataKey::ContractBalance, amount);
16+
}

contracts/rent-a-car/src/storage/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod admin;
22
pub mod car;
3+
pub mod contract_balance;
34
pub mod rental;
45
pub mod structs;
56
pub mod token;

contracts/rent-a-car/src/storage/structs/car.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ use crate::storage::types::car_status::CarStatus;
77
pub struct Car {
88
pub price_per_day: i128,
99
pub car_status: CarStatus,
10+
pub available_to_withdraw: i128,
1011
}

contracts/rent-a-car/src/storage/types/storage.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use soroban_sdk::{contracttype, Address};
55
pub enum DataKey {
66
Admin,
77
Token,
8+
ContractBalance,
89
Car(Address),
910
Rental(Address, Address),
1011
}

0 commit comments

Comments
 (0)