wisdom-sdk
Version:
Core business logic and data access layer for prediction markets
153 lines (150 loc) • 5.17 kB
text/typescript
import { MarketQueryOptions, PaginatedResult } from './utils.cjs';
import { TxBroadcastResult } from '@stacks/transactions';
import { PredictionMarketInfo } from './prediction-contract-store.cjs';
interface Market {
id: string;
type: 'binary' | 'multiple';
name: string;
description: string;
outcomes: {
id: number;
name: string;
votes?: number;
amount?: number;
}[];
createdBy: string;
category: string;
endDate: string;
imageUrl?: string;
createdAt: string;
participants: number;
poolAmount: number;
status: string;
resolvedOutcomeId?: number;
resolvedAt?: string;
resolvedBy?: string;
adminFee?: number;
remainingPot?: number;
totalWinningAmount?: number;
}
interface MarketSyncResult {
marketId: string;
name: string;
status: 'already_synced' | 'updated' | 'error';
onChainData?: {
'is-open': boolean;
'is-resolved': boolean;
'winning-outcome'?: number;
};
localData?: {
status: string;
resolvedOutcomeId?: number;
};
error?: string;
}
declare const marketStore: {
getMarkets(options?: MarketQueryOptions): Promise<PaginatedResult<Market>>;
getMarket(id: string, options?: {
verifyWithBlockchain?: boolean;
}): Promise<Market | undefined>;
/**
* Get market information directly from the blockchain
* This calls the prediction contract store to get the on-chain market data
* @param id Market ID
* @returns Market information from blockchain or null if not found
*/
getMarketInfo(id: string): Promise<PredictionMarketInfo | null>;
/**
* Helper function to create a market on-chain
* @param marketId Unique ID for the market
* @param name Name of the market
* @param description Description of the market
* @param outcomes List of outcome names
* @returns Promise resolving to the broadcast transaction result
*/
createMarketOnChain(marketId: string, name: string, description: string, outcomes: {
id: number;
name: string;
}[]): Promise<TxBroadcastResult | null>;
createMarket(data: {
type: "binary" | "multiple";
name: string;
description: string;
outcomes: {
id: number;
name: string;
}[];
createdBy: string;
category: string;
endDate: string;
imageUrl?: string;
}): Promise<{
id: string;
type: "binary" | "multiple";
name: string;
description: string;
outcomes: {
id: number;
name: string;
}[];
createdBy: string;
category: string;
endDate: string;
imageUrl: string | undefined;
createdAt: string;
participants: number;
poolAmount: number;
status: string;
}>;
/**
* Resolve a market on-chain with the winning outcome
* @param marketId Unique ID for the market
* @param winningOutcomeId The ID of the winning outcome
* @returns Promise resolving to the broadcast transaction result
*/
resolveMarketOnChain(marketId: string, winningOutcomeId: number): Promise<TxBroadcastResult | null>;
updateMarket(id: string, marketData: any): Promise<any>;
deleteMarket(id: string): Promise<boolean>;
updateMarketStats(marketId: string, outcomeId: number, amount: number, userId: string): Promise<any>;
getRelatedMarkets(marketId: string, limit?: number): Promise<Market[]>;
getMarketsByCategory(category: string, options?: Omit<MarketQueryOptions, "category">): Promise<PaginatedResult<Market>>;
searchMarkets(searchText: string, options?: Omit<MarketQueryOptions, "search">): Promise<PaginatedResult<Market>>;
getTrendingMarkets(limit?: number): Promise<Market[]>;
calculateSimilarity(market1: any, market2: any): number;
buildMarketIndexes(): Promise<{
success: boolean;
indexed: number;
}>;
/**
* Close a market on-chain
* @param marketId Unique ID for the market
* @returns Promise resolving to the broadcast transaction result
*/
closeMarketOnChain(marketId: string): Promise<TxBroadcastResult | null>;
/**
* Automatically close markets that have passed their end date
* This function is meant to be called by a cron job
* @returns Object with stats about markets that were closed
*/
autoCloseExpiredMarkets(): Promise<{
success: boolean;
processed: number;
closed: number;
errors: number;
onChainSucceeded?: number;
onChainFailed?: number;
}>;
/**
* Synchronize market statuses with blockchain state
* This checks all markets against their on-chain state and updates them if they don't match
* @returns Results of the synchronization operation
*/
syncMarketsWithBlockchain(): Promise<{
success: boolean;
processed: number;
updated: number;
errors: number;
syncResults: MarketSyncResult[];
}>;
};
export { type Market, type MarketSyncResult, marketStore };