-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
funds-manager: execution-client: Add quote execution helpers
- Loading branch information
Showing
8 changed files
with
263 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
funds-manager/funds-manager-server/src/custody_client/withdraw.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
funds-manager/funds-manager-server/src/execution_client/swap.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! Handlers for executing swaps | ||
|
||
use std::str::FromStr; | ||
use std::sync::Arc; | ||
|
||
use ethers::{ | ||
providers::Middleware, | ||
signers::LocalWallet, | ||
types::{Address, Eip1559TransactionRequest, TransactionReceipt, U256}, | ||
}; | ||
|
||
use crate::helpers::ERC20; | ||
|
||
use super::{error::ExecutionClientError, quotes::ExecutionQuote, ExecutionClient}; | ||
|
||
impl ExecutionClient { | ||
/// Execute a quoted swap | ||
pub async fn execute_swap( | ||
&self, | ||
quote: ExecutionQuote, | ||
wallet: LocalWallet, | ||
) -> Result<(), ExecutionClientError> { | ||
// Approve the necessary ERC20 allowance | ||
let amount = U256::from_str("e.sell_amount).map_err(ExecutionClientError::parse)?; | ||
self.approve_erc20_allowance(quote.sell_token_address, quote.to, amount, &wallet).await?; | ||
|
||
// Execute the swap | ||
self.execute_swap_tx(quote, wallet).await.map(|_| ()) | ||
} | ||
|
||
/// Approve an erc20 allowance | ||
async fn approve_erc20_allowance( | ||
&self, | ||
token_address: Address, | ||
spender: Address, | ||
amount: U256, | ||
wallet: &LocalWallet, | ||
) -> Result<TransactionReceipt, ExecutionClientError> { | ||
let client = self.get_signer(wallet.clone()); | ||
let erc20 = ERC20::new(token_address, Arc::new(client)); | ||
let tx = erc20.approve(spender, amount); | ||
let pending_tx = tx.send().await.map_err(ExecutionClientError::arbitrum)?; | ||
|
||
pending_tx | ||
.await | ||
.map_err(ExecutionClientError::arbitrum)? | ||
.ok_or_else(|| ExecutionClientError::arbitrum("Transaction failed")) | ||
} | ||
|
||
/// Execute a swap | ||
async fn execute_swap_tx( | ||
&self, | ||
quote: ExecutionQuote, | ||
wallet: LocalWallet, | ||
) -> Result<TransactionReceipt, ExecutionClientError> { | ||
let client = self.get_signer(wallet.clone()); | ||
let tx = Eip1559TransactionRequest::new() | ||
.to(quote.to) | ||
.from(quote.from) | ||
.value(quote.value) | ||
.gas(quote.gas_price) | ||
.data(quote.data); | ||
|
||
// Send the transaction | ||
let pending_tx = client | ||
.send_transaction(tx, None /* block */) | ||
.await | ||
.map_err(ExecutionClientError::arbitrum)?; | ||
pending_tx | ||
.await | ||
.map_err(ExecutionClientError::arbitrum)? | ||
.ok_or_else(|| ExecutionClientError::arbitrum("Transaction failed")) | ||
} | ||
} |
Oops, something went wrong.