UNPKG

wisdom-sdk

Version:

Core business logic and data access layer for prediction markets

232 lines (230 loc) 8.09 kB
declare enum TransactionType { TRANSFER = "transfer", PREDICT = "predict", CLAIM_REWARD = "claim-reward" } type UserFacingStatus = 'unresolved' | 'won' | 'lost' | 'redeemed'; interface CustodyTransaction { signature: string; nonce: number; signer: string; type: TransactionType; subnetId: string; to?: string; amount?: number; marketId?: string | number; outcomeId?: number; receiptId?: number; id: string; userId: string; takenCustodyAt: string; status: 'pending' | 'submitted' | 'confirmed' | 'rejected'; submittedAt?: string; confirmedAt?: string; rejectedAt?: string; rejectionReason?: string; marketName?: string; outcomeName?: string; nftReceipt?: any; potentialPayout?: number; blockchainStatus?: UserFacingStatus; verifiedAt?: string; isVerifiedWinner?: boolean; } declare const custodyStore: { takeCustody(data: { signature: string; nonce: number; signer: string; type: TransactionType; subnetId: string; to?: string; amount?: number; marketId?: string | number; outcomeId?: number; receiptId?: number; userId: string; }): Promise<{ success: boolean; transaction?: CustodyTransaction; error?: string; }>; getUserTransactions(userId: string): Promise<CustodyTransaction[]>; /** * Get all potentially redeemable (won) predictions for a user with blockchain verification * This is specifically for showing accurate "Ready to Redeem" predictions to users */ getUserRedeemablePredictions(userId: string): Promise<{ redeemablePredictions: CustodyTransaction[]; totalPotentialPayout: number; }>; getSignerTransactions(signer: string): Promise<CustodyTransaction[]>; getMarketTransactions(marketId: string): Promise<CustodyTransaction[]>; /** * Helper function to get the user-facing status for a transaction * This combines the internal status with blockchain verification data */ getUserFacingStatus(transaction: CustodyTransaction): UserFacingStatus; /** * Get a specific transaction by ID * @param id The transaction ID * @param options Optional parameters * @param options.verifyBlockchain Whether to verify status against blockchain for submitted transactions * @returns The transaction object */ getTransaction(id: string, options?: { verifyBlockchain?: boolean; }): Promise<CustodyTransaction>; findBySignature(signature: string): Promise<(CustodyTransaction)[]>; getNFTReceipt(id: string): Promise<{} | undefined>; getPendingPredictionsForMarket(marketId: string): Promise<CustodyTransaction[]>; getAllPendingPredictions(): Promise<CustodyTransaction[]>; updateTransactionStatus(id: string, status: "pending" | "submitted" | "confirmed" | "rejected", details?: { reason?: string; }): Promise<CustodyTransaction | undefined>; markAsSubmitted(id: string): Promise<CustodyTransaction | undefined>; markAsConfirmed(id: string): Promise<CustodyTransaction | undefined>; markAsRejected(id: string, reason?: string): Promise<CustodyTransaction | undefined>; deleteTransaction(transactionId: string): Promise<boolean>; /** * Synchronize submitted prediction statuses with blockchain state * This is a batch operation to update all submitted predictions in custody * This should be called periodically (e.g., by a cron job) */ syncSubmittedPredictionStatuses(): Promise<{ success: boolean; updated: number; errors: number; error?: string; details?: { won: number; lost: number; pending: number; redeemed: number; }; }>; generateNftImage(marketName: string, outcomeName: string, amount: number): string; /** * Check if a prediction can be returned by the user * This checks if the prediction is still within the return window and hasn't been submitted on-chain * * @param transactionId The ID of the transaction to check * @returns A boolean indicating if the prediction can be returned */ canReturnPrediction(transactionId: string): Promise<{ canReturn: boolean; reason?: string; transaction?: CustodyTransaction; }>; /** * Return a prediction receipt to the user * This will delete all records for the prediction * * @param userId User ID requesting the return * @param transactionId The ID of the transaction to return * @returns Result of the return operation */ returnPrediction(userId: string, transactionId: string): Promise<{ success: boolean; error?: string; transaction?: CustodyTransaction; }>; /** * Process pending prediction transactions in batches to send them on-chain * This function is intended to be called by a cron job every hour * It will process up to maxBatchSize predictions at a time, FIFO order * Only processes transactions that are at least minAgeMinutes old by default * @param options Optional parameters to customize batch processing * @returns Results of the batch processing operation */ batchProcessPredictions(options?: { forceProcess?: boolean; marketId?: string; }): Promise<{ success: boolean; processed: number; batched: number; errors: number; error?: string; txid?: string; }>; createPredictionWithCustody(data: { signature: string; nonce: number; signer: string; subnetId: string; marketId: string; outcomeId: number; userId: string; amount: number; }): Promise<{ success: boolean; transaction?: CustodyTransaction; error?: string; market?: Record<string, unknown>; txid?: string; }>; /** * Create a claim reward transaction with custody * This function allows users to claim rewards for winning predictions via Signet * * @param data Transaction and claim data * @returns Result with transaction details or error */ createClaimRewardWithCustody(data: { signature: string; nonce: number; signer: string; subnetId: string; predictionId: string; receiptId: number; userId: string; }): Promise<{ success: boolean; transaction?: any; error?: string; txid?: string; }>; /** * Get all claim reward transactions for a user * * @param userId User ID to get claim reward transactions for * @returns Array of claim reward transactions */ getUserClaimRewardTransactions(userId: string): Promise<any[]>; /** * Get a specific claim reward transaction by ID * * @returns The transaction object */ getAllClaimRewardTransactions(): Promise<any[]>; /** * Get a specific claim reward transaction by ID * * @param id The transaction ID * @returns The transaction object */ getClaimRewardTransaction(id: string): Promise<any>; /** * Get all pending claim reward transactions * @returns Array of pending claim reward transactions */ getAllPendingClaimRewards(): Promise<any[]>; /** * Process pending claim reward transactions in batches to send them on-chain * This function is intended to be called by a cron job * @param options Optional parameters to customize batch processing * @returns Results of the batch processing operation */ batchProcessClaimRewards(options?: { forceProcess?: boolean; }): Promise<{ success: boolean; processed: number; batched: number; errors: number; error?: string; txid?: string; }>; }; export { type CustodyTransaction, TransactionType, type UserFacingStatus, custodyStore };