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

Add FeeBumpTransaction class #328

Merged
merged 17 commits into from
Apr 17, 2020
Merged

Conversation

abuiles
Copy link
Contributor

@abuiles abuiles commented Apr 16, 2020

What

Add the class FeeBumpTransaction to represent fee bump transactions.

Why

This pull request updates some of the code introduced in #321, creating a different class to represent fee bump transaction.

While having a single class could work, long term, having different classes to separate both type of transactions is less error prone and makes the interface on fee bump transactions more explicit.

When you create a new FeeBumpTransaction you can reach the inner transaction via .innerTransaction, this returns an instance of Transaction which gives access to all the properties on the inner transaction, like the original fee, sourceAccount, operations, and memo.

@abuiles abuiles marked this pull request as ready for review April 16, 2020 02:27
@tamirms
Copy link
Contributor

tamirms commented Apr 16, 2020

if I have a xdr envelope string and I want to parse it into a Transaction or FeeBumpTransaction, how do I do that?

if I call new Transaction(myXDRString) I will get an exception if myXDRString is a fee bump transaction envelope. Similarly, if I call new FeeBumpTransaction(myXDRString) I will get an exception if myXDRString is a normal transaction envelope. The problem is I don't know which type of envelope I have unless I decode myXDRString.

It seems like we want a FromEnvelope(envelope: string | xdr.TransactionEnvelope): Transaction | FeeBumpTransaction function. But, assuming something like FromEnvelope() exists there is still the problem that most users of the library will ignore it and call new Transaction(myXDRString) since that was the way you created Transaction objects prior to v3.0.0.

Is there a way to limit access to the Transaction and FeeBumpTransaction constructors so only the builders can invoke them? Ideally, there should only be 2 ways to create a Transaction / FeeBumpTransaction instance: (1) using a builder or (2) using FromEnvelope.

We should compare the tradeoffs between this approach vs just having a single Transaction class with an innerTransaction() accessor.

@abuiles
Copy link
Contributor Author

abuiles commented Apr 16, 2020

if I have a xdr envelope string and I want to parse it into a Transaction or FeeBumpTransaction, how do I do that?

Good point! I didn't add that yet. I think we can add it in the builder so it does the right handling depending on the envelope type.

Is there a way to limit access to the Transaction and FeeBumpTransaction constructors so only the builders can invoke them? Ideally, there should only be 2 ways to create a Transaction / FeeBumpTransaction instance: (1) using a builder or (2) using FromEnvelope.

I don't think there is an out the box way to achieve this, as long as people can get a hold on the class they can create new instances. I'm actually okay by allowing people to use this directly, however, we should add a helper to the builder so people can do fromEnvelope(envelope, network).

@abuiles
Copy link
Contributor Author

abuiles commented Apr 16, 2020

@tamirms also added TransactionBuilder.fromXDR which returns a Transaction or FeeBumpTransaction

