Skip to content

Commit

Permalink
Merge pull request #45 from bcnmy/dev
Browse files Browse the repository at this point in the history
fix: token amount in approval (#44)
  • Loading branch information
AmanRaj1608 authored Jul 17, 2024
2 parents 3cd7103 + b900ebf commit 1e00004
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
4 changes: 4 additions & 0 deletions crates/routing-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub enum RouteError {
#[derive(Debug)]
pub struct BridgeResult {
route: Route,
source_amount: f64,
source_amount_in_usd: f64,
from_address: String,
to_address: String,
Expand Down Expand Up @@ -147,6 +148,7 @@ impl BridgeResult {
from_token_id: &String,
to_token_id: &String,
is_smart_contract_deposit: bool,
source_amount: f64,
source_amount_in_usd: f64,
from_address: String,
to_address: String,
Expand All @@ -160,6 +162,7 @@ impl BridgeResult {
to_token_id,
is_smart_contract_deposit,
)?,
source_amount,
source_amount_in_usd,
from_address,
to_address,
Expand Down Expand Up @@ -200,6 +203,7 @@ mod test {
&"USDT".to_string(),
false,
100.0,
200.0,
"0x123".to_string(),
"0x456".to_string(),
)
Expand Down
26 changes: 14 additions & 12 deletions crates/routing-engine/src/routing_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ use std::cmp;
use std::collections::HashMap;
use std::sync::Arc;

use alloy::dyn_abi::abi::Token;
use futures::stream::{self, StreamExt};
use log::{debug, error, info};
use thiserror::Error;
use tokio::sync::{Mutex, RwLock};

use account_aggregation::{service::AccountAggregationService, types::TokenWithBalance};
use config::{ChainConfig, Config, config::BucketConfig, SolverConfig, TokenConfig};
use config::{config::BucketConfig, ChainConfig, Config, SolverConfig, TokenConfig};
use storage::{KeyValueStore, RedisClient, RedisClientError};

use crate::token_price::utils::{get_token_price, Errors};
use crate::token_price::TokenPriceProvider;
use crate::{
BridgeResult,
BridgeResultVecWrapper, estimator::{Estimator, LinearRegressionEstimator}, Route,
estimator::{Estimator, LinearRegressionEstimator},
BridgeResult, BridgeResultVecWrapper, Route,
};
use crate::token_price::TokenPriceProvider;
use crate::token_price::utils::{Errors, get_token_price};

const FETCH_REDIS_KEYS_BATCH_SIZE: usize = 50;

Expand Down Expand Up @@ -241,6 +240,7 @@ impl<PriceProvider: TokenPriceProvider> RoutingEngine<PriceProvider> {
to_chain,
&balance.token,
to_token,
balance.amount,
amount_to_take,
false,
&balance.address,
Expand Down Expand Up @@ -321,6 +321,7 @@ impl<PriceProvider: TokenPriceProvider> RoutingEngine<PriceProvider> {
to_chain_id: u32,
from_token_id: &str,
to_token_id: &str,
token_amount: f64,
token_amount_in_usd: f64,
is_smart_contract_deposit: bool,
from_address: &str,
Expand All @@ -344,6 +345,7 @@ impl<PriceProvider: TokenPriceProvider> RoutingEngine<PriceProvider> {

Ok(BridgeResult {
route: Route { from_chain, to_chain, from_token, to_token, is_smart_contract_deposit },
source_amount: token_amount,
source_amount_in_usd: token_amount_in_usd,
from_address: from_address.to_string(),
to_address: to_address.to_string(),
Expand All @@ -366,20 +368,20 @@ mod tests {

use account_aggregation::service::AccountAggregationService;
use config::{
BucketConfig, ChainConfig, get_sample_config, SolverConfig, TokenConfig,
get_sample_config, BucketConfig, ChainConfig, SolverConfig, TokenConfig,
TokenConfigByChainConfigs,
};
use storage::{KeyValueStore, RedisClientError};
use storage::mongodb_client::MongoDBClient;
use storage::{KeyValueStore, RedisClientError};

use crate::estimator::Estimator;
use crate::routing_engine::PathQuery;
use crate::token_price::TokenPriceProvider;
use crate::{
CoingeckoClient,
estimator::{DataPoint, LinearRegressionEstimator},
routing_engine::{RoutingEngine, RoutingEngineError},
CoingeckoClient,
};
use crate::estimator::Estimator;
use crate::routing_engine::PathQuery;
use crate::token_price::TokenPriceProvider;

#[derive(Error, Debug, Display)]
struct Err;
Expand Down
37 changes: 29 additions & 8 deletions crates/routing-engine/src/settlement_engine.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp;
use std::collections::HashMap;
use std::sync::Arc;

Expand All @@ -15,11 +16,11 @@ use tokio::sync::Mutex;

use config::{Config, TokenAddress};

use crate::{blockchain, BridgeResult, BridgeResultVecWrapper, Route};
use crate::blockchain::erc20::IERC20::IERC20Instance;
use crate::source::{EthereumTransaction, RequiredApprovalDetails, RouteSource};
use crate::token_price::utils::{get_token_amount_from_value_in_usd, Errors};
use crate::token_price::TokenPriceProvider;
use crate::token_price::utils::{Errors, get_token_amount_from_value_in_usd};
use crate::{blockchain, BridgeResult, BridgeResultVecWrapper};

pub struct SettlementEngine<Source: RouteSource, PriceProvider: TokenPriceProvider> {
source: Source,
Expand Down Expand Up @@ -100,13 +101,28 @@ impl<Source: RouteSource, PriceProvider: TokenPriceProvider>
.await
.map_err(|err| SettlementEngineErrors::GetTokenAmountFromValueInUsdError(err))?;

info!("Token amount: {:?} for route {}", token_amount, route);
let max_user_balance = &route.source_amount
* f64::powi(
10.0,
route
.route
.from_token
.by_chain
.get(&route.route.from_chain.id)
.unwrap()
.decimals
.into(),
);
let max_user_balance = max_user_balance.floor().to_string().parse()?;
let user_balance: Uint<256, 4> = cmp::min(max_user_balance, token_amount);

info!("Token amount: {:?} for route {}", user_balance, route);

let (ethereum_transactions, required_approval_details) = self
.source
.generate_route_transactions(
&route.route,
&token_amount,
&user_balance,
&route.from_address,
&route.to_address,
)
Expand All @@ -126,7 +142,7 @@ impl<Source: RouteSource, PriceProvider: TokenPriceProvider>
from_address: route.from_address.clone(),
to_address: route.to_address.clone(),
token: route.route.from_token.symbol.clone(),
token_amount: token_amount.clone(),
token_amount: user_balance.clone(),
},
t,
)
Expand Down Expand Up @@ -224,7 +240,7 @@ impl<Source: RouteSource, PriceProvider: TokenPriceProvider>
return Ok(None);
}

let required_approval = required_approval_details.amount - current_approval;
let required_approval = required_approval_details.amount;
info!(
"Required Approval: {:?} against requirement: {:?}",
required_approval, required_approval_details
Expand Down Expand Up @@ -353,6 +369,9 @@ pub enum SettlementEngineErrors<Source: RouteSource, PriceProvider: TokenPricePr

#[error("Error while calling ERC20 Contract: {0}")]
AlloyError(#[from] alloy::contract::Error),

#[error("Error while parsing U256: {0}")]
ParseError(#[from] ruint::ParseError),
}

pub fn generate_erc20_instance_map(
Expand Down Expand Up @@ -440,14 +459,14 @@ mod tests {
use thiserror::Error;
use tokio::sync::Mutex;

use config::{Config, get_sample_config};
use config::{get_sample_config, Config};
use storage::{KeyValueStore, RedisClientError};

use crate::{BridgeResult, BungeeClient, CoingeckoClient};
use crate::settlement_engine::{
generate_erc20_instance_map, SettlementEngine, SettlementEngineErrors, TransactionType,
};
use crate::source::{EthereumTransaction, RequiredApprovalDetails};
use crate::{BridgeResult, BungeeClient, CoingeckoClient};

#[derive(Error, Debug, Display)]
struct Err;
Expand Down Expand Up @@ -768,6 +787,7 @@ mod tests {
&"USDC".to_string(),
&"USDC".to_string(),
false,
200.0,
1000.0,
TEST_OWNER_WALLET.to_string(),
TEST_OWNER_WALLET.to_string(),
Expand Down Expand Up @@ -805,6 +825,7 @@ mod tests {
&"USDC".to_string(),
&"USDT".to_string(),
false,
200.0,
1000.0,
TEST_OWNER_WALLET.to_string(),
TEST_OWNER_WALLET.to_string(),
Expand Down

0 comments on commit 1e00004

Please sign in to comment.