Skip to content

Commit

Permalink
feat: Update logic to aggregate token info data: txs count, number of…
Browse files Browse the repository at this point in the history
… holders, total volume, volume 24h
  • Loading branch information
Sotatek-DucPhung committed May 6, 2024
1 parent 2301f1b commit 261c187
Show file tree
Hide file tree
Showing 18 changed files with 1,033 additions and 1,065 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/cardanofoundation/job/model/TokenTxCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cardanofoundation.job.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TokenTxCount {
Long ident;
Long txCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ public void insertAll(List<TokenInfo> tokenInfos) {
field(entityUtil.getColumnField(TokenInfo_.MULTI_ASSET_ID)),
field(entityUtil.getColumnField(TokenInfo_.NUMBER_OF_HOLDERS)),
field(entityUtil.getColumnField(TokenInfo_.VOLUME24H)),
field(entityUtil.getColumnField(TokenInfo_.TOTAL_VOLUME)),
field(entityUtil.getColumnField(TokenInfo_.TX_COUNT)),
field(entityUtil.getColumnField(TokenInfo_.UPDATE_TIME)))
.values(
tokenInfo.getBlockNo(),
tokenInfo.getMultiAssetId(),
tokenInfo.getNumberOfHolders(),
tokenInfo.getVolume24h(),
tokenInfo.getTotalVolume(),
tokenInfo.getTxCount(),
tokenInfo.getUpdateTime());

queries.add(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.cardanofoundation.explorer.common.entity.compositeKey.AddressTxAmountId;
import org.cardanofoundation.explorer.common.entity.ledgersync.AddressTxAmount;
import org.cardanofoundation.job.model.TokenTxCount;
import org.cardanofoundation.job.model.TokenVolume;
import org.cardanofoundation.job.projection.StakeTxProjection;
import org.cardanofoundation.job.projection.UniqueAccountTxCountProjection;

Expand Down Expand Up @@ -52,4 +54,72 @@ Long getCountTxByStakeInDateRange(
@Param("fromDate") Long fromDate,
@Param("toDate") Long toDate);


@Query(value = """
SELECT new org.cardanofoundation.job.model.TokenVolume(ma.id, sum(ata.quantity))
FROM AddressTxAmount ata
JOIN MultiAsset ma ON ata.unit = ma.unit
JOIN Tx tx ON tx.hash = ata.txHash
WHERE ma.id >= :startIdent AND ma.id <= :endIdent
AND tx.id >= :txId
AND ata.quantity > 0
GROUP BY ma.id
""")
List<TokenVolume> sumBalanceAfterTx(@Param("startIdent") Long startIdent,
@Param("endIdent") Long endIdent,
@Param("txId") Long txId);

@Query(value = """
SELECT new org.cardanofoundation.job.model.TokenVolume(ma.id, sum(ata.quantity))
FROM AddressTxAmount ata
JOIN MultiAsset ma ON ata.unit = ma.unit
JOIN Tx tx ON tx.hash = ata.txHash
WHERE ma.id IN :multiAssetIds
AND tx.id >= :txId
AND ata.quantity > 0
GROUP BY ma.id
""")
List<TokenVolume> sumBalanceAfterTx(@Param("multiAssetIds") List<Long> multiAssetIds,
@Param("txId") Long txId);

@Query(value = """
SELECT new org.cardanofoundation.job.model.TokenVolume(ma.id, sum(ata.quantity))
FROM AddressTxAmount ata
JOIN MultiAsset ma ON ata.unit = ma.unit
WHERE ma.id >= :startIdent AND ma.id <= :endIdent
AND ata.quantity > 0
GROUP BY ma.id
""")
List<TokenVolume> getTotalVolumeByIdentInRange(@Param("startIdent") Long startIdent,
@Param("endIdent") Long endIdent);

@Query(value = """
SELECT new org.cardanofoundation.job.model.TokenTxCount(ma.id, count(distinct (ata.txHash)))
FROM AddressTxAmount ata
JOIN MultiAsset ma ON ata.unit = ma.unit
WHERE ma.id >= :startIdent AND ma.id <= :endIdent
GROUP BY ma.id
""")
List<TokenTxCount> getTotalTxCountByIdentInRange(@Param("startIdent") Long startIdent,
@Param("endIdent") Long endIdent);

@Query(value = """
SELECT new org.cardanofoundation.job.model.TokenVolume(ma.id, sum(ata.quantity))
FROM AddressTxAmount ata
JOIN MultiAsset ma ON ata.unit = ma.unit
WHERE ma.id IN :multiAssetIds
AND ata.quantity > 0
GROUP BY ma.id
""")
List<TokenVolume> getTotalVolumeByIdentIn(@Param("multiAssetIds") List<Long> multiAssetIds);

@Query(value = """
SELECT new org.cardanofoundation.job.model.TokenTxCount(ma.id, count(distinct (ata.txHash)))
FROM AddressTxAmount ata
JOIN MultiAsset ma ON ata.unit = ma.unit
WHERE ma.id IN :multiAssetIds
GROUP BY ma.id
""")
List<TokenTxCount> getTotalTxCountByIdentIn(@Param("multiAssetIds") List<Long> multiAssetIds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.cardanofoundation.explorer.common.entity.compositeKey.AddressBalanceId;
import org.cardanofoundation.explorer.common.entity.ledgersync.LatestTokenBalance;
import org.cardanofoundation.job.model.TokenNumberHolders;
import org.cardanofoundation.job.projection.ScriptNumberHolderProjection;

public interface LatestTokenBalanceRepository extends JpaRepository<LatestTokenBalance, AddressBalanceId> {
Expand All @@ -27,6 +28,25 @@ SELECT multiAsset.policy as scriptHash, COALESCE(COUNT(latestTokenBalance), 0) a
List<ScriptNumberHolderProjection> countHolderByPolicyIn(
@Param("policies") Collection<String> policies);

@Query("""
SELECT new org.cardanofoundation.job.model.TokenNumberHolders(multiAsset.id, COUNT(latestTokenBalance))
FROM MultiAsset multiAsset
LEFT JOIN LatestTokenBalance latestTokenBalance ON multiAsset.unit = latestTokenBalance.unit
WHERE multiAsset.id IN :multiAssetIds
GROUP BY multiAsset.id
""")
List<TokenNumberHolders> countHoldersByMultiAssetIdIn(@Param("multiAssetIds") List<Long> multiAssetIds);

@Query("""
SELECT new org.cardanofoundation.job.model.TokenNumberHolders(multiAsset.id, COUNT(latestTokenBalance))
FROM MultiAsset multiAsset
LEFT JOIN LatestTokenBalance latestTokenBalance ON multiAsset.unit = latestTokenBalance.unit
WHERE multiAsset.id >= :startIdent AND multiAsset.id <= :endIdent
GROUP BY multiAsset.id
""")
List<TokenNumberHolders> countHoldersByMultiAssetIdInRange(@Param("startIdent") Long startIdent,
@Param("endIdent") Long endIdent);

@Modifying(clearAutomatically = true)
@Transactional
@Query(value = "REFRESH MATERIALIZED VIEW latest_token_balance", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.cardanofoundation.job.repository.ledgersync;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -34,4 +35,29 @@ Set<String> findPolicyByTxIn(
@Param("fromTxId") Long fromTxId,
@Param("toTxId") Long toTxId,
@Param("types") Collection<ScriptType> types);

@Query("SELECT max(multiAsset.id) FROM MultiAsset multiAsset")
Long getCurrentMaxIdent();


@Query(
"select distinct multiAsset "
+ " from MultiAsset multiAsset "
+ " join AddressTxAmount addressTxAmount on multiAsset.unit = addressTxAmount.unit"
+ " join Tx tx on tx.hash = addressTxAmount.txHash"
+ " join Block block on block.id = tx.blockId"
+ " where block.blockNo > :fromBlockNo and block.blockNo <= :toBlockNo ")
List<MultiAsset> getTokensInTransactionInBlockRange(
@Param("fromBlockNo") Long fromBlockNo, @Param("toBlockNo") Long toBlockNo);

@Query(
"select distinct multiAsset "
+ " from MultiAsset multiAsset "
+ " join AddressTxAmount addressTxAmount on multiAsset.unit = addressTxAmount.unit"
+ " join Tx tx on tx.hash = addressTxAmount.txHash"
+ " join Block block on block.id = tx.blockId"
+ " where block.time >= :fromTime and block.time <= :toTime")
List<MultiAsset> getTokensInTransactionInTimeRange(
@Param("fromTime") Timestamp fromTime, @Param("toTime") Timestamp toTime);

}

This file was deleted.

This file was deleted.

0 comments on commit 261c187

Please sign in to comment.