Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
refactor: replace logic to get token metadata in get native script an…
Browse files Browse the repository at this point in the history
…d tx details
  • Loading branch information
Sotatek-ThinhVu committed Jan 15, 2024
1 parent 8055ccb commit 7e259ff
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.cardanofoundation.explorer.api.mapper;

import org.cardanofoundation.explorer.api.model.response.token.TokenMetadataResponse;
import org.cardanofoundation.explorer.api.model.response.token.TokenMintTxResponse;
import org.cardanofoundation.explorer.api.model.response.tx.TxMintingResponse;
import org.cardanofoundation.explorer.api.projection.AddressInputOutputProjection;
import org.cardanofoundation.explorer.api.projection.MintProjection;
import org.cardanofoundation.explorer.api.util.HexUtils;
import org.cardanofoundation.explorer.consumercommon.entity.MaTxMint;
import org.mapstruct.Mapper;
Expand All @@ -11,13 +13,6 @@
@Mapper(componentModel = "spring",imports={HexUtils.class})
public interface MaTxMintMapper {

@Mapping(target = "assetName",
expression = "java(HexUtils.fromHex(maTxMint.getIdent().getName(), maTxMint.getIdent().getFingerprint()))")
@Mapping(target = "policy", source = "ident.policy")
@Mapping(target = "assetQuantity", source = "quantity")
@Mapping(target = "assetId", expression = "java(maTxMint.getIdent().getFingerprint())")
TxMintingResponse fromMaTxMint(MaTxMint maTxMint);

@Mapping(target = "assetName",
expression = "java(HexUtils.fromHex(input.getAssetName(), input.getAssetId()))")
@Mapping(target = "assetId", expression = "java(input.getAssetId())")
Expand All @@ -28,4 +23,23 @@ TxMintingResponse fromAddressInputOutputProjection(
@Mapping(target = "amount", source = "quantity")
@Mapping(target = "time", source = "tx.block.time")
TokenMintTxResponse fromMaTxMintToTokenMintTx(MaTxMint maTxMint);

@Mapping(target = "assetName",
expression = "java(HexUtils.fromHex(mintProjection.getName(), mintProjection.getFingerprint()))")
@Mapping(target = "policy", source = "policy")
@Mapping(target = "assetQuantity", source = "assetQuantity")
@Mapping(target = "assetId", source = "fingerprint")
@Mapping(target = "metadata",
expression = "java(getMetadata(mintProjection))")
TxMintingResponse fromMintProjectionToTxMintingResponse(MintProjection mintProjection);

default TokenMetadataResponse getMetadata(MintProjection mintProjection){
TokenMetadataResponse tokenMetadataResponse = new TokenMetadataResponse();
tokenMetadataResponse.setUrl(mintProjection.getUrl());
tokenMetadataResponse.setTicker(mintProjection.getTicker());
tokenMetadataResponse.setLogo(mintProjection.getLogo());
tokenMetadataResponse.setDecimals(mintProjection.getDecimals());
tokenMetadataResponse.setDescription(mintProjection.getDescription());
return tokenMetadataResponse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.cardanofoundation.explorer.api.projection;

import java.math.BigInteger;

public interface MintProjection {
String getName();

String getPolicy();


BigInteger getAssetQuantity();

String getFingerprint();

String getUrl();

String getTicker();

Integer getDecimals();

String getLogo();

String getDescription();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cardanofoundation.explorer.api.repository.ledgersync;

import java.math.BigInteger;
import org.cardanofoundation.explorer.api.projection.MintProjection;
import org.cardanofoundation.explorer.consumercommon.entity.MaTxMint;
import org.cardanofoundation.explorer.consumercommon.entity.MaTxMint_;
import org.cardanofoundation.explorer.consumercommon.entity.Tx;
Expand All @@ -14,6 +15,15 @@

public interface MaTxMintRepository extends JpaRepository<MaTxMint, Long> {

@Query("SELECT ma.name as name, ma.policy as policy, mtm.quantity as assetQuantity,"
+ " am.fingerprint as fingerprint, am.url as url, am.ticker as ticker,"
+ " am.decimals as decimals, am.logo as logo, am.description as description"
+ " FROM MaTxMint mtm "
+ " JOIN MultiAsset ma ON ma.id = mtm.ident.id"
+ " JOIN AssetMetadata am ON am.fingerprint = ma.fingerprint"
+ " WHERE mtm.tx.id=:txId")
List<MintProjection> findByTxId(@Param("txId") Long txId);

@EntityGraph(attributePaths = {MaTxMint_.IDENT})
List<MaTxMint> findByTx(@Param("tx") Tx tx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ public interface MultiAssetRepository extends JpaRepository<MultiAsset, Long> {
List<PolicyProjection> countAssetHoldersByPolicyIn(@Param("policyList") List<String> policyList);

List<MultiAsset> findByPolicy(@Param("policy") String policy, Pageable pageable);

@Query(value = "SELECT topMultiAsset.* "
+ " FROM script s "
+ " CROSS JOIN LATERAL "
+ " (SELECT ma.* FROM multi_asset ma WHERE ma.policy = s.hash ORDER BY ma.tx_count DESC LIMIT 5)"
+ " AS topMultiAsset"
+ " WHERE s.hash IN :scriptHashes", nativeQuery = true)
List<MultiAsset> findTopMultiAssetByScriptHashIn(@Param("scriptHashes") List<String> scriptHashes);
@Query(value = "WITH firstResult AS ("
+ " SELECT topMultiAsset.*"
+ " FROM script s"
+ " CROSS JOIN LATERAL"
+ " (SELECT ma.name as name, ma.name_view as nameView, ma.policy as policy, ma.fingerprint as fingerprint FROM multi_asset ma WHERE ma.policy = s.hash ORDER BY ma.tx_count DESC LIMIT 5)"
+ " AS topMultiAsset WHERE s.hash IN :scriptHashes)"
+ " SELECT firstResult.policy as policy, firstResult.name as name, firstResult.nameView as nameView,"
+ " firstResult.fingerprint as fingerprint,"
+ " am.url as url, am.ticker as ticker, am.decimals as decimals, am.logo as logo, am.description as description"
+ " FROM firstResult"
+ " LEFT JOIN asset_metadata am ON am.fingerprint = firstResult.fingerprint",nativeQuery = true)
List<TokenProjection> findTopMultiAssetByScriptHashIn(@Param("scriptHashes") List<String> scriptHashes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.cardanofoundation.explorer.api.model.response.script.nativescript.NativeScriptResponse;
import org.cardanofoundation.explorer.api.model.response.token.TokenAddressResponse;
import org.cardanofoundation.explorer.api.model.response.token.TokenFilterResponse;
import org.cardanofoundation.explorer.api.model.response.token.TokenMetadataResponse;
import org.cardanofoundation.explorer.api.model.response.tx.ContractResponse;
import org.cardanofoundation.explorer.api.projection.AddressTokenProjection;
import org.cardanofoundation.explorer.api.projection.TokenProjection;
import org.cardanofoundation.explorer.api.repository.explorer.VerifiedScriptRepository;
import org.cardanofoundation.explorer.api.repository.explorer.NativeScriptInfoRepository;
import org.cardanofoundation.explorer.api.repository.explorer.SmartContractInfoRepository;
Expand Down Expand Up @@ -109,9 +111,9 @@ public BaseFilterResponse<NativeScriptFilterResponse> getNativeScripts(NativeScr
);
Page<NativeScriptInfo> nativeScriptPage = nativeScriptInfoRepository.findAll(
NativeScriptInfoSpecification.filter(currrentBlock.getSlotNo(), filterRequest), pageable);
List<MultiAsset> multiAssetList = multiAssetRepository.findTopMultiAssetByScriptHashIn(
List<TokenProjection> tokenProjectionList = multiAssetRepository.findTopMultiAssetByScriptHashIn(
nativeScriptPage.stream().map(NativeScriptInfo::getScriptHash).toList());
List<TokenFilterResponse> tokenResponses = createTokenResponse(multiAssetList);
List<TokenFilterResponse> tokenResponses = createTokenResponse(tokenProjectionList);
Map<String, List<TokenFilterResponse>> tokenResponseMap =
tokenResponses.stream().collect(Collectors.groupingBy(TokenFilterResponse::getPolicy));
Page<NativeScriptFilterResponse> nativeScriptPageResponse =
Expand All @@ -135,22 +137,16 @@ public BaseFilterResponse<NativeScriptFilterResponse> getNativeScripts(NativeScr
return new BaseFilterResponse<>(nativeScriptPageResponse);
}

private List<TokenFilterResponse> createTokenResponse(List<MultiAsset> tokens) {
private List<TokenFilterResponse> createTokenResponse(List<TokenProjection> tokens) {
List<TokenFilterResponse> tokenResponses;
Set<String> subjects = tokens.stream().map(
ma -> ma.getPolicy() + ma.getName()).collect(Collectors.toSet());
List<AssetMetadata> assetMetadataList = assetMetadataRepository.findBySubjectIn(subjects);
Map<String, AssetMetadata> assetMetadataMap = assetMetadataList.stream().collect(
Collectors.toMap(AssetMetadata::getSubject, Function.identity()));
tokenResponses = tokens.stream().map(
token -> {
TokenFilterResponse tokenFilterResponse = new TokenFilterResponse();
tokenFilterResponse.setPolicy(token.getPolicy());
tokenFilterResponse.setName(token.getName());
tokenFilterResponse.setFingerprint(token.getFingerprint());
tokenFilterResponse.setDisplayName(token.getNameView());
tokenFilterResponse.setMetadata(assetMetadataMapper.fromAssetMetadata(
assetMetadataMap.get(token.getPolicy() + token.getName())));
tokenFilterResponse.setMetadata(new TokenMetadataResponse(token.getUrl(),token.getTicker(),token.getDecimals(),token.getLogo(),token.getDescription()));
return tokenFilterResponse;
}).toList();
return tokenResponses;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,22 +631,10 @@ private void getInstantaneousRewards(Tx tx, TxResponse txResponse) {
* @param txResponse response data of transaction
*/
private void getMints(Tx tx, TxResponse txResponse) {
List<MaTxMint> maTxMints = maTxMintRepository.findByTx(tx);
Set<String> subjects = maTxMints.stream().map(
ma -> ma.getIdent().getPolicy() + ma.getIdent().getName()).collect(Collectors.toSet());
var assetMetadataList = assetMetadataRepository.findBySubjectIn(subjects);
var assetMetadataMap = assetMetadataList.stream().collect(Collectors.toMap(
AssetMetadata::getSubject, Function.identity()
));
List<MintProjection> maTxMints = maTxMintRepository.findByTxId(tx.getId());
if (!CollectionUtils.isEmpty(maTxMints)) {
txResponse.setMints(maTxMints.stream().map(
ma -> {
TxMintingResponse txMintingResponse = maTxMintMapper.fromMaTxMint(ma);
String subject = ma.getIdent().getPolicy() + ma.getIdent().getName();
txMintingResponse.setMetadata(
assetMetadataMapper.fromAssetMetadata(assetMetadataMap.get(subject)));
return txMintingResponse;
}).collect(Collectors.toList()));
maTxMintMapper::fromMintProjectionToTxMintingResponse).collect(Collectors.toList()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ void testGetNativeScripts() {
when(nativeScriptInfoRepository
.findAll(any(Specification.class), any(Pageable.class)))
.thenReturn(new PageImpl<>(scriptList));
when(assetMetadataRepository.findBySubjectIn(any()))
.thenReturn(List.of());
when(multiAssetRepository.findTopMultiAssetByScriptHashIn(any())).thenReturn(List.of());
var response = scriptService.getNativeScripts(request, pageable);
// Assert
Assertions.assertEquals(1, response.getTotalItems());
Expand Down

0 comments on commit 7e259ff

Please sign in to comment.