1

I'm trying to submit many operations in a single transaction that are built up dynamically. How can I convert these operations to a Stellar Transaction?

Please provide code samples if possible.

1 Answer 1

3

You could use the spread operator on an object of type []build.TransactionMutator that is passed into the build.Transaction method (on github.com/stellar/go/build).

Here's a code sample:

import (
    “github.com/stellar/go/build"
)

// BuildWithOps builds a transaction with a dynamic list of operations
func BuildWithOps(sourceAccount string, seq uint64, network build.Network, ops []build.TransactionMutator) (*build.TransactionBuilder, error) {
    // build an array of mutators initialized with the essential values
    muts := []build.TransactionMutator{
        build.Sequence{Sequence: seq},
        network,
        build.SourceAccount{AddressOrSeed: sourceAccount},
    }

    // dynamically add more mutators/operations as needed
    muts = append(muts, ops...)

    // build the transaction from these mutators and check for an error
    tx, err := build.Transaction(muts...)
    if err != nil {
        return nil, err
    }
    return tx, nil
}
1
  • 1
    One note, tx.Err not longer exists. build.Transaction returns tx and err. Commented Feb 1, 2018 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.