Beispiel #1
0
        public decimal GetTradePrice(TradeEventModel trade)
        {
            var amountGet  = Convert.ToDecimal(trade.AmountGet);
            var amountGive = Convert.ToDecimal(trade.AmountGive);

            if (amountGet == 0 || amountGive == 0)
            {
                return(0);
            }
            if (trade.TokenGet == NullAddress)
            {
                return(amountGive / amountGet);
            }
            else
            {
                return(amountGet / amountGive);
            }
        }
Beispiel #2
0
        public async Task ScanTradeByEventsAsync()
        {
            string latestScannedBlockKey = "TradeLatestScannedBlock";
            var    abi = await _abiProvider.GetCrowdsaleAbiAsync();

            var contract   = _nethereumClient.Eth.GetContract(abi, _ethSettings.EtherDeltaContractAddress);
            var tradeEvent = contract.GetEvent("Trade");

            var blockNumHex = await _nethereumClient.Eth.Blocks.GetBlockNumber.SendRequestAsync();

            var blockNumLong = Convert.ToInt64(blockNumHex.Value.ToString());

            //to avoid races conditions between services
            blockNumLong -= 5;

            var latestScannedBlock = await _parametersService.GetLatestScannedBlockAsync(latestScannedBlockKey);

            var fromBlock = new BlockParameter(Convert.ToUInt64(latestScannedBlock + 1));

            var filterInput = tradeEvent.CreateFilterInput(fromBlock);

            var logs = await _etherscanService.GetEventsAsync(filterInput, latestScannedBlock + 1, blockNumLong);

            var eventLogsAll = tradeEvent.DecodeAllEventsForEvent <TradeEvent>(logs);

            var detectedTransactionHashes = eventLogsAll.Select(l => l.Log.TransactionHash).ToList();

            var existingDbTransactions = _context.TradeEvent
                                         .Where(t => detectedTransactionHashes.Contains(t.TransactionHash))
                                         .ToDictionary(t => t.TransactionHash, t => t);
            var tokensToUpdate = new List <string>();

            foreach (var eventEntry in eventLogsAll)
            {
                if (existingDbTransactions.TryGetValue(eventEntry.Log.TransactionHash, out var dbTransaction))
                {
                    continue;
                }

                var transaction = await _nethereumClient.Eth.Transactions.GetTransactionByHash.SendRequestAsync(eventEntry.Log.TransactionHash);

                var receipt = await _nethereumClient.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(eventEntry.Log.TransactionHash);

                var block = await _nethereumClient.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(transaction.BlockNumber);

                var epoch = Convert.ToInt64(block.Timestamp.Value.ToString());

                var dbEvent = new TradeEventModel
                {
                    TokenGet   = eventEntry.Event.TokenGet,
                    AmountGet  = eventEntry.Event.AmountGet.ToString(),
                    TokenGive  = eventEntry.Event.TokenGive,
                    AmountGive = eventEntry.Event.AmountGive.ToString(),
                    Get        = eventEntry.Event.Get,
                    Give       = eventEntry.Event.Give,

                    TransactionHash = eventEntry.Log.TransactionHash,
                    Timestamp       = DateTimeOffset.FromUnixTimeSeconds(epoch),
                    BlockNum        = Convert.ToInt64(transaction.BlockNumber.Value.ToString()),
                    GasPriceWei     = Convert.ToInt64(transaction.GasPrice.Value.ToString()),
                    GasUsed         = Convert.ToInt64(receipt.GasUsed.Value.ToString()),
                };
                tokensToUpdate.Add(await WriteOHLC(eventEntry.Event as TradeEvent, dbEvent.Timestamp));
                await _context.TradeEvent.AddAsync(dbEvent);
            }

            await _parametersService.SetLatestScannedBlockAsync(latestScannedBlockKey, blockNumLong);

            await _context.SaveChangesAsync();

            tokensToUpdate.ForEach(token => { UpdateChart(token).Wait(); });
        }