This is exactly what I am after but in JS. How can I build a transaction with a dynamic list of mutators or operations using the Go SDK?

Something like this would be great, and then it'd be easy to loop over and abstract out from an array of operations. Likewise simply setting new Transaction.operations = an array of operations would be great.

    var transaction = new StellarSdk.TransactionBuilder(account)
        destination: pubKey,
        asset: StellarSdk.Asset.native(),
        amount: "1"

            destination: pubKey3,
            asset: StellarSdk.Asset.native(),
            amount: "1"

If I understand you correctly, it should look like this:

const horizon = new StellarSdk.Server('https://horizon-testnet.stellar.org/')
* @param sourceAccountSecret {String} - account secret key
* @param operations {Array<StellarSdk.Operation>} - operations to apply
* @returns {Promise<StellarSdk.Transaction>}
function submitOperations(sourceAccountSecret, operations) {
    if (operations.length > 100) return Promise.reject(new Error('A transaction may contain up to 100 operations. Use batching to send send more than 100 operations a time.'))
    //retrieve keypair from secret key
    let keypair = StellarSdk.Keypair.fromSecret(sourceAccountSecret)
    //load account sequence from Horizon server
    return server.loadAccount(keypair.publicKey())
        .then(account => {
            //build and sing the transaction
            const txBuilder = new StellarSdk.TransactionBuilder(account)
            txBuilder.operations = operations
            const tx = txBuilder.build()
            //submit the transaction to Stellar network
            return server.submitTransaction(tx)

And then call the function from your code:

submitOperations('SA45...8DA1', [
        destination: pubKey,
        asset: StellarSdk.Asset.native(),
        amount: "1"
        destination: pubKey3,
        asset: StellarSdk.Asset.native(),
        amount: "1"
    .then(txResult => console.log(txResult))
    .catch(err => console.error(err))
| improve this answer | |
  • Thank you so much. This is precisely what I was looking for; I reworked the Go version and arrived at a very similar approach. – Tyler Citrin Jul 20 '18 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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