Skip to content

Commit c6d838f

Browse files
committed
feat: 4to workshop: deploy de contrato en testnet usando Soroban y Rust
1 parent b9a5977 commit c6d838f

File tree

11 files changed

+127
-0
lines changed

11 files changed

+127
-0
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/rent-a-car/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "rent-a-car"
3+
description = "Admin sets up cars, anyone can rent a car"
4+
edition.workspace = true
5+
license.workspace = true
6+
repository.workspace = true
7+
publish = false
8+
version.workspace = true
9+
10+
[lib]
11+
crate-type = ["cdylib"]
12+
doctest = false
13+
14+
[dependencies]
15+
soroban-sdk = { workspace = true }
16+
stellar-non-fungible = { workspace = true }
17+
stellar-default-impl-macro = { workspace = true }
18+
19+
[dev-dependencies]
20+
soroban-sdk = { workspace = true, features = ["testutils"] }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::interfaces::contract::RentACarContractTrait;
2+
use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env, Symbol};
3+
4+
#[contract]
5+
pub struct RentACarContract;
6+
7+
pub const ADMIN_KEY: &Symbol = &symbol_short!("ADMIN");
8+
pub const TOKEN_KEY: &Symbol = &symbol_short!("TOKEN");
9+
10+
#[contractimpl]
11+
impl RentACarContractTrait for RentACarContract {
12+
fn __constructor(env: &Env, admin: Address, token: Address) {
13+
env.storage().instance().set(ADMIN_KEY, &admin);
14+
env.storage().instance().set(TOKEN_KEY, &token);
15+
}
16+
17+
fn get_admin(env: &Env) -> Address {
18+
env.storage()
19+
.instance()
20+
.get::<Symbol, Address>(ADMIN_KEY)
21+
.unwrap()
22+
}
23+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use soroban_sdk::{Address, Env};
2+
3+
pub trait RentACarContractTrait {
4+
fn __constructor(env: &Env, admin: Address, token: Address);
5+
fn get_admin(env: &Env) -> Address;
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod contract;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![no_std]
2+
mod contract;
3+
pub mod interfaces;
4+
mod tests;
5+
6+
pub use contract::RentACarContract;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::tests::config::utils::create_token_contract;
2+
use crate::{contract::RentACarContractClient, RentACarContract};
3+
use soroban_sdk::{testutils::Address as _, token, Address, Env};
4+
5+
pub struct ContractTest<'a> {
6+
pub env: Env,
7+
pub contract: RentACarContractClient<'a>,
8+
pub admin: Address,
9+
pub token: (token::Client<'a>, token::StellarAssetClient<'a>, Address),
10+
}
11+
12+
impl<'a> ContractTest<'a> {
13+
pub fn setup() -> Self {
14+
let env = Env::default();
15+
16+
let admin = Address::generate(&env);
17+
let token_issuer = Address::generate(&env);
18+
19+
let (token_client, token_admin) = create_token_contract(&env, &token_issuer);
20+
21+
let contract_id = env.register(RentACarContract, (&admin, &token_client.address));
22+
let contract = RentACarContractClient::new(&env, &contract_id);
23+
24+
ContractTest {
25+
env,
26+
contract,
27+
admin,
28+
token: (token_client, token_admin, token_issuer),
29+
}
30+
}
31+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod contract;
2+
pub mod utils;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use soroban_sdk::{token, Address, Env};
2+
3+
pub(crate) fn create_token_contract<'a>(
4+
e: &Env,
5+
admin: &Address,
6+
) -> (token::Client<'a>, token::StellarAssetClient<'a>) {
7+
let addr = e.register_stellar_asset_contract_v2(admin.clone());
8+
9+
(
10+
token::Client::new(e, &addr.address()),
11+
token::StellarAssetClient::new(e, &addr.address()),
12+
)
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use crate::tests::config::contract::ContractTest;
2+
3+
#[test]
4+
pub fn test_initialize() {
5+
let ContractTest {
6+
contract, admin, ..
7+
} = ContractTest::setup();
8+
9+
let contract_admin = contract.get_admin();
10+
11+
assert_eq!(admin, contract_admin);
12+
}

0 commit comments

Comments
 (0)