Skip to content

Commit a2fa0d5

Browse files
authored
Payment management improvements (extend deposit) (#176)
1 parent b24dcc3 commit a2fa0d5

File tree

7 files changed

+115
-24
lines changed

7 files changed

+115
-24
lines changed

.github/workflows/add-to-core-team-project.yml

Lines changed: 0 additions & 16 deletions
This file was deleted.

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
build:
1818
name: Build
1919
env:
20-
RUSTFLAGS: "-D warnings -C opt-level=z -C target-cpu=x86-64 -C debuginfo=1"
20+
RUSTFLAGS: "-D warnings -C opt-level=z -C target-cpu=native -C debuginfo=1"
2121
runs-on: ${{ matrix.os }}
2222
strategy:
2323
matrix:

examples/allocation_management.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ async fn main() -> Result<()> {
9797
address: None,
9898
payment_platform: Some(PaymentPlatformEnum::PaymentPlatformName(platform)),
9999
timeout: None,
100+
extend_timeout: None,
100101
})
101102
.await?;
102103
println!("{:#?}", allocation);

model/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ authors = ["Golem Factory <[email protected]>"]
66
homepage = "https://github.com/golemfactory/ya-client"
77
repository = "https://github.com/golemfactory/ya-client"
88
license = "LGPL-3.0"
9-
edition = "2018"
9+
edition = "2021"
1010

1111
[features]
1212
default = []
@@ -19,6 +19,7 @@ chrono = { version = "0.4", features = ["serde"]}
1919
derive_more = "0.99"
2020
rand = "0.8"
2121
serde = { version = "1.0.146", features = ["derive"] }
22+
serde_with = { version = "3" }
2223
serde_bytes = "0.11.14"
2324
serde_json = "1.0.96"
2425
strum = "0.24.1"

model/src/payment/acceptance.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ pub struct Acceptance {
77
pub total_amount_accepted: BigDecimal,
88
pub allocation_id: String,
99
}
10+
11+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
12+
#[serde(rename_all = "camelCase")]
13+
pub struct DebitNoteAcceptance {
14+
pub total_amount_accepted: BigDecimal,
15+
pub allocation_id: String,
16+
pub auto_accept_to: Option<BigDecimal>,
17+
}

model/src/payment/allocation.rs

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,32 @@ use std::collections::HashMap;
33
use bigdecimal::BigDecimal;
44
use chrono::{DateTime, Utc};
55
use serde::{Deserialize, Serialize};
6+
use serde_with::*;
7+
use std::time::Duration;
68

79
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10+
#[serde(rename_all = "camelCase")]
811
pub struct ValidateDepositCall {
912
#[serde(flatten)]
1013
pub arguments: HashMap<String, String>,
1114
}
1215

1316
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
17+
#[serde(rename_all = "camelCase")]
1418
pub struct Deposit {
1519
pub id: String,
1620
pub contract: String,
1721
pub validate: Option<ValidateDepositCall>,
1822
}
1923

2024
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
25+
#[serde(rename_all = "camelCase")]
2126
pub struct DepositUpdate {
2227
pub validate: Option<ValidateDepositCall>,
2328
}
2429

