TransactionBuilder

TransactionBuilder

Transaction builder helps constructs a new Transaction using the given Account as the transaction's "source account". The transaction will use the current sequence number of the given account as its sequence number and increment the given account's sequence number by one. The given source account must include a private key for signing the transaction or an error will be thrown.

Operations can be added to the transaction via their corresponding builder methods, and each returns the TransactionBuilder object so they can be chained together. After adding the desired operations, call the build() method on the TransactionBuilder to return a fully constructed Transaction that can be signed. The returned transaction will contain the sequence number of the source account and include the signature from the source account.

Be careful about unsubmitted transactions! When you build a transaction, stellar-sdk automatically increments the source account's sequence number. If you end up not submitting this transaction and submitting another one instead, it'll fail due to the sequence number being wrong. So if you decide not to use a built transaction, make sure to update the source account's sequence number with Server.loadAccount before creating another transaction.

The following code example creates a new transaction with Operation.createAccount and Operation.payment operations. The Transaction's source account first funds destinationA, then sends a payment to destinationB. The built transaction is then signed by sourceKeypair.

var transaction = new TransactionBuilder(source, { fee })
 .addOperation(Operation.createAccount({
        destination: destinationA,
        startingBalance: "20"
    })) // <- funds and creates destinationA
    .addOperation(Operation.payment({
        destination: destinationB,
        amount: "100",
        asset: Asset.native()
    })) // <- sends 100 XLM to destinationB
  .setTimeout(30)
  .build();

transaction.sign(sourceKeypair);

Constructor

new TransactionBuilder(sourceAccount, opts)

Source:
Parameters:
Name Type Description
sourceAccount Account

The source account for this transaction.

opts object

Options object

Name Type Attributes Description
fee number

The max fee willing to pay per operation in this transaction (in stroops). Required.

timebounds object <optional>

The timebounds for the validity of this transaction.

Name Type Attributes Description
minTime number | string | Date <optional>

64 bit unix timestamp or Date object

maxTime number | string | Date <optional>

64 bit unix timestamp or Date object

memo Memo <optional>

The memo for the transaction

Methods

addMemo(memo) → {TransactionBuilder}

Adds a memo to the transaction.

Source:
Parameters:
Name Type Description
memo Memo

Memo object

Returns:
Type:
TransactionBuilder

addOperation(operation) → {TransactionBuilder}

Adds an operation to the transaction.

Source:
Parameters:
Name Type Description
operation xdr.Operation

The xdr operation object, use Operation static methods.

Returns:
Type:
TransactionBuilder

build() → {Transaction}

This will build the transaction. It will also increment the source account's sequence number by 1.

Source:
Returns:
Type:
Transaction

This method will return the built Transaction.

setTimeout(timeout) → {TransactionBuilder}

Because of the distributed nature of the Stellar network it is possible that the status of your transaction will be determined after a long time if the network is highly congested. If you want to be sure to receive the status of the transaction within a given period you should set the TimeBounds with maxTime on the transaction (this is what setTimeout does internally; if there's minTime set but no maxTime it will be added). Call to TransactionBuilder.setTimeout is required if Transaction does not have max_time set. If you don't want to set timeout, use TimeoutInfinite. In general you should set TimeoutInfinite only in smart contracts.

Please note that Horizon may still return 504 Gateway Timeout error, even for short timeouts. In such case you need to resubmit the same transaction again without making any changes to receive a status. This method is using the machine system time (UTC), make sure it is set correctly.

Source:
See:
Parameters:
Name Type Description
timeout number

Number of seconds the transaction is good. Can't be negative. If the value is 0, the transaction is good indefinitely.

Returns:
Type:
TransactionBuilder