Skip to content

Commit f079fc5

Browse files
authored
Merge pull request #164 from semiotic-ai/aasseman/issue163
refactor!: Replace "collateral" with "escrow"
2 parents a6d788f + 6f9d0c7 commit f079fc5

File tree

14 files changed

+287
-304
lines changed

14 files changed

+287
-304
lines changed

tap_core/src/adapters/collateral_adapter.rs renamed to tap_core/src/adapters/escrow_adapter.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,51 @@
44
use async_trait::async_trait;
55
use ethereum_types::Address;
66

7-
/// `CollateralAdapter` defines a trait for adapters to handle collateral related operations.
7+
/// `EscrowAdapter` defines a trait for adapters to handle escrow related operations.
88
///
99
/// This trait is designed to be implemented by users of this library who want to
10-
/// customize the management of local accounting for available collateral. The error handling is also
10+
/// customize the management of local accounting for available escrow. The error handling is also
1111
/// customizable by defining an `AdapterError` type, which must implement both `Error`
1212
/// and `Debug` from the standard library.
1313
///
1414
/// # Usage
1515
///
16-
/// The `get_available_collateral` method should be used to retrieve the local accounting
17-
/// amount of available collateral for a specified gateway. Any errors during this operation
16+
/// The `get_available_escrow` method should be used to retrieve the local accounting
17+
/// amount of available escrow for a specified gateway. Any errors during this operation
1818
/// should be captured and returned in the `AdapterError` format.
1919
///
20-
/// The `subtract_collateral` method is used to deduct a specified value from the local accounting
21-
/// of available collateral of a specified gateway. Any errors during this operation should be captured
20+
/// The `subtract_escrow` method is used to deduct a specified value from the local accounting
21+
/// of available escrow of a specified gateway. Any errors during this operation should be captured
2222
/// and returned as an `AdapterError`.
2323
///
2424
/// This trait is utilized by [crate::tap_manager], which relies on these
25-
/// operations for managing collateral.
25+
/// operations for managing escrow.
2626
///
2727
/// # Example
2828
///
29-
/// For example code see [crate::adapters::collateral_adapter_mock]
29+
/// For example code see [crate::adapters::escrow_adapter_mock]
3030
3131
#[async_trait]
32-
pub trait CollateralAdapter {
32+
pub trait EscrowAdapter {
3333
/// Defines the user-specified error type.
3434
///
3535
/// This error type should implement the `Error` and `Debug` traits from the standard library.
3636
/// Errors of this type are returned to the user when an operation fails.
3737
type AdapterError: std::error::Error + std::fmt::Debug + Send + Sync + 'static;
3838

39-
/// Retrieves the local accounting amount of available collateral for a specified gateway.
39+
/// Retrieves the local accounting amount of available escrow for a specified gateway.
4040
///
41-
/// This method should be implemented to fetch the local accounting amount of available collateral for a
41+
/// This method should be implemented to fetch the local accounting amount of available escrow for a
4242
/// specified gateway from your system. Any errors that occur during this process should
4343
/// be captured and returned as an `AdapterError`.
44-
async fn get_available_collateral(
45-
&self,
46-
gateway_id: Address,
47-
) -> Result<u128, Self::AdapterError>;
44+
async fn get_available_escrow(&self, gateway_id: Address) -> Result<u128, Self::AdapterError>;
4845

49-
/// Deducts a specified value from the local accounting of available collateral for a specified gateway.
46+
/// Deducts a specified value from the local accounting of available escrow for a specified gateway.
5047
///
5148
/// This method should be implemented to deduct a specified value from the local accounting of
52-
/// available collateral of a specified gateway in your system. Any errors that occur during this
49+
/// available escrow of a specified gateway in your system. Any errors that occur during this
5350
/// process should be captured and returned as an `AdapterError`.
54-
async fn subtract_collateral(
51+
async fn subtract_escrow(
5552
&self,
5653
gateway_id: Address,
5754
value: u128,

tap_core/src/adapters/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
//! of use cases.
99
//!
1010
//! The following adapters are defined:
11-
//! - `collateral_adapter`: An interface for checking and updating collateral availability.
11+
//! - `escrow_adapter`: An interface for checking and updating escrow availability.
1212
//! - `rav_storage_adapter`: An interface for storing and retrieving/replacing RAVs.
1313
//! - `receipt_checks_adapter`: An interface for verifying TAP receipts.
1414
//! - `receipt_storage_adapter`: An interface for storing, retrieving, updating, and removing TAP receipts.
1515
//!
1616
//! In addition, this module also includes mock implementations of each adapter for testing and example purposes.
1717
18-
pub mod collateral_adapter;
18+
pub mod escrow_adapter;
1919
pub mod rav_storage_adapter;
2020
pub mod receipt_checks_adapter;
2121
pub mod receipt_storage_adapter;
2222

2323
mod test;
2424

25-
pub use test::collateral_adapter_mock;
25+
pub use test::escrow_adapter_mock;
2626
pub use test::rav_storage_adapter_mock;
2727
pub use test::receipt_checks_adapter_mock;
2828
pub use test::receipt_storage_adapter_mock;

tap_core/src/adapters/test/collateral_adapter_mock.rs

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2023-, Semiotic AI, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
use std::{collections::HashMap, sync::Arc};
5+
6+
use async_trait::async_trait;
7+
use ethereum_types::Address;
8+
use tokio::sync::RwLock;
9+
10+
use crate::adapters::escrow_adapter::EscrowAdapter;
11+
12+
pub struct EscrowAdapterMock {
13+
gateway_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>,
14+
}
15+
16+
use thiserror::Error;
17+
#[derive(Debug, Error)]
18+
pub enum AdpaterErrorMock {
19+
#[error("something went wrong: {error}")]
20+
AdapterError { error: String },
21+
}
22+
23+
impl EscrowAdapterMock {
24+
pub fn new(gateway_escrow_storage: Arc<RwLock<HashMap<Address, u128>>>) -> Self {
25+
EscrowAdapterMock {
26+
gateway_escrow_storage,
27+
}
28+
}
29+
pub async fn escrow(&self, gateway_id: Address) -> Result<u128, AdpaterErrorMock> {
30+
let gateway_escrow_storage = self.gateway_escrow_storage.read().await;
31+
if let Some(escrow) = gateway_escrow_storage.get(&gateway_id) {
32+
return Ok(*escrow);
33+
}
34+
Err(AdpaterErrorMock::AdapterError {
35+
error: "No escrow exists for provided gateway ID.".to_owned(),
36+
})
37+
}
38+
39+
pub async fn increase_escrow(&mut self, gateway_id: Address, value: u128) {
40+
let mut gateway_escrow_storage = self.gateway_escrow_storage.write().await;
41+
42+
if let Some(current_value) = gateway_escrow_storage.get(&gateway_id) {
43+
let mut gateway_escrow_storage = self.gateway_escrow_storage.write().await;
44+
gateway_escrow_storage.insert(gateway_id, current_value + value);
45+
} else {
46+
gateway_escrow_storage.insert(gateway_id, value);
47+
}
48+
}
49+
50+
pub async fn reduce_escrow(
51+
&self,
52+
gateway_id: Address,
53+
value: u128,
54+
) -> Result<(), AdpaterErrorMock> {
55+
let mut gateway_escrow_storage = self.gateway_escrow_storage.write().await;
56+
57+
if let Some(current_value) = gateway_escrow_storage.get(&gateway_id) {
58+
let checked_new_value = current_value.checked_sub(value);
59+
if let Some(new_value) = checked_new_value {
60+
gateway_escrow_storage.insert(gateway_id, new_value);
61+
return Ok(());
62+
}
63+
}
64+
Err(AdpaterErrorMock::AdapterError {
65+
error: "Provided value is greater than existing escrow.".to_owned(),
66+
})
67+
}
68+
}
69+
70+
#[async_trait]
71+
impl EscrowAdapter for EscrowAdapterMock {
72+
type AdapterError = AdpaterErrorMock;
73+
async fn get_available_escrow(&self, gateway_id: Address) -> Result<u128, Self::AdapterError> {
74+
self.escrow(gateway_id).await
75+
}
76+
async fn subtract_escrow(
77+
&self,
78+
gateway_id: Address,
79+
value: u128,
80+
) -> Result<(), Self::AdapterError> {
81+
self.reduce_escrow(gateway_id, value).await
82+
}
83+
}

tap_core/src/adapters/test/collateral_adapter_test.rs renamed to tap_core/src/adapters/test/escrow_adapter_test.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#[cfg(test)]
5-
mod collateral_adapter_unit_test {
5+
mod escrow_adapter_unit_test {
66
use std::{collections::HashMap, sync::Arc};
77

88
use ethers::signers::{coins_bip39::English, LocalWallet, MnemonicBuilder, Signer};
99
use rstest::*;
1010
use tokio::sync::RwLock;
1111

12-
use crate::adapters::{
13-
collateral_adapter::CollateralAdapter, collateral_adapter_mock::CollateralAdapterMock,
14-
};
12+
use crate::adapters::{escrow_adapter::EscrowAdapter, escrow_adapter_mock::EscrowAdapterMock};
1513

1614
#[rstest]
1715
#[tokio::test]
18-
async fn collateral_adapter_test() {
19-
let collateral_storage = Arc::new(RwLock::new(HashMap::new()));
20-
let mut collateral_adapter = CollateralAdapterMock::new(collateral_storage);
16+
async fn escrow_adapter_test() {
17+
let escrow_storage = Arc::new(RwLock::new(HashMap::new()));
18+
let mut escrow_adapter = EscrowAdapterMock::new(escrow_storage);
2119

2220
let wallet: LocalWallet = MnemonicBuilder::<English>::default()
2321
.phrase("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about")
@@ -35,49 +33,49 @@ mod collateral_adapter_unit_test {
3533

3634
let initial_value = 500u128;
3735

38-
collateral_adapter
39-
.increase_collateral(gateway_id, initial_value)
36+
escrow_adapter
37+
.increase_escrow(gateway_id, initial_value)
4038
.await;
4139

4240
// Check that gateway exists and has valid value through adapter
43-
assert!(collateral_adapter
44-
.get_available_collateral(gateway_id)
41+
assert!(escrow_adapter
42+
.get_available_escrow(gateway_id)
4543
.await
4644
.is_ok());
4745
assert_eq!(
48-
collateral_adapter
49-
.get_available_collateral(gateway_id)
46+
escrow_adapter
47+
.get_available_escrow(gateway_id)
5048
.await
5149
.unwrap(),
5250
initial_value
5351
);
5452

5553
// Check that subtracting is valid for valid gateway, and results in expected value
56-
assert!(collateral_adapter
57-
.subtract_collateral(gateway_id, initial_value)
54+
assert!(escrow_adapter
55+
.subtract_escrow(gateway_id, initial_value)
5856
.await
5957
.is_ok());
60-
assert!(collateral_adapter
61-
.get_available_collateral(gateway_id)
58+
assert!(escrow_adapter
59+
.get_available_escrow(gateway_id)
6260
.await
6361
.is_ok());
6462
assert_eq!(
65-
collateral_adapter
66-
.get_available_collateral(gateway_id)
63+
escrow_adapter
64+
.get_available_escrow(gateway_id)
6765
.await
6866
.unwrap(),
6967
0
7068
);
7169

72-
// Check that subtracting to negative collateral results in err
73-
assert!(collateral_adapter
74-
.subtract_collateral(gateway_id, initial_value)
70+
// Check that subtracting to negative escrow results in err
71+
assert!(escrow_adapter
72+
.subtract_escrow(gateway_id, initial_value)
7573
.await
7674
.is_err());
7775

7876
// Check that accessing non initialized gateway results in err
79-
assert!(collateral_adapter
80-
.get_available_collateral(invalid_gateway_id)
77+
assert!(escrow_adapter
78+
.get_available_escrow(invalid_gateway_id)
8179
.await
8280
.is_err());
8381
}

tap_core/src/adapters/test/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Copyright 2023-, Semiotic AI, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
pub mod collateral_adapter_test;
4+
pub mod escrow_adapter_test;
55
pub mod rav_storage_adapter_test;
66
pub mod receipt_checks_adapter_test;
77
pub mod receipt_storage_adapter_test;
88

9-
pub mod collateral_adapter_mock;
9+
pub mod escrow_adapter_mock;
1010
pub mod rav_storage_adapter_mock;
1111
pub mod receipt_checks_adapter_mock;
1212
pub mod receipt_storage_adapter_mock;

0 commit comments

Comments
 (0)