This repository was archived by the owner on Jun 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprediction-market.ts
More file actions
93 lines (83 loc) · 2.86 KB
/
Copy pathprediction-market.ts
File metadata and controls
93 lines (83 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* Prediction Market Demo
*
* Demonstrates asset price predictability and reputation using Glogos protocol.
*
* Nobel Prize 2013: Eugene Fama, Lars Peter Hansen, & Robert Shiller
*/
import {
generateZone,
createAttestation,
sha256Hex,
GLR,
computeCanonId,
type Attestation,
type AttestationInput,
type ZoneId,
type SubjectHash,
type UnixTimestamp,
type HashHex
} from '@glogos/core';
const MARKET_CANON = computeCanonId('opt:market:prediction:1.0');
const FORECAST_CANON = computeCanonId('opt:market:forecast:1.0');
const RESOLUTION_CANON = computeCanonId('opt:market:resolution:1.0');
interface Forecast {
predictor: ZoneId;
event: string;
prediction: string;
probability: number;
attestation: Attestation;
}
export async function runPredictionDemo() {
console.log('='.repeat(80));
console.log('PREDICTION MARKET DEMO');
console.log('Fama, Hansen, & Shiller - Nobel Prize 2013');
console.log('='.repeat(80));
const oracle = await generateZone();
const predictors = [await generateZone(), await generateZone()];
// 1. Create Market
const marketInput: AttestationInput = {
zone: oracle.id,
subject: sha256Hex(JSON.stringify({ event: 'Election 2024', type: 'Binary' })) as SubjectHash,
canon: MARKET_CANON,
time: Math.floor(Date.now() / 1000) as unknown as UnixTimestamp,
refs: [GLR as HashHex]
};
const { attestation: market } = await createAttestation(marketInput, oracle.privateKey);
console.log(` Market created: Election 2024 (${market.id.substring(0, 8)})`);
// 2. Predictors make forecasts
const forecasts: Forecast[] = [];
for (const p of predictors) {
const forecastInput: AttestationInput = {
zone: p.id,
subject: sha256Hex(
JSON.stringify({ prediction: 'Candidate A', probability: 0.75 })
) as SubjectHash,
canon: FORECAST_CANON,
time: Math.floor(Date.now() / 1000) as unknown as UnixTimestamp,
refs: [market.id]
};
const { attestation } = await createAttestation(forecastInput, p.privateKey);
forecasts.push({
predictor: p.id,
event: 'Election 2024',
prediction: 'Candidate A',
probability: 0.75,
attestation
});
console.log(` Forecast by ${p.id.substring(0, 8)}: Candidate A with 75%`);
}
// 3. Market Resolution
const resolutionInput: AttestationInput = {
zone: oracle.id,
subject: sha256Hex(JSON.stringify({ outcome: 'Candidate A' })) as SubjectHash,
canon: RESOLUTION_CANON,
time: (Math.floor(Date.now() / 1000) + 100) as unknown as UnixTimestamp,
refs: [market.id]
};
const { attestation: resolution } = await createAttestation(resolutionInput, oracle.privateKey);
console.log(` Market resolved: Candidate A won! (${resolution.id.substring(0, 8)})`);
console.log('✅ Prediction demo completed!');
console.log('='.repeat(80));
}
runPredictionDemo().catch(console.error);