1
1
'use client' ;
2
-
3
2
//React imports
4
- import React , { useState } from 'react' ;
3
+ import React , { useEffect , useState } from 'react' ;
4
+
5
+ //Axios imports
6
+ import axios from 'axios' ;
5
7
6
8
//Mui imports
7
9
import { Box , Typography } from '@mui/material' ;
8
10
9
11
//Local components
10
12
import useStore from '../store/store' ;
13
+ import { Accounts } from '../store/types' ;
14
+ import { getWalletBalance , signAndSentTx } from '../utils/walletUtils' ;
11
15
import WalletCard from '../components/Card' ;
12
16
import WSTTextField from '../components/WSTTextField' ;
13
17
import CopyTextField from '../components/CopyTextField' ;
14
18
15
19
export 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 ] ) ;
17
27
18
28
const getUserAccountDetails = ( ) => {
19
29
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 ;
23
33
} ;
24
34
} ;
25
35
26
36
// 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
+ } ;
29
84
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
+ }
33
96
} ;
34
97
35
98
const sendContent = < Box >
36
99
< WSTTextField
37
100
placeholder = '0.0000'
38
- value = { mintTokens }
101
+ value = { sendTokenAmount }
39
102
onChange = { ( e ) => setMintTokens ( Number ( e . target . value ) ) }
40
103
label = "Number of Tokens to Send"
41
104
fullWidth = { true }
42
105
/>
43
106
< WSTTextField
44
107
placeholder = "address"
45
- value = { recipientAddress }
46
- onChange = { ( e ) => setRecipientAddress ( e . target . value ) }
108
+ value = { sendRecipientAddress }
109
+ onChange = { ( e ) => setsendRecipientAddress ( e . target . value ) }
47
110
label = "Recipient’s Address"
48
111
fullWidth = { true }
49
112
/>
50
113
</ Box > ;
51
114
52
115
const receiveContent = < Box >
53
116
< CopyTextField
54
- value = { getUserAccountDetails ( ) ?. address }
117
+ value = { getUserAccountDetails ( ) ?. address ?? '' }
55
118
fullWidth = { true }
56
119
label = "Your Address"
57
120
/>
@@ -82,4 +145,4 @@ export default function Profile() {
82
145
</ div >
83
146
</ div >
84
147
) ;
85
- }
148
+ }
0 commit comments