-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003_blockchain.html
345 lines (319 loc) · 15.5 KB
/
003_blockchain.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>USDX Payments API Demo</title>
<style>
body {
background-color: black;
color: lime;
font-family: monospace;
font-size: 14px;
margin: 20px;
}
#output {
white-space: pre-wrap;
height: 400px;
overflow-y: scroll;
border: 1px solid lime;
padding: 10px;
background-color: #0a0a0a;
}
#command {
background-color: black;
color: lime;
border: none;
outline: none;
width: 100%;
font-family: monospace;
font-size: 14px;
margin-top: 10px;
}
#command:focus {
border-bottom: 1px dashed lime;
}
</style>
</head>
<body>
<div>
<div id="output"></div>
<input type="text" id="command" placeholder="Enter command (type 'help' for commands)...">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<script>
// Blockchain Class
class Block {
constructor(index, timestamp, transactions, previousHash = '0') {
this.index = index;
this.timestamp = timestamp;
this.transactions = transactions;
this.previousHash = previousHash;
this.hash = this.calculateHash();
}
calculateHash() {
return CryptoJS.SHA256(
this.index + this.previousHash + this.timestamp + JSON.stringify(this.transactions)
).toString();
}
}
class Blockchain {
constructor() {
this.chain = [this.createGenesisBlock()];
this.pendingTransactions = [];
this.lastYieldTime = new Date().getTime() / 1000; // in seconds
this.totalCollateral = 1000; // Initial collateral
this.totalShares = 1000; // Initial shares
this.rewardMultiplier = 1; // Initial reward multiplier
this.annualYieldRate = 0.05; // 5% annual yield
this.users = {
'user1': { collateral: 0, shares: 1000, wusdx: 0 },
'user2': { collateral: 1000, shares: 0, wusdx: 0 }
};
this.vault = { shares: 0, totalWUSDX: 0 };
}
createGenesisBlock() {
return new Block(0, new Date().toISOString(), [], "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
mineBlock() {
const currentTime = new Date().getTime() / 1000;
const timeElapsed = currentTime - this.lastYieldTime;
this.lastYieldTime = currentTime;
// Calculate and apply yield to totalCollateral
const yieldForPeriod = this.totalCollateral * this.annualYieldRate * (timeElapsed / 31536000);
this.totalCollateral += yieldForPeriod;
// Update rewardMultiplier
if (this.totalShares > 0) {
this.rewardMultiplier = this.totalCollateral / this.totalShares;
}
// Mine block
const latestBlock = this.getLatestBlock();
const index = latestBlock.index + 1;
const timestamp = new Date().toISOString();
const transactions = [...this.pendingTransactions];
this.pendingTransactions = [];
const block = new Block(index, timestamp, transactions, latestBlock.hash);
this.chain.push(block);
// Process transactions
processTransactions(transactions);
// Log mining
log.push(`[${new Date().toISOString()}] Mined block #${block.index} with ${block.transactions.length} transactions`);
}
}
// State and Constants
const blockchain = new Blockchain();
// Transaction Processing Functions
function processTransactions(transactions) {
transactions.forEach(tx => {
switch (tx.type) {
case 'deposit':
deposit(tx.user, tx.amount);
break;
case 'redeem':
redeem(tx.user, tx.amount);
break;
case 'send':
send(tx.from, tx.to, tx.amount);
break;
case 'deposit_vault':
depositVault(tx.user, tx.amount);
break;
case 'redeem_vault':
redeemVault(tx.user, tx.amount);
break;
}
});
}
function deposit(user, amount) {
if (blockchain.users[user] && blockchain.users[user].collateral >= amount) {
blockchain.users[user].collateral -= amount;
blockchain.totalCollateral += amount;
const sharesToAdd = amount / blockchain.rewardMultiplier;
blockchain.users[user].shares += sharesToAdd;
blockchain.totalShares += sharesToAdd;
log.push(`[${new Date().toISOString()}] Deposited ${amount} collateral for ${user}, minted ${amount} USDX`);
} else {
log.push(`[${new Date().toISOString()}] Failed to deposit for ${user}: insufficient collateral`);
}
}
function redeem(user, amount) {
const sharesToBurn = amount / blockchain.rewardMultiplier;
if (blockchain.users[user] && blockchain.users[user].shares >= sharesToBurn && blockchain.totalCollateral >= amount) {
blockchain.users[user].shares -= sharesToBurn;
blockchain.totalShares -= sharesToBurn;
blockchain.totalCollateral -= amount;
blockchain.users[user].collateral += amount;
log.push(`[${new Date().toISOString()}] Redeemed ${amount} USDX for ${user}, received ${amount} collateral`);
} else {
log.push(`[${new Date().toISOString()}] Failed to redeem for ${user}: insufficient shares or collateral`);
}
}
function send(from, to, amount) {
const sharesToTransfer = amount / blockchain.rewardMultiplier;
if (blockchain.users[from] && blockchain.users[to] && blockchain.users[from].shares >= sharesToTransfer) {
blockchain.users[from].shares -= sharesToTransfer;
blockchain.users[to].shares += sharesToTransfer;
log.push(`[${new Date().toISOString()}] Sent ${amount} USDX from ${from} to ${to}`);
} else {
log.push(`[${new Date().toISOString()}] Failed to send from ${from} to ${to}: insufficient shares`);
}
}
function depositVault(user, amount) {
const sharesToTransfer = amount / blockchain.rewardMultiplier;
if (blockchain.users[user] && blockchain.users[user].shares >= sharesToTransfer) {
blockchain.users[user].shares -= sharesToTransfer;
blockchain.vault.shares += sharesToTransfer;
let wUSDXToMint;
if (blockchain.vault.totalWUSDX == 0) {
wUSDXToMint = amount;
} else {
const totalAssets = blockchain.vault.shares * blockchain.rewardMultiplier;
wUSDXToMint = (amount * blockchain.vault.totalWUSDX) / totalAssets;
}
blockchain.users[user].wusdx += wUSDXToMint;
blockchain.vault.totalWUSDX += wUSDXToMint;
log.push(`[${new Date().toISOString()}] Deposited ${amount} USDX into vault for ${user}, received ${wUSDXToMint.toFixed(6)} wUSDX`);
} else {
log.push(`[${new Date().toISOString()}] Failed to deposit into vault for ${user}: insufficient shares`);
}
}
function redeemVault(user, amount) {
if (blockchain.users[user] && blockchain.users[user].wusdx >= amount && blockchain.vault.totalWUSDX >= amount) {
const totalAssets = blockchain.vault.shares * blockchain.rewardMultiplier;
const USDXToWithdraw = (amount * totalAssets) / blockchain.vault.totalWUSDX;
const sharesToTransfer = USDXToWithdraw / blockchain.rewardMultiplier;
if (blockchain.vault.shares >= sharesToTransfer) {
blockchain.vault.shares -= sharesToTransfer;
blockchain.users[user].shares += sharesToTransfer;
blockchain.users[user].wusdx -= amount;
blockchain.vault.totalWUSDX -= amount;
log.push(`[${new Date().toISOString()}] Redeemed ${amount} wUSDX for ${user}, received ${USDXToWithdraw.toFixed(6)} USDX`);
} else {
log.push(`[${new Date().toISOString()}] Failed to redeem from vault for ${user}: insufficient vault shares`);
}
} else {
log.push(`[${new Date().toISOString()}] Failed to redeem from vault for ${user}: insufficient wUSDX`);
}
}
// UI Functions
const log = [];
function updateOutput() {
const output = document.getElementById('output');
let text = '';
// Display log
const displayLog = log.slice(-20).join('\n');
text += displayLog + '\n\n';
// Display current state
text += `Current Time: ${new Date().toISOString()}\n`;
text += `Total Collateral: ${blockchain.totalCollateral.toFixed(2)}\n`;
text += `Total Shares: ${blockchain.totalShares.toFixed(6)}\n`;
text += `Reward Multiplier: ${blockchain.rewardMultiplier.toFixed(6)}\n`;
text += `Total Supply: ${(blockchain.totalShares * blockchain.rewardMultiplier).toFixed(6)}\n\n`;
text += 'Users:\n';
for (const user in blockchain.users) {
const usdxBalance = blockchain.users[user].shares * blockchain.rewardMultiplier;
text += `${user}:\n`;
text += ` Collateral: ${blockchain.users[user].collateral.toFixed(2)}\n`;
text += ` USDX Shares: ${blockchain.users[user].shares.toFixed(6)}\n`;
text += ` USDX Balance: ${usdxBalance.toFixed(6)}\n`;
text += ` wUSDX Balance: ${blockchain.users[user].wusdx.toFixed(6)}\n`;
}
text += '\nVault:\n';
const vaultUSDX = blockchain.vault.shares * blockchain.rewardMultiplier;
text += ` USDX Shares: ${blockchain.vault.shares.toFixed(6)}\n`;
text += ` USDX Balance: ${vaultUSDX.toFixed(6)}\n`;
text += ` Total wUSDX: ${blockchain.vault.totalWUSDX.toFixed(6)}\n`;
text += '\nBlockchain (Latest 5 Blocks):\n';
const startIdx = Math.max(0, blockchain.chain.length - 5);
blockchain.chain.slice(startIdx).forEach(block => {
text += `Block ${block.index}: ${block.timestamp}\n`;
block.transactions.forEach(tx => {
text += ` ${JSON.stringify(tx)}\n`;
});
});
output.textContent = text;
output.scrollTop = output.scrollHeight; // Scroll to bottom
}
function parseCommand(command) {
const parts = command.split(' ');
const action = parts[0].toLowerCase();
switch (action) {
case 'deposit':
if (parts.length === 3) {
const user = parts[1];
const amount = parseFloat(parts[2]);
if (isNaN(amount)) return;
blockchain.pendingTransactions.push({ type: 'deposit', user, amount });
log.push(`[${new Date().toISOString()}] Requested to deposit ${amount} collateral for ${user}`);
}
break;
case 'redeem':
if (parts.length === 3) {
const user = parts[1];
const amount = parseFloat(parts[2]);
if (isNaN(amount)) return;
blockchain.pendingTransactions.push({ type: 'redeem', user, amount });
log.push(`[${new Date().toISOString()}] Requested to redeem ${amount} USDX for ${user}`);
}
break;
case 'send':
if (parts.length === 5 && parts[2].toLowerCase() === 'to') {
const from = parts[1];
const to = parts[3];
const amount = parseFloat(parts[4]);
if (isNaN(amount)) return;
blockchain.pendingTransactions.push({ type: 'send', from, to, amount });
log.push(`[${new Date().toISOString()}] Requested to send ${amount} USDX from ${from} to ${to}`);
}
break;
case 'deposit_vault':
if (parts.length === 3) {
const user = parts[1];
const amount = parseFloat(parts[2]);
if (isNaN(amount)) return;
blockchain.pendingTransactions.push({ type: 'deposit_vault', user, amount });
log.push(`[${new Date().toISOString()}] Requested to deposit ${amount} USDX into vault for ${user}`);
}
break;
case 'redeem_vault':
if (parts.length === 3) {
const user = parts[1];
const amount = parseFloat(parts[2]);
if (isNaN(amount)) return;
blockchain.pendingTransactions.push({ type: 'redeem_vault', user, amount });
log.push(`[${new Date().toISOString()}] Requested to redeem ${amount} wUSDX from vault for ${user}`);
}
break;
case 'help':
const helpText = [
'Commands:',
' deposit <user> <amount> - Deposit collateral to mint USDX',
' redeem <user> <amount> - Burn USDX to redeem collateral',
' send <from> to <to> <amount> - Send USDX between users',
' deposit_vault <user> <amount> - Deposit USDX into vault to receive wUSDX',
' redeem_vault <user> <amount> - Redeem wUSDX from vault to receive USDX'
].join('\n');
log.push(`[${new Date().toISOString()}] Help:\n${helpText}`);
break;
}
}
// Event Listeners and Intervals
document.getElementById('command').addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
const command = event.target.value.trim();
event.target.value = '';
parseCommand(command);
}
});
setInterval(() => {
blockchain.mineBlock();
updateOutput();
}, 10000); // Mine blocks every 10 seconds
setInterval(updateOutput, 1000); // Update display every second
// Initial update
updateOutput();
</script>
</body>
</html>