1
- import { Effect , Either , Option , Schema } from "effect"
1
+ import { Effect , Option } from "effect"
2
2
import { RawTransferSvelte } from "./raw-transfer.svelte.ts"
3
3
import type { QuoteData , Token , WethTokenData } from "$lib/schema/token.ts"
4
4
import { tokensStore } from "$lib/stores/tokens.svelte.ts"
@@ -26,6 +26,7 @@ import {
26
26
hasFailedExit as hasAptosFailedExit ,
27
27
isComplete as isAptosComplete ,
28
28
nextState as aptosNextState ,
29
+ TransferSubmitState as AptosTransferSubmitState ,
29
30
TransferSubmission as AptosTransferSubmission ,
30
31
TransferReceiptState as AptosTransferReceiptState
31
32
} from "$lib/services/transfer-ucs03-aptos"
@@ -34,13 +35,13 @@ import { type Address, fromHex, type Hex } from "viem"
34
35
import { channels } from "$lib/stores/channels.svelte.ts"
35
36
import { getChannelInfoSafe } from "$lib/services/transfer-ucs03-evm/channel.ts"
36
37
import type { Channel } from "$lib/schema/channel.ts"
37
- import { TransferSchema } from "$lib/schema/transfer-args.ts"
38
38
import { getQuoteToken as getQuoteTokenEffect } from "$lib/services/shared"
39
39
import { getWethQuoteToken as getWethQuoteTokenEffect } from "$lib/services/shared"
40
40
import { cosmosStore } from "$lib/wallet/cosmos"
41
41
import { getParsedAmountSafe } from "$lib/services/shared"
42
42
import { getDerivedReceiverSafe } from "$lib/services/shared"
43
43
import { sortedBalancesStore } from "$lib/stores/sorted-balances.svelte.ts"
44
+ import { validateTransfer , type ValidationResult } from "$lib/components/Transfer/validation.ts"
44
45
45
46
export interface TransferState {
46
47
readonly _tag : string
@@ -300,47 +301,55 @@ export class Transfer {
300
301
const ucs03addressValue = Option . getOrNull ( this . ucs03address )
301
302
const wethQuoteTokenValue = Option . getOrNull ( this . wethQuoteToken )
302
303
304
+ const maybeQuoteToken =
305
+ quoteTokenValue &&
306
+ ( quoteTokenValue . type === "UNWRAPPED" || quoteTokenValue . type === "NEW_WRAPPED" )
307
+ ? quoteTokenValue . quote_token
308
+ : undefined
309
+
310
+ const maybeWethQuoteToken =
311
+ wethQuoteTokenValue && "wethQuoteToken" in wethQuoteTokenValue
312
+ ? ( wethQuoteTokenValue as { wethQuoteToken : string } ) . wethQuoteToken
313
+ : undefined
314
+
303
315
return {
304
- sourceChain : sourceChainValue
305
- ? sourceChainValue . rpc_type === "evm"
306
- ? sourceChainValue . toViemChain ( )
307
- : sourceChainValue
308
- : null ,
316
+ sourceChain : sourceChainValue ,
309
317
sourceRpcType : sourceChainValue ?. rpc_type ,
310
318
destinationRpcType : destinationChainValue ?. rpc_type ,
311
319
sourceChannelId : channelValue ?. source_channel_id ,
312
320
ucs03address : ucs03addressValue ,
313
321
baseToken : baseTokenValue ?. denom ,
314
322
baseAmount : parsedAmountValue ,
315
- quoteToken : quoteTokenValue ?. quote_token ,
323
+ quoteToken : maybeQuoteToken ,
316
324
quoteAmount : parsedAmountValue ,
317
325
receiver : derivedReceiverValue ,
318
- timeoutHeight : 0n ,
326
+ timeoutHeight : "0n" ,
319
327
timeoutTimestamp : "0x000000000000000000000000000000000000000000000000fffffffffffffffa" ,
320
- wethQuoteToken : wethQuoteTokenValue ?. wethQuoteToken
328
+ wethQuoteToken : maybeWethQuoteToken
321
329
}
322
330
} )
323
331
324
- transferResult = $derived . by ( ( ) => {
325
- const validationEffect = Schema . decode ( TransferSchema ) ( this . args )
326
- const result = Effect . runSync ( Effect . either ( validationEffect ) )
327
- return Either . isRight ( result )
328
- ? { isValid : true , args : result . right }
329
- : { isValid : false , args : this . args }
330
- } )
332
+ validation = $derived . by < ValidationResult > ( ( ) => validateTransfer ( this . args ) )
331
333
332
- isValid = $derived ( this . transferResult . isValid )
334
+ isValid = $derived ( this . validation . isValid )
333
335
334
336
submit = async ( ) => {
337
+ const validation = this . validation
338
+ if ( ! validation . isValid ) {
339
+ console . warn ( "Validation failed, errors:" , validation . messages )
340
+ return
341
+ }
342
+
343
+ const typedArgs = validation . value
344
+
335
345
if ( Option . isNone ( chains . data ) || Option . isNone ( this . sourceChain ) ) return
336
- console . log ( this . transferResult . args )
346
+ console . info ( "Validated args:" , typedArgs )
337
347
338
348
const sourceChainValue = this . sourceChain . value
339
349
340
350
if ( sourceChainValue . rpc_type === "evm" ) {
341
351
let evmState : EvmTransferSubmission
342
352
if ( this . state . _tag === "EVM" ) {
343
- // If failed, reset the failed step to InProgress
344
353
if ( hasEvmFailedExit ( this . state . state ) ) {
345
354
switch ( this . state . state . _tag ) {
346
355
case "SwitchChain" :
@@ -378,17 +387,12 @@ export class Transfer {
378
387
evmState = EvmTransferSubmission . Filling ( )
379
388
}
380
389
381
- const newState = await evmNextState ( evmState , this . transferResult . args , sourceChainValue )
390
+ const newState = await evmNextState ( evmState , typedArgs , sourceChainValue )
382
391
this . _stateOverride = newState !== null ? TransferState . EVM ( newState ) : TransferState . Empty ( )
383
392
384
- console . info ( "evmState: " , evmState )
385
393
let currentEvmState = newState
386
394
while ( currentEvmState !== null && ! hasEvmFailedExit ( currentEvmState ) ) {
387
- const nextEvmState = await evmNextState (
388
- currentEvmState ,
389
- this . transferResult . args ,
390
- sourceChainValue
391
- )
395
+ const nextEvmState = await evmNextState ( currentEvmState , typedArgs , sourceChainValue )
392
396
this . _stateOverride =
393
397
nextEvmState !== null ? TransferState . EVM ( nextEvmState ) : TransferState . Empty ( )
394
398
@@ -398,7 +402,6 @@ export class Transfer {
398
402
} else if ( sourceChainValue . rpc_type === "cosmos" ) {
399
403
let cosmosState : CosmosTransferSubmission
400
404
if ( this . state . _tag === "Cosmos" ) {
401
- // If failed, reset the failed step to InProgress
402
405
if ( hasCosmosFailedExit ( this . state . state ) ) {
403
406
switch ( this . state . state . _tag ) {
404
407
case "SwitchChain" :
@@ -428,7 +431,7 @@ export class Transfer {
428
431
429
432
const newState = await cosmosNextState (
430
433
cosmosState ,
431
- this . transferResult . args ,
434
+ typedArgs ,
432
435
sourceChainValue ,
433
436
cosmosStore . connectedWallet
434
437
)
@@ -439,7 +442,7 @@ export class Transfer {
439
442
while ( currentCosmosState !== null && ! hasCosmosFailedExit ( currentCosmosState ) ) {
440
443
const nextCosmosState = await cosmosNextState (
441
444
currentCosmosState ,
442
- this . transferResult . args ,
445
+ typedArgs ,
443
446
sourceChainValue ,
444
447
cosmosStore . connectedWallet
445
448
)
@@ -450,12 +453,8 @@ export class Transfer {
450
453
if ( currentCosmosState !== null && isCosmosComplete ( currentCosmosState ) ) break
451
454
}
452
455
} else if ( sourceChainValue . rpc_type === "aptos" ) {
453
- console . info ( "sourceChain is aptos" )
454
- console . info ( "this.state._tag is: " , this . state . _tag )
455
456
let aptosState : AptosTransferSubmission
456
457
if ( this . state . _tag === "Aptos" ) {
457
- console . info ( "state._tag is aptos" )
458
- // If failed, reset the failed step to InProgress
459
458
if ( hasAptosFailedExit ( this . state . state ) ) {
460
459
switch ( this . state . state . _tag ) {
461
460
case "SwitchChain" :
@@ -483,24 +482,18 @@ export class Transfer {
483
482
aptosState = AptosTransferSubmission . Filling ( )
484
483
}
485
484
486
- console . info ( "aptosState: " , aptosState )
487
-
488
- const newState = await aptosNextState ( aptosState , this . transferResult . args , sourceChainValue )
485
+ const newState = await aptosNextState ( aptosState , typedArgs , sourceChainValue )
489
486
this . _stateOverride =
490
487
newState !== null ? TransferState . Aptos ( newState ) : TransferState . Empty ( )
491
488
492
- let currentaptosState = newState
493
- while ( currentaptosState !== null && ! hasAptosFailedExit ( currentaptosState ) ) {
494
- const nextaptosState = await aptosNextState (
495
- currentaptosState ,
496
- this . transferResult . args ,
497
- sourceChainValue
498
- )
489
+ let currentAptosState = newState
490
+ while ( currentAptosState !== null && ! hasAptosFailedExit ( currentAptosState ) ) {
491
+ const nextAptosState = await aptosNextState ( currentAptosState , typedArgs , sourceChainValue )
499
492
this . _stateOverride =
500
- nextaptosState !== null ? TransferState . Aptos ( nextaptosState ) : TransferState . Empty ( )
493
+ nextAptosState !== null ? TransferState . Aptos ( nextAptosState ) : TransferState . Empty ( )
501
494
502
- currentaptosState = nextaptosState
503
- if ( currentaptosState !== null && isAptosComplete ( currentaptosState ) ) break
495
+ currentAptosState = nextAptosState
496
+ if ( currentAptosState !== null && isAptosComplete ( currentAptosState ) ) break
504
497
}
505
498
}
506
499
}
0 commit comments