diff --git a/lib/src/responses/submit_transaction_response.dart b/lib/src/responses/submit_transaction_response.dart index 23bd230..5e8beae 100644 --- a/lib/src/responses/submit_transaction_response.dart +++ b/lib/src/responses/submit_transaction_response.dart @@ -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'; @@ -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! @@ -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! @@ -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 json) => SubmitTransactionResponse( json['extras'] == null ? null : SubmitTransactionResponseExtras.fromJson(json['extras']), diff --git a/lib/src/xdr/xdr_account.dart b/lib/src/xdr/xdr_account.dart index 7858af6..ed3e074 100644 --- a/lib/src/xdr/xdr_account.dart +++ b/lib/src/xdr/xdr_account.dart @@ -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; diff --git a/test/claimable_balance_test.dart b/test/claimable_balance_test.dart index 8b968df..656448c 100644 --- a/test/claimable_balance_test.dart +++ b/test/claimable_balance_test.dart @@ -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!); + }); }