Skip to content

Commit

Permalink
Merge pull request #206 from cheng521521/main
Browse files Browse the repository at this point in the history
feat: add filter chain type
  • Loading branch information
yanyanho authored Mar 14, 2024
2 parents cf3e229 + 76341c1 commit 769c375
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 47 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/dl/officialsite/aave/Schedule.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dl.officialsite.aave;

import com.dl.officialsite.aave.service.AaveService;
import com.dl.officialsite.aave.vo.HealthInfoVo;
import com.dl.officialsite.mail.EmailService;
import com.dl.officialsite.member.Member;
import com.dl.officialsite.team.TeamService;
Expand Down Expand Up @@ -47,7 +49,7 @@ public void monitorPrice() throws Exception {
for (TeamsWithMembers teamAndMember : teamAndMembers) {
for (Member member : teamAndMember.getMembers()) {
String email = member.getEmail();
HealthInfo healthInfo = aaveService.getHealthInfo(member.getAddress());
HealthInfoVo healthInfo = aaveService.getHealthInfo(member.getAddress());
TokenInfoList tokenInfoList = aaveService.getVar();
emailService.sendMail(email, "Dapp Defi Monitor", tokenInfoList.toString() + "\n" + healthInfo.toString());
}
Expand All @@ -70,7 +72,7 @@ public void monitorHealth() {
for (TeamsWithMembers teamAndMember : teamAndMembers) {
for (Member member : teamAndMember.getMembers()) {
String email = member.getEmail();
HealthInfo healthInfo = aaveService.getHealthInfo(member.getAddress());
HealthInfoVo healthInfo = aaveService.getHealthInfo(member.getAddress());
float health = healthInfo.getHealthFactor().divide(e16).floatValue() / 100;
log.info("health is : " + health);
if (health < 1.2) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/dl/officialsite/aave/TokenAPYInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public class TokenAPYInfo {

String chainId;

Double tokenApy;
Double supply;

Double borrow;

String current;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.constants;

import com.dl.officialsite.common.constants.Constants;
import com.dl.officialsite.config.TokenConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.controller;

import com.dl.officialsite.aave.service.AaveTokenAPYService;
import com.dl.officialsite.aave.vo.HealthInfoVo;
import com.dl.officialsite.common.base.BaseResponse;
import com.dl.officialsite.config.ChainInfo;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -37,19 +39,19 @@ public BaseResponse chainList() {
}

/**
* 条件查询所有币种年华,按照最大年华排序
* get all token apy
*/
@PostMapping("/tokenApy")
public BaseResponse tokenApy(@RequestBody TokenAPYInfoQuery query) {
return BaseResponse.successWithData(aaveService.queryTokenApy(query));
@GetMapping("/tokenApy")
public BaseResponse tokenApy() {
return BaseResponse.successWithData(aaveService.queryTokenApy());
}

/**
* get healthInfo by wallet address
*/
@PostMapping("/healthInfo")
public BaseResponse detail(@RequestParam String address, @RequestBody ChainInfo chainInfo) {
HealthInfo healthInfo = aaveService.getHealthInfo(chainInfo, address);
HealthInfoVo healthInfo = aaveService.getHealthInfo(chainInfo, address);
return BaseResponse.successWithData(healthInfo);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.service;

import static org.web3j.tx.gas.DefaultGasProvider.GAS_LIMIT;
import static org.web3j.tx.gas.DefaultGasProvider.GAS_PRICE;

import com.dl.officialsite.aave.TokenInfo;
import com.dl.officialsite.aave.TokenInfoList;
import com.dl.officialsite.aave.vo.HealthInfoVo;
import com.dl.officialsite.contract.iaaveoracle.IAaveOracle;
import com.dl.officialsite.contract.ipool.IPool;
import com.dl.officialsite.contract.ipooladdressesprovider.IPoolAddressesProvider;
Expand Down Expand Up @@ -36,10 +39,7 @@ public class AaveService {
IPoolAddressesProvider poolAddressesProvider;





public HealthInfo getHealthInfo(String address) {
public HealthInfoVo getHealthInfo(String address) {
try {
String poolAddress = poolAddressesProvider.getPool().send();
log.info("poolAddress is : " + poolAddress);
Expand All @@ -50,7 +50,7 @@ public HealthInfo getHealthInfo(String address) {
BigInteger totalDebtBase = info.component2();
BigInteger ltv = info.component5();
BigInteger healthFactor = info.component6();
HealthInfo healthInfo = HealthInfo.builder()
HealthInfoVo healthInfo = HealthInfoVo.builder()
.healthFactor(healthFactor)
.totalBorrows(totalDebtBase.toString())
.totalCollateralETH(totalCollateralBase.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.service;

import static org.web3j.tx.gas.DefaultGasProvider.GAS_LIMIT;
import static org.web3j.tx.gas.DefaultGasProvider.GAS_PRICE;

import com.dl.officialsite.aave.TokenAPYInfo;
import com.dl.officialsite.aave.TokenAPYInfoRepository;
import com.dl.officialsite.aave.vo.ChainAndAPYVo;
import com.dl.officialsite.aave.vo.HealthInfoVo;
import com.dl.officialsite.aave.vo.TokenAPYInfoAllVo;
import com.dl.officialsite.config.ChainInfo;
import com.dl.officialsite.config.Web3jAutoConfiguration;
import com.dl.officialsite.contract.ipool.IPool;
Expand All @@ -11,13 +16,12 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import javax.persistence.criteria.Predicate;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.web3j.crypto.Credentials;
Expand Down Expand Up @@ -52,24 +56,36 @@ public AaveTokenAPYService(TokenAPYInfoRepository tokenAPYInfoRepository) {
}

@Override
public List<TokenAPYInfo> queryTokenApy(TokenAPYInfoQuery query) {

return tokenAPYInfoRepository.findAll(
(Specification<TokenAPYInfo>) (r, q, c) -> {
List<Predicate> predicates = new LinkedList<>();
if (query.getChainName() != null) {
predicates.add(c.equal(r.get("chainName"), query.getChainName()));
}
q.orderBy(c.desc(r.get("tokenApy")));
return null;
}
);
public List<TokenAPYInfoAllVo> queryTokenApy() {
Map<String, List<TokenAPYInfo>> tokenAPYInfoMap = tokenAPYInfoRepository.findAll()
.stream()
.collect(Collectors.groupingBy(TokenAPYInfo::getTokenName));

return tokenAPYInfoMap.entrySet().stream()
.map(entry -> {
TokenAPYInfoAllVo tokenAPYInfoAllVo = new TokenAPYInfoAllVo();
tokenAPYInfoAllVo.setTokenName(entry.getKey());
List<ChainAndAPYVo> chainAndAPYVos = entry.getValue().stream()
.map(tokenAPYInfo -> {
ChainAndAPYVo chainAndAPYVo = new ChainAndAPYVo();
chainAndAPYVo.setChainName(tokenAPYInfo.getChainName());
chainAndAPYVo.setSupply(tokenAPYInfo.getSupply());
chainAndAPYVo.setBorrow(tokenAPYInfo.getBorrow());
chainAndAPYVo.setTokenAddress(tokenAPYInfo.getTokenAddress());
return chainAndAPYVo;
})
.collect(Collectors.toList());
tokenAPYInfoAllVo.setChainAllAPY(chainAndAPYVos);
return tokenAPYInfoAllVo;
})
.collect(Collectors.toList());
}


@Override
public HealthInfo getHealthInfo(ChainInfo chainInfo, String address) {
public HealthInfoVo getHealthInfo(ChainInfo chainInfo, String address) {
try {
AtomicReference<HealthInfo> healthInfo = new AtomicReference<>();
AtomicReference<HealthInfoVo> healthInfo = new AtomicReference<>();
chainWithWeb3j.forEach((chain, web3j) -> {
if (chain.getId().equals(chainInfo.getId())) {
log.info("chain: {}", chain.getName());
Expand All @@ -92,7 +108,7 @@ public HealthInfo getHealthInfo(ChainInfo chainInfo, String address) {
BigInteger totalDebtBase = info.component2();
BigInteger ltv = info.component5();
BigInteger healthFactor = info.component6();
healthInfo.set(HealthInfo.builder()
healthInfo.set(HealthInfoVo.builder()
.healthFactor(healthFactor)
.totalBorrows(totalDebtBase.toString())
.totalCollateralETH(totalCollateralBase.toString())
Expand Down Expand Up @@ -146,13 +162,17 @@ public List<TokenAPYInfo> queryTokenApyOnChain() {
log.info("tokenName: {} tokenAddress: {}", token.getName(), token.getAddress());
try {
IPool.ReserveData reserveData = pool.getReserveData(token.getAddress()).send();
log.info("{} variable deposit interest: {}", token.getName(), reserveData.currentVariableBorrowRate.divide(E23).floatValue() / 100);
float tokenApy = reserveData.currentLiquidityRate.divide(E23).floatValue() / 100;
log.info("{} variable deposit interest: {}", token.getName(), reserveData.currentLiquidityRate.divide(E23).floatValue() / 100);
Double deposit = (double) (reserveData.currentLiquidityRate.divide(E23).floatValue() / 100);
log.info( "{} variable borrow interest: {}" ,token.getName(), reserveData.currentVariableBorrowRate.divide(E23).floatValue()/100) ;
Double borrow =
(double) (reserveData.currentVariableBorrowRate.divide(E23).floatValue() / 100);
TokenAPYInfo tokenAPYInfo = new TokenAPYInfo();
tokenAPYInfo.setTokenName(token.getName());
tokenAPYInfo.setTokenAddress(token.getAddress());
tokenAPYInfo.setChainName(chain.getName());
tokenAPYInfo.setTokenApy((double) tokenApy);
tokenAPYInfo.setSupply(deposit);
tokenAPYInfo.setBorrow(borrow);
tokenAPYInfo.setChainId(chain.getId());
tokenAPYInfo.setProtocol("Aave");
tokenAPYInfoList.add(tokenAPYInfo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.service;

import com.dl.officialsite.aave.vo.HealthInfoVo;
import com.dl.officialsite.aave.vo.TokenAPYInfoAllVo;
import com.dl.officialsite.config.ChainInfo;
import java.util.List;

Expand All @@ -14,9 +16,9 @@ public abstract class AbstractTokenAPY {
/**
* get token apy group by defi provider
*/
public abstract List<TokenAPYInfo> queryTokenApy(TokenAPYInfoQuery query);
public abstract List<TokenAPYInfoAllVo> queryTokenApy();

public abstract HealthInfo getHealthInfo(ChainInfo chainInfo, String address);
public abstract HealthInfoVo getHealthInfo(ChainInfo chainInfo, String address);


}
21 changes: 21 additions & 0 deletions src/main/java/com/dl/officialsite/aave/vo/ChainAndAPYVo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.dl.officialsite.aave.vo;

import lombok.Data;

/**
* @ClassName ChainAndAPYVo
* @Author jackchen
* @Date 2024/3/13 22:45
* @Description ChainAndAPYVo
**/
@Data
public class ChainAndAPYVo {

private String chainName;

private Double supply;

private Double borrow;

private String tokenAddress;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.vo;

import java.math.BigInteger;
import lombok.Builder;
Expand All @@ -12,7 +12,7 @@
**/
@Data
@Builder
public class HealthInfo {
public class HealthInfoVo {

//健康系数
private BigInteger healthFactor;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/dl/officialsite/aave/vo/TokenAPYInfoAllVo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dl.officialsite.aave.vo;

import java.util.List;
import lombok.Data;

/**
* @ClassName TokenAPYInfoAllVo
* @Author jackchen
* @Date 2024/3/13 22:43
* @Description TokenAPYInfoAllVo
**/
@Data
public class TokenAPYInfoAllVo {

private String tokenName;

private List<ChainAndAPYVo> chainAllAPY;

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dl.officialsite.aave;
package com.dl.officialsite.aave.vo;

import lombok.Data;

Expand All @@ -9,8 +9,7 @@
* @Description TokenAPYInfoQuery
**/
@Data
public class TokenAPYInfoQuery {

public class TokenAPYInfoQueryVo {

String chainName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;

import com.dl.officialsite.aave.Constant;
import com.dl.officialsite.aave.constants.Constant;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.apache.commons.logging.Log;
Expand Down

0 comments on commit 769c375

Please sign in to comment.