Skip to content

Commit

Permalink
add endpoint for minascan
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFoxogen committed May 14, 2024
1 parent 9d0b5cc commit c0bb430
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class IpfsZkCloudWorkerClient {
@Value("${zk-cloud-worker.ipfs-token}")
private String token;

private static final String PATH_TO_ZK_CLOUD_WORKER = "/ipfs/{ipfs}?pinataGatewayToken={token}";
private static final String PATH_TO_IPFS_ZK_CLOUD_WORKER = "/ipfs/{ipfs}?pinataGatewayToken={token}";

public IpfsZkCloudWorkerResponse getBlockByIpfs(String ipfs) {
return ipfsZkCloudWorkerRestTemplate.getForObject(PATH_TO_ZK_CLOUD_WORKER, IpfsZkCloudWorkerResponse.class, ipfs, token);
return ipfsZkCloudWorkerRestTemplate.getForObject(PATH_TO_IPFS_ZK_CLOUD_WORKER, IpfsZkCloudWorkerResponse.class, ipfs, token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
@Slf4j
public class ApiKeyRequestFilter extends GenericFilterBean {
private final ApiKeyRepository apiKeyRepository;
public static final String API_CREATE = "/api/domains/save";
public static final String API_UPDATE = "/api/domains/edit";
public static final String API_RESERVE = "/api/domains/reserve";
public static final String API_RESERVE_APPLY = "/api/domains/reserve/apply";
public static final String API_DOMAINS = "/api/domains";
public static final String API_ACTIVITIES = "/api/activities";

public ApiKeyRequestFilter(ApiKeyRepository apiKeyRepository) {
this.apiKeyRepository = apiKeyRepository;
Expand All @@ -36,8 +34,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
String path = req.getRequestURI();
String key = req.getHeader(API_KEY_HEADER) == null ? "" : req.getHeader(API_KEY_HEADER);

if (!path.startsWith(API_CREATE) && !path.startsWith(API_UPDATE) &&
!path.startsWith(API_RESERVE) && !path.startsWith(API_RESERVE_APPLY)) {
if (!path.startsWith(API_DOMAINS) && !path.startsWith(API_ACTIVITIES)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.staketab.minanames.dto.DomainReservationDTO;
import com.staketab.minanames.dto.DomainUpdateDTO;
import com.staketab.minanames.dto.ReservedDomainDTO;
import com.staketab.minanames.dto.SimpleDomainDTO;
import com.staketab.minanames.dto.request.BaseRequest;
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.dto.request.sort.DomainsSortColumn;
Expand Down Expand Up @@ -66,6 +67,12 @@ public Page<DomainEntity> getAccountDomains(@Valid @ParameterObject BaseRequest
return domainService.findAllByAccountPageable(request.withSortColumn(sortBy), accountAddress, searchParams);
}

@GetMapping("/simple")
@Operation(summary = "getDomains", description = "Get a page of all domainDTOs.")
public Page<SimpleDomainDTO> getDomainDTOs(@Valid @ParameterObject BaseRequest request) {
return domainService.findAllSimpleDomainsByPageable(request.withSortColumn(DomainsSortColumn.START_TIMESTAMP));
}

@PostMapping("/save")
public ResponseEntity<DomainEntity> create(@RequestBody DomainReservationDTO domainRequest) {
return ok(domainService.create(domainRequest));
Expand Down Expand Up @@ -110,4 +117,9 @@ public ResponseEntity<ReservedDomainDTO> isDomainNameReserved(@PathVariable Stri
public ResponseEntity<Boolean> setDefaultDomain(@PathVariable String id) {
return ok(domainService.setDefaultDomain(id));
}

@PutMapping("/{id}/default/remove")
public ResponseEntity<Boolean> removeDefaultDomain(@PathVariable String id) {
return ok(domainService.removeDefaultDomain(id));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.staketab.minanames.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/staketab/minanames/dto/SimpleDomainDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.staketab.minanames.dto;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class SimpleDomainDTO {

private String id;
private String ownerAddress;
private String ipfs;
private String domainName;
private String domainImg;
private long timestamp;
private boolean isDefault;
private String status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum DomainsSortColumn implements DbColumnProvider {
AMOUNT("amount"),
STATUS("status"),
RESERVATION_TIMESTAMP("reservation_timestamp"),
START_TIMESTAMP("start_timestamp"),
IS_SEND_TO_CLOUD_WORKER("is_send_to_cloud_worker");

private final String dbColumn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ public enum ActivityStatus {
APPLY_CART_RESERVED_DOMAINS("Apply cart reserved domains", null, false),
SEND_DOMAIN_TO_ZK_CLOUD_WORKER("Send domain to zk cloud worker", null, false),
SET_ACTIVE_STATUS_FOR_DOMAIN("Buy", "Price: %s MINA", true),
UPDATE_DOMAIN_IMAGE("Update record", "Image updated", true),
UPDATE_DOMAIN_IMAGE("Update image", "IPFS: %s", true),
UPDATE_DOMAIN_DESCRIPTION("Update record", "Description updated", true),
FAILED("Failed tx", null, false),
INCORRECT_AMOUNT("Incorrect amount", null, false),
APPLIED("Applied tx", null, false),
REMOVE_RESERVATION("Remove reservation", null, false),
SET_DEFAULT_DOMAIN("Set as default domain", null, true),
REMOVE_DEFAULT_DOMAIN("Remove default domain", null, true),
REMOVE_CART_RESERVATION("Remove cart reservation", null, false);

private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class DomainEntity {
@Column(name = "name", columnDefinition = "TEXT", unique = true)
private String domainName;
@Column(name = "img", columnDefinition = "TEXT")
private String domainImg; //remove?
private String domainImg;

private Long amount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public interface DomainRepository extends JpaRepository<DomainEntity, String> {
where (:searchStr is null or name = :searchStr)""")
Page<DomainEntity> findAllDomains(String searchStr, Pageable buildPageable);

@Query(nativeQuery = true,
value = """
select *
from domains
where status = 'ACTIVE'
""")
Page<DomainEntity> findAllDomains(Pageable buildPageable);

@Query(nativeQuery = true,
value = """
select *
Expand Down Expand Up @@ -59,9 +67,18 @@ public interface DomainRepository extends JpaRepository<DomainEntity, String> {
ELSE false
END
WHERE owner_address = (select owner_address from domains where id = :id)
""")
""")
int setDefaultDomain(String id);

@Modifying
@Query(nativeQuery = true,
value = """
UPDATE domains
SET is_default = false
WHERE id = :id
""")
int removeDefaultDomain(String id);

Optional<DomainEntity> findDomainEntityByDomainName(String domainName);

Optional<DomainEntity> findDomainEntityByDomainNameAndOwnerAddress(String domainName, String ownerAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.staketab.minanames.dto.DomainReservationDTO;
import com.staketab.minanames.dto.DomainUpdateDTO;
import com.staketab.minanames.dto.ReservedDomainDTO;
import com.staketab.minanames.dto.SimpleDomainDTO;
import com.staketab.minanames.dto.request.BaseRequest;
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.entity.DomainEntity;
Expand All @@ -15,6 +16,8 @@
public interface DomainService {
Page<DomainEntity> findAllByPageable(BaseRequest request, SearchParams searchParams);

Page<SimpleDomainDTO> findAllSimpleDomainsByPageable(BaseRequest request);

Page<DomainEntity> findAllByAccountPageable(BaseRequest request, String accountAddress, SearchParams searchParams);

DomainEntity create(DomainReservationDTO domainRequest);
Expand All @@ -35,6 +38,8 @@ public interface DomainService {

Boolean setDefaultDomain(String id);

Boolean removeDefaultDomain(String id);

void removeReservedDomains();

void removeCartReservedDomains();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private ActivityEntity buildActivityEntity(DomainEntity domainEntity, ActivitySt
.ownerAddress(domainEntity.getOwnerAddress())
.timestamp(System.currentTimeMillis())
.ipfs(domainEntity.getIpfs())
.amount(domainEntity.getAmount())
.amount(equals ? domainEntity.getAmount() : null)
.isShow(status.isShow())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.staketab.minanames.dto.DomainUpdateDTO;
import com.staketab.minanames.dto.OldMetadataDTO;
import com.staketab.minanames.dto.ReservedDomainDTO;
import com.staketab.minanames.dto.SimpleDomainDTO;
import com.staketab.minanames.dto.request.BaseRequest;
import com.staketab.minanames.dto.request.SearchParams;
import com.staketab.minanames.entity.DomainEntity;
Expand All @@ -23,6 +24,7 @@
import com.staketab.minanames.service.TxService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -41,6 +43,7 @@
import static com.staketab.minanames.entity.ActivityStatus.CREATE;
import static com.staketab.minanames.entity.ActivityStatus.DELETE_CART_RESERVE;
import static com.staketab.minanames.entity.ActivityStatus.REMOVE_CART_RESERVATION;
import static com.staketab.minanames.entity.ActivityStatus.REMOVE_DEFAULT_DOMAIN;
import static com.staketab.minanames.entity.ActivityStatus.REMOVE_RESERVATION;
import static com.staketab.minanames.entity.ActivityStatus.SET_DEFAULT_DOMAIN;
import static com.staketab.minanames.entity.DomainStatus.PENDING;
Expand All @@ -53,6 +56,11 @@
@Slf4j
public class DomainServiceImpl implements DomainService {

private static final String QUERY_PARAM_TO_IPFS_ZK_CLOUD_WORKER = "?pinataGatewayToken=%s";

@Value("${zk-cloud-worker.ipfs-token}")
private String token;

private final ActivityService activityService;
private final DomainRepository domainRepository;
private final TxService txService;
Expand All @@ -62,6 +70,12 @@ public Page<DomainEntity> findAllByPageable(BaseRequest request, SearchParams se
return domainRepository.findAllDomains(searchParams.getSearchStr(), request.buildPageable());
}

@Override
public Page<SimpleDomainDTO> findAllSimpleDomainsByPageable(BaseRequest request) {
return domainRepository.findAllDomains(request.buildPageable())
.map(this::buildSimpleDomainDTO);
}

@Override
public Page<DomainEntity> findAllByAccountPageable(BaseRequest request, String accountAddress, SearchParams searchParams) {
return domainRepository.findAllDomainsByAccount(searchParams.getSearchStr(), accountAddress, request.buildPageable());
Expand Down Expand Up @@ -171,6 +185,17 @@ public Boolean setDefaultDomain(String id) {
return result;
}

@Override
@Transactional
public Boolean removeDefaultDomain(String id) {
boolean result = domainRepository.removeDefaultDomain(id) > 0;
if (result) {
DomainEntity domainEntity = domainRepository.findById(id).get();
activityService.saveActivity(domainEntity, REMOVE_DEFAULT_DOMAIN, null);
}
return result;
}

@Override
@Transactional
public void removeReservedDomains() {
Expand Down Expand Up @@ -266,6 +291,20 @@ private DomainDTO buildDomainDTO(DomainEntity domainEntity) {
.build();
}

private SimpleDomainDTO buildSimpleDomainDTO(DomainEntity domainEntity) {
String image = domainEntity.getDomainImg() + String.format(QUERY_PARAM_TO_IPFS_ZK_CLOUD_WORKER, token);
return SimpleDomainDTO.builder()
.id(domainEntity.getId())
.domainName(domainEntity.getDomainName())
.isDefault(domainEntity.getIsDefault())
.ipfs(domainEntity.getIpfs())
.ownerAddress(domainEntity.getOwnerAddress())
.timestamp(domainEntity.getStartTimestamp())
.status(domainEntity.getDomainStatus())
.domainImg(domainEntity.getDomainImg() != null ? image : null)
.build();
}

private OldMetadataDTO buildOldMetadataDTO(DomainEntity domainEntity) {
return OldMetadataDTO.builder()
.xTwitter(domainEntity.getXTwitter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@
@RequiredArgsConstructor
public class ZkCloudWorkerServiceImpl implements ZkCloudWorkerService {

private static final String PATH_TO_IPFS_ZK_CLOUD_WORKER = "/ipfs/%s";

@Value("${zk-cloud-worker.mns-contract}")
private String mnsContract;

@Value("${zk-cloud-worker.ipfs-url}")
private String ipfsZkCloudWorkerUrl;

private final Gson gson;
private final ObjectMapper objectMapper;
private final ActivityService activityService;
Expand Down Expand Up @@ -220,20 +225,23 @@ private void updateDomain(ZkCloudWorkerBlocksResponse finalBlock, String name, S
return;
}
DomainEntity domainEntity = domain.get();
setMetadata(newMetadata, domainEntity);
setMetadata(newMetadata, domainEntity, finalBlock.getIpfs());
domainEntity.setDomainMetadata(newMetadata);
domainEntity.setBlockNumber(finalBlock.getBlockNumber());
domainEntity.setIpfs(finalBlock.getIpfs());
domainRepository.save(domainEntity);
}

private void setMetadata(String newMetadata, DomainEntity domainEntity) {
private void setMetadata(String newMetadata, DomainEntity domainEntity, String blockIpfs) {
IpfsDomainMetadataZkDataDTO domainMetadata = getDomainMetadata(newMetadata);
Map<String, IpfsDomainMetadataNftMetadataZkDataDTO> properties = domainMetadata.getNft().getProperties();
IpfsDomainMetadataNftMetadataZkDataDTO image = properties.get(IpfsMetadataCloudWorkerProperty.IMAGE.getName());
if (image != null) {
String storage = image.getLinkedObject().getStorage();
String imgIpfs = ipfsZkCloudWorkerUrl + String.format(PATH_TO_IPFS_ZK_CLOUD_WORKER, storage.substring(2));
domainEntity.setDomainImg(imgIpfs);
domainEntity.setIpfsImg(mapIpfsImgToString(image));
activityService.saveActivity(domainEntity, UPDATE_DOMAIN_IMAGE, UPDATE_DOMAIN_IMAGE.getDetails());
activityService.saveActivity(domainEntity, UPDATE_DOMAIN_IMAGE, String.format(UPDATE_DOMAIN_IMAGE.getDetails(), blockIpfs));
}
IpfsDomainMetadataNftMetadataZkDataDTO description = properties.get(IpfsMetadataCloudWorkerProperty.DESCRIPTION.getName());
if (description != null) {
Expand Down

0 comments on commit c0bb430

Please sign in to comment.