@@ -12,6 +12,7 @@ import type { ReactiveController, ReactiveElement } from 'lit';
12
12
import type { WalletContext } from '../../context' ;
13
13
import { walletContext } from '../../context' ;
14
14
import { MAINNET_EXPLORER_URL , TESTNET_EXPLORER_URL } from '../../constants' ;
15
+ import { validateAddress } from '../../utils' ;
15
16
import { buildEvmFungibleTransactions , executeNextEvmTransaction } from './evm' ;
16
17
17
18
export enum FungibleTransferState {
@@ -20,6 +21,7 @@ export enum FungibleTransferState {
20
21
MISSING_RESOURCE ,
21
22
MISSING_RESOURCE_AMOUNT ,
22
23
MISSING_DESTINATION_ADDRESS ,
24
+ INVALID_DESTINATION_ADDRESS ,
23
25
WALLET_NOT_CONNECTED ,
24
26
WRONG_CHAIN ,
25
27
PENDING_APPROVALS ,
@@ -40,7 +42,7 @@ export class FungibleTokenTransferController implements ReactiveController {
40
42
public destinationNetwork ?: Domain ;
41
43
public selectedResource ?: Resource ;
42
44
public resourceAmount : BigNumber = ethers . constants . Zero ;
43
- public destinationAddress : string = '' ;
45
+ public destinationAddress ? : string | null = '' ;
44
46
45
47
public supportedSourceNetworks : Domain [ ] = [ ] ;
46
48
public supportedDestinationNetworks : Domain [ ] = [ ] ;
@@ -110,6 +112,7 @@ export class FungibleTokenTransferController implements ReactiveController {
110
112
this . sourceNetwork = undefined ;
111
113
}
112
114
this . destinationNetwork = undefined ;
115
+ this . selectedResource = undefined ;
113
116
this . pendingEvmApprovalTransactions = [ ] ;
114
117
this . pendingEvmTransferTransaction = undefined ;
115
118
this . destinationAddress = '' ;
@@ -166,30 +169,31 @@ export class FungibleTokenTransferController implements ReactiveController {
166
169
167
170
onDestinationAddressChange = ( address : string ) : void => {
168
171
this . destinationAddress = address ;
169
- if ( this . destinationAddress . length === 0 ) {
172
+
173
+ if ( this . destinationAddress && this . destinationAddress . length === 0 ) {
170
174
this . pendingEvmApprovalTransactions = [ ] ;
171
175
this . pendingEvmTransferTransaction = undefined ;
176
+ this . destinationAddress = null ;
172
177
}
173
178
void this . buildTransactions ( ) ;
174
179
this . host . requestUpdate ( ) ;
175
180
} ;
176
181
177
182
getTransferState ( ) : FungibleTransferState {
183
+ // Enabled state
178
184
if ( this . transferTransactionId ) {
179
185
return FungibleTransferState . COMPLETED ;
180
186
}
187
+
188
+ // Loading states
181
189
if ( this . waitingUserConfirmation ) {
182
190
return FungibleTransferState . WAITING_USER_CONFIRMATION ;
183
191
}
184
192
if ( this . waitingTxExecution ) {
185
193
return FungibleTransferState . WAITING_TX_EXECUTION ;
186
194
}
187
- if ( this . pendingEvmApprovalTransactions . length > 0 ) {
188
- return FungibleTransferState . PENDING_APPROVALS ;
189
- }
190
- if ( this . pendingEvmTransferTransaction ) {
191
- return FungibleTransferState . PENDING_TRANSFER ;
192
- }
195
+
196
+ // Error States
193
197
if ( ! this . sourceNetwork ) {
194
198
return FungibleTransferState . MISSING_SOURCE_NETWORK ;
195
199
}
@@ -199,12 +203,24 @@ export class FungibleTokenTransferController implements ReactiveController {
199
203
if ( ! this . selectedResource ) {
200
204
return FungibleTransferState . MISSING_RESOURCE ;
201
205
}
202
- if ( this . resourceAmount . eq ( 0 ) ) {
203
- return FungibleTransferState . MISSING_RESOURCE_AMOUNT ;
204
- }
206
+
205
207
if ( this . destinationAddress === '' ) {
206
208
return FungibleTransferState . MISSING_DESTINATION_ADDRESS ;
207
209
}
210
+
211
+ if (
212
+ this . destinationAddress === null ||
213
+ this . destinationAddress === undefined ||
214
+ validateAddress ( this . destinationAddress , this . destinationNetwork . type )
215
+ ) {
216
+ return FungibleTransferState . INVALID_DESTINATION_ADDRESS ;
217
+ }
218
+
219
+ if ( this . resourceAmount . eq ( 0 ) ) {
220
+ return FungibleTransferState . MISSING_RESOURCE_AMOUNT ;
221
+ }
222
+
223
+ // Enabled States
208
224
if (
209
225
! this . walletContext . value ?. evmWallet &&
210
226
! this . walletContext . value ?. substrateWallet
@@ -218,6 +234,14 @@ export class FungibleTokenTransferController implements ReactiveController {
218
234
) {
219
235
return FungibleTransferState . WRONG_CHAIN ;
220
236
}
237
+
238
+ if ( this . pendingEvmApprovalTransactions . length > 0 ) {
239
+ return FungibleTransferState . PENDING_APPROVALS ;
240
+ }
241
+ if ( this . pendingEvmTransferTransaction ) {
242
+ return FungibleTransferState . PENDING_TRANSFER ;
243
+ }
244
+
221
245
return FungibleTransferState . UNKNOWN ;
222
246
}
223
247
0 commit comments