11'use client' ;
2-
32//React imports
4- import React , { useState } from 'react' ;
3+ import React , { useEffect , useState } from 'react' ;
4+
5+ //Axios imports
6+ import axios from 'axios' ;
57
68//Mui imports
79import { Box , Typography } from '@mui/material' ;
810
911//Local components
1012import useStore from '../store/store' ;
13+ import { Accounts } from '../store/types' ;
14+ import { getWalletBalance , signAndSentTx } from '../utils/walletUtils' ;
1115import WalletCard from '../components/Card' ;
1216import WSTTextField from '../components/WSTTextField' ;
1317import CopyTextField from '../components/CopyTextField' ;
1418
1519export default function Profile ( ) {
16- const { currentUser, userA, userB, walletUser, setAlertStatus } = useStore ( ) ;
20+ const { lucid, currentUser, mintAccount, changeAlertInfo, changeWalletAccountDetails } = useStore ( ) ;
21+ const accounts = useStore ( ( state ) => state . accounts ) ;
22+
23+ useEffect ( ( ) => {
24+ useStore . getState ( ) ;
25+ // console.log("accounts changed:", accounts);
26+ } , [ accounts ] ) ;
1727
1828 const getUserAccountDetails = ( ) => {
1929 switch ( currentUser ) {
20- case "User A" : return userA ;
21- case "User B" : return userB ;
22- case "Connected Wallet" : return walletUser ;
30+ case "User A" : return accounts . userA ;
31+ case "User B" : return accounts . userB ;
32+ case "Connected Wallet" : return accounts . walletUser ;
2333 } ;
2434 } ;
2535
2636 // temp state for each text field
27- const [ mintTokens , setMintTokens ] = useState ( 0 ) ;
28- const [ recipientAddress , setRecipientAddress ] = useState ( 'address' ) ;
37+ const [ sendTokenAmount , setMintTokens ] = useState ( 0 ) ;
38+ const [ sendRecipientAddress , setsendRecipientAddress ] = useState ( 'address' ) ;
39+
40+ const onSend = async ( ) => {
41+ if ( getUserAccountDetails ( ) ?. status === 'Frozen' ) {
42+ changeAlertInfo ( {
43+ severity : 'error' ,
44+ message : 'Cannot send WST with frozen account.' ,
45+ open : true ,
46+ } ) ;
47+ return ;
48+ }
49+ console . log ( 'start sending tokens' ) ;
50+ changeAlertInfo ( { severity : 'info' , message : 'Transaction processing' , open : true , } ) ;
51+ const accountInfo = getUserAccountDetails ( ) ;
52+ if ( ! accountInfo ) {
53+ console . error ( "No valid send account found! Cannot send." ) ;
54+ return ;
55+ }
56+ lucid . selectWallet . fromSeed ( accountInfo . mnemonic ) ;
57+ const requestData = {
58+ asset_name : Buffer . from ( 'WST' , 'utf8' ) . toString ( 'hex' ) , // Convert "WST" to hex
59+ issuer : mintAccount . address ,
60+ quantity : sendTokenAmount ,
61+ recipient : sendRecipientAddress ,
62+ sender : accountInfo . address ,
63+ } ;
64+ try {
65+ const response = await axios . post (
66+ '/api/v1/tx/programmable-token/transfer' ,
67+ requestData ,
68+ {
69+ headers : {
70+ 'Content-Type' : 'application/json;charset=utf-8' ,
71+ } ,
72+ }
73+ ) ;
74+ console . log ( 'Send response:' , response . data ) ;
75+ const tx = await lucid . fromTx ( response . data . cborHex ) ;
76+ await signAndSentTx ( lucid , tx ) ;
77+ await updateAccountBalance ( sendRecipientAddress ) ;
78+ await updateAccountBalance ( accountInfo . address ) ;
79+ changeAlertInfo ( { severity : 'success' , message : 'Transaction sent successfully!' , open : true , } ) ;
80+ } catch ( error ) {
81+ console . error ( 'Send failed:' , error ) ;
82+ }
83+ } ;
2984
30- const onSend = ( ) => {
31- console . log ( 'send tokens' ) ;
32- setAlertStatus ( true ) ;
85+ const updateAccountBalance = async ( address : string ) => {
86+ const newAccountBalance = await getWalletBalance ( address ) ;
87+ const walletKey = ( Object . keys ( accounts ) as ( keyof Accounts ) [ ] ) . find (
88+ ( key ) => accounts [ key ] . address === address
89+ ) ;
90+ if ( walletKey ) {
91+ changeWalletAccountDetails ( walletKey , {
92+ ...accounts [ walletKey ] ,
93+ balance : newAccountBalance ,
94+ } ) ;
95+ }
3396 } ;
3497
3598 const sendContent = < Box >
3699 < WSTTextField
37100 placeholder = '0.0000'
38- value = { mintTokens }
101+ value = { sendTokenAmount }
39102 onChange = { ( e ) => setMintTokens ( Number ( e . target . value ) ) }
40103 label = "Number of Tokens to Send"
41104 fullWidth = { true }
42105 />
43106 < WSTTextField
44107 placeholder = "address"
45- value = { recipientAddress }
46- onChange = { ( e ) => setRecipientAddress ( e . target . value ) }
108+ value = { sendRecipientAddress }
109+ onChange = { ( e ) => setsendRecipientAddress ( e . target . value ) }
47110 label = "Recipient’s Address"
48111 fullWidth = { true }
49112 />
50113 </ Box > ;
51114
52115 const receiveContent = < Box >
53116 < CopyTextField
54- value = { getUserAccountDetails ( ) ?. address }
117+ value = { getUserAccountDetails ( ) ?. address ?? '' }
55118 fullWidth = { true }
56119 label = "Your Address"
57120 />
@@ -82,4 +145,4 @@ export default function Profile() {
82145 </ div >
83146 </ div >
84147 ) ;
85- }
148+ }
0 commit comments