@@ -569,6 +569,16 @@ fn can_quote_price() {
569569 ) ,
570570 Some ( 60 )
571571 ) ;
572+ // including fee so should get less out...
573+ assert_eq ! (
574+ AssetConversion :: quote_price_exact_tokens_for_tokens(
575+ NativeOrAssetId :: Native ,
576+ NativeOrAssetId :: Asset ( 2 ) ,
577+ 3000 ,
578+ true ,
579+ ) ,
580+ Some ( 46 )
581+ ) ;
572582 // Check it still gives same price:
573583 // (if the above accidentally exchanged then it would not give same quote as before)
574584 assert_eq ! (
@@ -580,6 +590,16 @@ fn can_quote_price() {
580590 ) ,
581591 Some ( 60 )
582592 ) ;
593+ // including fee so should get less out...
594+ assert_eq ! (
595+ AssetConversion :: quote_price_exact_tokens_for_tokens(
596+ NativeOrAssetId :: Native ,
597+ NativeOrAssetId :: Asset ( 2 ) ,
598+ 3000 ,
599+ true ,
600+ ) ,
601+ Some ( 46 )
602+ ) ;
583603
584604 // Check inverse:
585605 assert_eq ! (
@@ -591,6 +611,247 @@ fn can_quote_price() {
591611 ) ,
592612 Some ( 3000 )
593613 ) ;
614+ // including fee so should get less out...
615+ assert_eq ! (
616+ AssetConversion :: quote_price_exact_tokens_for_tokens(
617+ NativeOrAssetId :: Asset ( 2 ) ,
618+ NativeOrAssetId :: Native ,
619+ 60 ,
620+ true ,
621+ ) ,
622+ Some ( 2302 )
623+ ) ;
624+
625+ //
626+ // same tests as above but for quote_price_tokens_for_exact_tokens:
627+ //
628+ assert_eq ! (
629+ AssetConversion :: quote_price_tokens_for_exact_tokens(
630+ NativeOrAssetId :: Native ,
631+ NativeOrAssetId :: Asset ( 2 ) ,
632+ 60 ,
633+ false ,
634+ ) ,
635+ Some ( 3000 )
636+ ) ;
637+ // including fee so should need to put more in...
638+ assert_eq ! (
639+ AssetConversion :: quote_price_tokens_for_exact_tokens(
640+ NativeOrAssetId :: Native ,
641+ NativeOrAssetId :: Asset ( 2 ) ,
642+ 60 ,
643+ true ,
644+ ) ,
645+ Some ( 4299 )
646+ ) ;
647+ // Check it still gives same price:
648+ // (if the above accidentally exchanged then it would not give same quote as before)
649+ assert_eq ! (
650+ AssetConversion :: quote_price_tokens_for_exact_tokens(
651+ NativeOrAssetId :: Native ,
652+ NativeOrAssetId :: Asset ( 2 ) ,
653+ 60 ,
654+ false ,
655+ ) ,
656+ Some ( 3000 )
657+ ) ;
658+ // including fee so should need to put more in...
659+ assert_eq ! (
660+ AssetConversion :: quote_price_tokens_for_exact_tokens(
661+ NativeOrAssetId :: Native ,
662+ NativeOrAssetId :: Asset ( 2 ) ,
663+ 60 ,
664+ true ,
665+ ) ,
666+ Some ( 4299 )
667+ ) ;
668+
669+ // Check inverse:
670+ assert_eq ! (
671+ AssetConversion :: quote_price_tokens_for_exact_tokens(
672+ NativeOrAssetId :: Asset ( 2 ) ,
673+ NativeOrAssetId :: Native ,
674+ 3000 ,
675+ false ,
676+ ) ,
677+ Some ( 60 )
678+ ) ;
679+ // including fee so should need to put more in...
680+ assert_eq ! (
681+ AssetConversion :: quote_price_tokens_for_exact_tokens(
682+ NativeOrAssetId :: Asset ( 2 ) ,
683+ NativeOrAssetId :: Native ,
684+ 3000 ,
685+ true ,
686+ ) ,
687+ Some ( 86 )
688+ ) ;
689+
690+ //
691+ // roundtrip: Without fees one should get the original number
692+ //
693+ let amount_in = 100 ;
694+
695+ assert_eq ! (
696+ AssetConversion :: quote_price_exact_tokens_for_tokens(
697+ NativeOrAssetId :: Asset ( 2 ) ,
698+ NativeOrAssetId :: Native ,
699+ amount_in,
700+ false ,
701+ )
702+ . and_then( |amount| AssetConversion :: quote_price_exact_tokens_for_tokens(
703+ NativeOrAssetId :: Native ,
704+ NativeOrAssetId :: Asset ( 2 ) ,
705+ amount,
706+ false ,
707+ ) ) ,
708+ Some ( amount_in)
709+ ) ;
710+ assert_eq ! (
711+ AssetConversion :: quote_price_exact_tokens_for_tokens(
712+ NativeOrAssetId :: Native ,
713+ NativeOrAssetId :: Asset ( 2 ) ,
714+ amount_in,
715+ false ,
716+ )
717+ . and_then( |amount| AssetConversion :: quote_price_exact_tokens_for_tokens(
718+ NativeOrAssetId :: Asset ( 2 ) ,
719+ NativeOrAssetId :: Native ,
720+ amount,
721+ false ,
722+ ) ) ,
723+ Some ( amount_in)
724+ ) ;
725+
726+ assert_eq ! (
727+ AssetConversion :: quote_price_tokens_for_exact_tokens(
728+ NativeOrAssetId :: Asset ( 2 ) ,
729+ NativeOrAssetId :: Native ,
730+ amount_in,
731+ false ,
732+ )
733+ . and_then( |amount| AssetConversion :: quote_price_tokens_for_exact_tokens(
734+ NativeOrAssetId :: Native ,
735+ NativeOrAssetId :: Asset ( 2 ) ,
736+ amount,
737+ false ,
738+ ) ) ,
739+ Some ( amount_in)
740+ ) ;
741+ assert_eq ! (
742+ AssetConversion :: quote_price_tokens_for_exact_tokens(
743+ NativeOrAssetId :: Native ,
744+ NativeOrAssetId :: Asset ( 2 ) ,
745+ amount_in,
746+ false ,
747+ )
748+ . and_then( |amount| AssetConversion :: quote_price_tokens_for_exact_tokens(
749+ NativeOrAssetId :: Asset ( 2 ) ,
750+ NativeOrAssetId :: Native ,
751+ amount,
752+ false ,
753+ ) ) ,
754+ Some ( amount_in)
755+ ) ;
756+ } ) ;
757+ }
758+
759+ #[ test]
760+ fn quote_price_exact_tokens_for_tokens_matches_execution ( ) {
761+ new_test_ext ( ) . execute_with ( || {
762+ let user = 1 ;
763+ let user2 = 2 ;
764+ let token_1 = NativeOrAssetId :: Native ;
765+ let token_2 = NativeOrAssetId :: Asset ( 2 ) ;
766+
767+ create_tokens ( user, vec ! [ token_2] ) ;
768+ assert_ok ! ( AssetConversion :: create_pool( RuntimeOrigin :: signed( user) , token_1, token_2) ) ;
769+
770+ assert_ok ! ( Balances :: force_set_balance( RuntimeOrigin :: root( ) , user, 100000 ) ) ;
771+ assert_ok ! ( Assets :: mint( RuntimeOrigin :: signed( user) , 2 , user, 1000 ) ) ;
772+
773+ assert_ok ! ( AssetConversion :: add_liquidity(
774+ RuntimeOrigin :: signed( user) ,
775+ token_1,
776+ token_2,
777+ 10000 ,
778+ 200 ,
779+ 1 ,
780+ 1 ,
781+ user,
782+ ) ) ;
783+
784+ let amount = 1 ;
785+ let quoted_price = 49 ;
786+ assert_eq ! (
787+ AssetConversion :: quote_price_exact_tokens_for_tokens( token_2, token_1, amount, true , ) ,
788+ Some ( quoted_price)
789+ ) ;
790+
791+ assert_ok ! ( Assets :: mint( RuntimeOrigin :: signed( user) , 2 , user2, amount) ) ;
792+ let prior_dot_balance = 20000 ;
793+ assert_eq ! ( prior_dot_balance, balance( user2, token_1) ) ;
794+ assert_ok ! ( AssetConversion :: swap_exact_tokens_for_tokens(
795+ RuntimeOrigin :: signed( user2) ,
796+ bvec![ token_2, token_1] ,
797+ amount,
798+ 1 ,
799+ user2,
800+ false ,
801+ ) ) ;
802+
803+ assert_eq ! ( prior_dot_balance + quoted_price, balance( user2, token_1) ) ;
804+ } ) ;
805+ }
806+
807+ #[ test]
808+ fn quote_price_tokens_for_exact_tokens_matches_execution ( ) {
809+ new_test_ext ( ) . execute_with ( || {
810+ let user = 1 ;
811+ let user2 = 2 ;
812+ let token_1 = NativeOrAssetId :: Native ;
813+ let token_2 = NativeOrAssetId :: Asset ( 2 ) ;
814+
815+ create_tokens ( user, vec ! [ token_2] ) ;
816+ assert_ok ! ( AssetConversion :: create_pool( RuntimeOrigin :: signed( user) , token_1, token_2) ) ;
817+
818+ assert_ok ! ( Balances :: force_set_balance( RuntimeOrigin :: root( ) , user, 100000 ) ) ;
819+ assert_ok ! ( Assets :: mint( RuntimeOrigin :: signed( user) , 2 , user, 1000 ) ) ;
820+
821+ assert_ok ! ( AssetConversion :: add_liquidity(
822+ RuntimeOrigin :: signed( user) ,
823+ token_1,
824+ token_2,
825+ 10000 ,
826+ 200 ,
827+ 1 ,
828+ 1 ,
829+ user,
830+ ) ) ;
831+
832+ let amount = 49 ;
833+ let quoted_price = 1 ;
834+ assert_eq ! (
835+ AssetConversion :: quote_price_tokens_for_exact_tokens( token_2, token_1, amount, true , ) ,
836+ Some ( quoted_price)
837+ ) ;
838+
839+ assert_ok ! ( Assets :: mint( RuntimeOrigin :: signed( user) , 2 , user2, amount) ) ;
840+ let prior_dot_balance = 20000 ;
841+ assert_eq ! ( prior_dot_balance, balance( user2, token_1) ) ;
842+ let prior_asset_balance = 49 ;
843+ assert_eq ! ( prior_asset_balance, balance( user2, token_2) ) ;
844+ assert_ok ! ( AssetConversion :: swap_tokens_for_exact_tokens(
845+ RuntimeOrigin :: signed( user2) ,
846+ bvec![ token_2, token_1] ,
847+ amount,
848+ 1 ,
849+ user2,
850+ false ,
851+ ) ) ;
852+
853+ assert_eq ! ( prior_dot_balance + amount, balance( user2, token_1) ) ;
854+ assert_eq ! ( prior_asset_balance - quoted_price, balance( user2, token_2) ) ;
594855 } ) ;
595856}
596857
0 commit comments