public Task UpdateBalancesAsync(IDbConnection con, IDbTransaction tx, PoolConfig poolConfig, IPayoutHandler payoutHandler, Block block, decimal blockReward) { var payoutConfig = poolConfig.PaymentProcessing.PayoutSchemeConfig; // PPLNS window (see https://bitcointalk.org/index.php?topic=39832) var window = payoutConfig?.ToObject <Config>()?.Factor ?? 2.0m; // calculate rewards var shares = new Dictionary <string, ulong>(); var rewards = new Dictionary <string, decimal>(); var shareCutOffDate = CalculateRewards(poolConfig, window, block, blockReward, shares, rewards); // update balances foreach (var address in rewards.Keys) { var amount = rewards[address]; if (amount > 0) { logger.Info(() => $"Adding {payoutHandler.FormatAmount(amount)} to balance of {address} for {shares[address]} shares"); balanceRepo.AddAmount(con, tx, poolConfig.Id, poolConfig.Coin.Type, address, amount); } } // delete obsolete shares if (shareCutOffDate.HasValue) { var cutOffCount = shareRepo.CountPoolSharesBefore(con, tx, poolConfig.Id, shareCutOffDate.Value); if (cutOffCount > 0) { logger.Info(() => $"Deleting {cutOffCount} obsolete shares before {shareCutOffDate.Value}"); shareRepo.DeletePoolSharesBefore(con, tx, poolConfig.Id, shareCutOffDate.Value); } //logger.Info(() => $"Shares before {shareCutOffDate.Value} can be deleted"); } // diagnostics var totalShareCount = shares.Values.ToList().Sum(x => new decimal(x)); var totalRewards = rewards.Values.ToList().Sum(x => x); if (totalRewards > 0) { logger.Info(() => $"{totalShareCount} shares contributed to a total payout of {payoutHandler.FormatAmount(totalRewards)} ({totalRewards / blockReward * 100:0.00}% of block reward)"); } return(Task.FromResult(true)); }