-
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
Scx1332/tokenrefaktor #3042
Scx1332/tokenrefaktor #3042
Conversation
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 believe this code could still be much more simpler than it is, but I'm not going to push further for now. I left a few comments, please look if you would like to apply them
impl Display for TokenName { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.0) | ||
} |
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.
impl Display for PaymentPlatformTriple { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}-{}-{}", self.driver, self.network, self.token) | ||
} | ||
} |
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.
core/payment/src/api/allocations.rs
Outdated
let default_platform = Self::default_testnet(); | ||
log::debug!("Empty paymentPlatform object, using {default_platform}"); | ||
default_platform | ||
} else if p.token.is_some() && p.network.is_none() && p.driver.is_none() { | ||
let token = p.token.as_ref().unwrap(); | ||
if token == "GLM" || token == "tGLM" { | ||
return Err(bad_req_and_log(format!( | ||
"Uppercase token names are not supported. Use lowercase glm or tglm instead of {}", | ||
token | ||
))); | ||
} else if token == "glm" { | ||
let default_platform = Self::default_mainnet(); | ||
log::debug!("Selected network {default_platform} (default for glm token)"); | ||
default_platform | ||
} else if token == "tglm" { | ||
let default_platform = Self::default_testnet(); | ||
log::debug!("Selected network {default_platform} (default for tglm token)"); | ||
default_platform | ||
} else { | ||
return Err(bad_req_and_log(format!( | ||
"Only glm or tglm token values are accepted vs {token} provided" | ||
))); | ||
} | ||
} else { | ||
let network_str = 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 {}, because token set to glm", | ||
DEFAULT_MAINNET_NETWORK | ||
); | ||
DEFAULT_MAINNET_NETWORK.into() | ||
} else { | ||
log::debug!( | ||
"Network not specified, using default {}", | ||
DEFAULT_TESTNET_NETWORK | ||
); | ||
DEFAULT_TESTNET_NETWORK.into() | ||
} | ||
} else { | ||
log::debug!( | ||
"Network not specified and token not specified, using default {}", | ||
DEFAULT_TESTNET_NETWORK | ||
); | ||
DEFAULT_TESTNET_NETWORK.into() | ||
} | ||
}); | ||
let network = validate_network(network_str).map_err(|err| { | ||
bad_req_and_log(format!("Validate network failed (1): {err}")) | ||
})?; | ||
|
||
let driver_str = p.driver.as_deref().unwrap_or_else(|| { | ||
log::debug!( | ||
"Network not specified, using default {}, because token set to glm", | ||
DEFAULT_MAINNET_NETWORK | ||
"Driver not specified, using default {}", | ||
DEFAULT_PAYMENT_DRIVER | ||
); | ||
DEFAULT_MAINNET_NETWORK.into() | ||
DEFAULT_PAYMENT_DRIVER.into() | ||
}); | ||
let driver = validate_driver(&network, driver_str) | ||
.map_err(|err| bad_req_and_log(format!("Validate driver failed (1): {err}")))?; | ||
|
||
if let Some(token) = p.token.as_ref() { | ||
let token = | ||
TokenName::from_token_string(&driver, &network, token).map_err(|err| { | ||
bad_req_and_log(format!("Validate token failed (1): {err}")) | ||
})?; | ||
log::debug!("Selected network {}-{}-{}", driver, network, token); | ||
Self { | ||
driver, | ||
network, | ||
token, | ||
} | ||
} else { | ||
let default_token = TokenName::default(&driver, &network); | ||
|
||
log::debug!( | ||
"Network not specified, using default {}", | ||
DEFAULT_TESTNET_NETWORK | ||
"Selected network with default token {}-{}-{}", | ||
driver, | ||
network, | ||
default_token | ||
); | ||
DEFAULT_TESTNET_NETWORK.into() | ||
Self { | ||
driver, | ||
network, | ||
token: default_token, | ||
} | ||
} | ||
} else { | ||
log::debug!( | ||
"Network not specified and token not specified, using default {}", | ||
DEFAULT_TESTNET_NETWORK | ||
); | ||
DEFAULT_TESTNET_NETWORK.into() | ||
} |
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 could be match statement probably:
match (p.driver, p.network, p.token) {
(None, None, None) => { ... },
(None, None, Some(token)) => { ... }
}
core/payment/src/api/allocations.rs
Outdated
if token == "GLM" || token == "tGLM" { | ||
return Err(bad_req_and_log(format!( | ||
"Uppercase token names are not supported. Use lowercase glm or tglm instead of {}", | ||
token | ||
))); | ||
} else if token == "glm" { |
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.
It looks similar to from_token_string
function implementation
|
||
let network = validate_network(network_str) | ||
.map_err(|err| bad_req_and_log(format!("Validate network failed (2): {err}")))?; | ||
fn validate_network(network: &str) -> Result<NetworkName, 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.
Common practice would be to use anyhow
if you need to return something generic as string.
Co-authored-by: nieznanysprawiciel <nieznany.sprawiciel@gmail.com>
Co-authored-by: nieznanysprawiciel <nieznany.sprawiciel@gmail.com>
No description provided.