Skip to content

Commit

Permalink
fix issue #40
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Apr 6, 2022
1 parent fca8a14 commit 5d23c70
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
45 changes: 43 additions & 2 deletions lib/src/responses/submit_transaction_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a license that can be
// found in the LICENSE file.

import '../../stellar_flutter_sdk.dart';
import 'response.dart';
import 'dart:convert';
import '../xdr/xdr_data_io.dart';
Expand Down Expand Up @@ -69,7 +70,7 @@ class SubmitTransactionResponse extends Response {
return null;
}

if ((result.result!.results[0] as XdrOperationResult?)
if ((result.result!.results[position] as XdrOperationResult?)
?.tr!
.manageOfferResult!
.success!
Expand All @@ -79,7 +80,7 @@ class SubmitTransactionResponse extends Response {
return null;
}

return (result.result!.results[0] as XdrOperationResult)
return (result.result!.results[position] as XdrOperationResult)
.tr!
.manageOfferResult!
.success!
Expand All @@ -89,6 +90,46 @@ class SubmitTransactionResponse extends Response {
.uint64;
}

/// Helper method that returns Claimable Balance Id for CreateClaimableBalance from TransactionResult Xdr.
/// This is helpful when you need the created Claimable Balance ID to show it to the user
String? getClaimableBalanceIdIdFromResult(int position) {
if (!this.success) {
return null;
}

XdrDataInputStream xdrInputStream = XdrDataInputStream(base64Decode(this.resultXdr!));
XdrTransactionResult result;

try {
result = XdrTransactionResult.decode(xdrInputStream);
} catch (e) {
return null;
}

if (result.result!.results[position] == null) {
return null;
}

XdrOperationType? disc =
(result.result!.results[position] as XdrOperationResult).tr!.discriminant;
if (disc != XdrOperationType.CREATE_CLAIMABLE_BALANCE) {
return null;
}

if ((result.result!.results[position] as XdrOperationResult?)
?.tr!
.createClaimableBalanceResult!
.balanceID ==
null) {
return null;
}

return Util.bytesToHex((result.result!.results[0] as XdrOperationResult)
.tr!
.createClaimableBalanceResult!
.balanceID!.v0!.hash!);
}

factory SubmitTransactionResponse.fromJson(Map<String, dynamic> json) =>
SubmitTransactionResponse(
json['extras'] == null ? null : SubmitTransactionResponseExtras.fromJson(json['extras']),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/xdr/xdr_account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ class XdrCreateClaimableBalanceResult {
decoded.discriminant = discriminant;
switch (decoded.discriminant) {
case XdrCreateClaimableBalanceResultCode.CREATE_CLAIMABLE_BALANCE_SUCCESS:
XdrClaimableBalanceID.decode(stream);
decoded.balanceID = XdrClaimableBalanceID.decode(stream);
break;
default:
break;
Expand Down
40 changes: 40 additions & 0 deletions test/claimable_balance_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,44 @@ void main() {
response = await sdk.submitTransaction(transaction);
assert(response.success);
});

test('test issue 40', () async {
final network = Network.TESTNET;

final sourceKeyPair = KeyPair.fromSecretSeed(
"SAW74SJ4ERDAKW4GSL63ASQAN7EVFCI4A44JMRKPPGAP6ROGPBL6SN3J",
// GAS4N4UW4CU24AIVQIIGDW6ENUYZUVZ7Z3MH5GVYGQHVKXLJ2GBDP6RQ
);

await FriendBot.fundTestAccount(sourceKeyPair.accountId);

final createClaimableBalanceBuilder = CreateClaimableBalanceOperationBuilder(
[
Claimant(
sourceKeyPair.accountId,
Claimant.predicateNot(
Claimant.predicateBeforeRelativeTime(60),
),
)
],
Asset.NATIVE,
"100",
);

final sourceAccount = await sdk.accounts.account(
sourceKeyPair.accountId,
);

final transactionBuilder = TransactionBuilder(sourceAccount)
..addOperation(createClaimableBalanceBuilder.build());

final transaction = transactionBuilder.build()..sign(sourceKeyPair, network);

final result = await sdk.submitTransaction(transaction);
assert(result.success);

var balanceID = result.getClaimableBalanceIdIdFromResult(0);
assert(balanceID != null);
print("BALANCE ID: " + balanceID!);
});
}

0 comments on commit 5d23c70

Please sign in to comment.