Skip to content

Commit

Permalink
Merge pull request #385 from cardano-foundation/feat/MET-2055-update-…
Browse files Browse the repository at this point in the history
…votes-section-dev-test

Feat/met 2055 update votes section dev test
  • Loading branch information
Sotatek-DucPhung committed May 9, 2024
2 parents f8a75dd + 5cd3105 commit f404b8e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
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 @@ -132,7 +138,6 @@ private void saveDRepInfo(
if (dRepRegistrationEntity.getType().equals(DRepActionType.REG_DREP_CERT)) {
dRepInfo = dRepMapper.fromDRepRegistration(dRepRegistrationEntity);
dRepInfo.setCreatedAt(dRepRegistrationEntity.getBlockTime());
dRepInfo.setUpdatedAt(dRepRegistrationEntity.getBlockTime());
// TODO calculate live stake, active vote stake, delegators
dRepInfo.setActiveVoteStake(null);
dRepInfo.setLiveStake(null);
Expand All @@ -147,12 +152,10 @@ private void saveDRepInfo(
dRepMapper.updateByDRepRegistration(dRepInfo, dRepRegistrationEntity);
dRepInfo.setUpdatedAt(dRepRegistrationEntity.getBlockTime());
}

// TODO calculate live stake, active vote stake, voting power
dRepInfo.setActiveVoteStake(null);
dRepInfo.setActiveVoteStake(
activeStakeMap.getOrDefault(dRepInfo.getDrepHash(), BigInteger.ZERO));
dRepInfo.setLiveStake(null);
dRepInfo.setVotingPower(null);

dRepInfo.setDelegators(
countDelegation.getOrDefault(dRepInfo.getDrepHash(), 0L).intValue());
dRepInfo.setStatus(
Expand All @@ -170,4 +173,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 f404b8e

Please sign in to comment.