Skip to content

Commit

Permalink
add protocol 14 support
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Oct 8, 2020
1 parent c9bc26a commit ec9499d
Show file tree
Hide file tree
Showing 15 changed files with 234 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.1.0] - 09.Oct.2020.
- add protocol 14 support

## [1.0.7] - 23.Aug.2020.
- make sep-0005 functions async
- minor improvements
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

![Dart](https://img.shields.io/badge/Dart-green.svg)
![Flutter](https://img.shields.io/badge/Flutter-blue.svg)
![Supports Stellar Horizon v1.4.0](https://img.shields.io/badge/Horizon-v1.4.0-blue.svg)
![Supports Stellar Horizon v1.5.0](https://img.shields.io/badge/Horizon-v1.5.0-blue.svg)
![Supports Stellar Core v13](https://img.shields.io/badge/Core-v13-blue.svg)
![Supports Stellar Core v14](https://img.shields.io/badge/Core-v14-blue.svg)

The Soneso open source Stellar SDK for Flutter is build with Dart and provides APIs to build and sign transactions, connect and query [Horizon](https://github.com/stellar/horizon).

Expand All @@ -14,7 +13,7 @@ The Soneso open source Stellar SDK for Flutter is build with Dart and provides A
1. Add the dependency to your pubspec.yaml file:
```
dependencies:
stellar_flutter_sdk: ^1.0.7
stellar_flutter_sdk: ^1.1.0
```
2. Install it (command line or IDE):
```
Expand Down
Binary file modified documentation/sdk_api_doc.zip
Binary file not shown.
11 changes: 11 additions & 0 deletions lib/src/assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'xdr/xdr_asset.dart';
import 'key_pair.dart';
import 'util.dart';
import 'asset_type_native.dart';
import 'asset_type_credit_alphanum.dart';
import 'asset_type_credit_alphanum4.dart';
import 'asset_type_credit_alphanum12.dart';

Expand Down Expand Up @@ -59,6 +60,16 @@ abstract class Asset {
return null;
}

static String canonicalForm(Asset asset) {
if (asset is AssetTypeNative) {
return 'native';
} else if (asset is AssetTypeCreditAlphaNum) {
AssetTypeCreditAlphaNum creditAsset = asset;
return creditAsset.code + ":" + creditAsset.issuerId;
} else {
throw Exception("unsupported asset " + asset.type);
}
}
/// Generates an Asset object from a given XDR object [xdr_asset].
static Asset fromXdr(XdrAsset xdrAsset) {
switch (xdrAsset.discriminant) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/claimant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Claimant {
XdrClaimPredicateType.CLAIM_PREDICATE_BEFORE_RELATIVE_TIME;
XdrInt64 i = XdrInt64();
i.int64 = seconds;
pred.absBefore = i;
pred.relBefore = i;
return pred;
}

Expand Down
20 changes: 19 additions & 1 deletion lib/src/requests/claimable_balance_request_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:http/http.dart' as http;
import '../assets.dart';
import '../responses/claimable_balance_response.dart';
import 'dart:async';
import '../responses/response.dart';
Expand All @@ -11,6 +12,8 @@ import 'request_builder.dart';
/// See <a href="https://developers.stellar.org/api/resources/claimablebalances/" target="_blank">Claimable Balance</a>
class ClaimableBalancesRequestBuilder extends RequestBuilder {
static const String SPONSOR_PARAMETER_NAME = "sponsor";
static const String CLAIMANT_PARAMETER_NAME = "claimant";
static const String ASSET_PARAMETER_NAME = "asset";

ClaimableBalancesRequestBuilder(http.Client httpClient, Uri serverURI)
: super(httpClient, serverURI, ["claimable_balances"]);
Expand All @@ -31,7 +34,7 @@ class ClaimableBalancesRequestBuilder extends RequestBuilder {

/// Requests details about the claimable balance to fetch by [balanceId].
/// See <a href="https://developers.stellar.org/api/resources/claimablebalances/" target="_blank">Claimable Balances</a>
Future<ClaimableBalanceResponse> account(String balanceId) {
Future<ClaimableBalanceResponse> forBalanceId(String balanceId) {
this.setSegments(["claimable_balances", balanceId]);
return this.claimableBalance(this.buildUri());
}
Expand All @@ -43,10 +46,25 @@ class ClaimableBalancesRequestBuilder extends RequestBuilder {
return this;
}

/// Returns all claimable balances for the accountId of a claimant.
/// See: <a href="https://developers.stellar.org/api/resources/accounts/" target="_blank">Claimable Balances</a>
ClaimableBalancesRequestBuilder forClaimant(String claimantAccountId) {
queryParameters.addAll({CLAIMANT_PARAMETER_NAME: claimantAccountId});
return this;
}

/// Returns all claimable balances for an asset.
/// See: <a href="https://developers.stellar.org/api/resources/accounts/" target="_blank">Claimable Balances</a>
ClaimableBalancesRequestBuilder forAsset(Asset asset) {
queryParameters.addAll({ASSET_PARAMETER_NAME: Asset.canonicalForm(asset)});
return this;
}

/// Requests specific uri and returns Page of ClaimableBalanceResponse.
/// This method is helpful for getting the next set of results.
static Future<Page<ClaimableBalanceResponse>> requestExecute(
http.Client httpClient, Uri uri) async {
print(uri.toString());
TypeToken type = new TypeToken<Page<ClaimableBalanceResponse>>();
ResponseHandler<Page<ClaimableBalanceResponse>> responseHandler =
new ResponseHandler<Page<ClaimableBalanceResponse>>(type);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/responses/claimable_balance_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Use of this source code is governed by a license that can be
// found in the LICENSE file.

import 'package:stellar_flutter_sdk/src/responses/asset_response.dart';
import '../assets.dart';
import 'response.dart';

class ClaimableBalanceResponse extends Response {
String balanceId;
AssetResponse asset;
Asset asset;
String amount;
String sponsor;
int lastModifiedLedger;
Expand All @@ -30,7 +30,7 @@ class ClaimableBalanceResponse extends Response {
json['id'] as String,
json['asset'] == null
? null
: new AssetResponse.fromJson(json['asset'] as Map<String, dynamic>),
: Asset.createFromCanonicalForm(json['asset'] as String),
json['amount'] as String,
json['sponsor'] as String,
convertInt(json['last_modified_ledger']),
Expand Down
5 changes: 5 additions & 0 deletions lib/src/responses/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:http/http.dart' as http;
import 'package:stellar_flutter_sdk/src/responses/claimable_balance_response.dart';
import 'dart:async';
import '../util.dart';
import '../requests/request_builder.dart';
Expand Down Expand Up @@ -185,6 +186,8 @@ class ResponseConverter {
return TransactionResponse.fromJson(json);
case FederationResponse:
return FederationResponse.fromJson(json);
case ClaimableBalanceResponse:
return ClaimableBalanceResponse.fromJson(json);
}

switch (T.toString()) {
Expand Down Expand Up @@ -216,6 +219,8 @@ class ResponseConverter {
return Page<TradeResponse>.fromJson(json);
case "Page<TransactionResponse>":
return Page<TransactionResponse>.fromJson(json);
case "Page<ClaimableBalanceResponse>":
return Page<ClaimableBalanceResponse>.fromJson(json);
}
}
}
18 changes: 9 additions & 9 deletions lib/src/revoke_sponsorship_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ class RevokeSponsorshipOperation extends Operation {
if (_ledgerKey != null) {
op.discriminant =
XdrRevokeSponsorshipType.REVOKE_SPONSORSHIP_LEDGER_ENTRY;
op.ledgerKey = this._ledgerKey;
} else {
op.discriminant = XdrRevokeSponsorshipType.REVOKE_SPONSORSHIP_SIGNER;
}

XdrAccountID signerId = XdrAccountID();
signerId.accountID =
KeyPair.fromAccountId(this._signerAccountId).xdrPublicKey;
XdrAccountID signerId = XdrAccountID();
signerId.accountID =
KeyPair.fromAccountId(this._signerAccountId).xdrPublicKey;

XdrRevokeSponsorshipSigner signer = XdrRevokeSponsorshipSigner();
signer.accountId = signerId;
signer.signerKey = _signerKey;
XdrRevokeSponsorshipSigner signer = XdrRevokeSponsorshipSigner();
signer.accountId = signerId;
signer.signerKey = _signerKey;

op.signer = signer;
op.signer = signer;
}

XdrOperationBody body = XdrOperationBody();
body.discriminant = XdrOperationType.REVOKE_SPONSORSHIP;
Expand Down
4 changes: 4 additions & 0 deletions lib/src/stellar_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

import 'package:http/http.dart' as http;
import 'package:stellar_flutter_sdk/src/requests/claimable_balance_request_builder.dart';
import 'dart:async';
import 'dart:convert';
import 'assets.dart';
Expand Down Expand Up @@ -95,6 +96,9 @@ class StellarSDK {
TradesRequestBuilder get trades =>
new TradesRequestBuilder(httpClient, _serverURI);

ClaimableBalancesRequestBuilder get claimableBalances =>
new ClaimableBalancesRequestBuilder(httpClient, _serverURI);

/// Returns TradeAggregationsRequestBuilder instance.
TradeAggregationsRequestBuilder tradeAggregations(
Asset baseAsset,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/xdr/xdr_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,11 @@ class XdrRevokeSponsorshipOp {

XdrLedgerKey _ledgerKey;
XdrLedgerKey get ledgerKey => this._ledgerKey;
set ledgerKey(XdrLedgerKey value) => this.ledgerKey = value;
set ledgerKey(XdrLedgerKey value) => this._ledgerKey = value;

XdrRevokeSponsorshipSigner _signer;
XdrRevokeSponsorshipSigner get signer => this._signer;
set signer(XdrRevokeSponsorshipSigner value) => this.signer = value;
set signer(XdrRevokeSponsorshipSigner value) => this._signer = value;

static void encode(
XdrDataOutputStream stream, XdrRevokeSponsorshipOp encoded) {
Expand Down
11 changes: 8 additions & 3 deletions lib/src/xdr/xdr_ledger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class XdrClaimPredicate {

XdrClaimPredicate _notPredicate;
XdrClaimPredicate get notPredicate => this._notPredicate;
set notPredicate(XdrClaimPredicate value) => this.notPredicate = value;
set notPredicate(XdrClaimPredicate value) => this._notPredicate = value;

XdrInt64 _absBefore; // Predicate will be true if closeTime < absBefore
XdrInt64 get absBefore => this._absBefore;
Expand Down Expand Up @@ -178,7 +178,12 @@ class XdrClaimPredicate {
}
break;
case XdrClaimPredicateType.CLAIM_PREDICATE_NOT:
XdrClaimPredicate.encode(stream, encodedClaimPredicate.notPredicate);
if (encodedClaimPredicate.notPredicate != null) {
stream.writeInt(1);
XdrClaimPredicate.encode(stream, encodedClaimPredicate.notPredicate);
} else {
stream.writeInt(0);
}
break;
case XdrClaimPredicateType.CLAIM_PREDICATE_BEFORE_ABSOLUTE_TIME:
XdrInt64.encode(stream, encodedClaimPredicate.absBefore);
Expand Down Expand Up @@ -256,7 +261,7 @@ class XdrClaimant {

XdrClaimantV0 _v0;
XdrClaimantV0 get v0 => this._v0;
set v0(XdrClaimantV0 value) => this.v0 = value;
set v0(XdrClaimantV0 value) => this._v0 = value;

static void encode(XdrDataOutputStream stream, XdrClaimant encodedClaimant) {
stream.writeInt(encodedClaimant.discriminant.value);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: stellar_flutter_sdk
description: A stellar blockchain sdk that query's horizon, build, signs and submits transactions to the stellar netweok.
version: 1.0.7
version: 1.1.0
homepage: https://github.com/Soneso/stellar_flutter_sdk

environment:
Expand Down
67 changes: 67 additions & 0 deletions test/claimable_balance_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';

void main() {
StellarSDK sdk = StellarSDK.TESTNET;

test('test claimable balance', () async {
KeyPair sourceAccountKeyxPair = KeyPair.random();
String sourceAccountId = sourceAccountKeyxPair.accountId;
await FriendBot.fundTestAccount(sourceAccountId);
AccountResponse sourceAccount = await sdk.accounts.account(sourceAccountId);

KeyPair firstClaimantKp = KeyPair.random();
String fistClaimantId = firstClaimantKp.accountId;
KeyPair secondClaimantKp = KeyPair.random();
Claimant firstClaimant =
Claimant(fistClaimantId, Claimant.predicateUnconditional());
XdrClaimPredicate predicateA = Claimant.predicateBeforeRelativeTime(100);
XdrClaimPredicate predicateB =
Claimant.predicateBeforeAbsoluteTime(1634000400);
XdrClaimPredicate predicateC = Claimant.predicateNot(predicateA);
XdrClaimPredicate predicateD =
Claimant.predicateAnd(predicateC, predicateB);
XdrClaimPredicate predicateE =
Claimant.predicateBeforeAbsoluteTime(1601671345);
XdrClaimPredicate predicateF = Claimant.predicateOr(predicateD, predicateE);
Claimant secondClaimant = Claimant(secondClaimantKp.accountId, predicateF);
List<Claimant> claimants = List<Claimant>();
claimants.add(firstClaimant);
claimants.add(secondClaimant);
CreateClaimableBalanceOperationBuilder opb =
CreateClaimableBalanceOperationBuilder(
claimants, Asset.NATIVE, "12.33");

Transaction transaction = new TransactionBuilder(sourceAccount)
.addOperation(opb.build())
.addMemo(Memo.text("createclaimablebalance"))
.build();

transaction.sign(sourceAccountKeyxPair, Network.TESTNET);

SubmitTransactionResponse response =
await sdk.submitTransaction(transaction);
assert(response.success);

Page<ClaimableBalanceResponse> claimableBalances = await sdk
.claimableBalances
.forClaimant(firstClaimantKp.accountId)
.execute();
assert(claimableBalances.records.length == 1);
ClaimableBalanceResponse cb = claimableBalances.records[0];
await FriendBot.fundTestAccount(fistClaimantId);

ClaimClaimableBalanceOperationBuilder opc =
ClaimClaimableBalanceOperationBuilder(cb.balanceId);

transaction = new TransactionBuilder(sourceAccount)
.addOperation(opb.build())
.addMemo(Memo.text("claimclaimablebalance"))
.build();

transaction.sign(sourceAccountKeyxPair, Network.TESTNET);

response = await sdk.submitTransaction(transaction);
assert(response.success);
});
}
Loading

0 comments on commit ec9499d

Please sign in to comment.