@@ -36,20 +36,13 @@ import {
36
36
} from " @unionlabs/sdk/cosmos"
37
37
import { fromHex , http , isHex } from " viem"
38
38
import { truncate } from " $lib/utils/format.ts"
39
- import {
40
- ApprovalRequired ,
41
- Filling ,
42
- getStepDescription ,
43
- SubmitInstruction ,
44
- type TransferStep ,
45
- WaitForIndex
46
- } from " ./transfer-step.ts"
39
+ import * as TransferStep from " ./transfer-step.ts"
47
40
import { isValidBech32ContractAddress } from " @unionlabs/client"
48
41
import IndexPage from " $lib/components/Transfer/pages/IndexPage.svelte"
49
42
50
43
let showDetails = $state (false )
51
44
let currentPage = $state (0 )
52
- let instruction: Option .Option <Instruction > = $state (Option .none ())
45
+ let instruction: Option .Option <Instruction . Instruction > = $state (Option .none ())
53
46
let allowances: Option .Option <Array <{ token: string ; allowance: bigint }>> = $state (Option .none ())
54
47
55
48
// This should now handle cosmos, evm and aptos (when aptos is implemented)
@@ -90,7 +83,7 @@ let transferIntents = $derived.by(() => {
90
83
return Option .some ([
91
84
{
92
85
sender: sender .value ,
93
- receiver: transferValue .receiver ,
86
+ receiver: transferValue .receiver . toLowerCase () ,
94
87
baseToken: isHex (transferValue .baseToken )
95
88
? fromHex (transferValue .baseToken , " string" )
96
89
: transferValue .baseToken ,
@@ -108,7 +101,8 @@ let requiredApprovals = $derived.by(() => {
108
101
const requiredAmounts = new Map <string , bigint >()
109
102
for (const intent of transferIntents .value ) {
110
103
const currentAmount = requiredAmounts .get (intent .baseToken ) || 0n
111
- requiredAmounts .set (intent .baseToken , intent .baseAmount )
104
+ // FIX: Add the new amount to the current amount instead of replacing it
105
+ requiredAmounts .set (intent .baseToken , currentAmount + intent .baseAmount )
112
106
}
113
107
114
108
// Filter for tokens that need approval (allowance < required amount)
@@ -127,7 +121,7 @@ let requiredApprovals = $derived.by(() => {
127
121
128
122
// Derive the steps based on required approvals and instruction
129
123
let transferSteps = $derived .by (() => {
130
- const steps: Array <TransferStep > = [Filling ()]
124
+ const steps: Array <TransferStep . TransferStep > = [TransferStep . Filling ()]
131
125
132
126
// Add approval steps if needed
133
127
if (Option .isSome (requiredApprovals )) {
@@ -137,7 +131,7 @@ let transferSteps = $derived.by(() => {
137
131
const tokenAllowance = allowances .value .find (a => a .token === approval .token )
138
132
if (tokenAllowance ) {
139
133
steps .push (
140
- ApprovalRequired ({
134
+ TransferStep . ApprovalRequired ({
141
135
token: approval .token ,
142
136
requiredAmount: approval .requiredAmount ,
143
137
currentAllowance: tokenAllowance .allowance
@@ -150,8 +144,8 @@ let transferSteps = $derived.by(() => {
150
144
151
145
// Add the instruction submission step if we have an instruction
152
146
if (Option .isSome (instruction )) {
153
- steps .push (SubmitInstruction ({ instruction: instruction .value }))
154
- steps .push (WaitForIndex ())
147
+ steps .push (TransferStep . SubmitInstruction ({ instruction: instruction .value }))
148
+ steps .push (TransferStep . WaitForIndex ())
155
149
}
156
150
157
151
return steps .length > 0 ? Option .some (steps ) : Option .none ()
@@ -218,8 +212,7 @@ const intentsToBatch = (ti: typeof transferIntents) =>
218
212
const provideCosmWasmClientSource = Effect .provideServiceEffect (
219
213
CosmWasmClientSource ,
220
214
pipe (
221
- Option .some (" https://rpc.rpc-node.union-testnet-10.union.build" ),
222
- // transfer.sourceChain.value.getRpcUrl("rpc"),
215
+ transfer .sourceChain .value .getRpcUrl (" rpc" ),
223
216
Option .map (createCosmWasmClient ),
224
217
Effect .flatten ,
225
218
Effect .map (client => ({ client }))
@@ -229,8 +222,7 @@ const intentsToBatch = (ti: typeof transferIntents) =>
229
222
const provideCosmWasmClientDestination = Effect .provideServiceEffect (
230
223
CosmWasmClientDestination ,
231
224
pipe (
232
- Option .some (" https://rpc.rpc-node.union-testnet-10.union.build" ),
233
- // transfer.destinationChain.value.getRpcUrl("rpc"),
225
+ transfer .destinationChain .value .getRpcUrl (" rpc" ),
234
226
Option .map (createCosmWasmClient ),
235
227
Effect .flatten ,
236
228
Effect .map (client => ({ client }))
@@ -308,7 +300,7 @@ const intentsToBatch = (ti: typeof transferIntents) =>
308
300
Effect .sync (() => console .log (" batch: Cosmos->EVM order created" , order ))
309
301
),
310
302
Effect .catchAll (error => {
311
- console .error (" batch: Error creating Cosmos->EVM order" , error . cause )
303
+ console .error (" batch: Error creating Cosmos->EVM order" , error )
312
304
return Effect .fail (error )
313
305
}),
314
306
provideCosmWasmClientSource ,
@@ -339,7 +331,9 @@ const intentsToBatch = (ti: typeof transferIntents) =>
339
331
340
332
// Handle both array and single order cases
341
333
console .log (` batch: Orders created: ` , orders )
342
- const batch = Array .isArray (orders ) ? Batch (orders ) : Batch ([orders ])
334
+ const batch = new Batch ({
335
+ operand: Array .isArray (orders ) ? orders : [orders ]
336
+ })
343
337
console .log (` batch: Batch created: ` , batch )
344
338
return batch
345
339
}).pipe (
@@ -410,9 +404,7 @@ const checkAllowances = (ti: typeof transferIntents) =>
410
404
// For Cosmos chains use a CosmWasm client to query CW20 allowances.
411
405
const rpcUrl = sourceChain .getRpcUrl (" rpc" )
412
406
if (Option .isNone (rpcUrl )) return Option .none ()
413
- const cosmwasmClient = yield * createCosmWasmClient (
414
- " https://rpc.rpc-node.union-testnet-10.union.build"
415
- )
407
+ const cosmwasmClient = yield * createCosmWasmClient (rpcUrl )
416
408
417
409
// Query each token (assumed to be a CW20 contract) for the allowance.
418
410
const allowanceChecks = yield * Effect .all (
@@ -421,7 +413,6 @@ const checkAllowances = (ti: typeof transferIntents) =>
421
413
const decodedAddr = fromHex (tokenAddress , " string" )
422
414
423
415
if (! isValidBech32ContractAddress (decodedAddr )) {
424
- console .log (" It's native token, returning none. Token:" , tokenAddress )
425
416
return Option .none ()
426
417
}
427
418
@@ -491,33 +482,33 @@ let actionButtonText = $derived.by(() => {
491
482
return " Complete"
492
483
}
493
484
494
- if (currentStep ._tag === " Filling" ) {
495
- return " Continue"
496
- }
497
-
498
- if (currentStep ._tag === " ApprovalRequired" ) {
499
- return " Approve"
500
- }
501
-
502
- if (currentStep ._tag === " SubmitInstruction" ) {
503
- return " Submit"
504
- }
505
-
506
- return " Next"
485
+ return TransferStep .match (currentStep , {
486
+ Filling : () => " Continue" ,
487
+ ApprovalRequired : () => " Approve" ,
488
+ SubmitInstruction : () => " Submit" ,
489
+ WaitForIndex : () => " Submit"
490
+ })
507
491
})
508
492
509
493
function handleActionButtonClick() {
510
494
if (Option .isNone (transferSteps )) return
511
495
496
+ console .log (" handleActionButtonClick called" , {
497
+ transferSteps: transferSteps ,
498
+ currentPage: currentPage
499
+ })
500
+
512
501
const currentStep = transferSteps .value [currentPage ]
513
502
514
- if (currentStep . _tag === " Filling" ) {
503
+ if (TransferStep . is ( " Filling" )( currentStep ) ) {
515
504
// Lock the transfer values before proceeding
516
505
if (Option .isNone (lockedTransferStore .get ())) {
517
506
const newLockedTransfer = LockedTransfer .fromTransfer (
518
507
transfer .sourceChain ,
519
508
transfer .destinationChain ,
520
509
transfer .channel ,
510
+ transfer .parsedAmount ,
511
+ transfer .baseToken ,
521
512
transferSteps
522
513
)
523
514
@@ -532,27 +523,27 @@ function handleActionButtonClick() {
532
523
return
533
524
}
534
525
535
- if (currentStep . _tag === " ApprovalRequired" ) {
526
+ if (TransferStep . is ( " ApprovalRequired" )( currentStep ) ) {
536
527
goToNextPage ()
537
528
return
538
529
}
539
530
540
- if (currentStep . _tag === " SubmitInstruction" ) {
531
+ if (TransferStep . is ( " SubmitInstruction" )( currentStep ) ) {
541
532
goToNextPage ()
542
533
return
543
534
}
544
535
}
545
536
</script >
546
537
547
538
<Card
548
- divided
549
- class =" w-sm my-24 relative self-center flex flex-col justify-between min-h-[450px] overflow-hidden"
539
+ divided
540
+ class =" w-sm my-24 relative self-center flex flex-col justify-between min-h-[450px] overflow-hidden"
550
541
>
551
542
<div class =" p-4 w-full" >
552
543
<StepProgressBar
553
- class =" w-full"
554
- currentStep ={currentPage + 1 }
555
- totalSteps ={lockedTransferStore .get ().pipe (
544
+ class =" w-full"
545
+ currentStep ={currentPage + 1 }
546
+ totalSteps ={lockedTransferStore .get ().pipe (
556
547
Option .map ((lts ) => lts .steps .length ),
557
548
Option .getOrElse (() =>
558
549
transferSteps .pipe (
@@ -561,10 +552,12 @@ function handleActionButtonClick() {
561
552
),
562
553
),
563
554
)}
564
- stepDescriptions ={lockedTransferStore .get ().pipe (
565
- Option .map ((lts ) => lts .steps .map (getStepDescription )),
555
+ stepDescriptions ={lockedTransferStore .get ().pipe (
556
+ Option .map ((lts ) => lts .steps .map (TransferStep . description )),
566
557
Option .orElse (() =>
567
- transferSteps .pipe (Option .map ((ts ) => ts .map (getStepDescription ))),
558
+ transferSteps .pipe (
559
+ Option .map ((ts ) => ts .map (TransferStep .description )),
560
+ ),
568
561
),
569
562
Option .getOrElse (() => [" Configure your transfer" ]),
570
563
)}
@@ -575,34 +568,33 @@ function handleActionButtonClick() {
575
568
<div class =" relative flex-1 overflow-hidden" >
576
569
<!-- Pages wrapper with horizontal sliding -->
577
570
<div
578
- class =" absolute inset-0 flex transition-transform duration-300 ease-in-out"
579
- style ="transform: translateX(- {currentPage * 100 }%);"
571
+ class =" absolute inset-0 flex transition-transform duration-300 ease-in-out"
572
+ style ="transform: translateX(- {currentPage * 100 }%);"
580
573
>
581
574
<!-- Page 1: Filling -->
582
- <FillingPage onContinue ={handleActionButtonClick } {actionButtonText }/>
575
+ <FillingPage onContinue ={handleActionButtonClick } {actionButtonText } />
583
576
584
577
<!-- Dynamic pages for each step -->
585
578
{#if Option .isSome (lockedTransferStore .get ())}
586
579
{#each lockedTransferStore .get ().value .steps .slice (1 ) as step , i }
587
- {#if step . _tag === " ApprovalRequired" }
580
+ {#if TransferStep . is ( " ApprovalRequired" )( step ) }
588
581
<ApprovalPage
589
- stepIndex ={i + 1 }
590
- onBack ={goToPreviousPage }
591
- onApprove ={handleActionButtonClick }
592
- {actionButtonText }
582
+ stepIndex ={i + 1 }
583
+ onBack ={goToPreviousPage }
584
+ onApprove ={handleActionButtonClick }
585
+ {actionButtonText }
593
586
/>
594
- {:else if step . _tag === " SubmitInstruction" }
587
+ {:else if TransferStep . is ( " SubmitInstruction" )( step ) }
595
588
<SubmitPage
596
- stepIndex ={i + 1 }
597
- onBack ={goToPreviousPage }
598
- onSubmit ={handleActionButtonClick }
599
- {actionButtonText }
589
+ stepIndex ={i + 1 }
590
+ onBack ={goToPreviousPage }
591
+ onSubmit ={handleActionButtonClick }
592
+ {actionButtonText }
600
593
/>
601
- {:else if step . _tag === " WaitForIndex" }
594
+ {:else if TransferStep . is ( " WaitForIndex" )( step ) }
602
595
<IndexPage
603
- stepIndex ={i + 1 }
604
- onBack ={goToPreviousPage }
605
- {actionButtonText }
596
+ stepIndex ={i + 1 }
597
+ onBack ={goToPreviousPage }
606
598
/>
607
599
{/if }
608
600
{/each }
@@ -611,7 +603,7 @@ function handleActionButtonClick() {
611
603
</div >
612
604
613
605
{#if showDetails }
614
- <ShowData />
606
+ <ShowData />
615
607
{/if }
616
608
</Card >
617
609
@@ -623,22 +615,22 @@ function handleActionButtonClick() {
623
615
<ol class =" list-decimal pl-5 mt-2" >
624
616
{#each lockedTransferStore
625
617
.get ()
626
- .pipe (Option .map ((lts ) => lts .steps ), Option .orElse (() => transferSteps ), Option .getOrElse (() => [],), ) as step , index }
618
+ .pipe ( Option .map ((lts ) => lts .steps ), Option .orElse (() => transferSteps ), Option .getOrElse ( () => [], ), ) as step , index }
627
619
<li class ="mb-2" class:font-bold ={index === currentPage }>
628
- {#if step . _tag === " Filling" }
620
+ {#if TransferStep . is ( " Filling" )( step ) }
629
621
<div >Configure transfer details</div >
630
- {:else if step . _tag === " ApprovalRequired" }
622
+ {:else if TransferStep . is ( " ApprovalRequired" )( step ) }
631
623
<div >
632
624
Approve token: <span class =" font-mono"
633
- >{truncate (step .token , 8 , " middle" )}</span
634
- >
625
+ >{truncate (step .token , 8 , " middle" )}</span
626
+ >
635
627
<div class =" text-sm" >
636
628
Current allowance: {step .currentAllowance .toString ()}
637
- <br />
629
+ <br />
638
630
Required amount: {step .requiredAmount .toString ()}
639
631
</div >
640
632
</div >
641
- {:else if step . _tag === " SubmitInstruction" }
633
+ {:else if TransferStep . is ( " SubmitInstruction" )( step ) }
642
634
<div >Submit transfer instruction</div >
643
635
{/if }
644
636
</li >
0 commit comments