Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use muxed account as feeSource #1

Merged
merged 1 commit into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions lib/src/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import "dart:convert";
import 'dart:typed_data';
import 'package:stellar_flutter_sdk/src/muxed_account.dart';

import 'key_pair.dart';
import 'memo.dart';
import 'network.dart';
Expand Down Expand Up @@ -403,14 +405,14 @@ class TransactionBuilder {
/// Represents <a href="https://github.com/stellar/stellar-protocol/blob/master/core/cap-0015.md" target="_blank">Fee Bump Transaction</a> in Stellar network.
class FeeBumpTransaction extends AbstractTransaction {
int _mFee;
String _mFeeAccount;
MuxedAccount _mFeeAccount;
Transaction _mInner;

int get fee => this._mFee;
String get feeAccount => this._mFeeAccount;
MuxedAccount get feeAccount => this._mFeeAccount;
Transaction get innerTransaction => this._mInner;

FeeBumpTransaction(String feeAccount, int fee, Transaction innerTransaction)
FeeBumpTransaction(MuxedAccount feeAccount, int fee, Transaction innerTransaction)
: super(innerTransaction.network) {
_mFeeAccount = checkNotNull(feeAccount, "feeAccount cannot be null");
_mFee = checkNotNull(fee, "fee cannot be null");
Expand All @@ -421,10 +423,8 @@ class FeeBumpTransaction extends AbstractTransaction {
XdrFeeBumpTransactionEnvelope envelope, Network network) {
Transaction inner =
Transaction.fromV1EnvelopeXdr(envelope.tx.innerTx.v1, network);
String feeAccount =
KeyPair.fromPublicKey(envelope.tx.feeSource.ed25519.uint256).accountId;
int fee = envelope.tx.fee.int64;
FeeBumpTransaction feeBump = FeeBumpTransaction(feeAccount, fee, inner);
FeeBumpTransaction feeBump = FeeBumpTransaction(MuxedAccount.fromXdr(envelope.tx.feeSource), fee, inner);
return feeBump;
}

Expand Down Expand Up @@ -457,11 +457,7 @@ class FeeBumpTransaction extends AbstractTransaction {
xdrFee.int64 = _mFee;
xdr.fee = xdrFee;

XdrMuxedAccount feeSrc = XdrMuxedAccount();
feeSrc.discriminant = XdrCryptoKeyType.KEY_TYPE_ED25519;
feeSrc.ed25519 =
KeyPair.fromAccountId(_mFeeAccount).xdrPublicKey.getEd25519();
xdr.feeSource = feeSrc;
xdr.feeSource = _mFeeAccount.toXdr();

XdrFeeBumpTransactionInnerTx innerXDR = XdrFeeBumpTransactionInnerTx();
innerXDR.discriminant = XdrEnvelopeType.ENVELOPE_TYPE_TX;
Expand Down Expand Up @@ -500,7 +496,7 @@ class FeeBumpTransaction extends AbstractTransaction {
class FeeBumpTransactionBuilder {
Transaction _mInner;
int _mBaseFee;
String _mFeeAccount;
MuxedAccount _mFeeAccount;

/// Construct a new fee bump transaction builder.
FeeBumpTransactionBuilder(Transaction inner) {
Expand Down Expand Up @@ -554,6 +550,15 @@ class FeeBumpTransactionBuilder {
}

FeeBumpTransactionBuilder setFeeAccount(String feeAccount) {
if (_mFeeAccount != null) {
throw new Exception("fee account has been already been set.");
}
checkNotNull(feeAccount, "feeAccount cannot be null");
_mFeeAccount = MuxedAccount(feeAccount, null);
return this;
}

FeeBumpTransactionBuilder setMuxedFeeAccount(MuxedAccount feeAccount) {
if (_mFeeAccount != null) {
throw new Exception("fee account has been already been set.");
}
Expand Down
59 changes: 59 additions & 0 deletions test/fee_bump_transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,63 @@ void main() {
await sdk.transactions.transaction(transaction.innerTransaction.hash);
assert(transaction.sourceAccount == sourceId);
});

test('submit fee bump transaction - muxed accounts', () async {
KeyPair sourceKeyPair = KeyPair.random();
String sourceId = sourceKeyPair.accountId;
KeyPair destinationKeyPair = KeyPair.random();
String destinationId = destinationKeyPair.accountId;
KeyPair payerKeyPair = KeyPair.random();
String payerId = payerKeyPair.accountId;

await FriendBot.fundTestAccount(sourceId);
await FriendBot.fundTestAccount(payerId);

MuxedAccount muxedSourceAccount = MuxedAccount(sourceId, 97839283928292);
MuxedAccount muxedPayerAccount = MuxedAccount(payerId, 24242423737333);

AccountResponse sourceAccount = await sdk.accounts.account(sourceId);

// fund account C.
Transaction innerTx = new TransactionBuilder(sourceAccount, Network.TESTNET)
.addOperation(
new CreateAccountOperationBuilder(destinationId, "10").setMuxedSourceAccount(muxedSourceAccount).build())
.build();

innerTx.sign(sourceKeyPair);

FeeBumpTransaction feeBump = new FeeBumpTransactionBuilder(innerTx)
.setBaseFee(200)
.setMuxedFeeAccount(muxedPayerAccount)
.build();
feeBump.sign(payerKeyPair);

SubmitTransactionResponse response =
await sdk.submitFeeBumpTransaction(feeBump);
assert(response.success);
print(response.hash);

bool found = false;
AccountResponse destination = await sdk.accounts.account(destinationId);
for (Balance balance in destination.balances) {
if (balance.assetType == Asset.TYPE_NATIVE) {
assert(double.parse(balance.balance) > 9);
found = true;
break;
}
}

assert(found);

TransactionResponse transaction =
await sdk.transactions.transaction(response.hash);
assert(transaction.feeBumpTransaction != null);
assert(transaction.feeBumpTransaction.signatures.length > 0);
assert(transaction.innerTransaction.hash != null);
assert(transaction.innerTransaction.maxFee == 100);

transaction =
await sdk.transactions.transaction(transaction.innerTransaction.hash);
assert(transaction.sourceAccount == sourceId);
});
}