Skip to content

Commit 38384bd

Browse files
authored
Merge pull request #533 from AmbireTech/channel-routes-documentation
Documentation for other channel routes
2 parents 44fec2a + 166a2cd commit 38384bd

11 files changed

+194
-15
lines changed

primitives/Cargo.toml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ postgres = ["bytes", "tokio-postgres", "deadpool-postgres"]
2020
# All Addresses and keystore files exist in the ganache-cli setup for testing with the EthereumAdapter
2121
test-util = []
2222

23+
[[example]]
24+
name = "accounting_response"
25+
26+
[[example]]
27+
name = "all_spenders_response"
28+
2329
[[example]]
2430
name = "analytics_query"
2531

@@ -45,11 +51,17 @@ name = "channel_last_approved_response"
4551
name = "channel_last_approved_query"
4652

4753
[[example]]
48-
name = "create_campaign"
54+
name = "channel_pay_request"
55+
56+
[[example]]
57+
name = "create_campaign_request"
4958
required-features = ["test-util"]
5059

5160
[[example]]
52-
name = "modify_campaign"
61+
name = "modify_campaign_request"
62+
63+
[[example]]
64+
name = "spender_response"
5365

5466
[[example]]
5567
name = "validator_messages_create_request"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use primitives::{balances::CheckedState, sentry::AccountingResponse};
2+
use serde_json::{from_value, json};
3+
4+
fn main() {
5+
// Empty balances
6+
{
7+
let json = json!({
8+
"earners": {},
9+
"spenders": {},
10+
});
11+
assert!(from_value::<AccountingResponse::<CheckedState>>(json).is_ok());
12+
}
13+
14+
// Non-empty balances
15+
{
16+
// earners sum and spenders sum should always match since balances are CheckedState
17+
let json = json!({
18+
"earners": {
19+
"0x80690751969B234697e9059e04ed72195c3507fa": "10000000000",
20+
"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7": "20000000000",
21+
"0xE882ebF439207a70dDcCb39E13CA8506c9F45fD9": "30000000000",
22+
},
23+
"spenders": {
24+
"0xaCBaDA2d5830d1875ae3D2de207A1363B316Df2F": "60000000000",
25+
},
26+
});
27+
assert!(from_value::<AccountingResponse::<CheckedState>>(json).is_ok());
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use primitives::sentry::AllSpendersResponse;
2+
use serde_json::{from_value, json};
3+
4+
fn main() {
5+
let json = json!({
6+
"spenders": {
7+
"0xaCBaDA2d5830d1875ae3D2de207A1363B316Df2F": {
8+
"totalDeposited": "10000000000",
9+
"totalSpent": "100000000",
10+
},
11+
"0xDd589B43793934EF6Ad266067A0d1D4896b0dff0": {
12+
"totalDeposited": "90000000000",
13+
"totalSpent": "20000000000",
14+
},
15+
"0x541b401362Ea1D489D322579552B099e801F3632": {
16+
"totalDeposited": "1000000000",
17+
"totalSpent": "1000000000",
18+
},
19+
},
20+
"totalPages": 1,
21+
"page": 0
22+
});
23+
assert!(from_value::<AllSpendersResponse>(json).is_ok());
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use primitives::sentry::ChannelPayRequest;
2+
use serde_json::json;
3+
use std::str::FromStr;
4+
5+
fn main() {
6+
let channel_pay_json = json!({
7+
"payouts": {
8+
"0x80690751969B234697e9059e04ed72195c3507fa": "10000000000",
9+
"0xf3f583AEC5f7C030722Fe992A5688557e1B86ef7": "20000000000",
10+
"0x0e880972A4b216906F05D67EeaaF55d16B5EE4F1": "30000000000",
11+
},
12+
});
13+
14+
let channel_pay_json = serde_json::to_string(&channel_pay_json).expect("should serialize");
15+
16+
assert!(serde_json::from_str::<ChannelPayRequest>(&channel_pay_json).is_ok());
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use primitives::sentry::SpenderResponse;
2+
use serde_json::{from_value, json};
3+
4+
fn main() {
5+
let json = json!({
6+
"spender": {
7+
"totalDeposited": "10000000000",
8+
"totalSpent": "100000000",
9+
},
10+
});
11+
assert!(from_value::<SpenderResponse>(json).is_ok());
12+
}

primitives/src/sentry.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ pub use event::{Event, EventType, CLICK, IMPRESSION};
2222

2323
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
2424
#[serde(rename_all = "camelCase")]
25-
/// Channel Accounting response.
25+
/// Channel Accounting response
2626
///
27-
/// A collection of all `Accounting`s for a specific [`Channel`](crate::Channel).
27+
/// A collection of all `Accounting`s for a specific [`Channel`](`crate::Channel`)
28+
///
29+
/// # Examples
30+
///
31+
/// ```
32+
#[doc = include_str!("../examples/accounting_response.rs")]
33+
/// ```
2834
pub struct AccountingResponse<S: BalancesState> {
2935
#[serde(flatten, bound = "S: BalancesState")]
3036
pub balances: Balances<S>,
@@ -628,12 +634,26 @@ pub struct SuccessResponse {
628634
pub success: bool,
629635
}
630636

637+
/// Spender limits for a spender on a `Channel`.
638+
///
639+
/// # Examples
640+
///
641+
/// ```
642+
#[doc = include_str!("../examples/spender_response.rs")]
643+
/// ```
631644
#[derive(Serialize, Deserialize, Debug)]
632645
#[serde(rename_all = "camelCase")]
633646
pub struct SpenderResponse {
634647
pub spender: Spender,
635648
}
636649

650+
/// Spender limits for all spenders on a `Channel`.
651+
///
652+
/// # Examples
653+
///
654+
/// ```
655+
#[doc = include_str!("../examples/all_spenders_response.rs")]
656+
/// ```
637657
#[derive(Serialize, Deserialize, Debug)]
638658
#[serde(rename_all = "camelCase")]
639659
pub struct AllSpendersResponse {
@@ -649,6 +669,13 @@ pub struct AllSpendersQuery {
649669
pub page: u64,
650670
}
651671

672+
/// Payouts to be performed for the given [`Channel`](`crate::Channel`).
673+
///
674+
/// # Examples
675+
///
676+
/// ```
677+
#[doc = include_str!("../examples/channel_pay_request.rs")]
678+
/// ```
652679
#[derive(Debug, Serialize, Deserialize)]
653680
pub struct ChannelPayRequest {
654681
pub payouts: UnifiedMap,
@@ -667,6 +694,13 @@ pub struct ValidatorMessagesListResponse {
667694
pub messages: Vec<ValidatorMessage>,
668695
}
669696

697+
/// Contains all the different validator messages to be created.
698+
///
699+
/// # Examples
700+
///
701+
/// ```
702+
#[doc = include_str!("../examples/validator_messages_create_request.rs")]
703+
/// ```
670704
#[derive(Serialize, Deserialize, Debug)]
671705
#[serde(rename_all = "camelCase")]
672706
pub struct ValidatorMessagesCreateRequest {
@@ -862,7 +896,7 @@ pub mod campaign_create {
862896
/// # Examples
863897
///
864898
/// ```
865-
#[doc = include_str!("../examples/create_campaign.rs")]
899+
#[doc = include_str!("../examples/create_campaign_request.rs")]
866900
/// ```
867901
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
868902
#[serde(rename_all = "camelCase")]
@@ -955,7 +989,7 @@ pub mod campaign_modify {
955989
///
956990
/// # Examples:
957991
/// ```
958-
#[doc = include_str!("../examples/modify_campaign.rs")]
992+
#[doc = include_str!("../examples/modify_campaign_request.rs")]
959993
/// ```
960994
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
961995
pub struct ModifyCampaign {

sentry/src/db/validator_message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use primitives::{
1111
use super::{DbPool, PoolError};
1212

1313
/// Inserts a new validator [`MessageTypes`] using the `from` [`ValidatorId`] and `received` at: [`Utc::now()`][Utc]
14-
pub async fn insert_validator_messages(
14+
pub async fn insert_validator_message(
1515
pool: &DbPool,
1616
channel: &Channel,
1717
from: &ValidatorId,

sentry/src/routes.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,38 @@
5959
//!
6060
//! #### GET `/v5/channel/:id/accounting`
6161
//!
62+
//! Gets all of the accounting entries for a channel from the database and checks the balances.
63+
//!
6264
//! The route is handled by [`channel::get_accounting_for_channel()`].
6365
//!
64-
//! Response: [`AccountingResponse::<CheckedState>`](primitives::sentry::AccountingResponse)
66+
//! Response: [`AccountingResponse`]
67+
//!
68+
//! ##### Examples
69+
//!
70+
//! Response:
71+
//!
72+
//! ```
73+
#![doc = include_str!("../../primitives/examples/accounting_response.rs")]
74+
//! ```
6575
//!
6676
//! #### GET `/v5/channel/:id/spender/:addr` (auth required)
6777
//!
78+
//! Gets the spender limits for a spender on a [`Channel`]. It does so by fetching the
79+
//! latest Spendable entry from the database (or creating one if it doesn't exist yet) from which
80+
//! the total deposited amount is retrieved, and the latest NewState from which the total spent
81+
//! amount is retrieved.
82+
//!
6883
//! The route is handled by [`channel::get_spender_limits()`].
6984
//!
70-
//! Response: [`SpenderResponse`](primitives::sentry::SpenderResponse)
85+
//! Response: [`SpenderResponse`]
86+
//!
87+
//! ##### Examples
88+
//!
89+
//! Response:
90+
//!
91+
//! ```
92+
#![doc = include_str!("../../primitives/examples/spender_response.rs")]
93+
//! ```
7194
//!
7295
//! #### POST `/v5/channel/:id/spender/:addr` (auth required)
7396
//!
@@ -81,9 +104,19 @@
81104
//!
82105
//! #### GET `/v5/channel/:id/spender/all` (auth required)
83106
//!
107+
//! This routes gets total_deposited and total_spent for every spender on a [`Channel`]
108+
//!
84109
//! The route is handled by [`channel::get_all_spender_limits()`].
85110
//!
86-
//! Response: [`AllSpendersResponse`](primitives::sentry::AllSpendersResponse)
111+
//! Response: [`AllSpendersResponse`]
112+
//!
113+
//! ##### Examples
114+
//!
115+
//! Response:
116+
//!
117+
//! ```
118+
#![doc = include_str!("../../primitives/examples/all_spenders_response.rs")]
119+
//! ```
87120
//!
88121
//! #### GET `/v5/channel/:id/validator-messages`
89122
//!
@@ -153,6 +186,13 @@
153186
//!
154187
//! The same is true of the [`Heartbeat`]s messages if they are requested with the query parameter.
155188
//!
189+
//! Retrieves the latest [`ApproveState`] and the corresponding [`NewState`]
190+
//! validator messages for the given [`Channel`].
191+
//!
192+
//! If the [`Channel`] is new one or both of the states might have not been generated yet.
193+
//!
194+
//! The same is true of the [`Heartbeat`]s messages if they are requested with the query parameter.
195+
//!
156196
//! The route is handled by [`channel::last_approved()`].
157197
//!
158198
//! Request query parameters: [`LastApprovedQuery`][primitives::sentry::LastApprovedQuery]
@@ -184,10 +224,18 @@
184224
//!
185225
//! The route is handled by [`channel::channel_payout()`].
186226
//!
187-
//! Request JSON body: [`ChannelPayRequest`](primitives::sentry::ChannelPayRequest)
227+
//! Request JSON body: [`ChannelPayRequest`]
188228
//!
189229
//! Response: [`SuccessResponse`](primitives::sentry::SuccessResponse)
190230
//!
231+
//! ##### Examples
232+
//!
233+
//! Request (json):
234+
//!
235+
//! ```
236+
#![doc = include_str!("../../primitives/examples/channel_pay_request.rs")]
237+
//! ```
238+
//!
191239
//!
192240
//! #### GET `/v5/channel/:id/get-leaf`
193241
//!
@@ -298,7 +346,7 @@
298346
//! ##### Examples
299347
//!
300348
//! ```
301-
#![doc = include_str!("../../primitives/examples/create_campaign.rs")]
349+
#![doc = include_str!("../../primitives/examples/create_campaign_request.rs")]
302350
//! ```
303351
//!
304352
//! #### POST `/v5/campaign/:id` (auth required)
@@ -316,7 +364,7 @@
316364
//! ##### Examples
317365
//!
318366
//! ```
319-
#![doc = include_str!("../../primitives/examples/modify_campaign.rs")]
367+
#![doc = include_str!("../../primitives/examples/modify_campaign_request.rs")]
320368
//! ```
321369
//!
322370
//! #### POST `/v5/campaign/:id/events`
@@ -425,6 +473,7 @@
425473
//! [`ApproveState`]: primitives::validator::ApproveState
426474
//! [`Accounting`]: crate::db::accounting::Accounting
427475
//! [`AccountingResponse`]: primitives::sentry::AccountingResponse
476+
//! [`AllSpendersResponse`]: primitives::sentry::AllSpendersResponse
428477
//! [`AnalyticsResponse`]: primitives::sentry::AnalyticsResponse
429478
//! [`AnalyticsQuery`]: primitives::analytics::AnalyticsQuery
430479
//! [`Auth.uid`]: crate::Auth::uid
@@ -436,12 +485,14 @@
436485
//! [`Channel.leader`]: primitives::Channel::leader
437486
//! [`Channel.follower`]: primitives::Channel::follower
438487
//! [`ChannelId`]: primitives::ChannelId
488+
//! [`ChannelPayRequest`]: primitives::sentry::ChannelPayRequest
439489
//! [`check_access()`]: crate::access::check_access
440490
//! [`Config.msgs_find_limit`]: primitives::Config::msgs_find_limit
441491
//! [`Event`]: primitives::sentry::Event
442492
//! [`Heartbeat`]: primitives::validator::Heartbeat
443493
//! [`MessageTypes`]: primitives::validator::MessageTypes
444494
//! [`NewState`]: primitives::validator::NewState
495+
//! [`SpenderResponse`]: primitives::sentry::SpenderResponse
445496
//! [`SuccessResponse`]: primitives::sentry::SuccessResponse
446497
//! [`ValidatorId`]: primitives::ValidatorId
447498

sentry/src/routes/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ pub async fn channel_dummy_deposit<C: Locked + 'static>(
613613
///
614614
pub mod validator_message {
615615
use crate::{
616-
db::validator_message::{get_validator_messages, insert_validator_messages},
616+
db::validator_message::{get_validator_messages, insert_validator_message},
617617
Auth,
618618
};
619619
use crate::{
@@ -743,7 +743,7 @@ pub mod validator_message {
743743
None => Err(ResponseError::Unauthorized),
744744
_ => {
745745
try_join_all(create_request.messages.iter().map(|message| {
746-
insert_validator_messages(&app.pool, &channel, &auth.uid, message)
746+
insert_validator_message(&app.pool, &channel, &auth.uid, message)
747747
}))
748748
.await?;
749749

0 commit comments

Comments
 (0)