Skip to content

Commit 866341f

Browse files
Merge pull request #17 from brozorec/stellar-fixes
Stellar: fixes and improvements
2 parents b634988 + 1e3cc26 commit 866341f

File tree

14 files changed

+240
-236
lines changed

14 files changed

+240
-236
lines changed

content/stellar-contracts/access/access-control.mdx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,48 +46,54 @@ Roles exist only through their relationships with accounts, so a role with zero
4646
Here’s a simple example of using the Access Control module:
4747

4848
```rust
49-
use soroban_sdk::contract, contractimpl, symbol_short, Address, Env;
50-
use stellar_access::access_control::self as access_control, AccessControl;
51-
use stellar_macros::has_role, only_admin;
49+
use soroban_sdk::{contract, contractimpl, symbol_short, Address, Env};
50+
use stellar_access::access_control::{self as access_control, AccessControl};
51+
use stellar_macros::{has_role, only_admin};
5252

5353
#[contract]
5454
pub struct MyContract;
5555

5656
#[contractimpl]
57-
impl MyContract
58-
pub fn __constructor(e: &Env, admin: Address)
57+
impl MyContract {
58+
pub fn __constructor(e: &Env, admin: Address) {
5959
// Set the contract admin
6060
access_control::set_admin(e, &admin);
6161

6262
// Create a "minter" role with admin as its admin
6363
access_control::set_role_admin_no_auth(e, &symbol_short!("minter"), &symbol_short!("admin"));
64-
64+
}
65+
6566

6667
#[only_admin]
67-
pub fn admin_restricted_function(e: &Env) -> Vec<String>
68+
pub fn admin_restricted_function(e: &Env) -> Vec<String> {
6869
vec![&e, String::from_str(e, "seems sus")]
69-
70+
}
71+
7072

7173
// we want `require_auth()` provided by the macro, since there is no
7274
// `require_auth()` in `Base::mint`.
7375
#[only_role(caller, "minter")]
74-
pub fn mint(e: &Env, caller: Address, to: Address, token_id: u32)
76+
pub fn mint(e: &Env, caller: Address, to: Address, token_id: u32) {
7577
Base::mint(e, &to, token_id)
76-
78+
79+
}
80+
7781

7882
// allows either minter or burner role, does not enforce `require_auth` in the macro
7983
#[has_any_role(caller, ["minter", "burner"])]
80-
pub fn multi_role_action(e: &Env, caller: Address) -> String
84+
pub fn multi_role_action(e: &Env, caller: Address) -> String {
8185
caller.require_auth();
8286
String::from_str(e, "multi_role_action_success")
83-
87+
}
88+
8489

8590
// allows either minter or burner role AND enforces `require_auth` in the macro
8691
#[only_any_role(caller, ["minter", "burner"])]
87-
pub fn multi_role_auth_action(e: &Env, caller: Address) -> String
88-
String::from_str(e, "multi_role_auth_action_success")
89-
92+
pub fn multi_role_auth_action(e: &Env, caller: Address) -> String {
9093

94+
String::from_str(e, "multi_role_auth_action_success")
95+
}
96+
}
9197
```
9298

9399
## Benefits and Trade-offs

content/stellar-contracts/access/ownable.mdx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ This feature is useful for contracts that need to become fully decentralized aft
2929

3030
### Procedural Macro
3131

32-
The module includes a procedural macro to simplify owner authorization checks:
33-
34-
#### @only_owner
35-
36-
Ensures the caller is the owner before executing the function:
32+
The module includes the `#[only_owner]` procedural macro to simplify authorization checks by ensuring the owner is properly authorized.
3733

3834
```rust
3935
#[only_owner]
40-
pub fn restricted_function(e: &Env, other_param: u32)
36+
pub fn restricted_function(e: &Env, other_param: u32) {
4137
// Function body - only accessible to owner
42-
38+
}
4339
```
4440

4541
This expands to code that retrieves the owner from storage and requires authorization before executing the function body.
@@ -49,32 +45,32 @@ This expands to code that retrieves the owner from storage and requires authoriz
4945
Here’s a simple example of using the Ownable module:
5046

