@@ -491,19 +491,32 @@ class Client {
491
491
required String amount,
492
492
required String price,
493
493
String ? memo}) async {
494
- if (! _currencies.currencies.containsKey (sellingAsset)) {
494
+ if (! _currencies.currencies.containsKey (sellingAsset) ||
495
+ sellingAsset == 'XLM' ) {
495
496
throw Exception ('Sell asset $sellingAsset is not available.' );
496
497
}
497
- if (! _currencies.currencies.containsKey (buyingAsset)) {
498
+ if (! _currencies.currencies.containsKey (buyingAsset) ||
499
+ buyingAsset == 'XLM' ) {
498
500
throw Exception ('Buy asset $buyingAsset is not available.' );
499
501
}
500
502
501
- final Asset sellAsset = AssetTypeCreditAlphaNum4 (
502
- _currencies.currencies[sellingAsset]! .assetCode,
503
- _currencies.currencies[sellingAsset]! .issuer);
504
- final Asset buyAsset = AssetTypeCreditAlphaNum4 (
505
- _currencies.currencies[buyingAsset]! .assetCode,
506
- _currencies.currencies[buyingAsset]! .issuer);
503
+ late final Asset sellAsset;
504
+ late final Asset buyAsset;
505
+
506
+ if (sellingAsset == 'XLM' ) {
507
+ sellAsset = AssetTypeNative ();
508
+ } else {
509
+ sellAsset = AssetTypeCreditAlphaNum4 (
510
+ _currencies.currencies[sellingAsset]! .assetCode,
511
+ _currencies.currencies[sellingAsset]! .issuer);
512
+ }
513
+ if (sellingAsset == 'XLM' ) {
514
+ buyAsset = AssetTypeNative ();
515
+ } else {
516
+ buyAsset = AssetTypeCreditAlphaNum4 (
517
+ _currencies.currencies[buyingAsset]! .assetCode,
518
+ _currencies.currencies[buyingAsset]! .issuer);
519
+ }
507
520
508
521
final ManageBuyOfferOperation buyOfferOperation =
509
522
ManageBuyOfferOperationBuilder (sellAsset, buyAsset, amount, price)
@@ -547,10 +560,12 @@ class Client {
547
560
required String buyingAsset,
548
561
required String offerId,
549
562
String ? memo}) async {
550
- if (! _currencies.currencies.containsKey (sellingAsset)) {
563
+ if (! _currencies.currencies.containsKey (sellingAsset) ||
564
+ sellingAsset == 'XLM' ) {
551
565
throw Exception ('Sell asset $sellingAsset is not available.' );
552
566
}
553
- if (! _currencies.currencies.containsKey (buyingAsset)) {
567
+ if (! _currencies.currencies.containsKey (buyingAsset) ||
568
+ buyingAsset == 'XLM' ) {
554
569
throw Exception ('Buy asset $buyingAsset is not available.' );
555
570
}
556
571
@@ -561,12 +576,23 @@ class Client {
561
576
'Offer with ID $offerId not found in user\' s account.' ),
562
577
);
563
578
564
- final Asset sellAsset = AssetTypeCreditAlphaNum4 (
565
- _currencies.currencies[sellingAsset]! .assetCode,
566
- _currencies.currencies[sellingAsset]! .issuer);
567
- final Asset buyAsset = AssetTypeCreditAlphaNum4 (
568
- _currencies.currencies[buyingAsset]! .assetCode,
569
- _currencies.currencies[buyingAsset]! .issuer);
579
+ late final Asset sellAsset;
580
+ late final Asset buyAsset;
581
+
582
+ if (sellingAsset == 'XLM' ) {
583
+ sellAsset = AssetTypeNative ();
584
+ } else {
585
+ sellAsset = AssetTypeCreditAlphaNum4 (
586
+ _currencies.currencies[sellingAsset]! .assetCode,
587
+ _currencies.currencies[sellingAsset]! .issuer);
588
+ }
589
+ if (sellingAsset == 'XLM' ) {
590
+ buyAsset = AssetTypeNative ();
591
+ } else {
592
+ buyAsset = AssetTypeCreditAlphaNum4 (
593
+ _currencies.currencies[buyingAsset]! .assetCode,
594
+ _currencies.currencies[buyingAsset]! .issuer);
595
+ }
570
596
571
597
final ManageBuyOfferOperation cancelOfferOperation =
572
598
ManageBuyOfferOperationBuilder (sellAsset, buyAsset, '0' , '1' )
@@ -591,26 +617,36 @@ class Client {
591
617
}
592
618
}
593
619
594
- // native --> Represents the Stellar native asset (XLM)
595
- // credit_alphanum4: Represents a credit asset with a 4-character code.
596
- // credit_alphanum12: Represents a credit asset with a 12-character code
597
-
598
620
Future <void > getOrderBook (
599
621
{required String sellingAssetCode,
600
622
required String buyingAssetCode}) async {
601
- if (! _currencies.currencies.containsKey (sellingAssetCode)) {
623
+ if (! _currencies.currencies.containsKey (sellingAssetCode) ||
624
+ sellingAssetCode == 'XLM' ) {
602
625
throw Exception ('Sell asset $sellingAssetCode is not available.' );
603
626
}
604
- if (! _currencies.currencies.containsKey (buyingAssetCode)) {
627
+ if (! _currencies.currencies.containsKey (buyingAssetCode) ||
628
+ buyingAssetCode == 'XLM' ) {
605
629
throw Exception ('Buy asset $buyingAssetCode is not available.' );
606
630
}
607
631
http.Client httpClient = http.Client ();
608
632
Uri serverURI = Uri .parse (_horizonServerUrls[_network.toString ()]! );
633
+ late final Asset sellingAsset;
634
+ late final Asset buyingAsset;
609
635
610
- Asset sellingAsset = Asset .createNonNativeAsset (
611
- sellingAssetCode, _currencies.currencies[sellingAssetCode]! .issuer);
612
- Asset buyingAsset = Asset .createNonNativeAsset (
613
- buyingAssetCode, _currencies.currencies[buyingAssetCode]! .issuer);
636
+ if (sellingAssetCode == 'XLM' ) {
637
+ sellingAsset = AssetTypeNative ();
638
+ } else {
639
+ sellingAsset = AssetTypeCreditAlphaNum4 (
640
+ _currencies.currencies[sellingAssetCode]! .assetCode,
641
+ _currencies.currencies[sellingAssetCode]! .issuer);
642
+ }
643
+ if (buyingAssetCode == 'XLM' ) {
644
+ buyingAsset = AssetTypeNative ();
645
+ } else {
646
+ buyingAsset = AssetTypeCreditAlphaNum4 (
647
+ _currencies.currencies[buyingAssetCode]! .assetCode,
648
+ _currencies.currencies[buyingAssetCode]! .issuer);
649
+ }
614
650
615
651
OrderBookRequestBuilder orderBookRequest =
616
652
OrderBookRequestBuilder (httpClient, serverURI)
@@ -666,93 +702,93 @@ class Client {
666
702
}
667
703
}
668
704
669
- Future <Transaction ?> _buildOrderTransaction (
670
- {required String sellingAsset,
671
- required String buyingAsset,
672
- required String amount,
673
- required String price,
674
- String ? memo,
675
- required bool funded}) async {
676
- if (! _currencies.currencies.containsKey (sellingAsset)) {
677
- throw Exception ('Sell asset $sellingAsset is not available.' );
678
- }
679
- if (! _currencies.currencies.containsKey (buyingAsset)) {
680
- throw Exception ('Buy asset $buyingAsset is not available.' );
681
- }
682
-
683
- final Asset sellAsset = AssetTypeCreditAlphaNum4 (
684
- _currencies.currencies[sellingAsset]! .assetCode,
685
- _currencies.currencies[sellingAsset]! .issuer);
686
- final Asset buyAsset = AssetTypeCreditAlphaNum4 (
687
- _currencies.currencies[buyingAsset]! .assetCode,
688
- _currencies.currencies[buyingAsset]! .issuer);
689
-
690
- final ManageBuyOfferOperation buyOfferOperation =
691
- ManageBuyOfferOperationBuilder (sellAsset, buyAsset, amount, price)
692
- .build ();
693
-
694
- final account = await _sdk.accounts.account (accountId);
695
-
696
- final balances = account.balances;
697
- final sellAssetBalance = balances.firstWhere (
698
- (balance) => balance.assetCode == sellingAsset,
699
- orElse: () => throw Exception ('Insufficient balance in $sellingAsset ' ),
700
- );
701
-
702
- final double sellAmount = double .parse (amount);
703
- final double availableBalance = double .parse (sellAssetBalance.balance);
704
- if (sellAmount > availableBalance) {
705
- throw Exception (
706
- 'Insufficient balance in $sellingAsset . Available: $availableBalance ' );
707
- }
708
- Transaction ? transaction;
709
- if (funded) {
710
- Operation ? operation = await _makeFundPaymentOperation (
711
- assetCode: sellingAsset,
712
- issuer: _currencies.currencies[sellingAsset]! .issuer);
713
- transaction = TransactionBuilder (account)
714
- .addOperation (operation ! )
715
- .addOperation (buyOfferOperation)
716
- .addMemo (memo != null ? Memo .text (memo) : Memo .none ())
717
- .build ();
718
- } else {
719
- transaction = TransactionBuilder (account)
720
- .addOperation (buyOfferOperation)
721
- .addMemo (memo != null ? Memo .text (memo) : Memo .none ())
722
- .build ();
723
- }
724
- return transaction;
725
- }
726
-
727
- Future <void > createOrderThroughThreefoldService (
728
- {required String sellingAsset,
729
- required String buyingAsset,
730
- required String amount,
731
- required String price,
732
- String ? memo}) async {
733
- Transaction ? fundedOrder = await _buildOrderTransaction (
734
- sellingAsset: sellingAsset,
735
- buyingAsset: buyingAsset,
736
- amount: amount,
737
- price: price,
738
- funded: true );
739
-
740
- fundedOrder! .sign (_keyPair, _stellarNetwork);
741
-
742
- print ('Sending to' );
743
- print (
744
- '${_serviceUrls [_network .toString ()]}/transactionfunding_service/fund_transaction' );
745
- try {
746
- final response = await http.post (
747
- Uri .parse (
748
- '${_serviceUrls [_network .toString ()]}/transactionfunding_service/fund_transaction' ),
749
- headers: {'Content-Type' : 'application/json' },
750
- body: jsonEncode ({'transaction' : fundedOrder.toEnvelopeXdrBase64 ()}),
751
- );
752
-
753
- print (response.body);
754
- } catch (error) {
755
- throw Exception ('Something went wrong! $error ' );
756
- }
757
- }
705
+ // Future<Transaction?> _buildOrderTransaction(
706
+ // {required String sellingAsset,
707
+ // required String buyingAsset,
708
+ // required String amount,
709
+ // required String price,
710
+ // String? memo,
711
+ // required bool funded}) async {
712
+ // if (!_currencies.currencies.containsKey(sellingAsset)) {
713
+ // throw Exception('Sell asset $sellingAsset is not available.');
714
+ // }
715
+ // if (!_currencies.currencies.containsKey(buyingAsset)) {
716
+ // throw Exception('Buy asset $buyingAsset is not available.');
717
+ // }
718
+
719
+ // final Asset sellAsset = AssetTypeCreditAlphaNum4(
720
+ // _currencies.currencies[sellingAsset]!.assetCode,
721
+ // _currencies.currencies[sellingAsset]!.issuer);
722
+ // final Asset buyAsset = AssetTypeCreditAlphaNum4(
723
+ // _currencies.currencies[buyingAsset]!.assetCode,
724
+ // _currencies.currencies[buyingAsset]!.issuer);
725
+
726
+ // final ManageBuyOfferOperation buyOfferOperation =
727
+ // ManageBuyOfferOperationBuilder(sellAsset, buyAsset, amount, price)
728
+ // .build();
729
+
730
+ // final account = await _sdk.accounts.account(accountId);
731
+
732
+ // final balances = account.balances;
733
+ // final sellAssetBalance = balances.firstWhere(
734
+ // (balance) => balance.assetCode == sellingAsset,
735
+ // orElse: () => throw Exception('Insufficient balance in $sellingAsset'),
736
+ // );
737
+
738
+ // final double sellAmount = double.parse(amount);
739
+ // final double availableBalance = double.parse(sellAssetBalance.balance);
740
+ // if (sellAmount > availableBalance) {
741
+ // throw Exception(
742
+ // 'Insufficient balance in $sellingAsset. Available: $availableBalance');
743
+ // }
744
+ // Transaction? transaction;
745
+ // if (funded) {
746
+ // // Operation? operation = await _makeFundPaymentOperation(
747
+ // // assetCode: sellingAsset,
748
+ // // issuer: _currencies.currencies[sellingAsset]!.issuer);
749
+ // transaction = TransactionBuilder(account)
750
+ // .addOperation(buyOfferOperation )
751
+ // .addOperation(buyOfferOperation)
752
+ // .addMemo(memo != null ? Memo.text(memo) : Memo.none())
753
+ // .build();
754
+ // } else {
755
+ // transaction = TransactionBuilder(account)
756
+ // .addOperation(buyOfferOperation)
757
+ // .addMemo(memo != null ? Memo.text(memo) : Memo.none())
758
+ // .build();
759
+ // }
760
+ // return transaction;
761
+ // }
762
+
763
+ // Future<void> createOrderThroughThreefoldService(
764
+ // {required String sellingAsset,
765
+ // required String buyingAsset,
766
+ // required String amount,
767
+ // required String price,
768
+ // String? memo}) async {
769
+ // Transaction? fundedOrder = await _buildOrderTransaction(
770
+ // sellingAsset: sellingAsset,
771
+ // buyingAsset: buyingAsset,
772
+ // amount: amount,
773
+ // price: price,
774
+ // funded: true);
775
+
776
+ // fundedOrder!.sign(_keyPair, _stellarNetwork);
777
+
778
+ // print('Sending to');
779
+ // print(
780
+ // '${_serviceUrls[_network.toString()]}/transactionfunding_service/fund_transaction');
781
+ // try {
782
+ // final response = await http.post(
783
+ // Uri.parse(
784
+ // '${_serviceUrls[_network.toString()]}/transactionfunding_service/fund_transaction'),
785
+ // headers: {'Content-Type': 'application/json'},
786
+ // body: jsonEncode({'transaction': fundedOrder.toEnvelopeXdrBase64()}),
787
+ // );
788
+
789
+ // print(response.body);
790
+ // } catch (error) {
791
+ // throw Exception('Something went wrong! $error');
792
+ // }
793
+ // }
758
794
}
0 commit comments