*/
export class TransactionBase {
constructor(tx, signatures, fee, networkPassphrase) {
if (typeof networkPassphrase !== 'string') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were suppose to make this change on the previous major bump but we forgot, not this is a good time.

import { Memo } from './memo';
import { Keypair } from './keypair';
import { TransactionBase } from './transaction_base';

/**
* Use {@link TransactionBuilder} to build a transaction object, unless you have
Copy link
Contributor

@tamirms tamirms Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worth adding a comment that we generally discourage creating Transaction instances manually. Instead people should be using builder.fromXdr() to construct Transaction instances. The same comment applies to FeeBumpTransaction as well.

If this were implemented in typescript I think we'd make Transaction and FeeBumpTransaction interfaces which are exported but the implementations would not be exported and only be local to the builder code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@abuiles abuiles merged commit ce7907e into release-v3.0.0 Apr 17, 2020
@abuiles abuiles deleted the add-fee-bump-transaction-class branch April 17, 2020 13:29
abuiles added a commit that referenced this pull request Apr 20, 2020
* Handle transaction envelope with MuxedAccount in sourceAccount.

* Add FeeBumpTransaction.

* Add more test for FeeBumpTransaction.

* Improve docs.

* Add TransactionBase and extend from it.

* Use named import and inherit documentation.

* Inherit TransactionBase in Transaction.

* Document readonly attrs.

* Document properties on fee bump tx.

* Show M accounts.

* Return a copy of tx and signatures when building toEnvelope.

* Add helper function fromXDR.

* Update types.

* Update XDR.

* Handle muxed accoutns in fee bump transactions.

* Add link to fromXDR in Transaction and FeeBumpTransaction docs.

* Extend fromXDR to take a xdr object or a string.
@abuiles abuiles mentioned this pull request Apr 27, 2020
abuiles added a commit that referenced this pull request Apr 27, 2020
This version brings protocol 13 support with backwards compatibility support for protocol 12.

### Add
- Add `TransactionBuilder.buildFeeBumpTransaction` which makes it easy to create `FeeBumpTransaction` ([#321](#321)).
- Adds a feature flag which allow consumers of this library to create V1 (protocol 13) transactions using the `TransactionBuilder` ([#321](#321)).
- Add support for [CAP0027](https://github.com/stellar/stellar-protocol/blob/master/core/cap-0027.md): First-class multiplexed accounts ([#325](#325)).
- Add `Keypair.xdrMuxedAccount` which creates a new `xdr.MuxedAccount`([#325](#325)).
- Add `FeeBumpTransaction` which makes it easy to work with fee bump transactions ([#328](#328)).
- Add `TransactionBuilder.fromXDR` which receives an xdr envelope and return a `Transaction` or `FeeBumpTransaction` ([#328](#328)).

### Update
- Update XDR definitions with protocol 13 ([#317](#317)).
- Extend `Transaction` to work with `TransactionV1Envelope` and `TransactionV0Envelope` ([#317](#317)).
- Add backward compatibility support for [CAP0018](https://github.com/stellar/stellar-protocol/blob/f01c9354aaab1e8ca97a25cf888829749cadf36a/core/cap-0018.md) ([#317](#317)).
- Update operations builder to support multiplexed accounts ([#337](#337)).

  This allows you to specify an `M` account as the destination or source:
  ```
  var destination = 'MAAAAAAAAAAAAAB7BQ2L7E5NBWMXDUCMZSIPOBKRDSBYVLMXGSSKF6YNPIB7Y77ITLVL6';
  var amount = '1000.0000000';
  var asset = new StellarBase.Asset(
    'USDUSD',
    'GDGU5OAPHNPU5UCLE5RDJHG7PXZFQYWKCFOEXSXNMR6KRQRI5T6XXCD7'
  );
  var source =
    'MAAAAAAAAAAAAAB7BQ2L7E5NBWMXDUCMZSIPOBKRDSBYVLMXGSSKF6YNPIB7Y77ITLVL6';
  StellarBase.Operation.payment({
    destination,
    asset,
    amount,
    source
  });
  ```

  **To use multiplexed accounts you need an instance of Stellar running on Protocol 13 or higher**

### Breaking changes

- `Transaction.toEnvelope()` returns a protocol 13 `xdr.TransactionEnvelope` which is an `xdr.Union` ([#317](#317)).
  If you have code that looks like this `transaction.toEnvelope().tx` you have two options:
    - You can grab the value wrapped by the union, calling `value()` like `transaction.toEnvelope().value().tx`.
    - You can check which is the discriminant by using `switch()` and then call `v0()`, `v1()`, or `feeBump()`.
- The return value from `Transaction.fee` changed from `number` to `string`. This brings support for `Int64` values ([#321](#321)).
- The const `BASE_FEE` changed from `number` to `string` ([#321](#321)).
- The option `fee` passed to  `new TransactionBuilder({fee: ..})` changed from `number` to `string` ([#321](#321)).
- The following fields, which were previously an `xdr.AccountID` are now a  `xdr.MuxedAccount` ([#325](#325)):
  - `PaymentOp.destination`
  - `PathPaymentStrictReceiveOp.destination`
  - `PathPaymentStrictSendOp.destination`
  - `Operation.sourceAccount`
  - `Operation.destination` (for `ACCOUNT_MERGE`)
  - `Transaction.sourceAccount`
  - `FeeBumpTransaction.feeSource`

  You can get the string representation by calling `StrKey.encodeMuxedAccount` which will return a `G..` or `M..` account.
- Remove the following deprecated functions ([#331](#331)):
  - `Operation.manageOffer`
  - `Operation.createPassiveOffer`
  - `Operation.pathPayment`
  - `Keypair.fromBase58Seed`
- Remove the `Network` class ([#331](#331)).
- Remove `vendor/base58.js` ([#331](#331)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants