Skip to content

Commit

Permalink
Payment management improvements (extend deposit) (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
prekucki committed Jun 20, 2024
1 parent b24dcc3 commit a2fa0d5
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 24 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/add-to-core-team-project.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
build:
name: Build
env:
RUSTFLAGS: "-D warnings -C opt-level=z -C target-cpu=x86-64 -C debuginfo=1"
RUSTFLAGS: "-D warnings -C opt-level=z -C target-cpu=native -C debuginfo=1"
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand Down
1 change: 1 addition & 0 deletions examples/allocation_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ async fn main() -> Result<()> {
address: None,
payment_platform: Some(PaymentPlatformEnum::PaymentPlatformName(platform)),
timeout: None,
extend_timeout: None,
})
.await?;
println!("{:#?}", allocation);
Expand Down
3 changes: 2 additions & 1 deletion model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Golem Factory <contact@golem.network>"]
homepage = "https://github.com/golemfactory/ya-client"
repository = "https://github.com/golemfactory/ya-client"
license = "LGPL-3.0"
edition = "2018"
edition = "2021"

[features]
default = []
Expand All @@ -19,6 +19,7 @@ chrono = { version = "0.4", features = ["serde"]}
derive_more = "0.99"
rand = "0.8"
serde = { version = "1.0.146", features = ["derive"] }
serde_with = { version = "3" }
serde_bytes = "0.11.14"
serde_json = "1.0.96"
strum = "0.24.1"
Expand Down
8 changes: 8 additions & 0 deletions model/src/payment/acceptance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ pub struct Acceptance {
pub total_amount_accepted: BigDecimal,
pub allocation_id: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DebitNoteAcceptance {
pub total_amount_accepted: BigDecimal,
pub allocation_id: String,
pub auto_accept_to: Option<BigDecimal>,
}
76 changes: 72 additions & 4 deletions model/src/payment/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@ use std::collections::HashMap;
use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::*;
use std::time::Duration;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ValidateDepositCall {
#[serde(flatten)]
pub arguments: HashMap<String, String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Deposit {
pub id: String,
pub contract: String,
pub validate: Option<ValidateDepositCall>,
}

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

#[serde_as]
#[skip_serializing_none]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Allocation {
Expand All @@ -32,11 +39,12 @@ pub struct Allocation {
pub spent_amount: BigDecimal,
pub remaining_amount: BigDecimal,
pub timestamp: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub timeout: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub deposit: Option<Deposit>,
#[serde(default)]
pub make_deposit: bool,
#[serde_as(as = "Option<DurationSeconds<u64>>")]
pub extend_timeout: Option<Duration>,
}

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

#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewAllocation {
pub address: Option<String>,
pub payment_platform: Option<PaymentPlatformEnum>,
pub total_amount: BigDecimal,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub timeout: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub deposit: Option<Deposit>,
#[serde(default)]
pub make_deposit: bool,
#[serde_as(as = "Option<serde_with::DurationSeconds<u64>>")]
pub extend_timeout: Option<Duration>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand All @@ -77,3 +87,61 @@ pub struct AllocationUpdate {
#[serde(skip_serializing_if = "Option::is_none", default)]
pub deposit: Option<DepositUpdate>,
}

#[cfg(test)]
mod test {
use super::*;
use serde::de::DeserializeOwned;
use std::fmt::Debug;

fn can_parse_to<T: DeserializeOwned + Debug>(json: &str) {
let v: T = serde_json::from_str(json).unwrap();
eprintln!("{:?}", v);
}

#[test]
fn test_new_allocation() {
can_parse_to::<NewAllocation>(
r#"{
"paymentPlatform": "erc20-polygon-glm",
"totalAmount": 1.0,
"timeout": "2023-08-28T15:16:31.858Z",
"makeDeposit": false,
"extendTimeout": 3600
}"#,
);
can_parse_to::<NewAllocation>(
r#"{
"totalAmount": 5
}"#,
);
can_parse_to::<NewAllocation>(
r#"{
"paymentPlatform": { "token": "GLM" },
"totalAmount": "512.2345"
}"#,
);
}

#[test]
fn test_allocation() {
let j = serde_json::to_string(&Allocation {
allocation_id: "".to_string(),
address: "".to_string(),
payment_platform: "".to_string(),
total_amount: Default::default(),
spent_amount: Default::default(),
remaining_amount: Default::default(),
timestamp: Default::default(),
timeout: None,
deposit: None,
make_deposit: false,
extend_timeout: None,
})
.unwrap();
assert_eq!(
r#"{"allocationId":"","address":"","paymentPlatform":"","totalAmount":"0","spentAmount":"0","remainingAmount":"0","timestamp":"1970-01-01T00:00:00Z","makeDeposit":false}"#,
j
);
}
}
33 changes: 31 additions & 2 deletions specs/payment-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ paths:
- $ref: '#/components/parameters/debitNoteId'
- $ref: 'common.yaml#/parameters/ackTimeout'
requestBody:
$ref: '#/components/requestBodies/Acceptance'
$ref: '#/components/requestBodies/DebitNoteAcceptance'
responses:
200:
$ref: '#/components/responses/OK'
Expand Down Expand Up @@ -820,6 +820,14 @@ components:
schema:
$ref: '#/components/schemas/AllocationUpdate'

DebitNoteAcceptance:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/DebitNoteAcceptance'


Acceptance:
required: true
content:
Expand Down Expand Up @@ -1132,6 +1140,22 @@ components:
- CANCELLED
readOnly: true

DebitNoteAcceptance:
description: Message sent when Requestor accepts a Debit Note or Invoice.
type: object
properties:
totalAmountAccepted:
type: string
allocationId:
type: string
autoAcceptTo:
type: string
format: decimal
required:
- totalAmountAccepted
- allocationId


Acceptance:
description: Message sent when Requestor accepts a Debit Note or Invoice.
type: object
Expand Down Expand Up @@ -1217,6 +1241,12 @@ components:
format: date-time
makeDeposit:
type: boolean
extendTimeout:
description: >
in seconds, the time by which the allocation timeout is
extended after it is last used.
type: integer
format: int64
deposit:
type: object
properties:
Expand All @@ -1235,7 +1265,6 @@ components:
- spentAmount
- remainingAmount
- timestamp
- makeDeposit

AllocationUpdate:
type: object
Expand Down

0 comments on commit a2fa0d5

Please sign in to comment.