So, I'm having some problems getting a multi input/multi output transaction working. My code below works with 1 input and 2 output, but when adding more inputs/outputs the transaction gets rejected. I'm sure whatever I'm doing wrong in pretty simple, any ideas?

Code works for this (1 input, 2 outputs):

CTransaction(hash=01a3204517476812df2c2f77735a72a6d3e7eb8b9a5d5330ca433dd875fc4c3c, ver=1, vin.size=2, vout.size=4, nLockTime=0)
    CTxIn(COutPoint(6ee4049d6c75d6450e961446d28760207bd7036e2aa692afb80bb34245d3df84, 0), scriptSig=304502204cdfd276ff9c53bb)
    CTxOut(nValue=4.00000000, scriptPubKey=OP_DUP OP_HASH160 b07e181ce438)
    CTxOut(nValue=1.00000000, scriptPubKey=OP_DUP OP_HASH160 bf7484e469c8)

Code doesn't work for this (2 input, 4 outputs):

CTransaction(hash=01a3204517476812df2c2f77735a72a6d3e7eb8b9a5d5330ca433dd875fc4c3c, ver=1, vin.size=2, vout.size=4, nLockTime=0)
    CTxIn(COutPoint(6ee4049d6c75d6450e961446d28760207bd7036e2aa692afb80bb34245d3df84, 1), scriptSig=304502204aef3f393c273835)
    CTxIn(COutPoint(6ee4049d6c75d6450e961446d28760207bd7036e2aa692afb80bb34245d3df84, 0), scriptSig=304502204cdfd276ff9c53bb)
    CTxOut(nValue=4.00000000, scriptPubKey=OP_DUP OP_HASH160 bf7484e469c8)
    CTxOut(nValue=491.00000000, scriptPubKey=OP_DUP OP_HASH160 0796b7f3430f)
    CTxOut(nValue=4.00000000, scriptPubKey=OP_DUP OP_HASH160 b07e181ce438)
    CTxOut(nValue=1.00000000, scriptPubKey=OP_DUP OP_HASH160 bf7484e469c8)

Pseudo code showing working transaction:

    // These are how the vout's are made
    CScript scriptPubKey;
    scriptPubKey.SetDestination(address);    
    CScript s;
    s << OP_DUP << OP_HASH160 << scriptPubKey.GetID() << OP_EQUALVERIFY << OP_CHECKSIG;
 
    CTxOut out(nValue, s);
 
    ---------------


    CTransaction txNew;
    txNew.vin.clear();
    txNew.vout.clear();

    // vin and vout are already populated 
    for(unsigned int i = 0; i < vout.size(); i++){
        txNew.vout.push_back(vout[i]);
    }
    
    //add all vins
    for(unsigned int i = 0; i < vin.size(); i++){
        txNew.vin.push_back(vin[i]);
    }

    //add all vins
    for(unsigned int i = 0; i < vin.size(); i++){
        // this is signed with 2 separate keys for each vin
        if(!SignSignature(*keystore, prevPubKey, txNew, i, int(SIGHASH_ALL|SIGHASH_ANYONECANPAY))) 
            printf('signing failed!\n');

        // I was told I might need to serialize the inputs? Not sure how that would work
    }

    RelayTransaction(txNew, txNew.Hash());


---

CryptoFish