5147
```rust
52-
use soroban_sdk::contract, contractimpl, Address, Env;
53-
use stellar_access::ownable::self as ownable, Ownable;
48+
use soroban_sdk::{contract, contractimpl, Address, Env};
49+
use stellar_access::ownable::{self as ownable, Ownable};
5450
use stellar_macros::only_owner;
5551

5652
#[contract]
5753
pub struct MyContract;
5854

5955
#[contractimpl]
60-
impl MyContract
61-
pub fn __constructor(e: &Env, initial_owner: Address)
56+
impl MyContract {
57+
pub fn __constructor(e: &Env, initial_owner: Address) {
6258
// Set the contract owner
6359
ownable::set_owner(e, &initial_owner);
64-
60+
}
6561

6662
#[only_owner]
67-
pub fn update_config(e: &Env, new_value: u32)
63+
pub fn update_config(e: &Env, new_value: u32) {
6864
// Only the owner can call this function
6965
// Implementation...
70-
66+
}
7167

7268
// This function is accessible to anyone
73-
pub fn get_config(e: &Env) -> u32
69+
pub fn get_config(e: &Env) -> u32 {
7470
// Implementation...
7571
42
76-
77-
72+
}
73+
}
7874
```
7975

8076
## Benefits and Trade-offs

content/stellar-contracts/helpers/default-impl-macro.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ developers carefully consider and explicitly define their authorization logic ra
4343

4444
## Usage
4545

46-
To use the `<mark>[default_impl]` macro, place it above the `</mark>[contractimpl]` macro when implementing one of the supported traits:
46+
To use the `#[default_impl]` macro, place it above the `#[contractimpl]` macro when implementing one of the supported traits:
4747

4848
```rust
4949
#[default_impl] // IMPORTANT: place this above `#[contractimpl]`
5050
#[contractimpl]
51-
impl NonFungibleToken for MyContract
51+
impl NonFungibleToken for MyContract {
5252
type ContractType = Base;
5353

5454
// Only override the methods you need to customize
5555
// All other methods will be automatically implemented with their default behavior
56-
56+
}
5757
```
5858

5959
## How It Works
@@ -73,7 +73,7 @@ This process ensures that all trait methods are available to the client generate
7373
### Fungible Token Example
7474

7575
```rust
76-
use soroban_sdk::contract, contractimpl, Address, Env;
76+
use soroban_sdk::{contract, contractimpl, Address, Env};
7777
use stellar_tokens::fungible::FungibleToken;
7878
use stellar_macros::default_impl;
7979

@@ -82,14 +82,14 @@ pub struct MyToken;
8282

