Example #1
0
        /// <inheritdoc/>
        public override async Task Handle(UpdateDailyOutflow command)
        {
            var root = await _repository.Find <Wallet>(command.Target);

            if (root == null)
            {
                throw new ArgumentNullException(nameof(command.Address));
            }

            var coin = await _repository.Find <Coin>(root.Coin);

            if (coin == null)
            {
                throw new ArgumentNullException(nameof(root.Coin));
            }

            var txResults = await GetTransactions(command);

            if (txResults == null)
            {
                return;
            }

            var outflows = new Dictionary <Instant, (Instant time, double amount)>();

            foreach (var tx in txResults)
            {
                var dateTime = Instant.FromUnixTimeMilliseconds(tx.ReceiveTime).InUtc().LocalDateTime;
                var date     = new LocalDate(dateTime.Year, dateTime.Month, dateTime.Day);
                var instant  = date.AtMidnight().InUtc().ToInstant();
                var amount   = tx.Amount;
                if (tx.To == command.Target)
                {
                    amount *= -1;
                }

                if (outflows.ContainsKey(instant))
                {
                    amount += outflows[instant].amount;
                }
                outflows[instant] = (Instant.FromUnixTimeMilliseconds(tx.ReceiveTime), amount);
            }

            ICommand changeBalanceCommand = null;
            var      idx = 0;

            foreach (var v in outflows)
            {
                changeBalanceCommand = new RetroactiveCommand <ChangeWalletBalance>(new ChangeWalletBalance(command.Address, new Quantity(-v.Value.amount, coin.Asset), $"Out{command.Index}_{idx}"), v.Value.time);
                await _balanceHandler.Handle(changeBalanceCommand);

                ++idx;
            }

            command.EventType = changeBalanceCommand?.EventType;
        }
        /// <inheritdoc/>
        public override async Task Handle(UpdateDailyMining command)
        {
            var root = await _repository.Find <Wallet>(command.Target);

            if (root == null)
            {
                throw new ArgumentNullException(nameof(command.Address));
            }

            var coin = await _repository.Find <Coin>(root.Coin);

            if (coin == null)
            {
                throw new ArgumentNullException(nameof(root.Coin));
            }

            var minedBlocks = await GetMinedBlocks(command);

            if (minedBlocks == null)
            {
                return;
            }

            var blocks = new Dictionary <Instant, (Instant time, double amount)>();

            foreach (var block in minedBlocks)
            {
                var dateTime = Instant.FromUnixTimeMilliseconds(block.Timestamp).InUtc().LocalDateTime;
                var date     = new LocalDate(dateTime.Year, dateTime.Month, dateTime.Day);
                var instant  = date.AtMidnight().InUtc().ToInstant();
                var amount   = block.Feereward;
                if (blocks.ContainsKey(instant))
                {
                    amount += blocks[instant].amount;
                }
                blocks[instant] = (Instant.FromUnixTimeMilliseconds(block.Timestamp), amount);
            }

            ICommand mineCommand = null;
            var      idx         = 0;

            foreach (var v in blocks)
            {
                mineCommand = new RetroactiveCommand <MineCoin>(new MineCoin(command.Address, new Quantity(v.Value.amount, coin.Asset), $"Mining{command.Index}_{idx}"), v.Value.time);
                await _mineHandler.Handle(mineCommand);

                ++idx;
            }

            command.EventType = mineCommand?.EventType;
        }