public async Task IndexCashinEventsForErc20Deposits() { var indexerStatusResponse = await _indexerApi.ApiIsAliveGetWithHttpMessagesAsync(); if (indexerStatusResponse.Response.IsSuccessStatusCode) { var responseContent = await indexerStatusResponse.Response.Content.ReadAsStringAsync(); var indexerStatus = JObject.Parse(responseContent); var lastIndexedBlock = BigInteger.Parse(indexerStatus["blockchainTip"].Value <string>()); var lastSyncedBlock = await GetLastSyncedBlockNumber(HotWalletMarker); while (lastSyncedBlock <= lastIndexedBlock) { //Get all transfers from block var transfersResponse = await _indexerApi.ApiErc20TransferHistoryGetErc20TransfersPostAsync ( new GetErc20TransferHistoryRequest { BlockNumber = (long)lastSyncedBlock, } ); switch (transfersResponse) { case IEnumerable <Erc20TransferHistoryResponse> transfers: foreach (var transfer in transfers) { // Ignore transfers from not deposit contract addresses var checkResult = await _depositContractService.ContainsWithTypeAsync(transfer.To); if (!checkResult.Item1) { continue; } string trHash = transfer.TransactionHash ?? ""; string id = (await _airHotWalletCashoutTransactionRepository.GetByTransactionHashAsync(trHash)) ?.OperationId ?? (await _lpHotWalletCashoutTransactionRepository.GetByTransactionHashAsync(trHash)) ?.OperationId ?? null; await _rabbitQueuePublisher.PublshEvent(new TransferEvent(id, transfer.TransactionHash, transfer.TransferAmount, transfer.Contract, transfer.FromProperty, transfer.To, transfer.BlockHash, (ulong)transfer.BlockNumber, SenderType.Customer, EventType.Detected, (Job.EthereumCore.Contracts.Enums.LykkePay.WorkflowType)checkResult.Item2, DateTime.UtcNow )); } break; case ApiException exception: throw new Exception($"Ethereum indexer responded with error: {exception.Error.Message}"); default: throw new Exception($"Ethereum indexer returned unexpected response"); } var blockCurrent = (await _indexerApi.ApiBlockNumberByBlockNumberGetAsync((long)lastSyncedBlock)) as BlockResponse; if (blockCurrent == null) { return; } var parentBlock = await _blockSyncedRepository.GetByPartitionAndHashAsync(HotWalletMarker, blockCurrent.ParentHash); if (parentBlock == null) { lastSyncedBlock--; await _blockSyncedRepository.DeleteByPartitionAndHashAsync(HotWalletMarker, blockCurrent.ParentHash); continue; } await _blockSyncedRepository.InsertAsync(new BlockSyncedByHash() { BlockNumber = lastSyncedBlock.ToString(), Partition = HotWalletMarker, BlockHash = blockCurrent.BlockHash }); lastSyncedBlock++; } } else { throw new Exception("Can not obtain ethereum indexer status."); } }
/// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task ApiIsAliveGetAsync(this IEthereumSamuraiAPI operations, CancellationToken cancellationToken = default(CancellationToken)) { (await operations.ApiIsAliveGetWithHttpMessagesAsync(null, cancellationToken).ConfigureAwait(false)).Dispose(); }
public async Task IndexCashinEventsForErc20Deposits() { var indexerStatusResponse = await _indexerApi.ApiIsAliveGetWithHttpMessagesAsync(); if (indexerStatusResponse.Response.IsSuccessStatusCode) { var responseContent = await indexerStatusResponse.Response.Content.ReadAsStringAsync(); var indexerStatus = JObject.Parse(responseContent); var lastIndexedBlock = BigInteger.Parse(indexerStatus["blockchainTip"].Value <string>()); var lastSyncedBlock = await GetLastSyncedBlockNumber(Erc20HotWalletMarker); while (++lastSyncedBlock <= lastIndexedBlock - _baseSettings.Level2TransactionConfirmation) { var transfersResponse = await _indexerApi.ApiErc20TransferHistoryGetErc20TransfersPostAsync ( new GetErc20TransferHistoryRequest { AssetHolder = _settingsWrapper.Ethereum.HotwalletAddress?.ToLower(), BlockNumber = (long)lastSyncedBlock, } ); switch (transfersResponse) { case IEnumerable <Erc20TransferHistoryResponse> transfers: foreach (var transfer in transfers) { // Ignore transfers from not deposit contract addresses if (!await _depositContractService.ContainsAsync(transfer.FromProperty)) { continue; } var coinTransactionMessage = new CoinTransactionMessage { TransactionHash = transfer.TransactionHash }; await _cashinEventRepository.InsertAsync(new CashinEvent { CoinAdapterAddress = Erc20HotWalletMarker, Amount = transfer.TransferAmount, TransactionHash = transfer.TransactionHash, UserAddress = transfer.FromProperty, ContractAddress = transfer.Contract }); await _cointTransactionQueue.PutRawMessageAsync(JsonConvert.SerializeObject(coinTransactionMessage)); } break; case ApiException exception: throw new Exception($"Ethereum indexer responded with error: {exception.Error.Message}"); default: throw new Exception($"Ethereum indexer returned unexpected response"); } await _blockSyncedRepository.InsertAsync(new BlockSynced { BlockNumber = lastSyncedBlock.ToString(), CoinAdapterAddress = Erc20HotWalletMarker }); } } else { throw new Exception("Can not obtain ethereum indexer status."); } }