Skip to content

Commit 3b94cab

Browse files
committed
support native asset && comment orders with TF service
1 parent 3d6c320 commit 3b94cab

File tree

2 files changed

+159
-162
lines changed

2 files changed

+159
-162
lines changed
Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
11
import 'package:stellar_client/stellar_client.dart';
22

33
void main() async {
4-
// final stellarClient = Client.create(NetworkType.PUBLIC);
5-
// await stellarClient.activateThroughThreefoldService();
4+
final stellarClient = Client.create(NetworkType.PUBLIC);
5+
await stellarClient.activateThroughThreefoldService();
66

7-
// await stellarClient.transfer(
8-
// destinationAddress: "destination-public-key",
9-
// amount: "20",
10-
// currency: "TFT",
11-
// memoText: "Memo Text");
7+
await stellarClient.transfer(
8+
destinationAddress: "destination-public-key",
9+
amount: "20",
10+
currency: "TFT",
11+
memoText: "Memo Text");
1212

13-
// await stellarClient.getTransactions();
14-
15-
final stellarClient = Client(NetworkType.PUBLIC,
16-
"SDSVD7L2OZXHW2PLARRJ34ZRJVBPMJWKVG4HNIEJKFHI35E3D3WWAUL4");
17-
print(stellarClient.accountId);
18-
print(stellarClient.secretSeed);
19-
20-
// final response = await stellarClient.createOrderThroughThreefoldService(
21-
// sellingAsset: 'TFT', buyingAsset: 'USDC', amount: '5', price: '0.9');
22-
23-
// // await stellarClient.addTrustLineThroughThreefoldService('USDC');
24-
await stellarClient.createOrderThroughThreefoldService(
25-
sellingAsset: 'TFT', buyingAsset: 'USDC', amount: '3', price: '1');
26-
final balances = await stellarClient.getBalance();
27-
print(balances);
28-
// // final orderbook = await stellarClient.getOrderBook(
29-
// // sellingAssetCode: 'TFT', buyingAssetCode: 'USDC');
30-
// await stellarClient.listMyOffers();
31-
// await stellarClient.getOrderBook(
32-
// sellingAssetCode: 'USDC', buyingAssetCode: 'TFT');
33-
34-
// final stellarClient = Client(NetworkType.TESTNET,
35-
// "SA7C4BONCZUVAMPOAGKJMKIHN4FQYLRF6KVFEFUDQS6EBIOCLLNKQQCY");
36-
// print(stellarClient.accountId);
37-
// print(stellarClient.secretSeed);
38-
//SCG2NAYKV2AS4N3INA3DMFXKLMBAOH2MN2TA3FSGF6FUUJXJQVDYPXRN
39-
// await stellarClient.activateThroughFriendBot(
40-
// accountId: stellarClient.accountId);
41-
42-
// await stellarClient.addTrustLine();
43-
// final balances = await stellarClient.getBalance();
44-
// print(balances);
45-
46-
// await stellarClient.createOrder(
47-
// sellingAsset: 'TFT', buyingAsset: 'USDC', amount: '3', price: '1');
48-
// await stellarClient.listMyOffers();
49-
// final res = await stellarClient.cancelOrder(
50-
// sellingAsset: 'TFT', buyingAsset: 'USDC', offerId: '12910');
51-
// print(res.success);
52-
// await stellarClient.listMyOffers();
13+
await stellarClient.getTransactions();
5314
}

packages/stellar_client/lib/src/client.dart

Lines changed: 151 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -491,19 +491,32 @@ class Client {
491491
required String amount,
492492
required String price,
493493
String? memo}) async {
494-
if (!_currencies.currencies.containsKey(sellingAsset)) {
494+
if (!_currencies.currencies.containsKey(sellingAsset) ||
495+
sellingAsset == 'XLM') {
495496
throw Exception('Sell asset $sellingAsset is not available.');
496497
}
497-
if (!_currencies.currencies.containsKey(buyingAsset)) {
498+
if (!_currencies.currencies.containsKey(buyingAsset) ||
499+
buyingAsset == 'XLM') {
498500
throw Exception('Buy asset $buyingAsset is not available.');
499501
}
500502

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+
}
507520

508521
final ManageBuyOfferOperation buyOfferOperation =
509522
ManageBuyOfferOperationBuilder(sellAsset, buyAsset, amount, price)
@@ -547,10 +560,12 @@ class Client {
547560
required String buyingAsset,
548561
required String offerId,
549562
String? memo}) async {
550-
if (!_currencies.currencies.containsKey(sellingAsset)) {
563+
if (!_currencies.currencies.containsKey(sellingAsset) ||
564+
sellingAsset == 'XLM') {
551565
throw Exception('Sell asset $sellingAsset is not available.');
552566
}
553-
if (!_currencies.currencies.containsKey(buyingAsset)) {
567+
if (!_currencies.currencies.containsKey(buyingAsset) ||
568+
buyingAsset == 'XLM') {
554569
throw Exception('Buy asset $buyingAsset is not available.');
555570
}
556571

@@ -561,12 +576,23 @@ class Client {
561576
'Offer with ID $offerId not found in user\'s account.'),
562577
);
563578

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+
}
570596

571597
final ManageBuyOfferOperation cancelOfferOperation =
572598
ManageBuyOfferOperationBuilder(sellAsset, buyAsset, '0', '1')
@@ -591,26 +617,36 @@ class Client {
591617
}
592618
}
593619

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-
598620
Future<void> getOrderBook(
599621
{required String sellingAssetCode,
600622
required String buyingAssetCode}) async {
601-
if (!_currencies.currencies.containsKey(sellingAssetCode)) {
623+
if (!_currencies.currencies.containsKey(sellingAssetCode) ||
624+
sellingAssetCode == 'XLM') {
602625
throw Exception('Sell asset $sellingAssetCode is not available.');
603626
}
604-
if (!_currencies.currencies.containsKey(buyingAssetCode)) {
627+
if (!_currencies.currencies.containsKey(buyingAssetCode) ||
628+
buyingAssetCode == 'XLM') {
605629
throw Exception('Buy asset $buyingAssetCode is not available.');
606630
}
607631
http.Client httpClient = http.Client();
608632
Uri serverURI = Uri.parse(_horizonServerUrls[_network.toString()]!);
633+
late final Asset sellingAsset;
634+
late final Asset buyingAsset;
609635

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+
}
614650

615651
OrderBookRequestBuilder orderBookRequest =
616652
OrderBookRequestBuilder(httpClient, serverURI)
@@ -666,93 +702,93 @@ class Client {
666702
}
667703
}
668704

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+
// }
758794
}

0 commit comments

Comments
 (0)