11// Copyright 2023-, Semiotic AI, Inc.
22// SPDX-License-Identifier: Apache-2.0
33
4- use std:: {
5- collections:: HashMap ,
6- sync:: { Arc , RwLock } ,
7- } ;
4+ use std:: { collections:: HashMap , sync:: Arc } ;
85
6+ use async_trait:: async_trait;
97use ethereum_types:: Address ;
8+ use tokio:: sync:: RwLock ;
109
1110use crate :: adapters:: collateral_adapter:: CollateralAdapter ;
1211
@@ -27,8 +26,8 @@ impl CollateralAdapterMock {
2726 gateway_collateral_storage,
2827 }
2928 }
30- pub fn collateral ( & self , gateway_id : Address ) -> Result < u128 , AdpaterErrorMock > {
31- let gateway_collateral_storage = self . gateway_collateral_storage . read ( ) . unwrap ( ) ;
29+ pub async fn collateral ( & self , gateway_id : Address ) -> Result < u128 , AdpaterErrorMock > {
30+ let gateway_collateral_storage = self . gateway_collateral_storage . read ( ) . await ;
3231 if let Some ( collateral) = gateway_collateral_storage. get ( & gateway_id) {
3332 return Ok ( * collateral) ;
3433 }
@@ -37,23 +36,23 @@ impl CollateralAdapterMock {
3736 } )
3837 }
3938
40- pub fn increase_collateral ( & mut self , gateway_id : Address , value : u128 ) {
41- let mut gateway_collateral_storage = self . gateway_collateral_storage . write ( ) . unwrap ( ) ;
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 ;
4241
4342 if let Some ( current_value) = gateway_collateral_storage. get ( & gateway_id) {
44- let mut gateway_collateral_storage = self . gateway_collateral_storage . write ( ) . unwrap ( ) ;
43+ let mut gateway_collateral_storage = self . gateway_collateral_storage . write ( ) . await ;
4544 gateway_collateral_storage. insert ( gateway_id, current_value + value) ;
4645 } else {
4746 gateway_collateral_storage. insert ( gateway_id, value) ;
4847 }
4948 }
5049
51- pub fn reduce_collateral (
52- & mut self ,
50+ pub async fn reduce_collateral (
51+ & self ,
5352 gateway_id : Address ,
5453 value : u128 ,
5554 ) -> Result < ( ) , AdpaterErrorMock > {
56- let mut gateway_collateral_storage = self . gateway_collateral_storage . write ( ) . unwrap ( ) ;
55+ let mut gateway_collateral_storage = self . gateway_collateral_storage . write ( ) . await ;
5756
5857 if let Some ( current_value) = gateway_collateral_storage. get ( & gateway_id) {
5958 let checked_new_value = current_value. checked_sub ( value) ;
@@ -68,16 +67,20 @@ impl CollateralAdapterMock {
6867 }
6968}
7069
70+ #[ async_trait]
7171impl CollateralAdapter for CollateralAdapterMock {
7272 type AdapterError = AdpaterErrorMock ;
73- fn get_available_collateral ( & self , gateway_id : Address ) -> Result < u128 , Self :: AdapterError > {
74- self . collateral ( gateway_id)
73+ async fn get_available_collateral (
74+ & self ,
75+ gateway_id : Address ,
76+ ) -> Result < u128 , Self :: AdapterError > {
77+ self . collateral ( gateway_id) . await
7578 }
76- fn subtract_collateral (
77- & mut self ,
79+ async fn subtract_collateral (
80+ & self ,
7881 gateway_id : Address ,
7982 value : u128 ,
8083 ) -> Result < ( ) , Self :: AdapterError > {
81- self . reduce_collateral ( gateway_id, value)
84+ self . reduce_collateral ( gateway_id, value) . await
8285 }
8386}
0 commit comments