30+
#[serde_as]
31+
#[skip_serializing_none]
2532
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
2633
#[serde(rename_all = "camelCase")]
2734
pub struct Allocation {
@@ -32,11 +39,12 @@ pub struct Allocation {
3239
pub spent_amount: BigDecimal,
3340
pub remaining_amount: BigDecimal,
3441
pub timestamp: DateTime<Utc>,
35-
#[serde(skip_serializing_if = "Option::is_none", default)]
3642
pub timeout: Option<DateTime<Utc>>,
37-
#[serde(skip_serializing_if = "Option::is_none", default)]
3843
pub deposit: Option<Deposit>,
44+
#[serde(default)]
3945
pub make_deposit: bool,
46+
#[serde_as(as = "Option<DurationSeconds<u64>>")]
47+
pub extend_timeout: Option<Duration>,
4048
}
4149

4250
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -54,17 +62,19 @@ pub enum PaymentPlatformEnum {
5462
PaymentPlatform(PaymentPlatform),
5563
}
5664

65+
#[serde_as]
5766
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
5867
#[serde(rename_all = "camelCase")]
5968
pub struct NewAllocation {
6069
pub address: Option<String>,
6170
pub payment_platform: Option<PaymentPlatformEnum>,
6271
pub total_amount: BigDecimal,
63-
#[serde(skip_serializing_if = "Option::is_none", default)]
6472
pub timeout: Option<DateTime<Utc>>,
65-
#[serde(skip_serializing_if = "Option::is_none", default)]
6673
pub deposit: Option<Deposit>,
74+
#[serde(default)]
6775
pub make_deposit: bool,
76+
#[serde_as(as = "Option<serde_with::DurationSeconds<u64>>")]
77+
pub extend_timeout: Option<Duration>,
6878
}
6979

7080
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -77,3 +87,61 @@ pub struct AllocationUpdate {
7787
#[serde(skip_serializing_if = "Option::is_none", default)]
7888
pub deposit: Option<DepositUpdate>,
7989
}
90+
91+
#[cfg(test)]
92+
mod test {
93+
use super::*;
94+
use serde::de::DeserializeOwned;
95+
use std::fmt::Debug;
96+
97+
fn can_parse_to<T: DeserializeOwned + Debug>(json: &str) {
98+
let v: T = serde_json::from_str(json).unwrap();
99+
eprintln!("{:?}", v);
100+
}
101+
102+
#[test]
103+
fn test_new_allocation() {
104+
can_parse_to::<NewAllocation>(
105+
r#"{
106+
"paymentPlatform": "erc20-polygon-glm",
107+
"totalAmount": 1.0,
108+
"timeout": "2023-08-28T15:16:31.858Z",
109+
"makeDeposit": false,
110+
"extendTimeout": 3600
111+
}"#,
112+
);
113+
can_parse_to::<NewAllocation>(
114+
r#"{
115+
"totalAmount": 5
116+
}"#,
117+
);
118+
can_parse_to::<NewAllocation>(
119+
r#"{
120+
"paymentPlatform": { "token": "GLM" },
121+
"totalAmount": "512.2345"
122+
}"#,
123+
);
124+
}
125+
126+
#[test]
127+
fn test_allocation() {
128+
let j = serde_json::to_string(&Allocation {
129+
allocation_id: "".to_string(),
130+
address: "".to_string(),
131+
payment_platform: "".to_string(),
132+
total_amount: Default::default(),
133+
spent_amount: Default::default(),
134+
remaining_amount: Default::default(),
135+
timestamp: Default::default(),
136+
timeout: None,
137+
deposit: None,
138+
make_deposit: false,
139+
extend_timeout: None,
140+
})
141+
.unwrap();
142+
assert_eq!(
143+
r#"{"allocationId":"","address":"","paymentPlatform":"","totalAmount":"0","spentAmount":"0","remainingAmount":"0","timestamp":"1970-01-01T00:00:00Z","makeDeposit":false}"#,
144+
j
145+
);
146+
}
147+
}

specs/payment-api.yaml

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ paths:
396396
- $ref: '#/components/parameters/debitNoteId'
397397
- $ref: 'common.yaml#/parameters/ackTimeout'
398398
requestBody:
399-
$ref: '#/components/requestBodies/Acceptance'
399+
$ref: '#/components/requestBodies/DebitNoteAcceptance'
400400
responses:
401401
200:
402402
$ref: '#/components/responses/OK'
@@ -820,6 +820,14 @@ components:
820820
schema:
821821
$ref: '#/components/schemas/AllocationUpdate'
822822

823+
DebitNoteAcceptance:
824+
required: true
825+
content:
826+
application/json:
827+
schema:
828+
$ref: '#/components/schemas/DebitNoteAcceptance'
829+
830+
823831
Acceptance:
824832
required: true
825833
content:
@@ -1132,6 +1140,22 @@ components:
11321140
- CANCELLED
11331141
readOnly: true
11341142

1143+
DebitNoteAcceptance:
1144+
description: Message sent when Requestor accepts a Debit Note or Invoice.
1145+
type: object
1146+
properties:
1147+
totalAmountAccepted:
1148+
type: string
1149+
allocationId:
1150+
type: string
1151+
autoAcceptTo:
1152+
type: string
1153+
format: decimal
1154+
required:
1155+
- totalAmountAccepted
1156+
- allocationId
1157+
1158+
11351159
Acceptance:
11361160
description: Message sent when Requestor accepts a Debit Note or Invoice.
11371161
type: object
@@ -1217,6 +1241,12 @@ components:
12171241
format: date-time
12181242
makeDeposit:
12191243
type: boolean
1244+
extendTimeout:
1245+
description: >
1246+
in seconds, the time by which the allocation timeout is
1247+
extended after it is last used.
1248+
type: integer
1249+
format: int64
12201250
deposit:
12211251
type: object
12221252
properties:
@@ -1235,7 +1265,6 @@ components:
12351265
- spentAmount
12361266
- remainingAmount
12371267
- timestamp
1238-
- makeDeposit
12391268

12401269
AllocationUpdate:
12411270
type: object

0 commit comments

Comments
 (0)