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

Feat/get all live opps #205

Merged
merged 9 commits into from
Nov 4, 2024
Merged

Feat/get all live opps #205

merged 9 commits into from
Nov 4, 2024

Conversation

anihamde
Copy link
Contributor

@anihamde anihamde commented Nov 1, 2024

This PR adds options to help users get all live opportunities from the server, by paginating by time and altering the number of returned opportunities in the response.

.take(repository::OPPORTUNITY_PAGE_SIZE as usize)
.take(std::cmp::min(
query_params.limit,
OPPORTUNITY_PAGE_SIZE_CAP as usize,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a better place to handle this is at the beginning of the function. we shouldn't implicitly cap it but return an error if it's larger than this cap

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's better to return the max rather than error out. maybe we could add a note/warning field to the message, but i think it's bad to error here since the user can still recover some data and go on to recover more data by paginating

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

people are not going to read the docs carefully and will use a large number here. Let's say a thousand. Then they will see that the endpoint is returning them 44 values and think ok, this would work later on and can return 1000 values. This would lead to a silent failure and unnoticed bugs but if we validate everything in the beginning and throw errors they will catch it in testing phase and wouldn't have any issues on prod.

@@ -163,7 +163,7 @@ impl TryFrom<repository::Opportunity<repository::OpportunityMetadataEvm>> for Op
Ok(OpportunityEvm {
core_fields: OpportunityCoreFields {
id: val.id,
creation_time: val.creation_time.assume_utc().unix_timestamp_nanos(),
creation_time: val.creation_time.assume_utc().unix_timestamp_nanos() / 1000,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I wonder how this was working correctly so far 🤔

const client = createClient<paths>(this.clientOptions);
const fromTimeStr = fromTime
? `${new Date(fromTime / 1000).toISOString().replace("Z", "")}${(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to convert to microsecond format. I suggest getting the fromTime param as a Date type similar to what you did in the python sdk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's also an option. i did it this way to make it easier for the user to specify a microsecond time and have it query that, since Date() does not keep the microsecond-level granularity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could also make fromTime a Date and leave it to the user to specify that. i think i slightly prefer handling it for the user so as to mitigate their chances of unintended behavior with Date, but i'm not wedded to this. wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anybody is going to use such precise time formats anyway. If it's easier for user to use a Date object and do some simple day arithmetics with it, I would prefer accepting a Date object here.

@@ -21,7 +21,7 @@ mod remove_opportunities;
mod remove_opportunity;

pub use models::*;
pub const OPPORTUNITY_PAGE_SIZE: i32 = 20;
pub const OPPORTUNITY_PAGE_SIZE_CAP: i32 = 100;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not usize?

@@ -678,7 +685,7 @@ pub async fn post_opportunity(

/// Fetch opportunities ready for execution or historical opportunities
/// depending on the mode. You need to provide `chain_id` for historical mode.
/// Opportunities are sorted by creation time in ascending order in historical mode.
/// Opportunities are sorted by creation time in ascending order.
/// Total number of opportunities returned is limited by 20.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update number here

.take(repository::OPPORTUNITY_PAGE_SIZE as usize)
.take(std::cmp::min(
query_params.limit,
OPPORTUNITY_PAGE_SIZE_CAP as usize,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

people are not going to read the docs carefully and will use a large number here. Let's say a thousand. Then they will see that the endpoint is returning them 44 values and think ok, this would work later on and can return 1000 values. This would lead to a silent failure and unnoticed bugs but if we validate everything in the beginning and throw errors they will catch it in testing phase and wouldn't have any issues on prod.

const client = createClient<paths>(this.clientOptions);
const fromTimeStr = fromTime
? `${new Date(fromTime / 1000).toISOString().replace("Z", "")}${(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anybody is going to use such precise time formats anyway. If it's easier for user to use a Date object and do some simple day arithmetics with it, I would prefer accepting a Date object here.

#[param(example="2024-05-23T21:26:57.329954Z", value_type = Option<String>)]
#[serde(default, with = "crate::serde::nullable_datetime")]
pub from_time: Option<OffsetDateTime>,
/// The maximum number of opportunities to return. Capped at 100.
#[param(example = "20", value_type = usize)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better way to include the 100 parameter is using the maximum attribute in the param.
Even better we can use something like this to automate the validation:
https://github.com/Keats/validator

@anihamde anihamde merged commit 8e311d3 into main Nov 4, 2024
1 check passed
@anihamde anihamde deleted the feat/get-all-live-opps branch November 4, 2024 17:56
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.

2 participants