forked from Astrea-Payouts/astrea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe06-vertical-slice.ts
More file actions
364 lines (338 loc) · 11.6 KB
/
Copy pathe06-vertical-slice.ts
File metadata and controls
364 lines (338 loc) · 11.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// E06: the vertical slice demo. Proves E01-E05 work end to end, against the
// real database and real testnet — not another isolated spike of the TW API
// (that was K01's job). Reuses the K01 accounts so no fresh funding is
// needed: organizer funds, judge approves/releases/forwards (ADR-007),
// winner gets paid, and E04's reconciliation checks confirm it.
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 { netAmountAfterFee } from "@/lib/escrow/fees";
import { prepareOperation, submitOperation } from "@/lib/escrow/pipeline";
import {
buildForwardPaymentXdr,
submitForwardPayment,
} from "@/lib/escrow/stellar-payment";
import { trustlessWorkAdapter } from "@/lib/escrow/trustless-work-adapter";
import { findStalledForwardsInDb } from "@/lib/reconciliation/run";
import { isTransactionConfirmed } from "@/lib/reconciliation/transaction-confirmation";
import { transitionEvent, transitionPrize } 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 } });
}
async function main() {
const { organizer, judge, winner, resolver } = accounts;
const prizeAmount = 1;
const runId = Date.now();
step("Set up organizer and winner wallets");
const organizerWallet = await findOrCreateWallet(organizer.publicKey);
const winnerWallet = await findOrCreateWallet(winner.publicKey);
step(
"Judge must already hold a USDC trustline to receive a release (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 E06 vertical slice ${runId}`,
description:
"Automated vertical-slice demo — proves E01-E05 work end to end",
},
});
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: prizeAmount,
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-e06-${runId}`,
title: event.name,
description: event.description ?? "",
roles: {
approver: judge.publicKey,
serviceProvider: winner.publicKey,
platformAddress: organizer.publicKey,
releaseSigner: judge.publicKey,
disputeResolver: resolver.publicKey,
},
platformFee: 0,
milestones: [
{
description: "1st place",
amount: prizeAmount,
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: prizeAmount },
build: () =>
trustlessWorkAdapter.fundEscrow({
contractId,
signerPublicKey: organizer.publicKey,
amount: prizeAmount,
}),
});
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 winner + verify trustline (E05, registration checkpoint)");
if (!(await verifyAndRecordTrustline(winnerWallet.id, winner.publicKey))) {
throw new Error("winner 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");
step("Assign winner + re-verify trustline (E05, assignment checkpoint)");
await db.prize.update({
where: { id: prize.id },
data: { winnerWalletId: winnerWallet.id },
});
await transitionPrize(prize.id, "PENDING", "ASSIGNED");
if (!(await verifyAndRecordTrustline(winnerWallet.id, winner.publicKey))) {
throw new Error("winner trustline no longer valid at assignment");
}
step("Judge approves the milestone");
const approveKey = `approve-milestone:${prize.id}`;
const approvePrepared = await prepareOperation({
idempotencyKey: approveKey,
operation: "approve-milestone",
requestPayload: { contractId, milestoneIndex: prize.milestoneIndex },
build: () =>
trustlessWorkAdapter.approveMilestone({
contractId,
milestoneIndex: prize.milestoneIndex,
approverPublicKey: judge.publicKey,
}),
});
if (approvePrepared.alreadySucceeded)
throw new Error("unexpected: fresh prize already approved");
await submitOperation({
idempotencyKey: approveKey,
signedXdr: signXdr(approvePrepared.unsignedXdr, judge.secret),
submit: (signedXdr) =>
trustlessWorkAdapter.submitSignedTransaction(signedXdr),
});
await transitionPrize(prize.id, "ASSIGNED", "APPROVED");
step(
"Judge releases the milestone — funds land in the judge's own wallet (ADR-007)",
);
const releaseKey = `release-milestone:${prize.id}`;
const releasePrepared = await prepareOperation({
idempotencyKey: releaseKey,
operation: "release-milestone",
requestPayload: { contractId, milestoneIndex: prize.milestoneIndex },
build: () =>
trustlessWorkAdapter.releaseMilestone({
contractId,
milestoneIndex: prize.milestoneIndex,
releaseSignerPublicKey: judge.publicKey,
}),
});
if (releasePrepared.alreadySucceeded)
throw new Error("unexpected: fresh prize already released");
const releaseSubmitted = await submitOperation({
idempotencyKey: releaseKey,
signedXdr: signXdr(releasePrepared.unsignedXdr, judge.secret),
submit: (signedXdr) =>
trustlessWorkAdapter.submitSignedTransaction(signedXdr),
});
console.log(" released, tx:", releaseSubmitted.txHash);
await transitionPrize(prize.id, "APPROVED", "RELEASED", {
releaseTxHash: releaseSubmitted.txHash,
});
step(
"Judge forwards the net amount to the winner (ADR-007, plain Stellar payment)",
);
const netAmount = netAmountAfterFee(prizeAmount);
console.log(
` gross ${prizeAmount} USDC -> net ${netAmount} USDC (ADR-005 0.3% fee)`,
);
const forwardKey = `forward-payment:${prize.id}`;
const forwardPrepared = await prepareOperation({
idempotencyKey: forwardKey,
operation: "forward-payment",
requestPayload: { prizeId: prize.id, amount: netAmount },
build: () =>
buildForwardPaymentXdr({
fromPublicKey: judge.publicKey,
toPublicKey: winner.publicKey,
amount: netAmount,
}),
});
if (forwardPrepared.alreadySucceeded)
throw new Error("unexpected: fresh prize already forwarded");
const forwardSubmitted = await submitOperation({
idempotencyKey: forwardKey,
signedXdr: signXdr(forwardPrepared.unsignedXdr, judge.secret),
submit: submitForwardPayment,
});
console.log(" forwarded, tx:", forwardSubmitted.txHash);
await transitionPrize(prize.id, "RELEASED", "PAID_OUT", {
forwardTxHash: forwardSubmitted.txHash,
});
step("Mark event COMPLETED and record the audit Payout row");
await transitionEvent(event.id, "JUDGING", "COMPLETED");
await db.payout.create({
data: {
prizeId: prize.id,
txHash: forwardSubmitted.txHash,
amountUsdc: netAmount,
},
});
step(
"E04 — confirm both hops directly against Horizon (Principle 2, not TW's own indexer)",
);
const releaseConfirmed = await isTransactionConfirmed(
releaseSubmitted.txHash,
);
const forwardConfirmed = await isTransactionConfirmed(
forwardSubmitted.txHash,
);
console.log(" release tx confirmed on-chain:", releaseConfirmed);
console.log(" forward tx confirmed on-chain:", forwardConfirmed);
if (!(releaseConfirmed && forwardConfirmed)) {
throw new Error("a recorded tx hash did not confirm on Horizon");
}
step(
"E04 — stalled-forward check (should be empty; this prize just paid out)",
);
const stalled = await findStalledForwardsInDb();
const stillStalled = stalled.some((alert) => alert.prizeId === prize.id);
console.log(" this prize flagged as stalled:", stillStalled);
if (stillStalled)
throw new Error(
"prize incorrectly flagged as a stalled forward right after paying out",
);
step(
"E02 — idempotency check: replaying the release submit must short-circuit, not resubmit",
);
const replay = await submitOperation({
idempotencyKey: releaseKey,
signedXdr: "irrelevant-because-already-succeeded",
submit: () => {
throw new Error(
"submit must not be called again for an already-succeeded operation",
);
},
});
console.log(
" replay.alreadySucceeded:",
replay.alreadySucceeded,
"same txHash:",
replay.txHash === releaseSubmitted.txHash,
);
if (!replay.alreadySucceeded || replay.txHash !== releaseSubmitted.txHash) {
throw new Error(
"idempotency guard failed to short-circuit the replayed release",
);
}
console.log(
"\n✅ E06 vertical slice complete — the product guarantee works end to end.",
);
console.log(
` Event ${event.id} / Prize ${prize.id} / contract ${contractId}`,
);
await db.$disconnect();
}
main().catch(async (err) => {
console.error("\n[FATAL]", err);
await db.$disconnect();
process.exit(1);
});