@@ -197,22 +197,24 @@ fn destination_token_decimals(
197
197
storage : & dyn Storage ,
198
198
source_chain : & ChainNameRaw ,
199
199
destination_chain : & ChainNameRaw ,
200
- source_chain_decimals : u8 ,
200
+ source_token_decimals : u8 ,
201
201
) -> Result < u8 , Error > {
202
202
let source_chain_config =
203
203
state:: load_chain_config ( storage, source_chain) . change_context ( Error :: State ) ?;
204
204
let destination_chain_config =
205
205
state:: load_chain_config ( storage, destination_chain) . change_context ( Error :: State ) ?;
206
206
207
207
if source_chain_config
208
+ . truncation
208
209
. max_uint
209
- . le ( & destination_chain_config. max_uint )
210
+ . le ( & destination_chain_config. truncation . max_uint )
210
211
{
211
- source_chain_decimals
212
+ source_token_decimals
212
213
} else {
213
- source_chain_config
214
- . max_target_decimals
215
- . min ( source_chain_decimals)
214
+ destination_chain_config
215
+ . truncation
216
+ . max_decimals_when_truncating
217
+ . min ( source_token_decimals)
216
218
}
217
219
. then ( Result :: Ok )
218
220
}
@@ -244,6 +246,7 @@ fn destination_amount(
244
246
245
247
let destination_max_uint = state:: load_chain_config ( storage, destination_chain)
246
248
. change_context ( Error :: State ) ?
249
+ . truncation
247
250
. max_uint ;
248
251
249
252
// It's intentionally written in this way since the end result may still be fine even if
@@ -311,6 +314,7 @@ mod test {
311
314
312
315
use super :: Error ;
313
316
use crate :: contract:: execute:: interceptors;
317
+ use crate :: msg:: TruncationConfig ;
314
318
use crate :: state:: { self , TokenDeploymentType } ;
315
319
use crate :: { msg, DeployInterchainToken , InterchainTransfer , TokenInstance } ;
316
320
@@ -347,8 +351,10 @@ mod test {
347
351
msg:: ChainConfig {
348
352
chain : destination_chain. clone ( ) ,
349
353
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
350
- max_uint : Uint256 :: from ( 1_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
351
- max_target_decimals : 6 ,
354
+ truncation : TruncationConfig {
355
+ max_uint : Uint256 :: from ( 1_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
356
+ max_decimals_when_truncating : 6 ,
357
+ } ,
352
358
} ,
353
359
)
354
360
. unwrap ( ) ;
@@ -398,8 +404,10 @@ mod test {
398
404
msg:: ChainConfig {
399
405
chain : destination_chain. clone ( ) ,
400
406
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
401
- max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
402
- max_target_decimals : 6 ,
407
+ truncation : TruncationConfig {
408
+ max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
409
+ max_decimals_when_truncating : 6 ,
410
+ } ,
403
411
} ,
404
412
)
405
413
. unwrap ( ) ;
@@ -449,8 +457,10 @@ mod test {
449
457
msg:: ChainConfig {
450
458
chain : destination_chain. clone ( ) ,
451
459
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
452
- max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
453
- max_target_decimals : 6 ,
460
+ truncation : TruncationConfig {
461
+ max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
462
+ max_decimals_when_truncating : 6 ,
463
+ } ,
454
464
} ,
455
465
)
456
466
. unwrap ( ) ;
@@ -500,8 +510,10 @@ mod test {
500
510
msg:: ChainConfig {
501
511
chain : destination_chain. clone ( ) ,
502
512
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
503
- max_uint : Uint256 :: from ( 100_000u128 ) . try_into ( ) . unwrap ( ) ,
504
- max_target_decimals : 6 ,
513
+ truncation : TruncationConfig {
514
+ max_uint : Uint256 :: from ( 100_000u128 ) . try_into ( ) . unwrap ( ) ,
515
+ max_decimals_when_truncating : 6 ,
516
+ } ,
505
517
} ,
506
518
)
507
519
. unwrap ( ) ;
@@ -551,8 +563,10 @@ mod test {
551
563
msg:: ChainConfig {
552
564
chain : destination_chain. clone ( ) ,
553
565
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
554
- max_uint : Uint256 :: from ( 100_000u128 ) . try_into ( ) . unwrap ( ) ,
555
- max_target_decimals : 6 ,
566
+ truncation : TruncationConfig {
567
+ max_uint : Uint256 :: from ( 100_000u128 ) . try_into ( ) . unwrap ( ) ,
568
+ max_decimals_when_truncating : 6 ,
569
+ } ,
556
570
} ,
557
571
)
558
572
. unwrap ( ) ;
@@ -574,22 +588,17 @@ mod test {
574
588
let mut storage = MockStorage :: new ( ) ;
575
589
let source_chain: ChainNameRaw = "sourcechain" . try_into ( ) . unwrap ( ) ;
576
590
let destination_chain: ChainNameRaw = "destinationchain" . try_into ( ) . unwrap ( ) ;
577
- let deploy_token = DeployInterchainToken {
578
- token_id : [ 1u8 ; 32 ] . into ( ) ,
579
- name : "token" . to_string ( ) . try_into ( ) . unwrap ( ) ,
580
- symbol : "TKN" . to_string ( ) . try_into ( ) . unwrap ( ) ,
581
- decimals : 9 ,
582
- minter : None ,
583
- } ;
584
591
585
592
state:: save_chain_config (
586
593
& mut storage,
587
594
& source_chain,
588
595
msg:: ChainConfig {
589
596
chain : source_chain. clone ( ) ,
590
597
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
591
- max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
592
- max_target_decimals : 6 ,
598
+ truncation : TruncationConfig {
599
+ max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
600
+ max_decimals_when_truncating : 12 ,
601
+ } ,
593
602
} ,
594
603
)
595
604
. unwrap ( ) ;
@@ -599,11 +608,20 @@ mod test {
599
608
msg:: ChainConfig {
600
609
chain : destination_chain. clone ( ) ,
601
610
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
602
- max_uint : Uint256 :: from ( 1_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
603
- max_target_decimals : 6 ,
611
+ truncation : TruncationConfig {
612
+ max_uint : Uint256 :: from ( 1_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
613
+ max_decimals_when_truncating : 6 ,
614
+ } ,
604
615
} ,
605
616
)
606
617
. unwrap ( ) ;
618
+ let deploy_token = DeployInterchainToken {
619
+ token_id : [ 1u8 ; 32 ] . into ( ) ,
620
+ name : "token" . to_string ( ) . try_into ( ) . unwrap ( ) ,
621
+ symbol : "TKN" . to_string ( ) . try_into ( ) . unwrap ( ) ,
622
+ decimals : 9 ,
623
+ minter : None ,
624
+ } ;
607
625
608
626
let deploy_token = assert_ok ! ( interceptors:: calculate_scaling_factor(
609
627
& storage,
@@ -634,22 +652,17 @@ mod test {
634
652
let mut storage = MockStorage :: new ( ) ;
635
653
let source_chain: ChainNameRaw = "sourcechain" . try_into ( ) . unwrap ( ) ;
636
654
let destination_chain: ChainNameRaw = "destinationchain" . try_into ( ) . unwrap ( ) ;
637
- let deploy_token = DeployInterchainToken {
638
- token_id : [ 1u8 ; 32 ] . into ( ) ,
639
- name : "token" . to_string ( ) . try_into ( ) . unwrap ( ) ,
640
- symbol : "TKN" . to_string ( ) . try_into ( ) . unwrap ( ) ,
641
- decimals : 9 ,
642
- minter : None ,
643
- } ;
644
655
645
656
state:: save_chain_config (
646
657
& mut storage,
647
658
& source_chain,
648
659
msg:: ChainConfig {
649
660
chain : source_chain. clone ( ) ,
650
661
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
651
- max_uint : Uint256 :: from ( 1_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
652
- max_target_decimals : 6 ,
662
+ truncation : TruncationConfig {
663
+ max_uint : Uint256 :: from ( 1_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
664
+ max_decimals_when_truncating : 6 ,
665
+ } ,
653
666
} ,
654
667
)
655
668
. unwrap ( ) ;
@@ -659,18 +672,42 @@ mod test {
659
672
msg:: ChainConfig {
660
673
chain : destination_chain. clone ( ) ,
661
674
its_edge_contract : "itsedgecontract" . to_string ( ) . try_into ( ) . unwrap ( ) ,
662
- max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
663
- max_target_decimals : 6 ,
675
+ truncation : TruncationConfig {
676
+ max_uint : Uint256 :: from ( 1_000_000_000_000_000u128 ) . try_into ( ) . unwrap ( ) ,
677
+ max_decimals_when_truncating : 6 ,
678
+ } ,
664
679
} ,
665
680
)
666
681
. unwrap ( ) ;
667
682
683
+ let deploy_token = DeployInterchainToken {
684
+ token_id : [ 1u8 ; 32 ] . into ( ) ,
685
+ name : "token" . to_string ( ) . try_into ( ) . unwrap ( ) ,
686
+ symbol : "TKN" . to_string ( ) . try_into ( ) . unwrap ( ) ,
687
+ decimals : 9 ,
688
+ minter : None ,
689
+ } ;
668
690
let deploy_token = assert_ok ! ( interceptors:: calculate_scaling_factor(
669
691
& storage,
670
692
& source_chain,
671
693
& destination_chain,
672
694
deploy_token,
673
695
) ) ;
674
696
assert_eq ! ( deploy_token. decimals, 9 ) ;
697
+
698
+ let deploy_token = DeployInterchainToken {
699
+ token_id : [ 1u8 ; 32 ] . into ( ) ,
700
+ name : "token" . to_string ( ) . try_into ( ) . unwrap ( ) ,
701
+ symbol : "TKN" . to_string ( ) . try_into ( ) . unwrap ( ) ,
702
+ decimals : 3 ,
703
+ minter : None ,
704
+ } ;
705
+ let deploy_token = assert_ok ! ( interceptors:: calculate_scaling_factor(
706
+ & storage,
707
+ & source_chain,
708
+ & destination_chain,
709
+ deploy_token,
710
+ ) ) ;
711
+ assert_eq ! ( deploy_token. decimals, 3 ) ;
675
712
}
676
713
}
0 commit comments