Skip to content

Commit

Permalink
Merge pull request #1091 from cardano-foundation/feat/MET-1963-gov-ac…
Browse files Browse the repository at this point in the history
…tion-filter-dev-test

feat: filter type by votertype and find by poolView
  • Loading branch information
Sotatek-DucPhung committed Mar 27, 2024
2 parents 6072534 + e2b93b5 commit b7333bb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
Expand Up @@ -45,7 +45,7 @@ public class GovernanceActionController {
summary = "Get governance action that vote by DRep or pool",
tags = {"gov-actions"})
public ResponseEntity<BaseFilterResponse<GovernanceActionResponse>> getGovActionByFilter(
@PathVariable @Parameter(description = "The DRep hash or pool hash")
@PathVariable @Parameter(description = "The DRep hash or pool hash or pool view")
String dRepHashOrPoolHash,
@ParameterObject GovernanceActionFilter governanceActionFilter,
@ParameterObject
Expand Down
@@ -1,5 +1,6 @@
package org.cardanofoundation.explorer.api.repository.ledgersync;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -34,19 +35,21 @@ public interface GovernanceActionRepository
+ " where (:vote is null or (:vote != 'NONE' and :vote = vp.vote) or (:vote = 'NONE' and vp.vote is null))"
+ " and (:isRepeatVote is null or (vp.repeatVote = :isRepeatVote))"
+ " and (:gapStatus is null or (gapInfo.status = :gapStatus))"
+ " and (:type is null or (gap.type = :type))"
+ " and gap.type in (:type)"
+ " and (gap.slot >= :slot)"
+ " and (gap.blockTime >= :from)"
+ " and (gap.blockTime <= :to)")
+ " and (gap.blockTime <= :to)"
+ " and (:txHash is null or gap.txHash = :txHash)")
Page<GovernanceActionProjection> getAllByFilter(
@Param("isRepeatVote") Boolean isRepeatVote,
@Param("gapStatus") GovActionStatus gapStatus,
@Param("vote") Vote vote,
@Param("voterHash") String dRepHash,
@Param("type") GovActionType type,
@Param("type") List<GovActionType> type,
@Param("from") Long from,
@Param("to") Long to,
@Param("slot") Long slot,
@Param("txHash") String txHash,
Pageable pageable);

@Query(
Expand Down
Expand Up @@ -270,4 +270,7 @@ Page<PoolRegistrationProjection> getPoolRegistrationByPool(
+ " join Delegation d on d.poolHash.id = ph.id"
+ " where ph.hashRaw =:poolHash")
Long getSlotNoWhenFirstDelegationByPoolHash(@Param("poolHash") String poolHash);

@Query(value = "select ph.hashRaw from PoolHash ph where ph.view = :view")
Optional<String> getHashRawByView(@Param("view") String view);
}
Expand Up @@ -6,6 +6,7 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -80,6 +81,13 @@ public BaseFilterResponse<GovernanceActionResponse> getGovernanceActions(
BaseFilterResponse<GovernanceActionResponse> govActionResponse = new BaseFilterResponse<>();
govActionResponse.setData(List.of());

if (dRepHashOrPoolHash.toLowerCase().startsWith("pool")) {
dRepHashOrPoolHash =
poolHashRepository
.getHashRawByView(dRepHashOrPoolHash)
.orElseThrow(() -> new BusinessException(BusinessCode.POOL_NOT_FOUND));
}

Long slot = null;
if (governanceActionFilter.getVoterType().equals(VoterType.DREP_KEY_HASH)) {
slot = dRepRegistrationRepository.getSlotOfDRepRegistration(dRepHashOrPoolHash);
Expand All @@ -92,12 +100,10 @@ public BaseFilterResponse<GovernanceActionResponse> getGovernanceActions(
? null
: Vote.valueOf(governanceActionFilter.getVoteType().name());

org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
govActionType =
governanceActionFilter.getActionType().equals(GovActionType.ALL)
? null
: org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.valueOf(governanceActionFilter.getActionType().name());
List<org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType>
govActionTypeList =
getGovActionTypeByVoterType(
governanceActionFilter.getVoterType(), governanceActionFilter.getActionType());

long fromDate = Timestamp.valueOf(MIN_TIME).getTime() / 1000;
long toDate =
Expand Down Expand Up @@ -127,10 +133,11 @@ public BaseFilterResponse<GovernanceActionResponse> getGovernanceActions(
govActionStatus,
vote,
dRepHashOrPoolHash,
govActionType,
govActionTypeList,
fromDate,
toDate,
slot,
governanceActionFilter.getGovernanceActionTxHash(),
pageable);

List<GovernanceActionResponse> governanceActionResponses =
Expand All @@ -148,6 +155,47 @@ public BaseFilterResponse<GovernanceActionResponse> getGovernanceActions(
return govActionResponse;
}

private List<org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType>
getGovActionTypeByVoterType(VoterType voterType, GovActionType govActionType) {
List<org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType>
govActionTypeList =
new ArrayList<>(
Arrays.asList(
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration
.GovActionType.values()));

if (govActionType.equals(
org.cardanofoundation.explorer.api.common.enumeration.GovActionType.ALL)) {
if (voterType.equals(VoterType.STAKING_POOL_KEY_HASH)) {
govActionTypeList.remove(
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.UPDATE_COMMITTEE);
govActionTypeList.remove(
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.PARAMETER_CHANGE_ACTION);
govActionTypeList.remove(
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.TREASURY_WITHDRAWALS_ACTION);
}
} else {
govActionTypeList = new ArrayList<>();
govActionTypeList.add(
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType.valueOf(
govActionType.name()));
if (voterType.equals(VoterType.STAKING_POOL_KEY_HASH)) {
govActionTypeList.removeAll(
List.of(
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.UPDATE_COMMITTEE,
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.PARAMETER_CHANGE_ACTION,
org.cardanofoundation.explorer.common.entity.ledgersync.enumeration.GovActionType
.TREASURY_WITHDRAWALS_ACTION));
}
}
return govActionTypeList;
}

@Override
public GovernanceActionDetailsResponse getGovernanceActionDetails(
String dRepHashOrPoolHash, GovernanceActionRequest governanceActionRequest) {
Expand Down

0 comments on commit b7333bb

Please sign in to comment.