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

Revert "fix: rise and fall contracts" #28

Conversation

vinu-deriv
Copy link
Contributor

@vinu-deriv vinu-deriv commented Jan 17, 2025

Reverts #27

Summary by Sourcery

Bug Fixes:

  • Restored the previous behavior of the price proposal flow by removing the automatic contract purchase logic. The trade buttons no longer trigger a purchase action, and the price proposal subscription is handled within a dedicated effect hook.

Copy link

sourcery-ai bot commented Jan 17, 2025

Reviewer's Guide by Sourcery

This PR reverts the changes introduced in #27. It removes the buy contract functionality and the open contracts modal from the trading panel. The price proposal logic is moved back into the useEffect hook.

Sequence diagram for price proposal flow after revert

sequenceDiagram
    participant Component
    participant usePriceProposal
    participant DerivAPI

    Component->>usePriceProposal: Initialize with parameters
    activate usePriceProposal
    Note over usePriceProposal: useEffect triggers
    usePriceProposal->>DerivAPI: Subscribe to RISE proposal
    usePriceProposal->>DerivAPI: Subscribe to FALL proposal
    DerivAPI-->>usePriceProposal: Proposal updates
    usePriceProposal-->>Component: Return proposal data
    deactivate usePriceProposal
Loading

Class diagram showing removed functionality

classDiagram
    class TradingPanelStore {
        -duration: number
        -price: number
        -selectedStakeTab: string
        -durationError: string
        -priceError: string
        -is_rise_fall_valid: boolean
        +setDuration()
        +setPrice()
        +setSelectedStakeTab()
    }

    class usePriceProposal {
        +proposal: object
        +clearProposal()
        +isLoading: object
    }

    note for TradingPanelStore "Removed:
    - riseContractId
    - fallContractId
    - openContracts
    - isOpenContractsModalVisible"

    note for usePriceProposal "Moved proposal logic
back into useEffect"
Loading

File-Level Changes

Change Details Files
Removed buy contract functionality
  • Removed buyContract function and its usage.
  • Removed the onBuy prop from the TradeButton component.
  • Simplified the TradeButton component by removing the onClick handler.
  • Removed the call to the handleProposal function in the TradingPanel component
src/hooks/useRiseFallTrading.ts
src/components/TradingComponents/TradingPanel/TradingPanel.tsx
src/components/TradingComponents/TradeButton/TradeButton.tsx
Removed open contracts modal
  • Removed the open contracts modal component and its related logic.
  • Removed the open contracts button and its handler.
  • Removed the OpenContract type and related properties.
  • Removed the state and methods for managing open contracts.
  • Removed the styling for the open contracts modal and button.
  • Deleted the ContractItem, OpenContractsModal, and OpenContractsModal.scss files
src/components/TradingComponents/TradingPanel/TradingPanel.tsx
src/types/deriv-api.types.ts
src/stores/TradingPanelStore.ts
src/components/TradingComponents/TradingPanel/TradingPanel.scss
src/components/TradingComponents/OpenContractsModal/ContractItem.tsx
src/components/TradingComponents/OpenContractsModal/OpenContractsModal.scss
src/components/TradingComponents/OpenContractsModal/OpenContractsModal.tsx
Moved price proposal logic back to useEffect
  • Moved the price proposal logic back into the useEffect hook.
  • Made the unsubscribeByPrefix method private.
  • Removed the asynchronous handleProposal function and its call within the useEffect hook.
  • The price proposal is now handled directly within the useEffect hook
src/hooks/usePriceProposal.ts
src/services/deriv-api.service.ts
Removed unnecessary hooks
  • Removed the useProposalOpenContract and useBuyContract hooks.
  • Removed the calls to these hooks within the useRiseFallTrading hook.
  • Deleted the useBuyContract.ts and useProposalOpenContract.ts files
src/hooks/useRiseFallTrading.ts
src/hooks/useBuyContract.ts
src/hooks/useProposalOpenContract.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@vinu-deriv vinu-deriv closed this Jan 17, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @vinu-deriv - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Please provide context in the PR description explaining why this feature is being reverted. Understanding the motivation will help reviewers assess if this is the right approach.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -37,75 +37,67 @@ export const usePriceProposal = (
setProposal({});
};

const handleProposal = async () => {
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the price proposal subscription logic into a custom hook to improve code organization, testability, and maintainability.

The current implementation could be simplified by extracting the subscription logic into a custom hook while maintaining proper cleanup. Here's a suggested approach:

// useProposalSubscription.ts
const useProposalSubscription = (config: ProposalConfig) => {
  const [proposal, setProposal] = useState({});
  const [isLoading, setIsLoading] = useState({ rise: false, fall: false });
  const derivAPI = getDerivAPI();

  useEffect(() => {
    if (!config.isValid) return;

    const subscribeToType = async (type: "rise" | "fall") => {
      const contractConfig = {
        proposal: 1,
        subscribe: 1,
        amount: config.price,
        basis: config.basis,
        contract_type: type === "rise" ? "CALL" : "PUT",
        currency: "USD",
        duration: config.duration,
        duration_unit: "m",
        symbol: config.symbol,
      };

      try {
        await derivAPI.subscribeStream(
          contractConfig,
          handleProposalResponse(type),
          `PROPOSAL_${type.toUpperCase()}`
        );
      } catch (err) {
        handleError(err, type);
      }
    };

    setIsLoading({ rise: true, fall: true });
    Promise.all([subscribeToType("rise"), subscribeToType("fall")]);

    return () => derivAPI.unsubscribeByPrefix("PROPOSAL_");
  }, [config]);

  return { proposal, isLoading };
};

// Original component
const useTradingPanel = () => {
  const { proposal, isLoading } = useProposalSubscription({
    price: debouncedPrice,
    duration: debouncedDuration,
    basis,
    symbol,
    isValid: !durationError && !priceError && is_rise_fall_valid
  });

  return { proposal, clearProposal, isLoading };
};

This approach:

  1. Separates subscription logic into a focused custom hook
  2. Maintains proper cleanup via useEffect
  3. Reduces nesting and improves testability
  4. Keeps all functionality intact while being more maintainable

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.

1 participant