Exemplo n.º 1
0
        public async Task OneEventWithMapping()
        {
            if (_disableElasticSearch)
            {
                return;
            }

            const string INDEX_NAME = "mapped-transfer-logs";

            var elasticClient = CreateLocalElasticClient();

            //surround with "using" so that anything in a buffer is sent on dispose
            using (var elasticSearchService = new ElasticSearchService(elasticClient))
            {
                try
                {
                    //setup
                    await elasticSearchService.CreateIfNotExists(INDEX_NAME);

                    var indexer = elasticSearchService.CreateIndexerForEventLog <TransferEvent_ERC20, CustomTransferSearchDocumentDto>(
                        INDEX_NAME,
                        (from) => new CustomTransferSearchDocumentDto
                    {
                        From        = from.Event.From,
                        To          = from.Event.To,
                        Value       = from.Event.Value.ToString(),
                        BlockNumber = from.Log.BlockNumber.Value.ToString(),
                        TxHash      = from.Log.TransactionHash,
                        LogAddress  = from.Log.Address,
                        LogIndex    = (int)from.Log.LogIndex.Value,
                        DocumentKey = $"{from.Log.TransactionHash}_{from.Log.LogIndex.Value}"
                    },
                        documentsPerBatch: 1);

                    var web3 = new Web3.Web3(BlockchainUrl);
                    var blockchainProcessor     = web3.Processing.Logs.CreateProcessor <TransferEvent_ERC20>((transfer) => indexer.IndexAsync(transfer));
                    var cancellationTokenSource = new CancellationTokenSource();

                    //execute
                    await blockchainProcessor.ExecuteAsync(3146694, cancellationTokenSource.Token, 3146684);

                    //assert
                    await Task.Delay(1000); // allow time to index

                    Assert.Equal(19, await elasticSearchService.CountDocumentsAsync(INDEX_NAME));

                    //http://localhost:9200/transfer-logs/_search?q=from:0x1b31d19b6a9a942bbf3c197ca1e5efede3ff8ff2
                }
                finally
                {
                    await elasticSearchService.DeleteIndexAsync(INDEX_NAME);
                }
            }
        }
Exemplo n.º 2
0
        public async Task OneEvent()
        {
            if (_disableElasticSearch)
            {
                return;
            }

            const string INDEX_NAME = "transfer-logs";

            var elasticClient = CreateLocalElasticClient();

            //surround with "using" so that anything in a buffer is sent on dispose
            using (var elasticSearchService = new ElasticSearchService(elasticClient))
            {
                try
                {
                    //setup
                    await elasticSearchService.CreateIfNotExists(INDEX_NAME);

                    var indexer = elasticSearchService.CreateIndexerForEventLog <TransferEvent_ERC20>(INDEX_NAME, documentsPerBatch: 1);

                    var web3 = new Web3.Web3(BlockchainUrl);
                    var blockchainProcessor     = web3.Processing.Logs.CreateProcessor <TransferEvent_ERC20>((transfer) => indexer.IndexAsync(transfer));
                    var cancellationTokenSource = new CancellationTokenSource();

                    //execute
                    await blockchainProcessor.ExecuteAsync(3146694, cancellationTokenSource.Token, 3146684);

                    //assert
                    await Task.Delay(1000); // allow time to index

                    Assert.Equal(19, await elasticSearchService.CountDocumentsAsync(INDEX_NAME));

                    //http://localhost:9200/transfer-logs/_search?q=from:0x1b31d19b6a9a942bbf3c197ca1e5efede3ff8ff2
                }
                finally
                {
                    await elasticSearchService.DeleteIndexAsync(INDEX_NAME);
                }
            }
        }