|
| 1 | +// Copyright 2023-, Semiotic AI, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::{collections::HashMap, sync::Arc}; |
| 5 | + |
| 6 | +use alloy_primitives::Address; |
| 7 | +use async_trait::async_trait; |
| 8 | +use tokio::sync::RwLock; |
| 9 | + |
| 10 | +use crate::adapters::collateral_adapter::CollateralAdapter; |
| 11 | + |
| 12 | +pub struct CollateralAdapterMock { |
| 13 | + gateway_collateral_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 CollateralAdapterMock { |
| 24 | + pub fn new(gateway_collateral_storage: Arc<RwLock<HashMap<Address, u128>>>) -> Self { |
| 25 | + CollateralAdapterMock { |
| 26 | + gateway_collateral_storage, |
| 27 | + } |
| 28 | + } |
| 29 | + pub async fn collateral(&self, gateway_id: Address) -> Result<u128, AdpaterErrorMock> { |
| 30 | + let gateway_collateral_storage = self.gateway_collateral_storage.read().await; |
| 31 | + if let Some(collateral) = gateway_collateral_storage.get(&gateway_id) { |
| 32 | + return Ok(*collateral); |
| 33 | + } |
| 34 | + Err(AdpaterErrorMock::AdapterError { |
| 35 | + error: "No collateral exists for provided gateway ID.".to_owned(), |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + pub async fn increase_collateral(&mut self, gateway_id: Address, value: u128) { |
| 40 | + let mut gateway_collateral_storage = self.gateway_collateral_storage.write().await; |
| 41 | + |
| 42 | + if let Some(current_value) = gateway_collateral_storage.get(&gateway_id) { |
| 43 | + let mut gateway_collateral_storage = self.gateway_collateral_storage.write().await; |
| 44 | + gateway_collateral_storage.insert(gateway_id, current_value + value); |
| 45 | + } else { |
| 46 | + gateway_collateral_storage.insert(gateway_id, value); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + pub async fn reduce_collateral( |
| 51 | + &self, |
| 52 | + gateway_id: Address, |
| 53 | + value: u128, |
| 54 | + ) -> Result<(), AdpaterErrorMock> { |
| 55 | + let mut gateway_collateral_storage = self.gateway_collateral_storage.write().await; |
| 56 | + |
| 57 | + if let Some(current_value) = gateway_collateral_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_collateral_storage.insert(gateway_id, new_value); |
| 61 | + return Ok(()); |
| 62 | + } |
| 63 | + } |
| 64 | + Err(AdpaterErrorMock::AdapterError { |
| 65 | + error: "Provided value is greater than existing collateral.".to_owned(), |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[async_trait] |
| 71 | +impl CollateralAdapter for CollateralAdapterMock { |
| 72 | + type AdapterError = AdpaterErrorMock; |
| 73 | + async fn get_available_collateral( |
| 74 | + &self, |
| 75 | + gateway_id: Address, |
| 76 | + ) -> Result<u128, Self::AdapterError> { |
| 77 | + self.collateral(gateway_id).await |
| 78 | + } |
| 79 | + async fn subtract_collateral( |
| 80 | + &self, |
| 81 | + gateway_id: Address, |
| 82 | + value: u128, |
| 83 | + ) -> Result<(), Self::AdapterError> { |
| 84 | + self.reduce_collateral(gateway_id, value).await |
| 85 | + } |
| 86 | +} |
0 commit comments