Skip to content
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

Merged
merged 24 commits into from
Feb 2, 2024
Merged

Conversation

scx1332
Copy link
Collaborator

@scx1332 scx1332 commented Jan 30, 2024

resolves: #3023

@scx1332 scx1332 requested a review from a team as a code owner January 30, 2024 17:52
Comment on lines 166 to 169
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);
Copy link
Contributor

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:

Suggested change
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

Comment on lines 206 to 235
// 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);
}
Copy link
Contributor

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

Comment on lines 42 to 52
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}")),
}
}
Copy link
Contributor

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.

}
}

fn validate_driver(_network: &str, driver: &str) -> Result<(), String> {
Copy link
Contributor

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

Comment on lines 152 to 155
} else {
log::debug!("Network not specified and token not specified, using default holesky");
"holesky"
}
Copy link
Contributor

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.

Comment on lines 121 to 199
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()
}
};
Copy link
Contributor

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

@scx1332 scx1332 merged commit 2631a77 into master Feb 2, 2024
15 checks passed
@scx1332 scx1332 deleted the scx1332/extend_create_allocation_api branch February 2, 2024 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Driver & Network Selection - new mechanism
2 participants