-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend create allocation API #3031
Conversation
core/payment/src/api/allocations.rs
Outdated
if let Err(err) = validate_driver(network, driver) { | ||
let err_msg = format!("Validate driver failed (1): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could simplify this. I would do:
if let Err(err) = validate_driver(network, driver) { | |
let err_msg = format!("Validate driver failed (1): {err}"); | |
log::error!("{}", err_msg); | |
return response::bad_request(&err_msg); | |
if let Err(e) = validate_driver(network, driver).map_err(|e| format!("Validate driver failed (1): {err}")).log_err() | |
return response::bad_request(&e); |
We have LogErr
trait in yagna repository
core/payment/src/api/allocations.rs
Outdated
// payment_platform is of the form driver-network-token | ||
// eg. erc20-rinkeby-tglm | ||
let [driver, network, token]: [&str; 3] = match payment_platform | ||
.split('-') | ||
.collect::<Vec<_>>() | ||
.try_into() | ||
{ | ||
Ok(arr) => arr, | ||
Err(_e) => { | ||
let err_msg = format!("paymentPlatform must be of the form driver-network-token instead of {payment_platform}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
}; | ||
|
||
if let Err(err) = validate_network(network) { | ||
let err_msg = format!("Validate network failed (2): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
if let Err(err) = validate_driver(network, driver) { | ||
let err_msg = format!("Validate driver failed (2): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
if let Err(err) = validate_token(network, driver, token) { | ||
let err_msg = format!("Validate token failed (2): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From api you get these values already split. Why do you first build this string to dispatch it later?
You could avoid all these duplicated checks here
core/payment/src/api/allocations.rs
Outdated
fn validate_network(network: &str) -> Result<(), String> { | ||
match network { | ||
"mainnet" => Ok(()), | ||
"rinkeby" => Err("Rinkeby is no longer supported".to_string()), | ||
"goerli" => Ok(()), | ||
"holesky" => Ok(()), | ||
"polygon" => Ok(()), | ||
"mumbai" => Ok(()), | ||
_ => Err(format!("Invalid network name: {network}")), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think you are duplicating a lot of code here. We have enum for network already, which could be just deserialized.
core/payment/src/api/allocations.rs
Outdated
} | ||
} | ||
|
||
fn validate_driver(_network: &str, driver: &str) -> Result<(), String> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same here. We have enum for driver
core/payment/src/api/allocations.rs
Outdated
} else { | ||
log::debug!("Network not specified and token not specified, using default holesky"); | ||
"holesky" | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Driver has get_default_network
method. Probably you should use it, otherwise defaults will diverge.
core/payment/src/api/allocations.rs
Outdated
Some(PaymentPlatformEnum::PaymentPlatformName(name)) => { | ||
log::debug!("Using old API payment platform name as pure str: {}", name); | ||
name.clone() | ||
} | ||
Some(PaymentPlatformEnum::PaymentPlatform(p)) => { | ||
if p.driver.is_none() && p.network.is_none() && p.token.is_none() { | ||
log::debug!("Empty paymentPlatform object, using {DEFAULT_PAYMENT_PLATFORM}"); | ||
DEFAULT_PAYMENT_PLATFORM.to_string() | ||
} else if p.token.is_some() && p.network.is_none() && p.driver.is_none() { | ||
let token = p.token.as_ref().unwrap(); | ||
if token == "glm" { | ||
log::debug!("Selected network {DEFAULT_PAYMENT_PLATFORM_FOR_GLM} (default for glm token)"); | ||
DEFAULT_PAYMENT_PLATFORM_FOR_GLM.to_string() | ||
} else if token == "tglm" { | ||
log::debug!("Selected network {DEFAULT_PAYMENT_PLATFORM_FOR_TGLM} (default for tglm token)"); | ||
DEFAULT_PAYMENT_PLATFORM_FOR_TGLM.to_string() | ||
} else { | ||
let err_msg = | ||
format!("Only glm or tglm token values are accepted vs {token} provided"); | ||
return response::bad_request(&err_msg); | ||
} | ||
} else { | ||
let network = p.network.as_deref().unwrap_or_else(|| { | ||
if let Some(token) = p.token.as_ref() { | ||
if token == "glm" { | ||
log::debug!("Network not specified, using default polygon, because token set to glm"); | ||
"polygon" | ||
} else { | ||
log::debug!("Network not specified, using default holesky"); | ||
"holesky" | ||
} | ||
} else { | ||
log::debug!("Network not specified and token not specified, using default holesky"); | ||
"holesky" | ||
} | ||
}); | ||
if let Err(err) = validate_network(network) { | ||
let err_msg = format!("Validate network failed (1): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
let driver = p.driver.as_deref().unwrap_or_else(|| { | ||
log::debug!("Driver not specified, using default erc20"); | ||
"erc20" | ||
}); | ||
if let Err(err) = validate_driver(network, driver) { | ||
let err_msg = format!("Validate driver failed (1): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
if let Some(token) = p.token.as_ref() { | ||
if let Err(err) = validate_token(network, driver, token) { | ||
let err_msg = format!("Validate token failed (1): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
log::debug!("Selected network {driver}-{network}-{token}"); | ||
format!("{}-{}-{}", driver, network, token) | ||
} else { | ||
let default_token = match get_default_token(driver, network) { | ||
Ok(token) => token, | ||
Err(err) => { | ||
let err_msg = format!("Get default token failed (1): {err}"); | ||
log::error!("{}", err_msg); | ||
return response::bad_request(&err_msg); | ||
} | ||
}; | ||
log::debug!( | ||
"Selected network with default token {driver}-{network}-{default_token}" | ||
); | ||
format!("{}-{}-{}", driver, network, default_token) | ||
} | ||
} | ||
} | ||
None => { | ||
log::debug!("No paymentPlatform entry found, using {DEFAULT_PAYMENT_PLATFORM}"); | ||
DEFAULT_PAYMENT_PLATFORM.to_string() | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is so complicated, that you should hide it in separate function
….com/golemfactory/yagna into scx1332/extend_create_allocation_api
….com/golemfactory/yagna into scx1332/extend_create_allocation_api
resolves: #3023