Skip to content

Commit

Permalink
fix the redpacket support for sepolia
Browse files Browse the repository at this point in the history
  • Loading branch information
liberhe committed Dec 21, 2023
1 parent f5f7703 commit 35a5f56
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class RedPacketController {


@PostMapping("/create")
//todo @Auth("admin")
public BaseResponse createRedPacket(@Valid @RequestBody RedPacket redPacket, @RequestParam String address) {
redPacket.setCreator(address);
RedPacket redPacket1 = redPacketRepository.save(redPacket);
Expand All @@ -61,30 +60,31 @@ BaseResponse getRedpacketById(@RequestParam String id, @RequestParam String addr

@RequestMapping(value = "/query/all", method = RequestMethod.GET)
BaseResponse getRedPacketAll(@RequestParam String address,

@RequestParam(defaultValue = "1") Integer pageNumber,
@RequestParam(defaultValue = "10") Integer pageSize) {
@RequestParam(defaultValue = "10") Integer pageSize) {

Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
return BaseResponse.successWithData(redPacketRepository.findAll(pageable));
}


@RequestMapping(value = "/query/user", method = RequestMethod.GET)
BaseResponse getRedPacketByAddress(@RequestParam String address, @RequestParam(required = false) Integer status) {
BaseResponse getRedPacketByAddress(@RequestParam String address, @RequestParam(required = false) Integer status, @RequestParam String chainId) {
List<RedPacket> result;
if(status == 0) {
result = redPacketRepository.findByUnclaimedPacket("%" + address + "%", 0);
result = redPacketRepository.findByUnclaimedPacket("%" + address + "%", 0, chainId);
} else {
result = redPacketRepository.findByClaimedPacket("%" + address + "%");
result = redPacketRepository.findByClaimedPacket("%" + address + "%", chainId);

}
return BaseResponse.successWithData(result);
}

@RequestMapping(value = "/query/user/timeout", method = RequestMethod.GET)
BaseResponse getTimeoutRedPacketByAddress(@RequestParam String address) {
BaseResponse getTimeoutRedPacketByAddress(@RequestParam String address, @RequestParam String chainId) {
List<RedPacket> result;
result = redPacketRepository.findByUnclaimedTimeOutPacket("%" + address + "%");
result = redPacketRepository.findByUnclaimedTimeOutPacket("%" + address + "%", chainId );
return BaseResponse.successWithData(result);
}

Expand All @@ -106,6 +106,9 @@ public Predicate toPredicate(Root<RedPacket> root, CriteriaQuery<?> criteriaQuer
if (redPacket.getName() != null) {
predicates.add(criteriaBuilder.like(root.get("name"), "%"+ redPacket.getName() +"%") );
}
if (redPacket.getChainId() != null) {
predicates.add(criteriaBuilder.equal(root.get("chainId"), redPacket.getChainId() ) );
}
if (redPacket.getCreator() != null) {
predicates.add(criteriaBuilder.equal(root.get("creator"), redPacket.getCreator()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public interface RedPacketRepository extends JpaRepository<RedPacket, String>,
List<RedPacket> findByAddressAndStatus(@Param("address") String address, @Param("status") Integer status);


@Query(value = "select * from red_packet where claimed_address like ?1 ", nativeQuery = true)
List<RedPacket> findByClaimedPacket(@Param("address") String address);
@Query(value = "select * from red_packet where claimed_address like ?1 and chain_id = ?2 ", nativeQuery = true)
List<RedPacket> findByClaimedPacket(@Param("address") String address, @Param("chainId") String chainId);


@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and expire_time < UNIX_TIMESTAMP(current_timestamp()) ", nativeQuery = true)
List<RedPacket> findByUnclaimedTimeOutPacket(@Param("address") String address);
@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and expire_time < UNIX_TIMESTAMP(current_timestamp() and chain_id = ?2) ", nativeQuery = true)
List<RedPacket> findByUnclaimedTimeOutPacket(@Param("address") String address, @Param("chainId") String chainId );

// @Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and status = ?2 and expire_time > UNIX_TIMESTAMP(current_timestamp()) ", nativeQuery = true)
@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and status = ?2 and expire_time > UNIX_TIMESTAMP(current_timestamp()) ", nativeQuery = true)
List<RedPacket> findByUnclaimedPacket(@Param("address") String address, @Param("status") Integer status);
@Query(value = "select * from red_packet where address_list like ?1 and claimed_address not like ?1 and status = ?2 and expire_time > UNIX_TIMESTAMP(current_timestamp() and chain_id = ?3) ", nativeQuery = true)
List<RedPacket> findByUnclaimedPacket(@Param("address") String address, @Param("status") Integer status, @Param("chainId") String chainId);


RedPacket findByIdAndStatus(@Param("id") String id, @Param("status") Integer status);

List<RedPacket> findByStatus(@Param("status") Integer status);
List<RedPacket> findByStatusAndChainId(@Param("status") Integer status, @Param("chainId") String chainId);

}

114 changes: 54 additions & 60 deletions src/main/java/com/dl/officialsite/redpacket/RedPacketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,58 @@ public class RedPacketService {
@Scheduled(cron = "0 0/1 * * * ? ")
public void updateRedpacketStatus() throws IOException {
log.info("schedule task begin --------------------- ");
HttpPost request = new HttpPost("http://api.studio.thegraph.com/proxy/55957/dapp-learning-redpacket/version/latest");
for (String chainId : new String[]{"10", "11155111"}) {
HttpEntity entity = getHttpEntityFromChain(chainId);
if (entity != null) {
String jsonResponse = EntityUtils.toString(entity);
log.info("response from the graph: chainId " + chainId + jsonResponse.substring(0, 30));
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonObject data = jsonObject.getAsJsonObject("data");
JsonArray redpacketsArray = data.getAsJsonArray("redpackets");
List<RedPacket> redPacketList = redPacketRepository.findByStatusAndChainId(0, chainId);

for (int i = 0; i < redpacketsArray.size(); i++) {
// Access each element in the array
JsonObject redpacketObject = redpacketsArray.get(i).getAsJsonObject();
String id = redpacketObject.get("id").getAsString();
for (int j = 0; j < redPacketList.size(); j++) {
RedPacket redPacket = redPacketList.get(j);
if (!redPacketList.get(j).getId().toLowerCase().equals(id.toLowerCase()))
continue;
JsonArray claimers = redpacketObject.getAsJsonArray("claimers");

ArrayList<String> claimersList = new ArrayList<>();
for (int k = 0; k < claimers.size(); k++) {
String s = claimers.get(k).getAsJsonObject().get("claimer").getAsString();
//log.info("claimer address: " + s);
claimersList.add(s);
}
redPacket.setClaimedAddress(claimersList);

//refund or claimed all
Boolean claimed = redpacketObject.get("hasRefundedOrAllClaimed").getAsBoolean();
if (claimed) {
log.info("redpacket id: " + id + "claimed: ");
redPacket.setStatus(1);
}

redPacketRepository.save(redPacket);
}
}
}
}
}

private HttpEntity getHttpEntityFromChain(String chainId) throws IOException {
HttpPost request = null;
switch (chainId) {
case "10": // op
request = new HttpPost("http://api.studio.thegraph.com/proxy/55957/dapp-learning-redpacket/version/latest");
break;
case "11155111": //sepolia
request = new HttpPost("https://api.studio.thegraph.com/query/55957/redpacket-/version/latest");
}

request.setHeader("Content-Type", "application/json");
// Define your GraphQL query
long currentTimeMillis = System.currentTimeMillis();
Expand All @@ -60,69 +111,12 @@ public void updateRedpacketStatus() throws IOException {

request.setEntity(new StringEntity(query));
HttpResponse response = httpClient.execute(request);
// System.out.println("response" + response);
// System.out.println("response" + response);
HttpEntity entity = response.getEntity();

if (entity != null) {
String jsonResponse = EntityUtils.toString(entity);
log.info("response from the graph: "+ jsonResponse.substring(0,30));
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonObject data = jsonObject.getAsJsonObject("data");
JsonArray redpacketsArray = data.getAsJsonArray("redpackets");
List<RedPacket> redPacketList = redPacketRepository.findByStatus(0);

for (int i = 0; i < redpacketsArray.size(); i++) {
// Access each element in the array
JsonObject redpacketObject = redpacketsArray.get(i).getAsJsonObject();
// log.info("redpackt object : " + redpacketObject);
String id = redpacketObject.get("id").getAsString();
for (int j = 0; j < redPacketList.size(); j++) {

RedPacket redPacket = redPacketList.get(j);
// log.info("redPacket: " + redPacket);
if (!redPacketList.get(j).getId().toLowerCase().equals(id.toLowerCase()))
continue;

JsonArray claimers = redpacketObject.getAsJsonArray("claimers");

ArrayList<String> claimersList = new ArrayList<>();
for (int k = 0; k < claimers.size(); k++) {
String s = claimers.get(k).getAsJsonObject().get("claimer").getAsString();
//log.info("claimer address: " + s);
claimersList.add(s);
}
redPacket.setClaimedAddress(claimersList);

// redPacket.getAddressList() is the whole claimer.
// if (claimers.size() == redPacket.getAddressList().size()) {
// log.info("redpacket id: " + id + " aLL claimed ");
// redPacket.setStatus(1);
// redPacketRepository.save(redPacket);
// continue;
// }

// if (claimers.size() > redPacket.getClaimedAddress().size()) {
// log.info("update claimed address : " + id);
// redPacketRepository.save(redPacket);
// }

//refund or claimed all
Boolean claimed = redpacketObject.get("hasRefundedOrAllClaimed").getAsBoolean();
if (claimed) {
log.info("redpacket id: " + id + "claimed: ");
redPacket.setStatus(1);
}

redPacketRepository.save(redPacket);
}
}
}
return entity;
}





// @Scheduled(cron = "0 0/5 * * * ? ")
// public void syncNotSavingDBRedpacket() throws IOException {
// log.info("schedule task begin --------------------- ");
Expand Down

0 comments on commit 35a5f56

Please sign in to comment.