Skip to content

Commit cd499b6

Browse files
committed
Add DemoEnvironment type
1 parent 0652f29 commit cd499b6

File tree

14 files changed

+250
-93
lines changed

14 files changed

+250
-93
lines changed

frontend/src/app/[username]/index.tsx

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22
//React imports
3-
import React, { useEffect, useState } from 'react';
3+
import React, { useContext, useEffect, useState } from 'react';
44

55
//Axios imports
66
import axios from 'axios';
@@ -15,11 +15,13 @@ import { getWalletBalance, signAndSentTx } from '../utils/walletUtils';
1515
import WalletCard from '../components/Card';
1616
import WSTTextField from '../components/WSTTextField';
1717
import CopyTextField from '../components/CopyTextField';
18+
import DemoEnvironmentContext from '../context/demoEnvironmentContext';
1819

1920
export default function Profile() {
2021
const { lucid, currentUser, mintAccount, changeAlertInfo, changeWalletAccountDetails } = useStore();
2122
const accounts = useStore((state) => state.accounts);
2223
const [overrideTx, setOverrideTx] = useState<boolean>(false);
24+
const demoEnv = useContext(DemoEnvironmentContext);
2325

2426
useEffect(() => {
2527
useStore.getState();
@@ -34,6 +36,16 @@ export default function Profile() {
3436
};
3537
};
3638

39+
const getUserMnemonic = () => {
40+
switch (currentUser) {
41+
case "Alice": return demoEnv.user_a;
42+
case "Bob": return demoEnv.user_b;
43+
case "Mint Authority": return demoEnv.mint_authority;
44+
case "Connected Wallet": return ""; //TODO: this seems to be broken
45+
};
46+
47+
}
48+
3749
// temp state for each text field
3850
const [sendTokenAmount, setMintTokens] = useState(0);
3951
const [sendRecipientAddress, setsendRecipientAddress] = useState('address');
@@ -55,7 +67,7 @@ export default function Profile() {
5567
console.error("No valid send address found! Cannot send.");
5668
return;
5769
}
58-
lucid.selectWallet.fromSeed(accountInfo.mnemonic);
70+
lucid.selectWallet.fromSeed(getUserMnemonic());
5971
const requestData = {
6072
asset_name: Buffer.from('WST', 'utf8').toString('hex'), // Convert "WST" to hex
6173
issuer: mintAccount.address,
@@ -79,14 +91,14 @@ export default function Profile() {
7991
const txId = await signAndSentTx(lucid, tx);
8092
await updateAccountBalance(sendRecipientAddress);
8193
await updateAccountBalance(accountInfo.address);
82-
changeAlertInfo({severity: 'success', message: 'Transaction sent successfully!', open: true, link: `https://preview.cexplorer.io/tx/${txId}`});
94+
changeAlertInfo({severity: 'success', message: 'Transaction sent successfully!', open: true, link: `${demoEnv.explorer_url}/${txId}`});
8395
} catch (error) {
8496
console.error('Send failed:', error);
8597
}
8698
};
8799

88100
const updateAccountBalance = async (address: string) => {
89-
const newAccountBalance = await getWalletBalance(address);
101+
const newAccountBalance = await getWalletBalance(demoEnv, address);
90102
const walletKey = (Object.keys(accounts) as (keyof Accounts)[]).find(
91103
(key) => accounts[key].address === address
92104
);

frontend/src/app/clientLayout.tsx

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22
//React imports
3-
import React, { useEffect } from "react";
3+
import React, { useEffect, useState } from "react";
44

55
//Mui imports
66
import { AppRouterCacheProvider } from '@mui/material-nextjs/v15-appRouter';
@@ -13,24 +13,42 @@ import { makeLucid, getWalletFromSeed } from "./utils/walletUtils";
1313
import useStore from './store/store';
1414
import WSTAppBar from "./components/WSTAppBar";
1515
import AlertBar from './components/AlertBar';
16+
import { DemoEnvironment, previewEnv } from "./store/types";
17+
import axios from "axios";
18+
import DemoEnvironmentContext from "./context/demoEnvironmentContext";
19+
20+
async function loadDemoEnvironment(): Promise<DemoEnvironment> {
21+
const response = await axios.get<DemoEnvironment>("/api/v1/demo-environment",
22+
{
23+
headers: {
24+
'Content-Type': 'application/json;charset=utf-8',
25+
},
26+
});
27+
return response?.data;
28+
}
1629

1730
export default function ClientLayout({ children }: { children: React.ReactNode }) {
1831
const { mintAccount, accounts, changeMintAccountDetails, changeWalletAccountDetails, setLucidInstance } = useStore();
32+
const [demoEnv, setDemoEnv] = useState<DemoEnvironment>(previewEnv);
1933

2034
useEffect(() => {
2135
const fetchUserWallets = async () => {
2236
try {
37+
const demoEnv = await loadDemoEnvironment();
38+
console.log("DemoEnvironment:", demoEnv);
39+
setDemoEnv(demoEnv);
40+
2341
// retrieve wallet info
24-
const mintAuthorityWallet = await getWalletFromSeed(mintAccount.mnemonic);
25-
const walletA = await getWalletFromSeed(accounts.alice.mnemonic);
26-
const walletB = await getWalletFromSeed(accounts.bob.mnemonic);
42+
const mintAuthorityWallet = await getWalletFromSeed(demoEnv.mint_authority);
43+
const walletA = await getWalletFromSeed(demoEnv.user_a);
44+
const walletB = await getWalletFromSeed(demoEnv.user_b);
2745

2846
// Update Zustand store with the initialized wallet information
2947
changeMintAccountDetails({ ...mintAccount, address: mintAuthorityWallet.address});
3048
changeWalletAccountDetails('alice', { ...accounts.alice, address: walletA.address},);
3149
changeWalletAccountDetails('bob', { ...accounts.bob, address: walletB.address});
3250

33-
const initialLucid = await makeLucid();
51+
const initialLucid = await makeLucid(demoEnv);
3452
setLucidInstance(initialLucid);
3553
console.log('Wallets initialized');
3654
} catch (error) {
@@ -50,12 +68,14 @@ export default function ClientLayout({ children }: { children: React.ReactNode }
5068
return (
5169
<AppRouterCacheProvider>
5270
<ThemeModeProvider>
71+
<DemoEnvironmentContext.Provider value={demoEnv}>
5372
<main>
5473
<WSTAppBar />
5574
<NavDrawer />
5675
<div className="contentSection">{children}</div>
5776
<AlertBar/>
5877
</main>
78+
</DemoEnvironmentContext.Provider>
5979
</ThemeModeProvider>
6080
</AppRouterCacheProvider>
6181
);

frontend/src/app/components/ProfileSwitcher.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';
1616
import useStore from '../store/store';
1717
import { UserName } from '../store/types';
1818
import { selectLucidWallet, getWalletBalance } from '../utils/walletUtils';
19+
import DemoEnvironmentContext from '../context/demoEnvironmentContext';
1920

2021
export default function ProfileSwitcher() {
2122
const [anchorEl, setAnchorEl] = React.useState<HTMLElement | null>(null);
2223
const { currentUser, accounts, changeWalletAccountDetails } = useStore();
2324
const lucid = useStore(state => state.lucid);
2425
const changeUserAccount = useStore(state => state.changeUserAccount);
2526
const router = useRouter();
27+
const demoContext = React.useContext(DemoEnvironmentContext);
2628

2729
React.useEffect(() => {
2830
// Check the current path and redirect if the currentUser doesn't match
@@ -62,7 +64,7 @@ export default function ProfileSwitcher() {
6264
const handleWalletConnect = async (user: UserName) => {
6365
await selectLucidWallet(lucid, "Lace");
6466
const userAddress = await lucid.wallet().address();
65-
const userBalance = await getWalletBalance(userAddress);
67+
const userBalance = await getWalletBalance(demoContext, userAddress);
6668
changeWalletAccountDetails('walletUser', {
6769
...accounts.walletUser,
6870
address: userAddress,

frontend/src/app/components/WSTTable.tsx

+9-7
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ import ContentCopyIcon from '@mui/icons-material/ContentCopy';
1616

1717
//Local Imports
1818
import useStore from '../store/store';
19-
import { useEffect } from "react";
19+
import { useContext, useEffect } from "react";
2020
import IconButton from './WSTIconButton';
21+
import DemoEnvironmentContext from "../context/demoEnvironmentContext";
2122

22-
const progLogicBase : LucidCredential = {
23-
type: "Script",
24-
hash: "fca77bcce1e5e73c97a0bfa8c90f7cd2faff6fd6ed5b6fec1c04eefa"
25-
}
26-
27-
const stableCoin : Unit = toUnit("b34a184f1f2871aa4d33544caecefef5242025f45c3fa5213d7662a9", "575354")
2823

2924

3025
export default function WSTTable() {
3126
const { lucid, accounts } = useStore();
3227
const accountArray = Object.values(accounts);
28+
const demoEnv = useContext(DemoEnvironmentContext);
29+
const stableCoin : Unit = toUnit(demoEnv.minting_policy, demoEnv.token_name);
30+
31+
const progLogicBase : LucidCredential = {
32+
type: "Script",
33+
hash: demoEnv.prog_logic_base_hash
34+
}
3335

3436
const getAccounts = async () => {
3537
const progUTxOs : UTxO[] = await lucid.utxosAtWithUnit(progLogicBase, stableCoin);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { createContext } from "react";
2+
import { DemoEnvironment, previewEnv } from "../store/types";
3+
4+
const DemoEnvironmentContext = createContext<DemoEnvironment>(previewEnv);
5+
6+
export default DemoEnvironmentContext;

frontend/src/app/mint-authority/page.tsx

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22
//React imports
3-
import React, { useEffect, useState } from 'react';
3+
import React, { useContext, useEffect, useState } from 'react';
44

55
//Axios imports
66
import axios from 'axios';
@@ -22,6 +22,7 @@ import WSTTextField from '../components/WSTTextField';
2222
import CopyTextField from '../components/CopyTextField';
2323
import WSTTable from '../components/WSTTable';
2424
import { getWalletBalance, signAndSentTx, getBlacklist } from '../utils/walletUtils';
25+
import DemoEnvironmentContext from '../context/demoEnvironmentContext';
2526

2627

2728

@@ -38,19 +39,21 @@ export default function Home() {
3839
const [freezeReason, setFreezeReason] = useState('Enter reason here');
3940
const [seizeAccountNumber, setSeizeAccountNumber] = useState('address to seize');
4041
const [seizeReason, setSeizeReason] = useState('Enter reason here');
42+
43+
const demoEnv = useContext(DemoEnvironmentContext);
4144

4245
useEffect(() => {
4346
const initialize = async () => {
4447
await fetchUserDetails();
4548
await fetchBlacklistStatus();
4649
};
4750
initialize();
48-
}, []);
51+
}, [demoEnv]);
4952

5053
const fetchUserDetails = async () => {
51-
const mintBalance = await getWalletBalance(mintAccount.address);
52-
const userABalance = await getWalletBalance(accounts.alice.address);
53-
const userBBalance = await getWalletBalance(accounts.bob.address);
54+
const mintBalance = await getWalletBalance(demoEnv, mintAccount.address);
55+
const userABalance = await getWalletBalance(demoEnv, accounts.alice.address);
56+
const userBBalance = await getWalletBalance(demoEnv, accounts.bob.address);
5457

5558
// Update Zustand store with the initialized wallet information
5659
await changeMintAccountDetails({ ...mintAccount, balance: mintBalance});
@@ -59,7 +62,7 @@ export default function Home() {
5962
};
6063

6164
const fetchBlacklistStatus = async () => {
62-
const blacklist = await getBlacklist();
65+
const blacklist = await getBlacklist(demoEnv);
6366
const { accounts, changeWalletAccountDetails } = useStore.getState();
6467

6568
Object.entries(accounts).map(async ([key, account]) => {
@@ -86,7 +89,7 @@ export default function Home() {
8689
return;
8790
}
8891
changeAlertInfo({severity: 'info', message: 'Processing Mint Request', open: true, link: ''});
89-
lucid.selectWallet.fromSeed(mintAccount.mnemonic);
92+
lucid.selectWallet.fromSeed(demoEnv.mint_authority);
9093
const requestData = {
9194
asset_name: Buffer.from('WST', 'utf8').toString('hex'), // Convert "WST" to hex
9295
issuer: mintAccount.address,
@@ -108,7 +111,7 @@ export default function Home() {
108111
const tx = await lucid.fromTx(response.data.cborHex);
109112
const txId = await signAndSentTx(lucid, tx);
110113

111-
changeAlertInfo({severity: 'success', message: 'Successful new WST mint. View the transaction here:', open: true, link: `https://preview.cexplorer.io/tx/${txId}`});
114+
changeAlertInfo({severity: 'success', message: 'Successful new WST mint. View the transaction here:', open: true, link: `${demoEnv.explorer_url}/${txId}`});
112115

113116
await fetchUserDetails();
114117
} catch (error) {
@@ -117,7 +120,7 @@ export default function Home() {
117120
};
118121

119122
const onSend = async () => {
120-
lucid.selectWallet.fromSeed(mintAccount.mnemonic);
123+
lucid.selectWallet.fromSeed(demoEnv.mint_authority);
121124
console.log('send tokens');
122125
changeAlertInfo({severity: 'info', message: 'Transaction processing', open: true, link: ''});
123126
const requestData = {
@@ -140,7 +143,7 @@ export default function Home() {
140143
console.log('Send response:', response.data);
141144
const tx = await lucid.fromTx(response.data.cborHex);
142145
const txId = await signAndSentTx(lucid, tx);
143-
const newAccountBalance = await getWalletBalance(sendRecipientAddress);
146+
const newAccountBalance = await getWalletBalance(demoEnv, sendRecipientAddress);
144147
const recipientWalletKey = (Object.keys(accounts) as (keyof Accounts)[]).find(
145148
(key) => accounts[key].address === sendRecipientAddress
146149
);
@@ -150,7 +153,7 @@ export default function Home() {
150153
balance: newAccountBalance,
151154
});
152155
}
153-
changeAlertInfo({severity: 'success', message: 'Transaction sent successfully!', open: true, link: `https://preview.cexplorer.io/tx/${txId}`});
156+
changeAlertInfo({severity: 'success', message: 'Transaction sent successfully!', open: true, link: `${demoEnv.explorer_url}/${txId}`});
154157
await fetchUserDetails();
155158
} catch (error) {
156159
console.error('Send failed:', error);
@@ -159,7 +162,7 @@ export default function Home() {
159162

160163
const onFreeze = async () => {
161164
console.log('freeze an address');
162-
lucid.selectWallet.fromSeed(mintAccount.mnemonic);
165+
lucid.selectWallet.fromSeed(demoEnv.mint_authority);
163166
changeAlertInfo({severity: 'info', message: 'Freeze request processing', open: true, link: ''});
164167
const requestData = {
165168
issuer: mintAccount.address,
@@ -180,7 +183,7 @@ export default function Home() {
180183
const tx = await lucid.fromTx(response.data.cborHex);
181184
const txId = await signAndSentTx(lucid, tx);
182185
console.log(txId);
183-
changeAlertInfo({severity: 'success', message: 'Address successfully frozen', open: true, link: `https://preview.cexplorer.io/tx/${txId}`});
186+
changeAlertInfo({severity: 'success', message: 'Address successfully frozen', open: true, link: `${demoEnv.explorer_url}/${txId}`});
184187
const frozenWalletKey = (Object.keys(accounts) as (keyof Accounts)[]).find(
185188
(key) => accounts[key].address === freezeAccountNumber
186189
);
@@ -206,7 +209,7 @@ export default function Home() {
206209

207210
const onUnfreeze = async () => {
208211
console.log('unfreeze an account');
209-
lucid.selectWallet.fromSeed(mintAccount.mnemonic);
212+
lucid.selectWallet.fromSeed(demoEnv.mint_authority);
210213
changeAlertInfo({severity: 'info', message: 'Unfreeze request processing', open: true, link: ''});
211214
const requestData = {
212215
issuer: mintAccount.address,
@@ -226,7 +229,7 @@ export default function Home() {
226229
console.log('Unfreeze response:', response.data);
227230
const tx = await lucid.fromTx(response.data.cborHex);
228231
const txId = await signAndSentTx(lucid, tx);
229-
changeAlertInfo({severity: 'success', message: 'Address successfully unfrozen', open: true, link: `https://preview.cexplorer.io/tx/${txId}`});
232+
changeAlertInfo({severity: 'success', message: 'Address successfully unfrozen', open: true, link: `${demoEnv.explorer_url}/${txId}`});
230233
const unfrozenWalletKey = (Object.keys(accounts) as (keyof Accounts)[]).find(
231234
(key) => accounts[key].address === freezeAccountNumber
232235
);
@@ -252,7 +255,7 @@ export default function Home() {
252255

253256
const onSeize = async () => {
254257
console.log('seize account funds');
255-
lucid.selectWallet.fromSeed(mintAccount.mnemonic);
258+
lucid.selectWallet.fromSeed(demoEnv.mint_authority);
256259
changeAlertInfo({severity: 'info', message: 'WST seizure processing', open: true, link: ''});
257260
const requestData = {
258261
issuer: mintAccount.address,
@@ -272,7 +275,7 @@ export default function Home() {
272275
console.log('Seize response:', response.data);
273276
const tx = await lucid.fromTx(response.data.cborHex);
274277
const txId = await signAndSentTx(lucid, tx);
275-
const newAccountBalance = await getWalletBalance(seizeAccountNumber);
278+
const newAccountBalance = await getWalletBalance(demoEnv, seizeAccountNumber);
276279
const seizeWalletKey = (Object.keys(accounts) as (keyof Accounts)[]).find(
277280
(key) => accounts[key].address === seizeAccountNumber
278281
);
@@ -282,7 +285,7 @@ export default function Home() {
282285
balance: newAccountBalance,
283286
});
284287
}
285-
changeAlertInfo({severity: 'success', message: 'Funds successfully seized', open: true, link: `https://preview.cexplorer.io/tx/${txId}`});
288+
changeAlertInfo({severity: 'success', message: 'Funds successfully seized', open: true, link: `${demoEnv.explorer_url}/${txId}`});
286289
await fetchUserDetails();
287290
} catch (error) {
288291
console.error('Seize failed:', error);

frontend/src/app/store/store.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,21 @@ const useStore = create<State & Actions>((set) => ({
2828
mintAccount: {
2929
name: 'Mint Authority',
3030
address: 'addr_test1qq986m3uel86pl674mkzneqtycyg7csrdgdxj6uf7v7kd857kquweuh5kmrj28zs8czrwkl692jm67vna2rf7xtafhpqk3hecm',
31-
mnemonic: 'problem alert infant glance toss gospel tonight sheriff match else hover upset chicken desert anxiety cliff moment song large seed purpose chalk loan onion',
3231
balance: {ada: 0, wst: 0},
3332
},
3433
accounts: {
3534
alice: {
3635
address: '',
37-
mnemonic: 'during dolphin crop lend pizza guilt hen earn easy direct inhale deputy detect season army inject exhaust apple hard front bubble emotion short portion',
3836
balance: {ada: 0, wst: 0},
3937
status: 'Active',
4038
},
4139
bob: {
4240
address: '',
43-
mnemonic: 'silver legal flame powder fence kiss stable margin refuse hold unknown valid wolf kangaroo zero able waste jewel find salad sadness exhibit hello tape',
4441
balance: {ada: 0, wst: 0},
4542
status: 'Active',
4643
},
4744
walletUser: {
4845
address: '',
49-
mnemonic: '',
5046
balance: {ada: 0, wst: 0},
5147
status: 'Active',
5248
},

0 commit comments

Comments
 (0)