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 support for CAP0015 (FeeBump). #321

Merged
merged 31 commits into from
Apr 14, 2020
Merged

Add support for CAP0015 (FeeBump). #321

merged 31 commits into from
Apr 14, 2020

Conversation

abuiles
Copy link
Contributor

@abuiles abuiles commented Apr 10, 2020

What

Add support for CAP0015.

Why

Protocol 13 introduces a new kind of transaction which allows other accounts to pay the fee for a given transaction.

This pull request adds a new helper functions which makes it easy for consumers of this library to create a fee bump transaction.

This PR:

  • Add .buildFeeBumpTransaction to TransactionBuilder which makes it easy to create FeeBumpTransaction.
  • Adds a new helper function to the Transaction class which allows consumer to know if the transaction is a FeeBump transaction or not.
  • Adds a feature flag which allow consumers of this library to create V1 (protocol 13) transactions using the TransactionBuilder.
  • Extends TS definitions.

Fixes #319

How to create FeeBump transactions?

Assuming you have a valid version of stellar-core and Horizon running with protocol 13, you can use the following example to create a fee bump transaction (replace keypairs):

let sourceKP = Keypair.fromSecret("SAQZZ7K4YUOEX3MMLX5YM4VCK4E7NSKRODKNPNYCMH44JGGXNLUDNZH7");
// account to which the inner transactions belongs to
let account = await server.loadAccount(sourceKP.publicKey())
let PASS_PHRASE = "Standalone Network ; February 2017";
let destination = 'GDQERENWDDSQZS7R7WKHZI3BSOYMV3FSWR7TFUYFTKQ447PIX6NREOJM';
let amount = '20';
let innerTX = new TransactionBuilder(account, {
  fee: 100,
  networkPassphrase: PASS_PHRASE,
  timebounds: {
    minTime: 0,
    maxTime: 0,
  },
  v1: true,
})
    .addOperation(
      Operation.payment({ 
        destination,
        asset: Asset.native(),
        amount
      })
    )
    .addMemo(Memo.text('Happy birthday!'))
    .build()
innerTX.sign(sourceKP);

// Keypair for account paying for the FeeBumpTX
let feeSourceKP = Keypair.fromSecret("SB7ZMPZB3YMMK5CUWENXVLZWBK4KYX4YU5JBXQNZSK2DP2Q7V3LVTO5V");
let bumpFee = "25000000";
let transaction = TransactionBuilder.buildFeeBumpTransaction(
  feeSourceKP,
  bumpFee,
  innerTX,
  PASS_PHRASE
);
transaction.sign(feeSourceKP)

console.log("waiting for hash (fee bump tx hash)", transaction.hash().toString('hex'))
let result = await server.submitTransaction(transaction);

Breaking changes

  • The attribute fee in Transaction instances changed from number to string.
  • The const BASE_FEE changed from number to string.
  • The option fee passed to new TransactionBuilder({fee: ..}) changed from number to string

@abuiles abuiles changed the base branch from master to protocol-13 April 10, 2020 23:33
@abuiles abuiles requested review from 2opremio, ire-and-curses, tamirms and a team April 10, 2020 23:34
@abuiles abuiles changed the title Add support for CAP15 (FeeBump) Add support for CAP0015 (FeeBump). Apr 10, 2020
src/transaction.js Outdated Show resolved Hide resolved
Copy link

@2opremio 2opremio left a comment

Choose a reason for hiding this comment

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

Only nitpicks

@abuiles abuiles mentioned this pull request Apr 13, 2020
Copy link
Member

@ire-and-curses ire-and-curses left a comment

Choose a reason for hiding this comment

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

Some minor comments. Also we need to support all error codes, see https://github.com/stellar/stellar-protocol/blob/master/core/cap-0015.md

src/transaction.js Outdated Show resolved Hide resolved
src/transaction.js Show resolved Hide resolved
src/transaction.js Outdated Show resolved Hide resolved
src/transaction.js Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
@abuiles abuiles changed the base branch from protocol-13 to release-v3.0.0 April 13, 2020 18:27
src/transaction.js Outdated Show resolved Hide resolved
src/transaction.js Outdated Show resolved Hide resolved
src/transaction.js Outdated Show resolved Hide resolved
src/transaction_builder.js Outdated Show resolved Hide resolved
@abuiles abuiles merged commit b872059 into release-v3.0.0 Apr 14, 2020
@abuiles abuiles deleted the cap15 branch April 14, 2020 21:58
abuiles added a commit that referenced this pull request Apr 20, 2020
* Fix hash generation with v1 or v0 transactions.

* Fix transaction envelope for TransactionV1Envelope.

* Add hidden flag to allow building v1 tx with tx builder.

* Add Transaction.buildFeeBumpTransaction

* Change v1 feature flag name.

* Add docs for new methods.

* Fix ts types.

* Start fee and source from tx.

* Add innerFee and innerSignatures.

* Fix test and make all related args a string.

* Add tests against expected xdr.

* Move buildFeeBumpTransaction to TransactionBuilder class.

* Fix ts tests.

* Calculate fee from baseFee.

* Move helper back to transaction.

* Validate minFee.

* Show received envelope type.

* Fix dependency cycle.

* Move buildFeeBumpTransaction to transaction builder.

* Add second condition for minimum base base.

* Fix types.

* Add comment.

* Simplify minBase fee check.

* Receive a Transaction instead of a xdr.TransactionV1Envelope.

* Fix type for buildFeeBumpTransaction.

* Fix typo for _envelopeType.

* Remove unused import

* Use builder to generate innerTx.

* Fix test.

* Add test for invalid envelopeType.

* Validate transaction envelope to be of type v1.
@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.

Implement CAP 15 (Fee bump) support in JS BASE
4 participants