Insert the new state of an account at a particular point in time.

    The data must be for a never-before-seen (txid,block_id,vtxindex) set in the accounts table, but must
    correspond to an entry in the history table.

    If existing_account is not None, then copy all other remaining fields from it.

    Return True on success
    Raise an Exception on error

def namedb_account_transaction_save(cur, address, token_type, new_credit_value, new_debit_value, block_id, vtxindex, txid, existing_account):
    """
    Insert the new state of an account at a particular point in time.

    The data must be for a never-before-seen (txid,block_id,vtxindex) set in the accounts table, but must
    correspond to an entry in the history table.

    If existing_account is not None, then copy all other remaining fields from it.

    Return True on success
    Raise an Exception on error
    """
    if existing_account is None:
        existing_account = {}

    accounts_insert = {
        'address': address,
        'type': token_type,
        'credit_value': '{}'.format(new_credit_value),
        'debit_value': '{}'.format(new_debit_value),
        'lock_transfer_block_id': existing_account.get('lock_transfer_block_id', 0),        # unlocks immediately if the account doesn't exist
        'receive_whitelisted': existing_account.get('receive_whitelisted', True),           # new accounts are whitelisted by default (for now)
        'metadata': existing_account.get('metadata', None),
        'block_id': block_id,
        'txid': txid,
        'vtxindex': vtxindex
    }

    try:
        query, values = namedb_insert_prepare(cur, accounts_insert, 'accounts')
    except Exception as e:
        log.exception(e)
        log.fatal('FATAL: failed to append account history record for {} at ({},{})'.format(address, block_id, vtxindex))
        os.abort()
    
    namedb_query_execute(cur, query, values)
    return True