Skip to content

Commit 4e8e668

Browse files
authored
Merge pull request #24 from make-software/feature/docs
Reputation Token and utils documentation.
2 parents 30a3c3d + 136ee01 commit 4e8e668

19 files changed

Lines changed: 240 additions & 62 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ lint: clippy
2121
clean:
2222
cargo clean
2323
rm -rf tests/wasm/*.wasm
24+
25+
docs:
26+
cargo doc --features test-support --no-deps --open

README.md

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,29 @@
1-
# contracts
1+
# MVPR DAO for Casper
22

3-
Voting contract
4-
data:
5-
- voting_address - Address
3+
Reusable smart contracts for building DAOs on top of Casper.
64

7-
Reputation contract
8-
data:
9-
- reputation_address - Address
10-
- owner - Address
11-
- whitelist - Vec<Address>
5+
Repository contains following modules:
6+
- `contract` provides smart contracts implementation,
7+
- `utils` and `macros` makes writing code easier,
8+
- `tests` contain integration tests,
9+
- `client` implements a JavaScript client for smart contracts interactions.
1210

13-
methods:
14-
only_onwer:
15-
- change_onwership(new_owner: Address)
16-
- add_to_whitelist(addr: Address)
17-
- remove_from_whitelist(addr: Address)
18-
19-
whitelist_only:
20-
- mint
21-
- burn
22-
- transfer_from
23-
- stake
11+
## Build contracts
12+
Build `WASM` files.
2413

25-
Variable Repository
26-
data:
27-
...
28-
29-
methods:
30-
only_onwer:
31-
- change_onwership(new_owner: Address)
32-
- add_to_whitelist(addr: Address)
33-
- remove_from_whitelist(addr: Address)
34-
35-
whitelist_only:
36-
- set_string(name: String, value: String)
37-
- set_u256(name: String, value: U256)
14+
```bash
15+
$ make build-contracts
16+
```
3817

39-
all:
40-
- get_string(name: String) -> String
41-
- get_u256(name: String) -> U256
18+
## Test
19+
Run integration tests.
4220

43-
Master Voting Contract:
44-
- vote for whitelist changes.
21+
```bash
22+
$ make test
23+
```
24+
25+
## Docs
26+
Generate `rustdoc`. Opens a new browser window.
27+
```bash
28+
$ make docs
29+
```

contracts/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ path = "bin/reputation_contract.rs"
1414
bench = false
1515
doctest = false
1616
test = false
17+
doc = false
1718

1819
[profile.release]
1920
codegen-units = 1

contracts/src/reputation.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,108 @@ use casper_types::{
1717
EntryPointType, EntryPoints, Group, RuntimeArgs, URef, U256,
1818
};
1919

20+
/// Interface of the Reputation Contract.
21+
///
22+
/// It should be implemented by [`ReputationContract`], [`ReputationContractCaller`]
23+
/// and [`ReputationContractTest`].
2024
pub trait ReputationContractInterface {
25+
/// Constructor method.
26+
///
27+
/// It initializes contract elements:
28+
/// * Events dictionary.
29+
/// * Named keys of [`TokenWithStaking`], [`Owner`] and [`Whitelist`].
30+
/// * Set [`caller`] as the owner of the contract.
31+
/// * Add [`caller`] to the whitelist.
32+
///
33+
/// It emits [`OwnerChanged`](casper_dao_utils::owner::events::OwnerChanged),
34+
/// [`AddedToWhitelist`](casper_dao_utils::whitelist::events::AddedToWhitelist) events.
2135
fn init(&mut self);
36+
37+
/// Mint new tokens. Add `amount` of new tokens to the balance of the `recipient` and
38+
/// increment the total supply. Only whitelisted addresses are permited to call this method.
39+
///
40+
/// It throws [`NotWhitelisted`](casper_dao_utils::Error::NotWhitelisted) if caller
41+
/// is not whitelisted.
42+
///
43+
/// It emits [`Mint`](casper_dao_utils::token::events::Mint) event.
2244
fn mint(&mut self, recipient: Address, amount: U256);
45+
46+
/// Burn existing tokens. Remove `amount` of existing tokens from the balance of the `owner`
47+
/// and decrement the total supply. Only whitelisted addresses are permited to call this
48+
/// method.
49+
///
50+
/// It throws [`NotWhitelisted`](casper_dao_utils::Error::NotWhitelisted) if caller
51+
/// is not whitelisted.
52+
///
53+
/// It emits [`Burn`](casper_dao_utils::token::events::Burn) event.
2354
fn burn(&mut self, owner: Address, amount: U256);
55+
56+
/// Transfer `amount` of tokens from `owner` to `recipient`. Only whitelisted addresses are
57+
/// permited to call this method.
58+
///
59+
/// It throws [`NotWhitelisted`](casper_dao_utils::Error::NotWhitelisted) if caller
60+
/// is not whitelisted.
61+
///
62+
/// It throws [`InsufficientBalance`](casper_dao_utils::Error::InsufficientBalance)
63+
/// if `recipient`'s balance is less then `amount`.
64+
///
65+
/// It emits [`Transfer`](casper_dao_utils::token::events::Transfer) event.
2466
fn transfer_from(&mut self, owner: Address, recipient: Address, amount: U256);
67+
68+
/// Change ownership of the contract. Transfer the ownership to the `owner`. Only current owner
69+
/// is permited to call this method.
70+
///
71+
/// It throws [`NotAnOwner`](casper_dao_utils::Error::NotAnOwner) if caller
72+
/// is not the current owner.
73+
///
74+
/// It emits [`OwnerChanged`](casper_dao_utils::owner::events::OwnerChanged),
75+
/// [`AddedToWhitelist`](casper_dao_utils::whitelist::events::AddedToWhitelist) events.
2576
fn change_ownership(&mut self, owner: Address);
77+
78+
/// Add new address to the whitelist.
79+
///
80+
/// It throws [`NotAnOwner`](casper_dao_utils::Error::NotAnOwner) if caller
81+
/// is not the current owner.
82+
///
83+
/// It emits [`AddedToWhitelist`](casper_dao_utils::whitelist::events::AddedToWhitelist) event.
2684
fn add_to_whitelist(&mut self, address: Address);
85+
86+
/// Remove address from the whitelist.
87+
///
88+
/// It throws [`NotAnOwner`](casper_dao_utils::Error::NotAnOwner) if caller
89+
/// is not the current owner.
90+
///
91+
/// It emits [`RemovedFromWhitelist`](casper_dao_utils::whitelist::events::RemovedFromWhitelist)
92+
/// event.
2793
fn remove_from_whitelist(&mut self, address: Address);
94+
95+
/// Stake `amount` of tokens for the `address`. It decrements `address`'s balance by `amount`.
96+
///
97+
/// It throws [`NotAnOwner`](casper_dao_utils::Error::NotAnOwner) if caller
98+
/// is not the current owner.
99+
///
100+
/// It throws [`InsufficientBalance`](casper_dao_utils::Error::InsufficientBalance)
101+
/// if `address`'s balance is less then `amount`.
102+
///
103+
/// It emits [`TokensStaked`](casper_dao_utils::staking::events::TokensStaked)
104+
/// event.
28105
fn stake(&mut self, address: Address, amount: U256);
106+
107+
/// Unstake `amount` of tokens for the `address`. It increments `address`'s balance by
108+
/// `amount`.
109+
///
110+
/// It throws [`NotAnOwner`](casper_dao_utils::Error::NotAnOwner) if caller
111+
/// is not the current owner.
112+
///
113+
/// It throws [`InsufficientBalance`](casper_dao_utils::Error::InsufficientBalance)
114+
/// if `address`'s staked amount is less then `amount`.
115+
///
116+
/// It emits [`TokensUnstaked`](casper_dao_utils::staking::events::TokensUnstaked)
117+
/// event.
29118
fn unstake(&mut self, address: Address, amount: U256);
30119
}
31120

121+
/// Implementation of the Reputation Contract. See [`ReputationContractInterface`].
32122
#[derive(Default)]
33123
pub struct ReputationContract {
34124
pub token: TokenWithStaking,
@@ -148,6 +238,7 @@ impl ReputationContract {
148238
}
149239
}
150240

241+
/// Implementation of the Reputation Contract Caller. See [`ReputationContractInterface`].
151242
pub struct ReputationContractCaller {
152243
contract_package_hash: ContractPackageHash,
153244
}
@@ -275,6 +366,7 @@ mod tests {
275366

276367
use crate::{ReputationContract, ReputationContractInterface};
277368

369+
/// Implementation of the Reputation Contract Test. See [`ReputationContractInterface`].
278370
pub struct ReputationContractTest {
279371
env: TestEnv,
280372
package_hash: ContractPackageHash,

macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use proc_macro2::TokenStream as TokenStream2;
33
use quote::{quote, TokenStreamExt};
44
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Fields};
55

6+
/// Derive events on top of any struct.
67
#[proc_macro_derive(Event)]
78
pub fn derive_events(input: TokenStream) -> TokenStream {
89
let input = parse_macro_input!(input as DeriveInput);

tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ name = "reputation-tests"
1313
path = "src/reputation_tests.rs"
1414
bench = false
1515
doctest = false
16-
16+
doc = false

utils/src/casper_env.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Interact with the CasperVM inside the contract.
2+
13
use std::convert::TryInto;
24

35
use casper_contract::{
@@ -10,8 +12,9 @@ use casper_types::{
1012
CLTyped,
1113
};
1214

13-
use crate::{modules::events::Events, Address};
15+
use crate::{Address, Events};
1416

17+
/// Read value from the storage.
1518
pub fn get_key<T: FromBytes + CLTyped>(name: &str) -> Option<T> {
1619
match runtime::get_key(name) {
1720
None => None,
@@ -23,6 +26,7 @@ pub fn get_key<T: FromBytes + CLTyped>(name: &str) -> Option<T> {
2326
}
2427
}
2528

29+
/// Save value to the storage.
2630
pub fn set_key<T: ToBytes + CLTyped>(name: &str, value: T) {
2731
match runtime::get_key(name) {
2832
Some(key) => {
@@ -44,8 +48,8 @@ fn call_stack_element_to_address(call_stack_element: CallStackElement) -> Addres
4448
match call_stack_element {
4549
CallStackElement::Session { account_hash } => Address::from(account_hash),
4650
CallStackElement::StoredSession { account_hash, .. } => {
47-
// Stored session code acts in account's context, so if stored session wants to interact
48-
// with an ERC20 token caller's address will be used.
51+
// Stored session code acts in account's context, so if stored session
52+
// wants to interact, caller's address will be used.
4953
Address::from(account_hash)
5054
}
5155
CallStackElement::StoredContract {
@@ -71,14 +75,17 @@ pub fn caller() -> Address {
7175
call_stack_element_to_address(second_elem)
7276
}
7377

78+
/// Initialize events dictionary.
7479
pub fn init_events() {
7580
Events::default().init();
7681
}
7782

83+
/// Record event to the contract's storage.
7884
pub fn emit<T: ToBytes>(event: T) {
7985
Events::default().emit(event);
8086
}
8187

88+
/// Convert any key to base64.
8289
pub fn to_dictionary_key<T: ToBytes>(key: &T) -> String {
8390
let preimage = key.to_bytes().unwrap_or_revert();
8491
base64::encode(&preimage)

utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use modules::owner;
1515
pub use modules::staking;
1616
pub use modules::token;
1717
pub use modules::whitelist;
18+
use modules::Events;
1819

1920
#[cfg(feature = "test-support")]
2021
mod test_env;

utils/src/modules/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
pub mod events;
1+
mod events;
22
pub mod owner;
33
pub mod staking;
44
pub mod token;
55
pub mod whitelist;
6+
7+
pub(crate) use events::Events;

utils/src/modules/owner.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Single-owner-based access control system.
2+
13
use casper_contract::contract_api::runtime;
24

35
use crate::{
@@ -7,6 +9,7 @@ use crate::{
79

810
use self::events::OwnerChanged;
911

12+
/// The Owner module.
1013
pub struct Owner {
1114
pub owner: Variable<Option<Address>>,
1215
}
@@ -20,15 +23,18 @@ impl Default for Owner {
2023
}
2124

2225
impl Owner {
26+
/// Initialize the module.
2327
pub fn init(&mut self, owner: Address) {
2428
self.change_ownership(owner);
2529
}
2630

31+
/// Set the owner to the new address.
2732
pub fn change_ownership(&mut self, owner: Address) {
2833
self.owner.set(Some(owner));
2934
emit(OwnerChanged { new_owner: owner });
3035
}
3136

37+
/// Verify if the contract caller is the owner. Revert otherwise.
3238
pub fn ensure_owner(&self) {
3339
if let Some(owner) = self.owner.get() {
3440
if owner != caller() {
@@ -41,10 +47,11 @@ impl Owner {
4147
}
4248

4349
pub mod entry_points {
44-
use casper_types::{CLTyped, EntryPoint, EntryPointAccess, EntryPointType, Parameter};
45-
50+
//! Entry points definitions.
4651
use crate::{consts, Address};
52+
use casper_types::{CLTyped, EntryPoint, EntryPointAccess, EntryPointType, Parameter};
4753

54+
/// Public `change_ownership` entry point. Corresponds to [`change_ownership`](super::Owner::change_ownership).
4855
pub fn change_ownership() -> EntryPoint {
4956
EntryPoint::new(
5057
consts::EP_CHANGE_OWNERSHIP,
@@ -57,9 +64,11 @@ pub mod entry_points {
5764
}
5865

5966
pub mod events {
67+
//! Events definitions.
6068
use crate::Address;
6169
use casper_dao_macros::Event;
6270

71+
/// Informs the owner change.
6372
#[derive(Debug, PartialEq, Event)]
6473
pub struct OwnerChanged {
6574
pub new_owner: Address,

0 commit comments

Comments
 (0)