1- import { Effect , Either , Option , Schema } from "effect"
1+ import { Effect , Option } from "effect"
22import { RawTransferSvelte } from "./raw-transfer.svelte.ts"
33import type { QuoteData , Token , WethTokenData } from "$lib/schema/token.ts"
44import { tokensStore } from "$lib/stores/tokens.svelte.ts"
@@ -26,6 +26,7 @@ import {
2626 hasFailedExit as hasAptosFailedExit ,
2727 isComplete as isAptosComplete ,
2828 nextState as aptosNextState ,
29+ TransferSubmitState as AptosTransferSubmitState ,
2930 TransferSubmission as AptosTransferSubmission ,
3031 TransferReceiptState as AptosTransferReceiptState
3132} from "$lib/services/transfer-ucs03-aptos"
@@ -34,13 +35,13 @@ import { type Address, fromHex, type Hex } from "viem"
3435import { channels } from "$lib/stores/channels.svelte.ts"
3536import { getChannelInfoSafe } from "$lib/services/transfer-ucs03-evm/channel.ts"
3637import type { Channel } from "$lib/schema/channel.ts"
37- import { TransferSchema } from "$lib/schema/transfer-args.ts"
3838import { getQuoteToken as getQuoteTokenEffect } from "$lib/services/shared"
3939import { getWethQuoteToken as getWethQuoteTokenEffect } from "$lib/services/shared"
4040import { cosmosStore } from "$lib/wallet/cosmos"
4141import { getParsedAmountSafe } from "$lib/services/shared"
4242import { getDerivedReceiverSafe } from "$lib/services/shared"
4343import { sortedBalancesStore } from "$lib/stores/sorted-balances.svelte.ts"
44+ import { validateTransfer , type ValidationResult } from "$lib/components/Transfer/validation.ts"
4445
4546export interface TransferState {
4647 readonly _tag : string
@@ -300,47 +301,55 @@ export class Transfer {
300301 const ucs03addressValue = Option . getOrNull ( this . ucs03address )
301302 const wethQuoteTokenValue = Option . getOrNull ( this . wethQuoteToken )
302303
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+
303315 return {
304- sourceChain : sourceChainValue
305- ? sourceChainValue . rpc_type === "evm"
306- ? sourceChainValue . toViemChain ( )
307- : sourceChainValue
308- : null ,
316+ sourceChain : sourceChainValue ,
309317 sourceRpcType : sourceChainValue ?. rpc_type ,
310318 destinationRpcType : destinationChainValue ?. rpc_type ,
311319 sourceChannelId : channelValue ?. source_channel_id ,
312320 ucs03address : ucs03addressValue ,
313321 baseToken : baseTokenValue ?. denom ,
314322 baseAmount : parsedAmountValue ,
315- quoteToken : quoteTokenValue ?. quote_token ,
323+ quoteToken : maybeQuoteToken ,
316324 quoteAmount : parsedAmountValue ,
317325 receiver : derivedReceiverValue ,
318- timeoutHeight : 0n ,
326+ timeoutHeight : "0n" ,
319327 timeoutTimestamp : "0x000000000000000000000000000000000000000000000000fffffffffffffffa" ,
320- wethQuoteToken : wethQuoteTokenValue ?. wethQuoteToken
328+ wethQuoteToken : maybeWethQuoteToken
321329 }
322330 } )
323331
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 ) )
331333
332- isValid = $derived ( this . transferResult . isValid )
334+ isValid = $derived ( this . validation . isValid )
333335
334336 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+
335345 if ( Option . isNone ( chains . data ) || Option . isNone ( this . sourceChain ) ) return
336- console . log ( this . transferResult . args )
346+ console . info ( "Validated args:" , typedArgs )
337347
338348 const sourceChainValue = this . sourceChain . value
339349
340350 if ( sourceChainValue . rpc_type === "evm" ) {
341351 let evmState : EvmTransferSubmission
342352 if ( this . state . _tag === "EVM" ) {
343- // If failed, reset the failed step to InProgress
344353 if ( hasEvmFailedExit ( this . state . state ) ) {
345354 switch ( this . state . state . _tag ) {
346355 case "SwitchChain" :
@@ -378,17 +387,12 @@ export class Transfer {
378387 evmState = EvmTransferSubmission . Filling ( )
379388 }
380389
381- const newState = await evmNextState ( evmState , this . transferResult . args , sourceChainValue )
390+ const newState = await evmNextState ( evmState , typedArgs , sourceChainValue )
382391 this . _stateOverride = newState !== null ? TransferState . EVM ( newState ) : TransferState . Empty ( )
383392
384- console . info ( "evmState: " , evmState )
385393 let currentEvmState = newState
386394 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 )
392396 this . _stateOverride =
393397 nextEvmState !== null ? TransferState . EVM ( nextEvmState ) : TransferState . Empty ( )
394398
@@ -398,7 +402,6 @@ export class Transfer {
398402 } else if ( sourceChainValue . rpc_type === "cosmos" ) {
399403 let cosmosState : CosmosTransferSubmission
400404 if ( this . state . _tag === "Cosmos" ) {
401- // If failed, reset the failed step to InProgress
402405 if ( hasCosmosFailedExit ( this . state . state ) ) {
403406 switch ( this . state . state . _tag ) {
404407 case "SwitchChain" :
@@ -428,7 +431,7 @@ export class Transfer {
428431
429432 const newState = await cosmosNextState (
430433 cosmosState ,
431- this . transferResult . args ,
434+ typedArgs ,
432435 sourceChainValue ,
433436 cosmosStore . connectedWallet
434437 )
@@ -439,7 +442,7 @@ export class Transfer {
439442 while ( currentCosmosState !== null && ! hasCosmosFailedExit ( currentCosmosState ) ) {
440443 const nextCosmosState = await cosmosNextState (
441444 currentCosmosState ,
442- this . transferResult . args ,
445+ typedArgs ,
443446 sourceChainValue ,
444447 cosmosStore . connectedWallet
445448 )
@@ -450,12 +453,8 @@ export class Transfer {
450453 if ( currentCosmosState !== null && isCosmosComplete ( currentCosmosState ) ) break
451454 }
452455 } else if ( sourceChainValue . rpc_type === "aptos" ) {
453- console . info ( "sourceChain is aptos" )
454- console . info ( "this.state._tag is: " , this . state . _tag )
455456 let aptosState : AptosTransferSubmission
456457 if ( this . state . _tag === "Aptos" ) {
457- console . info ( "state._tag is aptos" )
458- // If failed, reset the failed step to InProgress
459458 if ( hasAptosFailedExit ( this . state . state ) ) {
460459 switch ( this . state . state . _tag ) {
461460 case "SwitchChain" :
@@ -483,24 +482,18 @@ export class Transfer {
483482 aptosState = AptosTransferSubmission . Filling ( )
484483 }
485484
486- console . info ( "aptosState: " , aptosState )
487-
488- const newState = await aptosNextState ( aptosState , this . transferResult . args , sourceChainValue )
485+ const newState = await aptosNextState ( aptosState , typedArgs , sourceChainValue )
489486 this . _stateOverride =
490487 newState !== null ? TransferState . Aptos ( newState ) : TransferState . Empty ( )
491488
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 )
499492 this . _stateOverride =
500- nextaptosState !== null ? TransferState . Aptos ( nextaptosState ) : TransferState . Empty ( )
493+ nextAptosState !== null ? TransferState . Aptos ( nextAptosState ) : TransferState . Empty ( )
501494
502- currentaptosState = nextaptosState
503- if ( currentaptosState !== null && isAptosComplete ( currentaptosState ) ) break
495+ currentAptosState = nextAptosState
496+ if ( currentAptosState !== null && isAptosComplete ( currentAptosState ) ) break
504497 }
505498 }
506499 }
0 commit comments