private async IAsyncEnumerable <EthereumTransactionHash> ListAddressHashesAsync(
            EthereumAddress address,
            Symbol symbol,
            string indexName,
            string attributeName)
        {
            var key = dataKeyProvider.GetKey(symbol);
            var lastEvaluatedKey = new Dictionary <string, AttributeValue>();

            while (!CancellationToken.IsCancellationRequested)
            {
                var response = await DynamoDB.QueryAsync(
                    new QueryRequest()
                {
                    TableName = TableName,
                    IndexName = indexName,
                    Select    = Select.ALL_PROJECTED_ATTRIBUTES,
                    KeyConditionExpression = string.Format(
                        "#{0} = :{0}Val AND #{1} = :{1}Val",
                        attributeName,
                        nameof(DataOperation.ContractAddress)),
                    ExpressionAttributeNames = new Dictionary <string, string>()
                    {
                        [$"#{attributeName}"] = attributeName,
                        [$"#{nameof(DataOperation.ContractAddress)}"] = nameof(DataOperation.ContractAddress)
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        [$":{attributeName}Val"] = new AttributeValue(address),
                        [$":{nameof(DataOperation.ContractAddress)}Val"] = new AttributeValue(key)
                    },
                    ExclusiveStartKey = lastEvaluatedKey.Any()
                            ? lastEvaluatedKey
                            : null
                },
                    CancellationToken);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
                }
                else
                {
                    lastEvaluatedKey = response.LastEvaluatedKey;
                }

                foreach (var attributes in response.Items)
                {
                    if (attributes.ContainsKey(nameof(DataOperation.Hash)))
                    {
                        yield return(new EthereumTransactionHash(attributes[nameof(DataOperation.Hash)].S));
                    }
                }

                if (!lastEvaluatedKey.Any())
                {
                    break;
                }
            }
        }
        private async Task <DataStakingPower> GetByBoundsAsync(EthereumAddress contractAddress, bool latest)
        {
            var response = await DynamoDB.QueryAsync(
                new QueryRequest()
            {
                TableName                = TableName,
                ScanIndexForward         = !latest,
                Select                   = Select.ALL_ATTRIBUTES,
                KeyConditionExpression   = $"#{nameof(DataStakingPower.Address)} = :{nameof(DataStakingPower.Address)}Val",
                ExpressionAttributeNames = new Dictionary <string, string>()
                {
                    [$"#{nameof(DataStakingPower.Address)}"] = nameof(DataStakingPower.Address),
                },
                ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                {
                    [$":{nameof(DataStakingPower.Address)}Val"] = new AttributeValue(contractAddress)
                },
                Limit = 1
            },
                CancellationToken);

            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
            }

            if (response.Items.Count == 1)
            {
                return(Map(response.Items.Single()));
            }

            return(null);
        }
