> For the complete documentation index, see [llms.txt](https://docs.polyquest.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.polyquest.xyz/features/apy-to-prediction-conversion/smart-contract-logic.md).

# Smart Contract Logic

This feature allows users to leverage their future yield (APY) from DeFi protocols as a means to participate in prediction markets on Polyquest, without losing their underlying collateral. The conversion process is facilitated through smart contracts that manage permissions, calculations, conversions, and settlements.

## **Smart Contract Components**

1. **User Wallet Integration Contract**
   * **Function**: Connects the user's DeFi protocol account to the Polyquest platform.
   * **Logic**:
     * The user initiates a connection request to link their wallet containing the collateralized assets and ongoing DeFi investment.
     * The contract verifies the user's APY, collateral, and yield-generating details through a DeFi protocol API (e.g., Jupiter, Scallop, etc.
     * The user grants permission for Polyquest to use their future APY rights.
2. **APY Assessment Contract**
   * **Function**: Calculates the user's projected future yield based on current APY rates and collateral value.
   * **Logic**:
     * Upon receiving permission, the contract fetches the user's APY and collateral data.
     * It calculates the future yield over a specified period (e.g., 7% APY on $100 collateral for one year).
     * The contract checks for changes in APY or collateral value periodically and adjusts the calculation accordingly.
3. **Yield-to-Token Conversion Contract**
   * **Function**: Converts the calculated future yield into USDC or other SPL tokens.
   * **Logic**:
     * The contract locks in the user's future yield by minting a tokenized representation of the yield (e.g., yUSDC).
     * It swaps the tokenized yield (yUSDC) to USDC or other SPL tokens via an integrated DEX API (e.g., Jupiter or Orca) using real-time data to optimize the swap.
     * A small service fee is deducted from the converted tokens before they are credited to the user’s Polyquest account.
   * **Security**: Ensures that the user’s principal collateral remains untouched and only future yield rights are exchanged.
4. **APY Rights Settlement Contract**
   * **Function**: Manages the settlement of APY rights once they mature or upon user request.
   * **Logic**:
     * If the quest outcome is favorable, the user's share of future yield (now tokenized and converted) is settled in USDC or $POLY.
     * If the user wishes to reclaim their yield before the end of the prediction period, they can trigger a settlement; the contract then calculates the available yield and completes the settlement, accounting for early withdrawal penalties if applicable.
   * **Reinvestment Option**: The contract allows for automatic reinvestment of yield into future quests or back into the user’s DeFi protocol to continue generating APY.
5. **Security and Compliance Contracts**
   * **Function**: Ensure all operations comply with platform rules and security standards.
   * **Logic**:
     * Multi-signature authorization for critical functions like withdrawals and settlements.
     * Automated audits and checks for anomalies or unauthorized actions.
     * Robust error handling and reversion mechanisms to protect user funds and data.

## **Workflow Summary**

1. **User connects** their DeFi protocol account and grants permission for Polyquest to manage their future yield rights.
2. **Smart contracts assess** the current APY and collateral value to calculate the projected future yield.
3. **Yield is converted** to tokens (USDC or SPL) via a DEX integration, deducting minimal fees.
4. **Users participate** in prediction quests using their converted tokens as voting power.
5. **Rewards or losses** are settled automatically based on quest outcomes.
6. **Future yield rights** are managed and settled securely, with options for reinvestment or early withdrawal.

By leveraging smart contracts for the **APY-to-Prediction Conversion** feature, Polyquest ensures a transparent, secure, and user-friendly experience, allowing users to maximize their DeFi yields without risking their principal collateral. This feature not only enhances user engagement but also drives greater utility for $POLY and potentially attracts new projects looking to provide additional use cases for their tokens.
