forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock-data.ts
More file actions
161 lines (151 loc) · 4.67 KB
/
Copy pathmock-data.ts
File metadata and controls
161 lines (151 loc) · 4.67 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
import type { CreateStreamInput, StreamData } from '@/types/stream'
import { NETWORKS } from '@/lib/stellar'
import { SECONDS_PER_DAY } from '@/lib/stream-utils'
/**
* In-memory mock store standing in for on-chain state.
*
* This exists ONLY so the frontend is fully interactive before the Soroban
* contracts are wired up. `lib/contract.ts` reads/writes through this store in
* mock mode. When you connect the real contract, the store is no longer used —
* the hooks read directly from chain data.
*/
const KNOWN_TOKENS = NETWORKS.testnet.knownTokens
const XLM = KNOWN_TOKENS[0]
const USDC = KNOWN_TOKENS[1]
const EURC = KNOWN_TOKENS[2]
const DEMO_ME = 'GBDEMO7KFY3R4VZ6N5LJ7WQH3M2PD8C9SAUVW4EXAMPLE0WALLET0DEM'
export const DEMO_ADDRESS = 'GBQ2X7KFY3R4VZ6N5LJ7WQH3M2PD8C9SAUTV4EXAMPLE0WALLET00ADDR'
const now = Math.floor(Date.now() / 1000)
const HOUR = 3600
const DAY = SECONDS_PER_DAY
function makeStream(partial: Partial<StreamData> & Pick<StreamData, 'id'>): StreamData {
const start = partial.startTime ?? BigInt(now - DAY)
const end = partial.endTime ?? BigInt(now + DAY * 29)
const deposited = partial.depositedAmount ?? 100_000n * 10n ** 7n
const cliffAmount = partial.cliffAmount ?? 0n
const duration = end - start
const linearAmount = partial.linearAmount ?? deposited - cliffAmount
const perSecond = partial.amountPerSecond ?? (duration > 0n ? linearAmount / duration : 0n)
return {
sender: DEMO_ADDRESS,
recipient: 'GD7HQ...RECIPIENT...4FJ2K',
token: XLM,
depositedAmount: deposited,
withdrawnAmount: 0n,
startTime: start,
endTime: end,
cliffTime: partial.cliffTime ?? start,
cliffAmount,
amountPerSecond: perSecond,
linearAmount,
duration,
cancelled: false,
...partial,
}
}
let streams: StreamData[] = [
makeStream({
id: '1',
recipient: 'GD7HQZX4...PAYROLL...4FJ2K',
token: USDC,
depositedAmount: 48_000n * 10n ** 7n,
withdrawnAmount: 6_200n * 10n ** 7n,
startTime: BigInt(now - DAY * 12),
endTime: BigInt(now + DAY * 78),
}),
makeStream({
id: '2',
sender: 'GCEO...COFOUNDER...9XQ2',
recipient: DEMO_ADDRESS,
token: XLM,
depositedAmount: 1_200_000n * 10n ** 7n,
withdrawnAmount: 150_000n * 10n ** 7n,
startTime: BigInt(now - DAY * 90),
endTime: BigInt(now + DAY * 275),
cliffTime: BigInt(now - DAY * 30),
cliffAmount: 100_000n * 10n ** 7n,
}),
makeStream({
id: '3',
recipient: 'GADVISOR...GRANT...K3MZ',
token: EURC,
depositedAmount: 25_000n * 10n ** 7n,
withdrawnAmount: 25_000n * 10n ** 7n,
startTime: BigInt(now - DAY * 120),
endTime: BigInt(now - DAY * 5),
}),
makeStream({
id: '4',
sender: 'GTREASURY...DAO...PL7X',
recipient: DEMO_ADDRESS,
token: USDC,
depositedAmount: 12_000n * 10n ** 7n,
withdrawnAmount: 0n,
startTime: BigInt(now + DAY * 3),
endTime: BigInt(now + DAY * 33),
cliffTime: BigInt(now + DAY * 3),
}),
makeStream({
id: '5',
recipient: 'GCONTRACT...VENDOR...88QW',
token: XLM,
depositedAmount: 5_000n * 10n ** 7n,
withdrawnAmount: 1_000n * 10n ** 7n,
startTime: BigInt(now - HOUR * 6),
endTime: BigInt(now + HOUR * 18),
cancelled: true,
}),
]
type Listener = () => void
const listeners = new Set<Listener>()
function emit() {
listeners.forEach((l) => l())
}
export const mockStore = {
subscribe(listener: Listener) {
listeners.add(listener)
return () => listeners.delete(listener)
},
getAll(): StreamData[] {
return streams
},
getById(id: string): StreamData | undefined {
return streams.find((s) => s.id === id)
},
create(input: CreateStreamInput, sender: string): StreamData {
const id = String(Math.max(0, ...streams.map((s) => Number(s.id))) + 1)
const duration = input.endTime - input.startTime
const linearAmount = input.totalAmount - input.cliffAmount
const amountPerSecond = duration > 0n ? linearAmount / duration : 0n
const stream: StreamData = {
id,
sender,
recipient: input.recipient,
token: input.token,
depositedAmount: input.totalAmount,
withdrawnAmount: 0n,
startTime: input.startTime,
endTime: input.endTime,
cliffTime: input.cliffTime,
cliffAmount: input.cliffAmount,
amountPerSecond,
linearAmount,
duration,
cancelled: false,
}
streams = [stream, ...streams]
emit()
return stream
},
withdraw(id: string, amount: bigint) {
streams = streams.map((s) =>
s.id === id ? { ...s, withdrawnAmount: s.withdrawnAmount + amount } : s,
)
emit()
},
cancel(id: string) {
streams = streams.map((s) => (s.id === id ? { ...s, cancelled: true } : s))
emit()
},
}
export { DEMO_ME }