-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfull-flow.ts
More file actions
97 lines (83 loc) · 3.46 KB
/
Copy pathfull-flow.ts
File metadata and controls
97 lines (83 loc) · 3.46 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
94
95
96
97
/**
* End-to-end flow: wallet setup → mint → approve → deposit → create market
*
* Usage:
* bun run examples/full-flow.ts
*
* Environment variables:
* PRIVATE_KEY — hex private key (generates a random one if not set)
* API_KEY — Context API key (default: none)
* QUESTION — question to submit (default: "Will Bitcoin hit $200k by end of 2026?")
*/
import { ContextClient } from "../src/index.js";
import { generatePrivateKey } from "viem/accounts";
const privateKey = (process.env.CONTEXT_PRIVATE_KEY as `0x${string}`) ?? generatePrivateKey();
const apiKey = process.env.CONTEXT_API_KEY;
const question = process.env.QUESTION ?? "Will Bitcoin hit $200k by end of 2026?";
async function main() {
console.log("=== Context SDK Full Flow ===\n");
// 1. Create client with signer
const ctx = new ContextClient({
apiKey,
signer: { privateKey },
});
console.log(`Wallet: ${ctx.address}`);
// 2. Check wallet status
console.log("\n--- Wallet Status ---");
const status = await ctx.account.status();
console.log(` ETH balance: ${status.ethBalance}`);
console.log(` USDC allowance: ${status.usdcAllowance}`);
console.log(` Operator approved: ${status.isOperatorApproved}`);
console.log(` Needs approvals: ${status.needsApprovals}`);
// 3. Mint test USDC
console.log("\n--- Minting Test USDC ---");
const mintResult = await ctx.account.mintTestUsdc(1000);
console.log(` Mint result:`, mintResult);
// 4. Approve & setup (USDC allowance + operator approval)
if (status.needsApprovals) {
console.log("\n--- Setting Up Approvals ---");
const setupResult = await ctx.account.setup();
console.log(` USDC approval tx: ${setupResult.usdcApprovalTx ?? "already approved"}`);
console.log(` Operator approval tx: ${setupResult.operatorApprovalTx ?? "already approved"}`);
} else {
console.log("\n--- Approvals already in place ---");
}
// 5. Deposit USDC into Holdings
console.log("\n--- Depositing 100 USDC ---");
const depositTx = await ctx.account.deposit(100);
console.log(` Deposit tx: ${depositTx}`);
// 6. Check balance after deposit
console.log("\n--- Balance After Deposit ---");
const balance = await ctx.portfolio.balance();
console.log(` USDC settlement balance: ${balance.usdc.settlementBalance}`);
console.log(` USDC wallet balance: ${balance.usdc.walletBalance}`);
// 7. Submit question and wait for generation
console.log("\n--- Submitting Question ---");
console.log(` Question: "${question}"`);
const submission = await ctx.questions.submitAndWait(question, {
pollIntervalMs: 2000,
maxAttempts: 45,
});
console.log(` Status: ${submission.status}`);
console.log(` Generated ${submission.questions.length} question(s):`);
for (const q of submission.questions) {
console.log(` - [${q.id}] ${q.text ?? "(no text)"}`);
if (q.criteria) console.log(` Criteria: ${q.criteria}`);
}
// 8. Create market from first generated question
const firstQuestion = submission.questions[0];
if (!firstQuestion) {
console.error("No questions generated!");
process.exit(1);
}
console.log("\n--- Creating Market ---");
console.log(` Using question ID: ${firstQuestion.id}`);
const market = await ctx.markets.create(firstQuestion.id);
console.log(` Market ID: ${market.marketId}`);
console.log(` Tx Hash: ${market.txHash}`);
console.log("\n=== Done! ===");
}
main().catch((err) => {
console.error("Flow failed:", err);
process.exit(1);
});