forked from Astrea-Payouts/astrea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-demo-event.ts
More file actions
209 lines (190 loc) · 6.38 KB
/
Copy pathseed-demo-event.ts
File metadata and controls
209 lines (190 loc) · 6.38 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// L01: seed the standing demo event — a real event, funded with a real
// testnet escrow, left LIVE and awaiting judging on purpose. L02's demo
// video records the rest of the lifecycle (assign winner, approve, release,
// forward) live against this event, instead of showing something that
// already happened. Reuses the K01 spike accounts, same pattern as E06.
import "dotenv/config";
import { readFileSync } from "node:fs";
import { Keypair, TransactionBuilder } from "@stellar/stellar-sdk";
import { db } from "@/lib/db";
import { env } from "@/lib/env";
import { prepareOperation, submitOperation } from "@/lib/escrow/pipeline";
import { trustlessWorkAdapter } from "@/lib/escrow/trustless-work-adapter";
import { transitionEvent } from "@/lib/state-machines/apply";
import { STELLAR_NETWORK_PASSPHRASE } from "@/lib/stellar-network";
import { verifyAndRecordTrustline } from "@/lib/trustline/verify-and-record";
import { hasUsdcTrustline } from "@/lib/trustline/verify-trustline";
interface Keys {
publicKey: string;
secret: string;
}
const accounts: Record<"organizer" | "judge" | "winner" | "resolver", Keys> =
JSON.parse(
readFileSync(
new URL("../spikes/k01-trustless-work/.accounts.json", import.meta.url),
"utf8",
),
);
function signXdr(unsignedXdr: string, secret: string): string {
const tx = TransactionBuilder.fromXDR(
unsignedXdr,
STELLAR_NETWORK_PASSPHRASE,
);
tx.sign(Keypair.fromSecret(secret));
return tx.toXDR();
}
function step(label: string) {
console.log(`\n[step] ${label}`);
}
async function findOrCreateWallet(address: string) {
const existing = await db.wallet.findUnique({ where: { address } });
if (existing) return existing;
const user = await db.user.create({ data: {} });
return db.wallet.create({ data: { address, userId: user.id } });
}
const PRIZE_AMOUNT = 1;
async function main() {
const { organizer, judge, winner } = accounts;
const runId = Date.now();
step("Set up organizer wallet");
const organizerWallet = await findOrCreateWallet(organizer.publicKey);
step(
"Judge must already hold a USDC trustline to receive a release later (ADR-007)",
);
if (!(await hasUsdcTrustline(judge.publicKey))) {
throw new Error(
`Judge ${judge.publicKey} has no USDC trustline — run the K01 setup script first`,
);
}
step("Create event (DRAFT)");
const event = await db.event.create({
data: {
organizerId: organizerWallet.userId,
organizerWalletId: organizerWallet.id,
name: "Astrea Demo Hackathon",
description:
"Standing demo event for the GrantFox application and L02's walkthrough video — funded on Stellar testnet, live and awaiting judging.",
},
});
console.log(" event:", event.id);
step("Add judge");
await db.judge.create({
data: {
eventId: event.id,
walletAddress: judge.publicKey,
displayName: "Demo Judge",
},
});
step("Create prize (PENDING)");
const prize = await db.prize.create({
data: {
eventId: event.id,
rank: 1,
amountUsdc: PRIZE_AMOUNT,
milestoneIndex: 0,
},
});
console.log(" prize:", prize.id);
step(
"Deploy escrow — judge is the milestone receiver, winner unknown yet (ADR-007)",
);
const deployKey = `deploy-escrow:${event.id}`;
const deployPrepared = await prepareOperation({
idempotencyKey: deployKey,
operation: "deploy-escrow",
requestPayload: { eventId: event.id },
build: () =>
trustlessWorkAdapter.deployEscrow({
signerPublicKey: organizer.publicKey,
engagementId: `astrea-demo-${runId}`,
title: event.name,
description: event.description ?? "",
roles: {
approver: judge.publicKey,
serviceProvider: winner.publicKey,
platformAddress: organizer.publicKey,
releaseSigner: judge.publicKey,
disputeResolver: accounts.resolver.publicKey,
},
platformFee: 0,
milestones: [
{
description: "1st place",
amount: PRIZE_AMOUNT,
receiver: judge.publicKey,
},
],
trustline: { symbol: env.USDC_SYMBOL, address: env.USDC_ISSUER },
}),
});
if (deployPrepared.alreadySucceeded)
throw new Error("unexpected: fresh event already has a deploy op");
const deploySubmitted = await submitOperation({
idempotencyKey: deployKey,
signedXdr: signXdr(deployPrepared.unsignedXdr, organizer.secret),
submit: (signedXdr) =>
trustlessWorkAdapter.submitSignedTransaction(signedXdr),
});
const contractId = deploySubmitted.contractId;
if (!contractId)
throw new Error("deploy submission did not return a contractId");
console.log(" contractId:", contractId, "tx:", deploySubmitted.txHash);
await db.event.update({
where: { id: event.id },
data: { escrowContractId: contractId },
});
await transitionEvent(event.id, "DRAFT", "CREATED");
step("Fund escrow");
const fundKey = `fund-escrow:${event.id}`;
const fundPrepared = await prepareOperation({
idempotencyKey: fundKey,
operation: "fund-escrow",
requestPayload: { contractId, amount: PRIZE_AMOUNT },
build: () =>
trustlessWorkAdapter.fundEscrow({
contractId,
signerPublicKey: organizer.publicKey,
amount: PRIZE_AMOUNT,
}),
});
if (fundPrepared.alreadySucceeded)
throw new Error("unexpected: fresh event already funded");
const fundSubmitted = await submitOperation({
idempotencyKey: fundKey,
signedXdr: signXdr(fundPrepared.unsignedXdr, organizer.secret),
submit: (signedXdr) =>
trustlessWorkAdapter.submitSignedTransaction(signedXdr),
});
console.log(" funded, tx:", fundSubmitted.txHash);
await transitionEvent(event.id, "CREATED", "FUNDED");
await transitionEvent(event.id, "FUNDED", "LIVE");
step("Register a demo participant + verify trustline (E05)");
const winnerWallet = await findOrCreateWallet(winner.publicKey);
if (!(await verifyAndRecordTrustline(winnerWallet.id, winner.publicKey))) {
throw new Error("demo participant has no USDC trustline");
}
await db.submission.create({
data: {
eventId: event.id,
participantWalletId: winnerWallet.id,
url: "https://github.com/astrea-example/demo",
},
});
step("Judging begins");
await transitionEvent(event.id, "LIVE", "JUDGING");
console.log(
"\n✅ Demo event seeded — funded, live, awaiting judging on purpose.",
);
console.log(
` Event ${event.id} / Prize ${prize.id} / contract ${contractId}`,
);
console.log(
" L02 records the rest live: assign winner -> approve -> release -> forward.",
);
await db.$disconnect();
}
main().catch(async (err) => {
console.error("\n[FATAL]", err);
await db.$disconnect();
process.exit(1);
});