-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathvaultService.ts
More file actions
144 lines (132 loc) · 4.87 KB
/
Copy pathvaultService.ts
File metadata and controls
144 lines (132 loc) · 4.87 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
/**
* vaultService.ts
*
* Promise-based data layer for vault operations.
*
* Currently backed by mock data copied verbatim from VaultDetail.tsx
* (the canonical source). Replace only the internals of each function
* with real Soroban/Horizon calls when the backend is ready — the page
* components depend only on the Promise-based interface and will need
* zero changes.
*/
import type { Vault, VaultTransaction, Milestone } from "../types/vault";
import { MASTER_VAULTS } from "../fixtures/vaults";
import { MASTER_ACTIVITY } from "../fixtures/transactions";
export { MASTER_VAULTS } from "../fixtures/vaults";
// ── Re-export canonical types for consumers that need them ────────────────────
export type { Vault, VaultTransaction };
// ── Rich transaction record for the all-vaults explorer page ─────────────────
// NOT the same as VaultTransaction — this type carries fee, block, from/to/memo
// data that is specific to the VaultTransactions explorer view.
export interface VaultActivityRecord {
id: string;
type: "create" | "validate" | "release" | "redirect";
vault: string;
amount: number;
fee: number;
block: number;
hash: string;
status: "confirmed" | "pending" | "failed";
from: string;
to: string;
timestamp: Date;
memo: string;
}
// ── Mock datasets moved to src/fixtures/ ──────────────────────────────────────
// MASTER_VAULTS lives in src/fixtures/vaults.ts and MASTER_ACTIVITY in
// src/fixtures/transactions.ts. They are imported above so this module stays a
// thin Promise-based seam. See docs/VAULT_DATA_LAYER.md.
// ── Service API ───────────────────────────────────────────────────────────────
// SEAM: Replace only the internals of these functions with Soroban/Horizon calls
// when the real backend lands. Page components depend solely on the
// Promise-based interface below and will need zero changes.
/**
* Return all vaults.
*
* SEAM → replace with: Horizon account fetch + Soroban contract reads.
*/
export async function listVaults(): Promise<Vault[]> {
return Object.values(MASTER_VAULTS);
}
/**
* Return a single vault by id, or undefined if not found.
* Does NOT throw for unknown ids — callers rely on the undefined branch.
*
* SEAM → replace with: Soroban contract state read for a given contract address.
*/
export async function getVault(id: string): Promise<Vault | undefined> {
return MASTER_VAULTS[id];
}
/**
* Return the on-chain transactions stored on a specific vault.
* Returns [] for unknown ids.
*
* SEAM → replace with: Horizon `/transactions?account=<contractAddress>` + filter.
*/
export async function getTransactions(id: string): Promise<VaultTransaction[]> {
return MASTER_VAULTS[id]?.transactions ?? [];
}
/**
* Return the current in-memory activity snapshot synchronously.
*
* This keeps the mock-backed UI populated on first render while the Promise
* seam remains the long-term replacement point for live Horizon/Soroban data.
*/
export function getCachedActivity(): VaultActivityRecord[] {
return [...MASTER_ACTIVITY];
}
/**
* Return the rich activity feed used by the VaultTransactions explorer page.
*
* SEAM → replace with: Horizon transaction stream aggregated across all vault
* contract addresses.
*/
export async function listAllActivity(): Promise<VaultActivityRecord[]> {
return [...MASTER_ACTIVITY];
}
/**
* Create a new vault in mock memory.
*
* SEAM → replace with: Soroban contract initialization invocation.
*/
export async function createVault(vaultData: {
name: string;
amount: number;
currency: string;
deadline: string;
creatorAddress: string;
successAddress: string;
failureAddress: string;
milestones: Omit<Milestone, "id" | "status">[];
}): Promise<Vault> {
const nextId = String(Object.keys(MASTER_VAULTS).length + 1);
const newVault: Vault = {
id: nextId,
name: vaultData.name,
status: "active",
amount: vaultData.amount,
currency: vaultData.currency,
createdAt: new Date().toISOString(),
deadline: vaultData.deadline,
creatorAddress: vaultData.creatorAddress,
successAddress: vaultData.successAddress,
failureAddress: vaultData.failureAddress,
contractAddress: `GCONT${nextId}KQKM4XNQPBEZMXPOLKQKM4XNQPBEZMXPOLKQK`,
milestones: vaultData.milestones.map((m, index) => ({
...m,
id: `m${nextId}_${index + 1}`,
status: "pending"
})),
transactions: [
{
id: `tx${nextId}_1`,
type: "create",
hash: `mockhash${nextId}234567890abcdef123456`,
timestamp: new Date().toISOString(),
amount: vaultData.amount,
}
]
};
MASTER_VAULTS[nextId] = newVault;
return newVault;
}