public long getGuaranteedBalanceNQT(final int numberOfConfirmations, final int currentHeight) { if (numberOfConfirmations >= Nxt.getBlockchain().getHeight()) { return 0; } if (numberOfConfirmations > 2880 || numberOfConfirmations < 0) { throw new IllegalArgumentException( "Number of required confirmations must be between 0 and " + 2880); } int height = currentHeight - numberOfConfirmations; try (Connection con = Db.getConnection(); PreparedStatement pstmt = con.prepareStatement( "SELECT SUM (additions) AS additions " + "FROM account_guaranteed_balance WHERE account_id = ? AND height > ? AND height <= ?")) { pstmt.setLong(1, this.id); pstmt.setInt(2, height); pstmt.setInt(3, currentHeight); try (ResultSet rs = pstmt.executeQuery()) { if (!rs.next()) { return balanceNQT; } return Math.max(Convert.safeSubtract(balanceNQT, rs.getLong("additions")), 0); } } catch (SQLException e) { throw new RuntimeException(e.toString(), e); } }
private void addToGuaranteedBalanceNQT(long amountNQT) { if (amountNQT <= 0) { return; } int blockchainHeight = Nxt.getBlockchain().getHeight(); try (Connection con = Db.getConnection(); PreparedStatement pstmtSelect = con.prepareStatement( "SELECT additions FROM account_guaranteed_balance " + "WHERE account_id = ? and height = ?"); PreparedStatement pstmtUpdate = con.prepareStatement( "MERGE INTO account_guaranteed_balance (account_id, " + " additions, height) KEY (account_id, height) VALUES(?, ?, ?)")) { pstmtSelect.setLong(1, this.id); pstmtSelect.setInt(2, blockchainHeight); try (ResultSet rs = pstmtSelect.executeQuery()) { long additions = amountNQT; if (rs.next()) { additions = Convert.safeAdd(additions, rs.getLong("additions")); } pstmtUpdate.setLong(1, this.id); pstmtUpdate.setLong(2, additions); pstmtUpdate.setInt(3, blockchainHeight); pstmtUpdate.executeUpdate(); } } catch (SQLException e) { throw new RuntimeException(e.toString(), e); } }
@Override public void trim(int height) { try (Connection con = Db.getConnection(); PreparedStatement pstmtDelete = con.prepareStatement( "DELETE FROM account_guaranteed_balance " + "WHERE height < ?")) { pstmtDelete.setInt(1, height - 1440); pstmtDelete.executeUpdate(); } catch (SQLException e) { throw new RuntimeException(e.toString(), e); } }
public static int getAssetAccountsCount(long assetId) { try (Connection con = Db.getConnection(); PreparedStatement pstmt = con.prepareStatement( "SELECT COUNT(*) FROM account_asset WHERE asset_id = ? AND latest = TRUE")) { pstmt.setLong(1, assetId); try (ResultSet rs = pstmt.executeQuery()) { rs.next(); return rs.getInt(1); } } catch (SQLException e) { throw new RuntimeException(e.toString(), e); } }