8383
#[default_impl]
8484
#[contractimpl]
85-
impl FungibleToken for MyToken
85+
impl FungibleToken for MyToken {
8686
type ContractType = Base;
8787

8888
// Only override methods that need custom behavior
89-
fn transfer(e: &Env, from: Address, to: Address, amount: i128)
89+
fn transfer(e: &Env, from: Address, to: Address, amount: i128) {
9090
// custom transfer logic here
91-
91+
}
9292

9393
// All other FungibleToken methods will be automatically implemented
94-
94+
}
9595
```

content/stellar-contracts/index.mdx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22
title: Stellar Smart Contracts Suite
33
---
44

5-
A comprehensive collection of secure, scalable smart contracts and utilities for the Stellar network,
6-
supporting Fungible, Non-Fungible, and Multi-Token standards.
5+
Explore our comprehensive suite of secure and scalable smart contract utilities for Stellar Soroban. Our libraries provide robust implementations for fungible and non-fungible tokens, along with powerful tools for access control and contract management.
76

87
## Tokens
9-
Explore our implementations for token standards on Stellar Soroban:
108

11-
* ***[Fungible Tokens](/stellar-contracts/tokens/fungible/fungible)***: Digital assets representing a fixed or dynamic supply of identical units.
12-
* ***[Non-Fungible Tokens](/stellar-contracts/tokens/non-fungible/non-fungible)***: Unique digital assets with verifiable ownership.
13-
* ***Multi-Token***: Hybrid tokens enabling both fungible and non-fungible token functionalities (work in progress).
9+
* **[Fungible Tokens](/stellar-contracts/tokens/fungible/fungible)**: Digital assets representing a fixed or dynamic supply of identical units.
10+
* **[Non-Fungible Tokens (NFTs)](/stellar-contracts/tokens/non-fungible/non-fungible)**: Unique digital assets with verifiable ownership.
11+
* **Multi-Tokens**: Hybrid tokens enabling both fungible and non-fungible token functionalities (work in progress).
12+
13+
## Access Control
14+
15+
* **[Ownable](/stellar-contracts/access/ownable)**: A simple mechanism with a single account authorized for all privileged actions.
16+
* **[Role-Based Access Control](/stellar-contracts/access/access-control)**: A flexible mechanism with distinct roles for each privileged action.
1417

1518
## Utilities
16-
Discover our utility contracts for Stellar Soroban, applicable to all token standards mentioned above:
1719

18-
* ***[Pausable](/stellar-contracts/utils/pausable)***
19-
* ***[Upgrades and Migrations](/stellar-contracts/utils/upgradeable)***
20+
* **[Pausable](/stellar-contracts/utils/pausable)**: Pause and unpause contract functions, useful for emergency response.
21+
* **[Upgradeable](/stellar-contracts/utils/upgradeable)**: Manage contract upgrades and data migrations seamlessly.
22+
* **[Cryptography](/stellar-contracts/utils/crypto)**: A set of cryptographic primitives and utilities for Soroban contracts.
23+
24+
## Security and Audits
25+
26+
Our contracts are built with security as a top priority. You can find our audit reports [here](https://github.com/OpenZeppelin/stellar-contracts/tree/main/audits).
2027

2128
## Error Codes
2229
In Stellar Soroban, each error variant is assigned an integer. To prevent duplication of error codes,

content/stellar-contracts/tokens/fungible/fungible.mdx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ and players can transfer tokens between accounts.
3131
Here’s what a basic fungible token contract might look like:
3232

3333
```rust
34-
use soroban_sdk::contract, contractimpl, Address, Env, String;
35-
use stellar_tokens::fungible::burnable::FungibleBurnable, Base, ContractOverrides, FungibleToken;
36-
use stellar_access::ownable::self as ownable, Ownable;
37-
use stellar_macros::default_impl, only_owner;
34+
use soroban_sdk::{contract, contractimpl, Address, Env, String};
35+
use stellar_tokens::fungible::{burnable::FungibleBurnable, Base, ContractOverrides, FungibleToken};
36+
use stellar_access::ownable::{self as ownable, Ownable};
37+
use stellar_macros::{default_impl, only_owner};
3838

3939
#[contract]
4040
pub struct GameCurrency;
4141

4242
#[contractimpl]
43-
impl GameCurrency
44-
pub fn __constructor(e: &Env, initial_owner: Address)
43+
impl GameCurrency {
44+
pub fn __constructor(e: &Env, initial_owner: Address) {
4545
// Set token metadata
4646
Base::set_metadata(
4747
e,
@@ -52,24 +52,24 @@ impl GameCurrency
5252

5353
// Set the contract owner
5454
ownable::set_owner(e, &initial_owner);
55-
55+
}
5656

5757
#[only_owner]
58-
pub fn mint_tokens(e: &Env, to: Address, amount: i128)
58+
pub fn mint_tokens(e: &Env, to: Address, amount: i128) {
5959
// Mint tokens to the recipient
6060
Base::mint(e, &to, amount);
61-
62-
61+
}
62+
}
6363

6464
#[default_impl]
6565
#[contractimpl]
66-
impl FungibleToken for GameCurrency
66+
impl FungibleToken for GameCurrency {
6767
type ContractType = Base;
68-
68+
}
6969

7070
#[default_impl]
7171
#[contractimpl]
72-
impl FungibleBurnable for GameCurrency
72+
impl FungibleBurnable for GameCurrency {}
7373
```
7474

7575
## Extensions
@@ -103,12 +103,6 @@ The `FungibleBlockList` trait extends the `FungibleToken` trait to provide a blo
103103
can be managed by an authorized account. This extension ensures that blocked accounts cannot transfer/receive
104104
tokens, or approve token transfers.
105105

106-
### TokenInterface Macro
107-
108-
For contracts that implement both `FungibleToken` and `FungibleBurnable` and also need to implement
109-
`soroban_sdk::token::TokenInterface`, we provide the `impl_token_interface!` macro. This macro automatically
110-
generates the required boilerplate, simplifying the implementation process.
111-
112106
## Utility Modules
113107

114108
The package includes utility modules to help with common token implementation patterns:

0 commit comments

Comments
 (0)