lfglabs-dev / api.starknet.quest

starknet.quest rust backend
7 stars 28 forks source link

Improve the verify_balance route #243

Closed Marchand-Nicolas closed 2 months ago

Marchand-Nicolas commented 3 months ago

Update src\endpoints\quests\verify_balance.rs to support a dynamic total amount.

The aim is to replace the static value "3000000000000000" here

            if result[0] < FieldElement::from_dec_str("3000000000000000").unwrap() {

(line 71)

by a "total_amount" field from the "task" document.

You'll need to update the QuestTaskDocument struct in models.rs to support this new field.

I suggest using the FieldElement type.

This issue would allow us to precise the amount we want for balance checking taks, rather than a static amount which corresponded to approximatively 10$ worth of ETH. This is even more important when we want to call contracts that use different currencies.

Also could be cool to make this field optional for balance tasks, and to use the static value when the "total_amount" field is not set.

TEMHITHORPHE commented 2 months ago

MorninG folks, these seem right within my alley, not too complex, can I please take it up ?

ScottyDavies commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I am front end developer with 6 years experience. i have worked with microsoft on several projects, this will be my first time contributing to the hackathon and i am ready to work

How I plan on tackling this issue

I would approach this issue with following steps Update the QuestTaskDocument struct in models.rs to include a new total_amount field of type FieldElement. Modify the verify_balance function in verify_balance.rs to use the total_amount field from the QuestTaskDocument. If total_amount is None, use the fallback static value. Update the usage of verify_balance to ensure the QuestTaskDocument instance includes the total_amount field. Document the changes, including the new total_amount field and the fallback behavior.

Ugo-X commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I am a Full Stack blockchain Developer with expertise in Next.js, Nest.js, TypeScript, JavaScript, React, Node.js, Three.js, and Solidity. My journey with OnlyDust hackathons began at Edition 1, and I've since made 47 contributions across 11 projects. With my extensive experience on the OnlyDust platform (profile: https://app.onlydust.com/u/Ugo-X), I've honed my skills in delivering quality solutions under pressure. I bring a unique blend of technical proficiency and user-focused design to every project, whether it's crafting immersive 3D experiences or developing smart contracts. My track record shows I can adapt quickly and contribute effectively to diverse challenges. As we surf through Edition 7, I'm excited to leverage my skills and hackathon experience to push the boundaries of blockchain development. I'm confident in my ability to tackle new challenges and drive innovation in this space.

How I plan on tackling this issue

I will address the issue in these steps:

  1. Model Update:

    • Modify the QuestTaskDocument struct in models.rs to include a new field named total_amount.
    • Make this field of type Option<FieldElement>, allowing it to be either present (containing a value) or absent (empty).
  2. Code Update:

    • In src\endpoints\quests\verify_balance.rs, replace the static value with a check on the total_amount field.
  3. Dynamic Amount Handling:

    • If the total_amount is present in the task document:
      • Compare the user's balance (result[0]) with the extracted total_amount value.
    • If the total_amount is absent:
      • Optionally, use a fallback mechanism like the previous static value (3000000000000000) for comparison.
  4. Testing and Refinement:

    • Thoroughly test the changes to ensure proper functionality with both dynamic and static total amounts.
    • Consider error handling for cases where the total_amount fails to parse as a FieldElement.

This approach provides flexibility in specifying the required balance for verification tasks while maintaining a fallback for missing values.

Dprof-in-tech commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

As an experienced Full Stack Blockchain Developer, I'm excited to contribute my expertise to Edition 7 of the OnlyDust hackathons. With a strong background in Next.js, TypeScript, JavaScript, React, Node.js, Solidity, and Cairo, I've honed my technical skills across the blockchain development landscape. My journey with OnlyDust began at Edition 2, and I've since made 28 contributions across 11 projects. This extensive experience on the platform has allowed me to develop a keen understanding of delivering high-quality solutions under tight deadlines. I bring a unique blend of technical prowess and user-centric design to every project, whether I'm crafting immersive 3D experiences or developing innovative smart contracts. My track record demonstrates my ability to adapt quickly and contribute effectively to diverse challenges. I'm confident in my capacity to tackle new problems and drive innovation in the blockchain space. As we embark on Edition 7, I'm eager to leverage my hackathon experience and technical skills to push the boundaries of what's possible in blockchain development. With a passion for creating cutting-edge solutions, I'm excited to collaborate with the OnlyDust community and contribute to the advancement of the blockchain ecosystem.

How I plan on tackling this issue

i would first update the task document to include a total amount field, then i am going to retrieve this total amount value in the code as requested and use optional chaining method to display the dummy text when total amount is undefined or empty

Jayse007 commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I am more commonly a Python developer but I have extensive knowledge in lots of field of programming as I have dabbled in Javascript, Java, Rust, Cairo, C, Typescript. I am good with solving problems, it's just about changing syntax to solve the problem for a particular programming language being used.

How I plan on tackling this issue

I would employ the concept of variables to replace the defined static quantity so that as the variable( using mut keyword for changes), so would the specified or static number change.

thesledge04 commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I have substantial experience in Rust programming, particularly with structuring and manipulating data within Rust applications. My expertise includes updating and managing data models, handling optional fields, and working with various data types like FieldElement.

Specific Skills and Approach: Experience with Rust and Data Models:

I have experience working with Rust’s strong type system and managing complex data structures. This includes defining and updating structs to support new fields and ensuring compatibility with existing code. Handling Optional Fields:

I’ve implemented optional fields in Rust, ensuring that default values are used when an optional field is not set. This involves using Option and providing fallback logic to handle cases where the field might be absent. Working with FieldElement Type:

I’m familiar with the FieldElement type and its use cases, including parsing and comparing large numbers. This experience is crucial for ensuring that the dynamic total_amount field is correctly handled. Approach to the Task: Update QuestTaskDocument Struct:

Add a new total_amount field to the QuestTaskDocument struct in models.rs. This field should be of type Option to handle cases where the field might not be set. Example Update to models.rs:

rust Copy code use some_crate::FieldElement;

[derive(Debug, Serialize, Deserialize)]

pub struct QuestTaskDocument { // Existing fields...

[serde(default)]

pub total_amount: Option<FieldElement>,

} Modify verify_balance.rs to Use total_amount:

Update the logic in verify_balance.rs to check if the total_amount field is set. Use this value if available, otherwise fall back to the static value. Example Update to verify_balance.rs:

rust Copy code use crate::models::QuestTaskDocument; use some_crate::FieldElement;

// Assuming task is an instance of QuestTaskDocument let static_amount = FieldElement::from_dec_str("3000000000000000").unwrap(); let amount_to_check = task.total_amount.unwrap_or(static_amount);

if result[0] < amount_to_check { // Handle the case where the balance is less than the amount to check } Testing:

Test the updated functionality to ensure that the total_amount field is correctly handled and that the fallback to the static value works as expected. Write unit tests to cover scenarios where total_amount is set and not set. Documentation:

Update any relevant documentation or comments to reflect the changes made to the QuestTaskDocument struct and how the dynamic total_amount field is used.

How I plan on tackling this issue

  1. Update QuestTaskDocument Struct in models.rs: Add total_amount Field:

Update the QuestTaskDocument struct to include a new optional field total_amount of type Option. This allows the field to be optional and provides a default value when it’s not set. Example Update in models.rs:

rust Copy code use some_crate::FieldElement; // Import FieldElement type

[derive(Debug, Serialize, Deserialize)]

pub struct QuestTaskDocument { // Existing fields...

[serde(default)]

pub total_amount: Option<FieldElement>,

}

  1. Modify verify_balance.rs to Use total_amount: Fetch the total_amount from the Task Document:

Retrieve the total_amount field from the QuestTaskDocument. Use this value for comparison if it exists; otherwise, fall back to the static value. Update the Logic:

Replace the static value with the dynamic total_amount in the verify_balance.rs file. Example Update in verify_balance.rs:

rust Copy code use crate::models::QuestTaskDocument; use some_crate::FieldElement;

pub fn verify_balance(task: &QuestTaskDocument, result: &[FieldElement]) { // Static amount let static_amount = FieldElement::from_dec_str("3000000000000000").unwrap();

// Determine the amount to check
let amount_to_check = task.total_amount.as_ref().unwrap_or(&static_amount);

// Compare the result with the amount to check
if result[0] < *amount_to_check {
    // Handle the case where the balance is less than the amount to check
}

}

  1. Testing and Validation: Unit Tests:

Write unit tests to ensure that the total_amount field is correctly used when present and that the static value is used as a fallback when it is not set. Example Unit Test:

rust Copy code

[cfg(test)]

mod tests { use super::*; use some_crate::FieldElement;

#[test]
fn test_verify_balance_with_dynamic_amount() {
    let dynamic_amount = FieldElement::from_dec_str("4000000000000000").unwrap();
    let task = QuestTaskDocument { total_amount: Some(dynamic_amount.clone()) };

    let result = [FieldElement::from_dec_str("3000000000000000").unwrap()];

    verify_balance(&task, &result);
    // Add assertions based on expected behavior
}

#[test]
fn test_verify_balance_with_static_amount() {
    let task = QuestTaskDocument { total_amount: None };

    let result = [FieldElement::from_dec_str("3000000000000000").unwrap()];

    verify_balance(&task, &result);
    // Add assertions based on expected behavior
}

} Integration Testing:

Test the entire application to confirm that the total_amount field is integrated correctly and does not cause issues with other parts of the system.

  1. Documentation: Update Documentation: Ensure that the documentation reflects the addition of the total_amount field and its optional nature. Provide examples of how this field can be set in the QuestTaskDocument.
Benjtalkshow commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

I am ready to work on improving the verify_balance route. My name is Benjamin, and I have extensive experience in backend development and API design. I have worked on updating and optimizing routes to handle dynamic data and improve flexibility.

Understanding the Issue

The current implementation of the verify_balance route in src/endpoints/quests/verify_balance.rs uses a static value "3000000000000000" for balance checking:

How I plan on tackling this issue

Update the Static Value

ShantelPeters commented 2 months ago

I am applying to this issue via OnlyDust platform.

My background and how it can be leveraged

How would you approach the problem:

  1. Update Data Model: Modify QuestTaskDocument in models.rs to include an optional total_amount field of type FieldElement.

  2. Modify Endpoint Logic: In verify_balance.rs, replace the static value with the total_amount field. Check if total_amount is None and fallback to the static value if not set.

  3. Testing: Ensure that both scenarios (with and without total_amount) are covered in the tests to validate correct behavior.

How I plan on tackling this issue

  1. Update Data Model: Add an optional total_amount field of type FieldElement to QuestTaskDocument in models.rs.

  2. Modify Endpoint Logic: In verify_balance.rs, replace the static value with the total_amount field. If total_amount is None, use the static value as a fallback.

  3. Test: Validate that both scenarios (with and without total_amount) work correctly by updating and running relevant tests.

onlydustapp[bot] commented 2 months ago

The maintainer Marchand-Nicolas has assigned Dprof-in-tech to this issue via OnlyDust Platform. Good luck!