func (db *DB) UpdateDeposits(deposits []match.Deposit, currentBlockHeight uint64, coinType *coinparam.Params) (err error) {

	coinSchema, err := util.GetSchemaNameFromParam(coinType)
	if err != nil {
		return fmt.Errorf("Error while updating deposits: \n%s", err)
	}

	tx, err := db.DBHandler.Begin()
	if err != nil {
		return fmt.Errorf("Error beginning transaction while updating deposits: \n%s", err)
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while updating deposits: \n%s", err)
			return
		}
		err = tx.Commit()
	}()

	if _, err = tx.Exec("USE " + db.pendingDepositSchema + ";"); err != nil {
		return
	}

	for _, deposit := range deposits {

		// Insert the deposit
		// TODO: replace this name stuff, check that the txid doesn't already exist in deposits. IMPORTANT!!
		insertDepositQuery := fmt.Sprintf("INSERT INTO %s VALUES ('%x', %d, %d, %d, '%s');", coinSchema, deposit.Pubkey.SerializeCompressed(), deposit.BlockHeightReceived+deposit.Confirmations, deposit.BlockHeightReceived, deposit.Amount, deposit.Txid)
		if _, err = tx.Exec(insertDepositQuery); err != nil {
			return
		}
	}

	areDepositsValidQuery := fmt.Sprintf("SELECT pubkey, amount, txid FROM %s WHERE expectedConfirmHeight=%d;", coinSchema, currentBlockHeight)
	rows, err := tx.Query(areDepositsValidQuery)
	if err != nil {
		return
	}

	// Now we start reflecting the changes for users whose deposits can be filled
	var pubkeys []*koblitz.PublicKey
	var amounts []uint64
	var txids []string

	for rows.Next() {
		var pubkeyBytes []byte
		var amount uint64
		var txid string

		if err = rows.Scan(&pubkeyBytes, &amount, &txid); err != nil {
			return
		}

		// so res.Scan does something weird because I'm not using prepared statements. I would prefer to be using a byte array to scan into, since the pubkey is a varbinary?
		if pubkeyBytes, err = hex.DecodeString(string(pubkeyBytes)); err != nil {
			return
		}

		var pubkey *koblitz.PublicKey
		pubkey, err = koblitz.ParsePubKey(pubkeyBytes, koblitz.S256())
		if err != nil {
			return
		}

		pubkeys = append(pubkeys, pubkey)
		amounts = append(amounts, amount)
		txids = append(txids, txid)
	}
	if err = rows.Close(); err != nil {
		return
	}

	if len(amounts) > 0 {
		if err = db.UpdateBalancesWithinTransaction(pubkeys, amounts, tx, coinType); err != nil {
			return
		}
	}

	// HAVE TO GO BACK TO THE DEPOSIT SCHEMA OR ELSE NOTHING WORKS -- dan right after spending 10 minutes on a dumb mistake
	if _, err = tx.Exec("USE " + db.pendingDepositSchema + ";"); err != nil {
		return
	}

	for _, txid := range txids {
		deleteDepositQuery := fmt.Sprintf("DELETE FROM %s WHERE txid='%s';", coinSchema, txid)
		if _, err = tx.Exec(deleteDepositQuery); err != nil {
			return
		}
	}

	return nil
}
// UpdateDeposits happens when a block is sent in, it updates the deposits table with correct deposits
UpdateDeposits happens when a block is sent in, it updates the deposits table with correct deposits
// UpdateDeposits happens when a block is sent in it updates the deposits table with correct deposits
func (db *DB) UpdateDeposits(deposits []match.Deposit, currentBlockHeight uint64, coinType *coinparam.Params) (err error) {
	coinSchema, err := util.GetSchemaNameFromParam(coinType)
	if err != nil {
		return fmt.Errorf("Error while updating deposits: \n%s", err)
	}
	tx, err := db.DBHandler.Begin()
	if err != nil {
		return fmt.Errorf("Error beginning transaction while updating deposits: \n%s", err)
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while updating deposits: \n%s", err)
			return
		}
		err = tx.Commit()
	}()
	if _, err = tx.Exec("USE " + db.pendingDepositSchema + ";"); err != nil {
		return
	}
	for _, deposit := range deposits {
		insertDepositQuery := fmt.Sprintf("INSERT INTO %s VALUES ('%x', %d, %d, %d, '%s');", coinSchema, deposit.Pubkey.SerializeCompressed(), deposit.BlockHeightReceived+deposit.Confirmations, deposit.BlockHeightReceived, deposit.Amount, deposit.Txid)
		if _, err = tx.Exec(insertDepositQuery); err != nil {
			return
		}
	}
	areDepositsValidQuery := fmt.Sprintf("SELECT pubkey, amount, txid FROM %s WHERE expectedConfirmHeight=%d;", coinSchema, currentBlockHeight)
	rows, err := tx.Query(areDepositsValidQuery)
	if err != nil {
		return
	}
	var pubkeys []*koblitz.PublicKey
	var amounts []uint64
	var txids []string
	for rows.Next() {
		var pubkeyBytes []byte
		var amount uint64
		var txid string
		if err = rows.Scan(&pubkeyBytes, &amount, &txid); err != nil {
			return
		}
		if pubkeyBytes, err = hex.DecodeString(string(pubkeyBytes)); err != nil {
			return
		}
		var pubkey *koblitz.PublicKey
		pubkey, err = koblitz.ParsePubKey(pubkeyBytes, koblitz.S256())
		if err != nil {
			return
		}
		pubkeys = append(pubkeys, pubkey)
		amounts = append(amounts, amount)
		txids = append(txids, txid)
	}
	if err = rows.Close(); err != nil {
		return
	}
	if len(amounts) > 0 {
		if err = db.UpdateBalancesWithinTransaction(pubkeys, amounts, tx, coinType); err != nil {
			return
		}
	}
	if _, err = tx.Exec("USE " + db.pendingDepositSchema + ";"); err != nil {
		return
	}
	for _, txid := range txids {
		deleteDepositQuery := fmt.Sprintf("DELETE FROM %s WHERE txid='%s';", coinSchema, txid)
		if _, err = tx.Exec(deleteDepositQuery); err != nil {
			return
		}
	}
	return nil
}
func ( db * DB ) UpdateDeposits ( deposits [ ] match . Deposit , currentBlockHeight uint64 , coinType * coinparam . Params ) ( err error ) { coinSchema , err := util . GetSchemaNameFromParam ( coinType ) if err != nil { return fmt . Errorf ( " \n " , err ) } tx , err := db . DBHandler . Begin ( ) if err != nil { return fmt . Errorf ( " \n " , err ) } defer func ( ) { if err != nil { tx . Rollback ( ) err = fmt . Errorf ( " \n " , err ) return } err = tx . Commit ( ) } ( ) if _ , err = tx . Exec ( " " + db . pendingDepositSchema + " " ) ; err != nil { return } for _ , deposit := range deposits { insertDepositQuery := fmt . Sprintf ( " " , coinSchema , deposit . Pubkey . SerializeCompressed ( ) , deposit . BlockHeightReceived + deposit . Confirmations , deposit . BlockHeightReceived , deposit . Amount , deposit . Txid ) if _ , err = tx . Exec ( insertDepositQuery ) ; err != nil { return } } areDepositsValidQuery := fmt . Sprintf ( " " , coinSchema , currentBlockHeight ) rows , err := tx . Query ( areDepositsValidQuery ) if err != nil { return } var pubkeys [ ] * koblitz . PublicKey var amounts [ ] uint64 var txids [ ] string for rows . Next ( ) { var pubkeyBytes [ ] byte var amount uint64 var txid string if err = rows . Scan ( & pubkeyBytes , & amount , & txid ) ; err != nil { return } if pubkeyBytes , err = hex . DecodeString ( string ( pubkeyBytes ) ) ; err != nil { return } var pubkey * koblitz . PublicKey pubkey , err = koblitz . ParsePubKey ( pubkeyBytes , koblitz . S256 ( ) ) if err != nil { return } pubkeys = append ( pubkeys , pubkey ) amounts = append ( amounts , amount ) txids = append ( txids , txid ) } if err = rows . Close ( ) ; err != nil { return } if len ( amounts ) > 0 { if err = db . UpdateBalancesWithinTransaction ( pubkeys , amounts , tx , coinType ) ; err != nil { return } } if _ , err = tx . Exec ( " " + db . pendingDepositSchema + " " ) ; err != nil { return } for _ , txid := range txids { deleteDepositQuery := fmt . Sprintf ( " " , coinSchema , txid ) if _ , err = tx . Exec ( deleteDepositQuery ) ; err != nil { return } } return nil }
UpdateDeposits happens when a block is sent in, it updates the deposits table with correct deposits
UpdateDeposits happens when a block is sent in it updates the deposits table with correct deposits
// Insert the deposit // TODO: replace this name stuff, check that the txid doesn't already exist in deposits. IMPORTANT!! // Now we start reflecting the changes for users whose deposits can be filled // so res.Scan does something weird because I'm not using prepared statements. I would prefer to be using a byte array to scan into, since the pubkey is a varbinary? // HAVE TO GO BACK TO THE DEPOSIT SCHEMA OR ELSE NOTHING WORKS -- dan right after spending 10 minutes on a dumb mistake