-2

How can I add multiple operations in one transaction using the Go SDK? I am trying to add a dynamic amount of payment operations under 1 transaction and that seems to be not possible in Go as it's building a function.

I am new to Go, so please let me know if there is any way to add multiple operations dynamically inside one transaction.

In my code I added 4 operations manually. I want to know how to build them dynamically using a loop.

paymentTx, err := build.Transaction(
    build.SourceAccount{recipient.Address()},
    build.TestNetwork,
    build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},

    build.Payment(
        build.Destination{AddressOrSeed: Reciver2},
        build.CreditAmount{cd.AssestTransfer.Asset[0].Code, issuerSeed, cd.AssestTransfer.Asset[0].Limit},
    ), build.Payment(
        build.Destination{AddressOrSeed: Reciver2},
        build.CreditAmount{cd.AssestTransfer.Asset[1].Code, issuerSeed, cd.AssestTransfer.Asset[1].Limit},
    ), build.Payment(
        build.Destination{AddressOrSeed: Reciver2},
        build.CreditAmount{cd.AssestTransfer.Asset[2].Code, issuerSeed, cd.AssestTransfer.Asset[2].Limit},
    ),

    build.Payment(
        build.SourceAccount{Reciver2},
        build.Destination{AddressOrSeed: recipient.Address()},
        build.CreditAmount{cd.AssestTransfer.Asset[3].Code, issuerSeed, cd.AssestTransfer.Asset[3].Limit},
    ),

1 Answer 1

1

I found the answer. Hopefully it will help someone in the future.

    muts := []build.TransactionMutator{
    build.SourceAccount{recipient.Address()},
    build.TestNetwork,
    build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},

    build.Payment(
        build.SourceAccount{Reciver2},
        build.Destination{AddressOrSeed: recipient.Address()},
        build.CreditAmount{cd.AssestTransfer.Asset[3].Code, issuerSeed, cd.AssestTransfer.Asset[3].Limit},
    ),
}
ops := []build.TransactionMutator{
    build.Payment(
        build.SourceAccount{Reciver2},
        build.Destination{AddressOrSeed: recipient.Address()},
        build.CreditAmount{cd.AssestTransfer.Asset[3].Code, issuerSeed, cd.AssestTransfer.Asset[3].Limit},
    ),
}
muts = append(muts, ops...)
tx, err := build.Transaction(muts...)

if err != nil {
    log.Fatal(err)
}

paymentTxe, err := tx.Sign(recipientSeed)

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.