Skip to content

Commit

Permalink
feat: update votes section
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-ThinhVu committed May 9, 2024
1 parent ad9eb6b commit b8f6eca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package org.cardanofoundation.job.repository.ledgersync;

import java.util.List;
import java.util.Set;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import org.cardanofoundation.explorer.common.entity.ledgersync.StakeAddress;

public interface StakeAddressRepository extends JpaRepository<StakeAddress, Long> {

StakeAddress findByView(@Param("aLong") String aLong);

@Query(
value =
"""
select sa from StakeAddress sa
where sa.view in :addresses and not exists (select true from StakeAddress sa2 where sa2.view = sa.view and sa2.id > sa.id)
""")
List<StakeAddress> findStakeAddressesByViewIn(@Param("addresses") Set<String> addresses);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cardanofoundation.job.schedules;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -24,6 +25,7 @@
import org.cardanofoundation.explorer.common.entity.explorer.DRepInfo;
import org.cardanofoundation.explorer.common.entity.ledgersync.DRepRegistrationEntity;
import org.cardanofoundation.explorer.common.entity.ledgersync.DRepRegistrationEntity_;
import org.cardanofoundation.explorer.common.entity.ledgersync.StakeAddress;
import org.cardanofoundation.job.mapper.DRepMapper;
import org.cardanofoundation.job.projection.DelegationVoteProjection;
import org.cardanofoundation.job.projection.LatestEpochVotingProcedureProjection;
Expand All @@ -33,6 +35,7 @@
import org.cardanofoundation.job.repository.ledgersync.EpochParamRepository;
import org.cardanofoundation.job.repository.ledgersync.EpochRepository;
import org.cardanofoundation.job.repository.ledgersync.LatestVotingProcedureRepository;
import org.cardanofoundation.job.repository.ledgersync.StakeAddressRepository;

@Service
@RequiredArgsConstructor
Expand All @@ -48,6 +51,7 @@ public class DRepInfoSchedule {
private final DelegationVoteRepository delegationVoteRepository;
private final EpochParamRepository epochParamRepository;
private final LatestVotingProcedureRepository latestVotingProcedureRepository;
private final StakeAddressRepository stakeAddressRepository;
private final EpochRepository epochRepository;

private final DRepMapper dRepMapper;
Expand Down Expand Up @@ -102,6 +106,8 @@ private void saveDRepInfo(
List<DelegationVoteProjection> delegationVoteProjectionList =
delegationVoteRepository.findAllByDRepHashIn(drepHashSet);

Map<String, BigInteger> activeStakeMap = calculateActiveStake(delegationVoteProjectionList);

Map<String, List<DelegationVoteProjection>> delegationVoteMap =
delegationVoteProjectionList.stream()
.collect(Collectors.groupingBy(DelegationVoteProjection::getDrepHash));
Expand Down Expand Up @@ -145,7 +151,8 @@ private void saveDRepInfo(
} else {
dRepMapper.updateByDRepRegistration(dRepInfo, dRepRegistrationEntity);
}
dRepInfo.setActiveVoteStake(null);
dRepInfo.setActiveVoteStake(
activeStakeMap.getOrDefault(dRepInfo.getDrepHash(), BigInteger.ZERO));
dRepInfo.setLiveStake(null);
dRepInfo.setDelegators(
countDelegation.getOrDefault(dRepInfo.getDrepHash(), 0L).intValue());
Expand All @@ -164,4 +171,36 @@ private void saveDRepInfo(

dRepInfoRepository.saveAll(dRepInfoMap.values());
}

private Map<String, BigInteger> calculateActiveStake(
List<DelegationVoteProjection> delegationVoteProjectionList) {
Map<String, Set<String>> delegatorMap =
delegationVoteProjectionList.stream()
.collect(
Collectors.groupingBy(
DelegationVoteProjection::getDrepHash,
Collectors.mapping(DelegationVoteProjection::getAddress, Collectors.toSet())));

Set<String> stakeAddressSet =
delegationVoteProjectionList.stream()
.map(DelegationVoteProjection::getAddress)
.collect(Collectors.toSet());
List<StakeAddress> stakeAddressList =
stakeAddressRepository.findStakeAddressesByViewIn(stakeAddressSet);

Map<String, BigInteger> stakeAddressBalanceMap =
stakeAddressList.stream()
.collect(Collectors.toMap(StakeAddress::getView, StakeAddress::getBalance));

return delegatorMap.entrySet().stream()
.collect(
Collectors.toMap(
Entry::getKey,
entry -> {
Set<String> delegators = entry.getValue();
return delegators.stream()
.map(stakeAddressBalanceMap::get)
.reduce(BigInteger.ZERO, BigInteger::add);
}));
}
}

0 comments on commit b8f6eca

Please sign in to comment.