Repository navigation
Expand file tree
/
Copy pathdemo.ps1
More file actions
259 lines (222 loc) · 9.61 KB
/
Copy pathdemo.ps1
File metadata and controls
259 lines (222 loc) · 9.61 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
# Complete Collateral Vault System Demo
# Run this after starting the backend server
param(
[string]$BaseUrl = "http://127.0.0.1:3000",
[string]$User = "9hQjYAiAeBpeCZ2yS1cuZorgcohnCG9qksa1V6pmev7h"
)
Write-Host "`n" -NoNewline
Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host "🎬 COLLATERAL VAULT SYSTEM DEMO" -ForegroundColor Cyan
Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host ""
# Check if server is running
Write-Host "🔍 Checking server status..." -ForegroundColor Yellow
try {
$health = Invoke-RestMethod -Uri "$BaseUrl/health" -Method Get -ErrorAction Stop
Write-Host "✅ Server is running!" -ForegroundColor Green
} catch {
Write-Host "❌ Server is not running!" -ForegroundColor Red
Write-Host "Please start the backend server first:" -ForegroundColor Yellow
Write-Host " .\start-backend.ps1" -ForegroundColor White
exit 1
}
Start-Sleep -Seconds 1
# ============================================
# 1. Health Check
# ============================================
Write-Host "`n" -NoNewline
Write-Host "1️⃣ HEALTH CHECK" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
try {
$health = Invoke-RestMethod -Uri "$BaseUrl/health" -Method Get
Write-Host "Status: $($health.status)" -ForegroundColor Green
} catch {
Write-Host "❌ Health check failed" -ForegroundColor Red
}
# ============================================
# 2. Initialize Vault
# ============================================
Write-Host "`n" -NoNewline
Write-Host "2️⃣ INITIALIZE VAULT" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
Write-Host "User: $User" -ForegroundColor White
Write-Host "Initial Deposit: 1,000,000 lamports (0.001 USDT)" -ForegroundColor White
try {
$body = @{
user_pubkey = $User
initial_deposit = 1000000
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "$BaseUrl/vault/initialize" `
-Method Post `
-Headers @{"Content-Type"="application/json"} `
-Body $body
if ($response.success) {
Write-Host "✅ Vault initialized successfully!" -ForegroundColor Green
Write-Host "Vault ID: $($response.data.vault_id)" -ForegroundColor White
} else {
Write-Host "⚠️ $($response.error)" -ForegroundColor Yellow
}
} catch {
Write-Host "❌ Failed to initialize vault" -ForegroundColor Red
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Start-Sleep -Seconds 1
# ============================================
# 3. Deposit Collateral
# ============================================
Write-Host "`n" -NoNewline
Write-Host "3️⃣ DEPOSIT COLLATERAL" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
Write-Host "Amount: 5,000,000,000 lamports (5,000 USDT)" -ForegroundColor White
try {
$body = @{
user_pubkey = $User
amount = 5000000000
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "$BaseUrl/vault/deposit" `
-Method Post `
-Headers @{"Content-Type"="application/json"} `
-Body $body
if ($response.success) {
Write-Host "✅ Deposit successful!" -ForegroundColor Green
Write-Host "Transaction ID: $($response.data.transaction_id)" -ForegroundColor White
Write-Host "New Balance:" -ForegroundColor White
Write-Host " Total: $($response.data.new_balance.total) lamports" -ForegroundColor Gray
Write-Host " Available: $($response.data.new_balance.available) lamports" -ForegroundColor Gray
} else {
Write-Host "⚠️ $($response.error)" -ForegroundColor Yellow
}
} catch {
Write-Host "❌ Failed to deposit" -ForegroundColor Red
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Start-Sleep -Seconds 1
# ============================================
# 4. Check Balance
# ============================================
Write-Host "`n" -NoNewline
Write-Host "4️⃣ CHECK BALANCE" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
try {
$response = Invoke-RestMethod -Uri "$BaseUrl/vault/balance/$User" -Method Get
if ($response.success) {
Write-Host "✅ Balance retrieved!" -ForegroundColor Green
Write-Host "`nBalance Details:" -ForegroundColor White
Write-Host " User: $($response.data.user)" -ForegroundColor Gray
Write-Host " Total: $($response.data.total) lamports" -ForegroundColor Gray
Write-Host " Locked: $($response.data.locked) lamports" -ForegroundColor Gray
Write-Host " Available: $($response.data.available) lamports" -ForegroundColor Green
Write-Host " Last Updated: $($response.data.last_updated)" -ForegroundColor Gray
}
} catch {
Write-Host "❌ Failed to get balance" -ForegroundColor Red
}
Start-Sleep -Seconds 1
# ============================================
# 5. Lock Collateral (for trading)
# ============================================
Write-Host "`n" -NoNewline
Write-Host "5️⃣ LOCK COLLATERAL (Trading Position)" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
Write-Host "Amount: 2,000,000,000 lamports (2,000 USDT)" -ForegroundColor White
Write-Host "Caller: Position Manager Program" -ForegroundColor White
try {
$body = @{
caller_program = "PositionMgrProgram1234567890123456789012345"
user_pubkey = $User
amount = 2000000000
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "$BaseUrl/internal/lock" `
-Method Post `
-Headers @{"Content-Type"="application/json"} `
-Body $body
if ($response.success) {
Write-Host "✅ Collateral locked successfully!" -ForegroundColor Green
Write-Host "Signature: $($response.signature)" -ForegroundColor Gray
Write-Host "Locked Amount: $($response.locked_amount) lamports" -ForegroundColor White
} else {
Write-Host "⚠️ $($response.message)" -ForegroundColor Yellow
}
} catch {
Write-Host "❌ Failed to lock collateral" -ForegroundColor Red
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Start-Sleep -Seconds 1
# ============================================
# 6. Check Balance After Lock
# ============================================
Write-Host "`n" -NoNewline
Write-Host "6️⃣ BALANCE AFTER LOCK" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
try {
$response = Invoke-RestMethod -Uri "$BaseUrl/vault/balance/$User" -Method Get
if ($response.success) {
Write-Host "✅ Balance retrieved!" -ForegroundColor Green
Write-Host "`nUpdated Balance:" -ForegroundColor White
Write-Host " Total: $($response.data.total) lamports" -ForegroundColor Gray
Write-Host " Locked: $($response.data.locked) lamports" -ForegroundColor Yellow
Write-Host " Available: $($response.data.available) lamports" -ForegroundColor Green
Write-Host "`n💡 Notice: Locked balance increased, available decreased" -ForegroundColor Cyan
}
} catch {
Write-Host "❌ Failed to get balance" -ForegroundColor Red
}
Start-Sleep -Seconds 1
# ============================================
# 7. Transaction History
# ============================================
Write-Host "`n" -NoNewline
Write-Host "7️⃣ TRANSACTION HISTORY" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
try {
$response = Invoke-RestMethod -Uri "$BaseUrl/vault/transactions/$User" -Method Get
if ($response.success) {
Write-Host "✅ Transaction history retrieved!" -ForegroundColor Green
$txs = $response.data
Write-Host "`nTotal Transactions: $($txs.Count)" -ForegroundColor White
if ($txs.Count -gt 0) {
Write-Host "`nRecent Transactions:" -ForegroundColor White
$txs | Select-Object -First 5 | ForEach-Object {
Write-Host " [$($_.transaction_type)] $($_.amount) lamports - $($_.status)" -ForegroundColor Gray
}
} else {
Write-Host "No transactions found" -ForegroundColor Gray
}
}
} catch {
Write-Host "❌ Failed to get transaction history" -ForegroundColor Red
}
Start-Sleep -Seconds 1
# ============================================
# 8. Total Value Locked (TVL)
# ============================================
Write-Host "`n" -NoNewline
Write-Host "8️⃣ TOTAL VALUE LOCKED (TVL)" -ForegroundColor Cyan
Write-Host "-" * 60 -ForegroundColor Gray
try {
$response = Invoke-RestMethod -Uri "$BaseUrl/vault/tvl" -Method Get
if ($response.success) {
Write-Host "✅ TVL retrieved!" -ForegroundColor Green
$tvl = $response.data
Write-Host "`nSystem Statistics:" -ForegroundColor White
Write-Host " Total Value Locked: $($tvl.total_value_locked) lamports" -ForegroundColor Green
Write-Host " Total Users: $($tvl.total_users)" -ForegroundColor White
Write-Host " Total Locked: $($tvl.total_locked) lamports" -ForegroundColor Yellow
Write-Host " Total Available: $($tvl.total_available) lamports" -ForegroundColor Cyan
}
} catch {
Write-Host "❌ Failed to get TVL" -ForegroundColor Red
}
# ============================================
# Demo Complete
# ============================================
Write-Host "`n" -NoNewline
Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host "✅ DEMO COMPLETE!" -ForegroundColor Green
Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host "`nNext Steps:" -ForegroundColor Yellow
Write-Host " • Try unlocking collateral: POST /internal/unlock" -ForegroundColor White
Write-Host " • Try withdrawing: POST /vault/withdraw" -ForegroundColor White
Write-Host " • Connect to WebSocket: ws://127.0.0.1:3000/ws" -ForegroundColor White
Write-Host " • View API docs: API_DOCUMENTATION.md" -ForegroundColor White
Write-Host ""