Exemplo n.º 3
0
        private async Task <long> GetBlockNumberAsync(EthereumAddress address, bool latest)
        {
            var response = await DynamoDB.QueryAsync(
                new QueryRequest()
            {
                TableName                = TableName,
                IndexName                = BlockNumberIndexName,
                ScanIndexForward         = !latest,
                Select                   = Select.ALL_PROJECTED_ATTRIBUTES,
                KeyConditionExpression   = $"#{nameof(DataTransaction.Address)} = :{nameof(DataTransaction.Address)}Val",
                ExpressionAttributeNames = new Dictionary <string, string>()
                {
                    [$"#{nameof(DataTransaction.Address)}"] = nameof(DataTransaction.Address),
                },
                ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                {
                    [$":{nameof(DataTransaction.Address)}Val"] = new AttributeValue(address.Address)
                },
                Limit = 1
            },
                CancellationToken);

            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
            }

            if (response.Items.Count == 1 &&
                response.Items.Single().ContainsKey(nameof(DataTransaction.BlockNumber)) &&
                long.TryParse(response.Items.Single()[nameof(DataTransaction.BlockNumber)].N, out long blockNumber))
            {
                return(blockNumber);
            }

            return(default);
        public async IAsyncEnumerable <DataFundPerformance> ListPerformancesAsync(Symbol symbol, DateTime from, DateTime to)
        {
            var key = dataKeyProvider.GetKey(symbol);
            var lastEvaluatedKey = new Dictionary <string, AttributeValue>();

            while (!CancellationToken.IsCancellationRequested)
            {
                var response = await DynamoDB.QueryAsync(
                    new QueryRequest()
                {
                    TableName              = TableName,
                    ScanIndexForward       = false,
                    Select                 = Select.ALL_ATTRIBUTES,
                    KeyConditionExpression = string.Format(
                        "#{0} = :{0}Val AND #{1} >= :{2}Val",
                        nameof(DataFundPerformance.Address),
                        nameof(DataFundPerformance.Date),
                        nameof(from)),
                    ExpressionAttributeNames = new Dictionary <string, string>()
                    {
                        [$"#{nameof(DataFundPerformance.Address)}"] = nameof(DataFundPerformance.Address),
                        [$"#{nameof(DataFundPerformance.Date)}"]    = nameof(DataFundPerformance.Date),
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        [$":{nameof(DataFundPerformance.Address)}Val"] = new AttributeValue(key),
                        [$":{nameof(from)}Val"] = new AttributeValue(from.ToISO8601String())
                    },
                    ExclusiveStartKey = lastEvaluatedKey
                },
                    CancellationToken);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
                }
                else
                {
                    lastEvaluatedKey = response.LastEvaluatedKey;
                }

                foreach (var perf in response.Items
                         .Select(Map)
                         .OrderBy(p => p.Date))
                {
                    if (perf.Date > to)
                    {
                        break;
                    }

                    yield return(perf);
                }

                if (!lastEvaluatedKey.Any())
                {
                    break;
                }
            }
        }
        public async IAsyncEnumerable <DataOperation> ListOperationsAsync(EthereumTransactionHash hash)
        {
            var lastEvaluatedKey = new Dictionary <string, AttributeValue>();

            while (!CancellationToken.IsCancellationRequested)
            {
                var response = await DynamoDB.QueryAsync(
                    new QueryRequest()
                {
                    TableName = TableName,
                    Select    = Select.ALL_ATTRIBUTES,
                    KeyConditionExpression   = string.Format("#{0} = :{0}Val", nameof(DataOperation.Hash)),
                    ExpressionAttributeNames = new Dictionary <string, string>()
                    {
                        [$"#{nameof(DataOperation.Hash)}"] = nameof(DataOperation.Hash),
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        [$":{nameof(DataOperation.Hash)}Val"] = new AttributeValue {
                            S = hash
                        },
                    },
                    ExclusiveStartKey = lastEvaluatedKey.Any()
                            ? lastEvaluatedKey
                            : null
                },
                    CancellationToken);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
                }
                else
                {
                    lastEvaluatedKey = response.LastEvaluatedKey;
                }

                foreach (var attributes in response.Items)
                {
                    yield return(Map(attributes));
                }

                if (!lastEvaluatedKey.Any())
                {
                    break;
                }
            }
        }
        private async Task <DateTime?> GetDateAsync(Symbol symbol, bool latest)
        {
            var key      = dataKeyProvider.GetKey(symbol);
            var response = await DynamoDB.QueryAsync(
                new QueryRequest()
            {
                TableName                = TableName,
                ScanIndexForward         = !latest,
                Select                   = Select.ALL_ATTRIBUTES,
                KeyConditionExpression   = $"#{nameof(DataFundPerformance.Address)} = :{nameof(DataFundPerformance.Address)}Val",
                ExpressionAttributeNames = new Dictionary <string, string>()
                {
                    [$"#{nameof(DataFundPerformance.Address)}"] = nameof(DataFundPerformance.Address),
                },
                ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                {
                    [$":{nameof(DataFundPerformance.Address)}Val"] = new AttributeValue(key)
                },
                Limit = 1
            },
                CancellationToken);

            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
            }

            if (response.Items.Count == 1 &&
                response.Items.Single().ContainsKey(nameof(DataFundPerformance.Date)) &&
                DateTimeOffset.TryParse(
                    response.Items.Single()[nameof(DataFundPerformance.Date)].S,
                    null,
                    DateTimeStyles.AssumeUniversal,
                    out DateTimeOffset date))
            {
                return(date.UtcDateTime);
            }

            return(null);
        }
        public async IAsyncEnumerable <DataTransaction> ListTransactionsAsync(
            Symbol symbol,
            EthereumTransactionHash?startHash,
            DateTime?offset,
            DateTime from,
            DateTime to)
        {
            var key = dataKeyProvider.GetKey(symbol);
            var lastEvaluatedKey = offset.HasValue && startHash.HasValue
                ? new Dictionary <string, AttributeValue>()
            {
                [nameof(DataTransaction.Address)]     = new AttributeValue(key),
                [nameof(DataTransaction.Hash)]        = new AttributeValue(startHash.Value),
                [nameof(DataTransaction.ConfirmedAt)] = new AttributeValue(offset.Value.ToISO8601String())
            }
                : null;

            var response = await DynamoDB.QueryAsync(
                new QueryRequest()
            {
                TableName              = TableName,
                ScanIndexForward       = true,
                IndexName              = DateIndexName,
                Select                 = Select.ALL_ATTRIBUTES,
                KeyConditionExpression = string.Format(
                    "#{0} = :{0}Val AND #{1} >= :{2}Val",
                    nameof(DataTransaction.Address),
                    nameof(DataTransaction.ConfirmedAt),
                    nameof(from)),
                ExpressionAttributeNames = new Dictionary <string, string>()
                {
                    [$"#{nameof(DataTransaction.Address)}"]     = nameof(DataTransaction.Address),
                    [$"#{nameof(DataTransaction.ConfirmedAt)}"] = nameof(DataTransaction.ConfirmedAt),
                },
                ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                {
                    [$":{nameof(DataTransaction.Address)}Val"] = new AttributeValue(key),
                    [$":{nameof(from)}Val"] = new AttributeValue {
                        S = from.ToISO8601String()
                    }
                },
                Limit             = PageSize,
                ExclusiveStartKey = lastEvaluatedKey
            },
                CancellationToken);

            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
            }
            else if (
                response.LastEvaluatedKey.Any() &&
                DateTime.Parse(response.LastEvaluatedKey[nameof(DataTransaction.ConfirmedAt)].S) <= to)
            {
                httpContextAccessor.HttpContext.Response.Headers.Add(
                    Headers.PaginationId,
                    response.LastEvaluatedKey[nameof(DataTransaction.Hash)].S);

                httpContextAccessor.HttpContext.Response.Headers.Add(
                    Headers.PaginationOffset,
                    response.LastEvaluatedKey[nameof(DataTransaction.ConfirmedAt)].S);
            }

            foreach (var transaction in response.Items
                     .Select(Map)
                     .Where(t => t.ConfirmedAt <= to)
                     .OrderBy(t => t.ConfirmedAt))
            {
                yield return(transaction);
            }
        }
        private async IAsyncEnumerable <DataTransaction> ListAddressTransactionsAsync(
            Symbol symbol,
            EthereumAddress address,
            bool outbound,
            EthereumAddress?filterAddress = null,
            DateTime?from = null,
            DateTime?to   = null)
        {
            var key = dataKeyProvider.GetKey(symbol);
            var lastEvaluatedKey = new Dictionary <string, AttributeValue>();
            var attributeName    = outbound
                ? nameof(DataTransaction.Sender)
                : nameof(DataTransaction.Recipient);
            var filterAttributeName = outbound
                ? nameof(DataTransaction.Recipient)
                : nameof(DataTransaction.Sender);

            while (!CancellationToken.IsCancellationRequested)
            {
                var request = new QueryRequest()
                {
                    TableName = TableName,
                    IndexName = outbound
                        ? OutboundIndexName
                        : InboundIndexName,
                    Select = Select.ALL_ATTRIBUTES,
                    KeyConditionExpression = string.Format(
                        "#{0} = :{0}Val AND #{1} = :{1}Val",
                        attributeName,
                        nameof(DataTransaction.Address)),
                    ExpressionAttributeNames = new Dictionary <string, string>()
                    {
                        [$"#{attributeName}"] = attributeName,
                        [$"#{nameof(DataTransaction.Address)}"] = nameof(DataTransaction.Address),
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        [$":{attributeName}Val"] = new AttributeValue(address),
                        [$":{nameof(DataTransaction.Address)}Val"] = new AttributeValue(key)
                    },
                    Limit             = PageSize,
                    ExclusiveStartKey = lastEvaluatedKey.Any()
                        ? lastEvaluatedKey
                        : null
                };

                var response = await DynamoDB.QueryAsync(request, CancellationToken);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
                }
                else
                {
                    lastEvaluatedKey = response.LastEvaluatedKey;
                }

                foreach (var attributes in response.Items
                         .Where(x =>
                                !filterAddress.HasValue ||
                                filterAddress.Value.Address.Equals(x[filterAttributeName].S, StringComparison.OrdinalIgnoreCase)))
                {
                    var transaction = Map(attributes);

                    if (!from.HasValue ||
                        !to.HasValue ||
                        (from.HasValue && to.HasValue &&
                         from <= transaction.ConfirmedAt &&
                         to >= transaction.ConfirmedAt))
                    {
                        yield return(transaction);
                    }
                }

                if (!lastEvaluatedKey.Any())
                {
                    break;
                }
            }
        }
        public async IAsyncEnumerable <DataStakingPower> ListStakingPowersAsync(
            EthereumAddress contractAddress,
            DateTime from,
            DateTime to)
        {
            var lastEvaluatedKey = new Dictionary <string, AttributeValue>();

            while (!CancellationToken.IsCancellationRequested)
            {
                var response = await DynamoDB.QueryAsync(
                    new QueryRequest()
                {
                    TableName              = TableName,
                    ScanIndexForward       = false,
                    Select                 = Select.SPECIFIC_ATTRIBUTES,
                    KeyConditionExpression = string.Format(
                        "#{0} = :{0}Val AND #{1} >= :{2}Val",
                        nameof(DataStakingPower.Address),
                        nameof(DataStakingPower.Date),
                        nameof(from)),
                    ExpressionAttributeNames = new Dictionary <string, string>()
                    {
                        [$"#{nameof(DataStakingPower.Address)}"] = nameof(DataStakingPower.Address),
                        [$"#{nameof(DataStakingPower.Date)}"]    = nameof(DataStakingPower.Date),
                    },
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        [$":{nameof(DataStakingPower.Address)}Val"] = new AttributeValue(contractAddress),
                        [$":{nameof(from)}Val"] = new AttributeValue(from.ToISO8601String())
                    },
                    ProjectionExpression = string.Join(",", new List <string>()
                    {
                        $"#{nameof(DataStakingPower.Address)}",
                        $"#{nameof(DataStakingPower.Date)}",
                        $"{nameof(DataStakingPower.Power)}",
                        $"{nameof(DataStakingPower.Summary)}"
                    }),
                    Limit             = PageSize,
                    ExclusiveStartKey = lastEvaluatedKey
                },
                    CancellationToken);

                if (response.HttpStatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException($"Response code did not indicate success: {response.HttpStatusCode}");
                }
                else
                {
                    lastEvaluatedKey = response.LastEvaluatedKey;
                }

                foreach (var power in response.Items
                         .Select(Map)
                         .OrderBy(p => p.Date))
                {
                    if (power.Date > to)
                    {
                        break;
                    }

                    yield return(power);
                }

                if (!lastEvaluatedKey.Any())
                {
                    break;
                }
            }
        }