Skip to content

Commit

Permalink
Handlers for Debt/Collateral F token and Legacy Cellar adaptors
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Sep 19, 2023
1 parent 7d473fb commit 236a84c
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 70 deletions.
2 changes: 1 addition & 1 deletion steward/src/cellars/adaptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub mod aave_v2_collateral;
pub mod aave_v3;
pub mod balancer_pool;
pub mod compound;
pub mod f_token;
pub mod fees_and_reserves;
pub mod frax;
pub mod morpho;
pub mod oneinch;
pub mod sommelier;
Expand Down
11 changes: 10 additions & 1 deletion steward/src/cellars/adaptors/aave_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ fn get_encoded_adaptor_calls(
)?,
),
FTokenV1Calls(params) => {
calls.extend(adaptors::f_token::f_token_adaptor_v1_calls(params)?)
calls.extend(adaptors::frax::f_token_adaptor_v1_calls(params)?)
}
MorphoAaveV2ATokenV1Calls(params) => calls.extend(
adaptors::morpho::morpho_aave_v2_a_token_adaptor_v1_calls(params)?,
Expand All @@ -253,6 +253,15 @@ fn get_encoded_adaptor_calls(
BalancerPoolV1Calls(params) => calls.extend(
adaptors::balancer_pool::balancer_pool_adaptor_v1_calls(params)?,
),
LegacyCellarV1Calls(params) => {
calls.extend(adaptors::sommelier::legacy_cellar_adaptor_v1_calls(params)?)
}
DebtFTokenV1Calls(params) => {
calls.extend(adaptors::frax::debt_f_token_adaptor_v1_calls(params)?)
}
CollateralFTokenV1Calls(params) => {
calls.extend(adaptors::frax::collateral_f_token_adaptor_v1_calls(params)?)
}
};

result.push(AbiAdaptorCall {
Expand Down
60 changes: 0 additions & 60 deletions steward/src/cellars/adaptors/f_token.rs

This file was deleted.

188 changes: 188 additions & 0 deletions steward/src/cellars/adaptors/frax.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
use ethers::{abi::AbiEncode, types::Bytes};
use steward_abi::{
collateral_f_token_adaptor_v1::{
AddCollateralCall, CollateralFTokenAdaptorV1Calls, RemoveCollateralCall,
RevokeApprovalCall as CollateralFTokenRevokeApprovalCall,
},
debt_f_token_adaptor_v1::{
BorrowFromFraxlendCall, CallAddInterestCall as DebtFTokenCallAddInterestCall,
DebtFTokenAdaptorV1Calls, RepayFraxlendDebtCall,
RevokeApprovalCall as DebtFTokenRevokeApprovalCall,
},
f_token_adaptor::{
CallAddInterestCall, FTokenAdaptorCalls, LendFraxCall, RedeemFraxShareCall,
RevokeApprovalCall, WithdrawFraxCall,
},
};

use crate::{
error::Error,
utils::{sp_call_error, sp_call_parse_address, string_to_u256},
};

pub(crate) fn f_token_adaptor_v1_calls(
params: steward_proto::steward::FTokenAdaptorV1Calls,
) -> Result<Vec<Bytes>, Error> {
let mut calls = Vec::new();
for c in params.calls {
let function = c
.function
.ok_or_else(|| sp_call_error("function cannot be empty".to_string()))?;

match function {
steward_proto::steward::f_token_adaptor_v1::Function::RevokeApproval(p) => {
let call = RevokeApprovalCall {
asset: sp_call_parse_address(p.asset)?,
spender: sp_call_parse_address(p.spender)?,
};
calls.push(FTokenAdaptorCalls::RevokeApproval(call).encode().into());
}
steward_proto::steward::f_token_adaptor_v1::Function::LendFrax(p) => {
let call = LendFraxCall {
f_token: sp_call_parse_address(p.f_token)?,
amount_to_deposit: string_to_u256(p.amount_to_deposit)?,
};
calls.push(FTokenAdaptorCalls::LendFrax(call).encode().into());
}
steward_proto::steward::f_token_adaptor_v1::Function::RedeemFraxShare(p) => {
let call = RedeemFraxShareCall {
f_token: sp_call_parse_address(p.f_token)?,
amount_to_redeem: string_to_u256(p.amount_to_redeem)?,
};
calls.push(FTokenAdaptorCalls::RedeemFraxShare(call).encode().into());
}
steward_proto::steward::f_token_adaptor_v1::Function::WithdrawFrax(p) => {
let call = WithdrawFraxCall {
f_token: sp_call_parse_address(p.f_token)?,
amount_to_withdraw: string_to_u256(p.amount_to_withdraw)?,
};
calls.push(FTokenAdaptorCalls::WithdrawFrax(call).encode().into());
}
steward_proto::steward::f_token_adaptor_v1::Function::CallAddInterest(p) => {
let call = CallAddInterestCall {
f_token: sp_call_parse_address(p.f_token)?,
};
calls.push(FTokenAdaptorCalls::CallAddInterest(call).encode().into());
}
}
}

Ok(calls)
}

pub(crate) fn debt_f_token_adaptor_v1_calls(
params: steward_proto::steward::DebtFTokenAdaptorV1Calls,
) -> Result<Vec<Bytes>, Error> {
let mut calls = Vec::new();
for c in params.calls {
let function = c
.function
.ok_or_else(|| sp_call_error("function cannot be empty".to_string()))?;

match function {
steward_proto::steward::debt_f_token_adaptor_v1::Function::RevokeApproval(p) => {
let call = DebtFTokenRevokeApprovalCall {
asset: sp_call_parse_address(p.asset)?,
spender: sp_call_parse_address(p.spender)?,
};

calls.push(
DebtFTokenAdaptorV1Calls::RevokeApproval(call)
.encode()
.into(),
);
}
steward_proto::steward::debt_f_token_adaptor_v1::Function::BorrowFromFraxlend(p) => {
let call = BorrowFromFraxlendCall {
amount_to_borrow: string_to_u256(p.amount_to_borrow)?,
fraxlend_pair: sp_call_parse_address(p.fraxlend_pair)?,
};

calls.push(
DebtFTokenAdaptorV1Calls::BorrowFromFraxlend(call)
.encode()
.into(),
);
}
steward_proto::steward::debt_f_token_adaptor_v1::Function::RepayFraxlendDebt(p) => {
let call = RepayFraxlendDebtCall {
debt_token_repay_amount: string_to_u256(p.debt_token_repay_amount)?,
fraxlend_pair: sp_call_parse_address(p.fraxlend_pair)?,
};

calls.push(
DebtFTokenAdaptorV1Calls::RepayFraxlendDebt(call)
.encode()
.into(),
);
}
steward_proto::steward::debt_f_token_adaptor_v1::Function::CallAddInterest(p) => {
let call = DebtFTokenCallAddInterestCall {
fraxlend_pair: sp_call_parse_address(p.fraxlend_pair)?,
};

calls.push(
DebtFTokenAdaptorV1Calls::CallAddInterest(call)
.encode()
.into(),
);
}
}
}

Ok(calls)
}

pub(crate) fn collateral_f_token_adaptor_v1_calls(
params: steward_proto::steward::CollateralFTokenAdaptorV1Calls,
) -> Result<Vec<Bytes>, Error> {
let mut calls = Vec::new();
for c in params.calls {
let function = c
.function
.ok_or_else(|| sp_call_error("function cannot be empty".to_string()))?;

match function {
steward_proto::steward::collateral_f_token_adaptor_v1::Function::RevokeApproval(p) => {
let call = CollateralFTokenRevokeApprovalCall {
asset: sp_call_parse_address(p.asset)?,
spender: sp_call_parse_address(p.spender)?,
};

calls.push(
CollateralFTokenAdaptorV1Calls::RevokeApproval(call)
.encode()
.into(),
);
}
steward_proto::steward::collateral_f_token_adaptor_v1::Function::AddCollateral(p) => {
let call = AddCollateralCall {
collateral_to_deposit: string_to_u256(p.collateral_to_deposit)?,
fraxlend_pair: sp_call_parse_address(p.fraxlend_pair)?,
};

calls.push(
CollateralFTokenAdaptorV1Calls::AddCollateral(call)
.encode()
.into(),
);
}
steward_proto::steward::collateral_f_token_adaptor_v1::Function::RemoveCollateral(
p,
) => {
let call = RemoveCollateralCall {
collateral_amount: string_to_u256(p.collateral_amount)?,
fraxlend_pair: sp_call_parse_address(p.fraxlend_pair)?,
};

calls.push(
CollateralFTokenAdaptorV1Calls::RemoveCollateral(call)
.encode()
.into(),
);
}
}
}

Ok(calls)
}
54 changes: 53 additions & 1 deletion steward/src/cellars/adaptors/sommelier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ethers::{abi::AbiEncode, types::Bytes};
use steward_abi::cellar_adaptor_v1::CellarAdaptorV1Calls as AbiCellarAdaptorV1Calls;
use steward_proto::steward::cellar_adaptor_v1;
use steward_abi::legacy_cellar_adaptor_v1::LegacyCellarAdaptorV1Calls as AbiLegacyCellarAdaptorV1Calls;
use steward_proto::steward::{cellar_adaptor_v1, legacy_cellar_adaptor_v1};

use crate::{
error::Error,
Expand Down Expand Up @@ -55,3 +56,54 @@ pub(crate) fn cellar_adaptor_v1_calls(

Ok(calls)
}

pub(crate) fn legacy_cellar_adaptor_v1_calls(
params: steward_proto::steward::LegacyCellarAdaptorV1Calls,
) -> Result<Vec<Bytes>, Error> {
let mut calls = Vec::new();
for c in params.calls {
let function = c
.function
.ok_or_else(|| sp_call_error("function cannot be empty".to_string()))?;

match function {
legacy_cellar_adaptor_v1::Function::DepositToCellar(p) => {
let call = steward_abi::legacy_cellar_adaptor_v1::DepositToCellarCall {
cellar: sp_call_parse_address(p.cellar)?,
assets: string_to_u256(p.assets)?,
oracle: sp_call_parse_address(p.oracle)?,
};
calls.push(
AbiLegacyCellarAdaptorV1Calls::DepositToCellar(call)
.encode()
.into(),
)
}
legacy_cellar_adaptor_v1::Function::WithdrawFromCellar(p) => {
let call = steward_abi::legacy_cellar_adaptor_v1::WithdrawFromCellarCall {
cellar: sp_call_parse_address(p.cellar)?,
assets: string_to_u256(p.assets)?,
oracle: sp_call_parse_address(p.oracle)?,
};
calls.push(
AbiLegacyCellarAdaptorV1Calls::WithdrawFromCellar(call)
.encode()
.into(),
)
}
legacy_cellar_adaptor_v1::Function::RevokeApproval(p) => {
let call = steward_abi::legacy_cellar_adaptor_v1::RevokeApprovalCall {
asset: sp_call_parse_address(p.asset)?,
spender: sp_call_parse_address(p.spender)?,
};
calls.push(
AbiLegacyCellarAdaptorV1Calls::RevokeApproval(call)
.encode()
.into(),
)
}
}
}

Ok(calls)
}
11 changes: 10 additions & 1 deletion steward/src/cellars/cellar_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn get_encoded_adaptor_calls(data: Vec<AdaptorCall>) -> Result<Vec<AbiAdaptorCal
)?,
),
FTokenV1Calls(params) => {
calls.extend(adaptors::f_token::f_token_adaptor_v1_calls(params)?)
calls.extend(adaptors::frax::f_token_adaptor_v1_calls(params)?)
}
MorphoAaveV2ATokenV1Calls(params) => calls.extend(
adaptors::morpho::morpho_aave_v2_a_token_adaptor_v1_calls(params)?,
Expand All @@ -260,6 +260,15 @@ fn get_encoded_adaptor_calls(data: Vec<AdaptorCall>) -> Result<Vec<AbiAdaptorCal
BalancerPoolV1Calls(params) => calls.extend(
adaptors::balancer_pool::balancer_pool_adaptor_v1_calls(params)?,
),
LegacyCellarV1Calls(params) => {
calls.extend(adaptors::sommelier::legacy_cellar_adaptor_v1_calls(params)?)
}
DebtFTokenV1Calls(params) => {
calls.extend(adaptors::frax::debt_f_token_adaptor_v1_calls(params)?)
}
CollateralFTokenV1Calls(params) => {
calls.extend(adaptors::frax::collateral_f_token_adaptor_v1_calls(params)?)
}
};

result.push(AbiAdaptorCall {
Expand Down
11 changes: 10 additions & 1 deletion steward/src/cellars/cellar_v2_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn get_encoded_adaptor_calls(data: Vec<AdaptorCall>) -> Result<Vec<AbiAdaptorCal
)?,
),
FTokenV1Calls(params) => {
calls.extend(adaptors::f_token::f_token_adaptor_v1_calls(params)?)
calls.extend(adaptors::frax::f_token_adaptor_v1_calls(params)?)
}
MorphoAaveV2ATokenV1Calls(params) => calls.extend(
adaptors::morpho::morpho_aave_v2_a_token_adaptor_v1_calls(params)?,
Expand All @@ -334,6 +334,15 @@ fn get_encoded_adaptor_calls(data: Vec<AdaptorCall>) -> Result<Vec<AbiAdaptorCal
BalancerPoolV1Calls(params) => calls.extend(
adaptors::balancer_pool::balancer_pool_adaptor_v1_calls(params)?,
),
LegacyCellarV1Calls(params) => {
calls.extend(adaptors::sommelier::legacy_cellar_adaptor_v1_calls(params)?)
}
DebtFTokenV1Calls(params) => {
calls.extend(adaptors::frax::debt_f_token_adaptor_v1_calls(params)?)
}
CollateralFTokenV1Calls(params) => {
calls.extend(adaptors::frax::collateral_f_token_adaptor_v1_calls(params)?)
}
};

result.push(AbiAdaptorCall {
Expand Down
Loading

0 comments on commit 236a84c

Please sign in to comment.