-
Notifications
You must be signed in to change notification settings - Fork 134
Convert exchange rate API to Java #12166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.dto; | ||
|
|
||
| import com.google.protobuf.GeneratedMessage; | ||
| import org.hiero.mirror.common.domain.file.FileData; | ||
|
|
||
| public record SystemFile<T extends GeneratedMessage>(FileData fileData, T protobuf) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.mapper; | ||
|
|
||
| import static org.hiero.mirror.restjava.mapper.CommonMapper.QUALIFIER_TIMESTAMP; | ||
|
|
||
| import com.hederahashgraph.api.proto.java.ExchangeRateSet; | ||
| import org.hiero.mirror.rest.model.ExchangeRate; | ||
| import org.hiero.mirror.rest.model.NetworkExchangeRateSetResponse; | ||
| import org.hiero.mirror.restjava.dto.SystemFile; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper(config = MapperConfiguration.class, uses = CommonMapper.class) | ||
| public interface ExchangeRateMapper { | ||
|
|
||
| @Mapping(source = "protobuf.currentRate", target = "currentRate") | ||
| @Mapping(source = "protobuf.nextRate", target = "nextRate") | ||
| @Mapping(source = "fileData.consensusTimestamp", target = "timestamp", qualifiedByName = QUALIFIER_TIMESTAMP) | ||
| NetworkExchangeRateSetResponse map(SystemFile<ExchangeRateSet> source); | ||
|
|
||
| @Mapping(source = "centEquiv", target = "centEquivalent") | ||
| @Mapping(source = "hbarEquiv", target = "hbarEquivalent") | ||
| ExchangeRate map(com.hederahashgraph.api.proto.java.ExchangeRate source); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.parameter; | ||
|
|
||
| import java.util.regex.Pattern; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.hiero.mirror.common.util.DomainUtils; | ||
| import org.hiero.mirror.restjava.common.RangeOperator; | ||
| import org.hiero.mirror.restjava.common.RangeParameter; | ||
|
|
||
| public record TimestampParameter(RangeOperator operator, Long value) implements RangeParameter<Long> { | ||
|
|
||
| public static final TimestampParameter EMPTY = new TimestampParameter(null, null); | ||
|
|
||
| private static final String ERROR = "Invalid timestamp parameter"; | ||
| private static final Pattern PATTERN = | ||
| Pattern.compile("^((eq|gt|gte|lt|lte|ne):)?(\\d{1,17})(\\.(\\d{1,9}))?$", Pattern.CASE_INSENSITIVE); | ||
|
|
||
| public static TimestampParameter valueOf(String param) { | ||
| if (StringUtils.isBlank(param)) { | ||
| return EMPTY; | ||
| } | ||
|
|
||
| var matcher = PATTERN.matcher(param); | ||
| if (!matcher.matches()) { | ||
| throw new IllegalArgumentException(ERROR); | ||
| } | ||
|
|
||
| final var operator = parseOperator(matcher.group(2)); | ||
| final var timestamp = parseTimestamp(matcher.group(3), matcher.group(5)); | ||
| return new TimestampParameter(operator, timestamp); | ||
| } | ||
|
|
||
| private static RangeOperator parseOperator(String name) { | ||
| if (StringUtils.isEmpty(name)) { | ||
| return RangeOperator.EQ; | ||
| } | ||
|
|
||
| final var operator = RangeOperator.of(name); | ||
|
|
||
| if (operator == RangeOperator.NE) { | ||
| throw new IllegalArgumentException(ERROR); | ||
| } | ||
|
|
||
| return operator; | ||
| } | ||
|
|
||
| private static long parseTimestamp(String secondsStr, String nanosStr) { | ||
| try { | ||
| final long seconds = Long.parseLong(secondsStr); | ||
| final long nanos = StringUtils.isNotEmpty(nanosStr) ? Long.parseLong(nanosStr) : 0L; | ||
| return DomainUtils.convertToNanos(seconds, nanos); | ||
| } catch (RuntimeException e) { | ||
| throw new IllegalArgumentException(ERROR); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.repository; | ||
|
|
||
| import java.util.Optional; | ||
| import org.hiero.mirror.common.domain.file.FileData; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.CrudRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface FileDataRepository extends CrudRepository<FileData, Long> { | ||
|
|
||
| @Query( | ||
|
Check notice on line 14 in rest-java/src/main/java/org/hiero/mirror/restjava/repository/FileDataRepository.java
|
||
| nativeQuery = true, | ||
| value = | ||
| """ | ||
| select | ||
| min(consensus_timestamp) as consensus_timestamp, | ||
| ?1 as entity_id, | ||
| string_agg(file_data, '' order by consensus_timestamp) as file_data, | ||
| null as transaction_type | ||
| from file_data | ||
| where entity_id = ?1 | ||
| and consensus_timestamp >= ( | ||
| select consensus_timestamp | ||
| from file_data | ||
| where entity_id = ?1 | ||
| and consensus_timestamp >= ?2 | ||
| and consensus_timestamp <= ?3 | ||
| and (transaction_type = 17 or (transaction_type = 19 and length(file_data) <> 0)) | ||
| order by consensus_timestamp desc | ||
| limit 1 | ||
| ) and consensus_timestamp >= ?2 and consensus_timestamp <= ?3 | ||
| """) | ||
| Optional<FileData> getFileAtTimestamp(long fileId, long lowerTimestamp, long upperTimestamp); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package org.hiero.mirror.restjava.service; | ||
|
|
||
| import com.hederahashgraph.api.proto.java.ExchangeRateSet; | ||
| import org.hiero.mirror.restjava.dto.SystemFile; | ||
| import org.jspecify.annotations.NullMarked; | ||
|
|
||
| @NullMarked | ||
| public interface FileService { | ||
|
|
||
| SystemFile<ExchangeRateSet> getExchangeRate(Bound timestamp); | ||
|
Check notice on line 12 in rest-java/src/main/java/org/hiero/mirror/restjava/service/FileService.java
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.