<commit_before>const Currency = require('./Currency');
const Redis = require('../redis/Redis');

const redis = new Redis();

// rate * convert to decimal
const INTEREST_MATURE_RATE = 0.0001 * 0.01;
const UPDATE_DURATION = 60 * 60 * 1000;

redis.db.getAsync('bankupdate').then(update => {
	setTimeout(() => Bank.applyInterest(), Math.max(0, (new Date(update) + UPDATE_DURATION) - Date.now()));
});

class Bank {
	static changeLedger(user, amount) {
		redis.db.hgetAsync('ledger', user).then(balance => {
			balance = parseInt(balance) || 0;
			redis.db.hsetAsync('ledger', user, amount + parseInt(balance));
		});
	}

	static async getBalance(user) {
		const balance = await redis.db.hgetAsync('ledger', user) || 0;

		return parseInt(balance);
	}

	static deposit(user, amount) {
		Currency.removeBalance(user, amount);
		this.changeLedger(user, amount);
	}

	static withdraw(user, amount) {
		Currency.addBalance(user, amount);
		this.changeLedger(user, -amount);
	}

	static async applyInterest() {
		const lastUpdate = await redis.db.getAsync('bankupdate');
		if (lastUpdate) return;

		const interestRate = await this.getInterestRate();

		const bankBalance = await Currency.getBalance('bank');
		const previousBankBalance = await redis.db.getAsync('lastbankbalance') || bankBalance;
		const bankBalanceDelta = bankBalance - previousBankBalance;

		redis.db.hgetallAsync('ledger').then(balances => {
			for (const [user, balance] in balances.entries()) {
				redis.db.hsetAsync('ledger', user, Math.round(balance * (interestRate + 1)));
			}
		});

		const newInterestRate = Math.max(0, interestRate + (bankBalanceDelta * -INTEREST_MATURE_RATE));
		redis.db.setAsync('interestrate', newInterestRate);
		redis.db.setAsync('lastbankbalance', bankBalance);

		redis.db.setAsync('bankupdate', new Date());
		redis.db.expire('bankupdate', UPDATE_DURATION);

		setTimeout(() => Bank.applyInterest(), UPDATE_DURATION);
	}

	static async getInterestRate() {
		const interestRate = await redis.db.getAsync('interestrate') || 0.01;

		return parseInt(interestRate);
	}

	static async nextUpdate() {
		const lastUpdate = await redis.db.getAsync('bankupdate');

		return UPDATE_DURATION - (Date.now() - lastUpdate);
	}
}
<commit_msg>Bugfix<commit_after>const Currency = require('./Currency');
const Redis = require('../redis/Redis');

const redis = new Redis();

// rate * convert to decimal
const INTEREST_MATURE_RATE = 0.0001 * 0.01;
const UPDATE_DURATION = 60 * 60 * 1000;

redis.db.getAsync('bankupdate').then(update => {
	setTimeout(() => Bank.applyInterest(), Math.max(0, (new Date(update) + UPDATE_DURATION) - Date.now()));
});

class Bank {
	static changeLedger(user, amount) {
		redis.db.hgetAsync('ledger', user).then(balance => {
			balance = parseInt(balance) || 0;
			redis.db.hsetAsync('ledger', user, amount + parseInt(balance));
		});
	}

	static async getBalance(user) {
		const balance = await redis.db.hgetAsync('ledger', user) || 0;

		return parseInt(balance);
	}

	static deposit(user, amount) {
		Currency.removeBalance(user, amount);
		this.changeLedger(user, amount);
	}

	static withdraw(user, amount) {
		Currency.addBalance(user, amount);
		this.changeLedger(user, -amount);
	}

	static async applyInterest() {
		const lastUpdate = await redis.db.getAsync('bankupdate');
		if (lastUpdate) return;

		const interestRate = await this.getInterestRate();

		const bankBalance = await Currency.getBalance('bank');
		const previousBankBalance = await redis.db.getAsync('lastbankbalance') || bankBalance;
		const bankBalanceDelta = bankBalance - previousBankBalance;

		redis.db.hgetallAsync('ledger').then(balances => {
			for (const [user, balance] in balances.entries()) {
				redis.db.hsetAsync('ledger', user, Math.round(balance * (interestRate + 1)));
			}
		});

		const newInterestRate = Math.max(0, interestRate + (bankBalanceDelta * -INTEREST_MATURE_RATE));
		redis.db.setAsync('interestrate', newInterestRate);
		redis.db.setAsync('lastbankbalance', bankBalance);

		redis.db.setAsync('bankupdate', Date.now());
		redis.db.expire('bankupdate', UPDATE_DURATION);

		setTimeout(() => Bank.applyInterest(), UPDATE_DURATION);
	}

	static async getInterestRate() {
		const interestRate = await redis.db.getAsync('interestrate') || 0.01;

		return parseInt(interestRate);
	}

	static async nextUpdate() {
		const lastUpdate = await redis.db.getAsync('bankupdate');

		return UPDATE_DURATION - (Date.now() - lastUpdate);
	}
}

{'fraction_non_alphanumeric': 0.15237452171955887, 'fraction_numerical': 0.010803511141120865, 'mean_word_length': 4.917443408788282, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2682975', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1470, 'n_words': 295}