Skip to content

Commit

Permalink
Merge pull request #260 from cheng521521/main
Browse files Browse the repository at this point in the history
update graph key
  • Loading branch information
cheng521521 authored Jul 2, 2024
2 parents 069dd52 + 92b4684 commit b51e02e
Show file tree
Hide file tree
Showing 13 changed files with 525 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public enum ConfigEnum {

ECDSA_PRIVATE_KEY("ECDSA_PRIVATE_KEY", EcdsaKeyConfigService.class),

CONTRACT_ADDRESS("CONTRACT_ADDRESS", EcdsaKeyConfigService.class);
CONTRACT_ADDRESS("CONTRACT_ADDRESS", EcdsaKeyConfigService.class),
RED_PACKET_API_KEY("RED_PACKET_API_KEY", EcdsaKeyConfigService.class),
MERKLE_DISTRIBUTION_API_KEY("MERKLE_DISTRIBUTION_API_KEY", EcdsaKeyConfigService.class);

private String configName;
private Class<? extends Refreshable> refreshClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.dl.officialsite.config.ChainInfo;
import com.dl.officialsite.defi.entity.Whale;
import com.dl.officialsite.defi.entity.WhaleTxRow;
import com.dl.officialsite.defi.handler.AaveWhaleProtocolHandler;
import com.dl.officialsite.defi.service.AaveTokenAPYService;
import com.dl.officialsite.defi.service.WhaleProtocolService;
import com.dl.officialsite.defi.service.WhaleService;
Expand Down Expand Up @@ -40,16 +41,20 @@ public class DeFiController {

private final WhaleProtocolService whaleProtocolService;

private final AaveWhaleProtocolHandler aaveWhaleProtocolHandler;

private final MemberService memberService;

private final TeamService teamService;

public DeFiController(AaveTokenAPYService aaveService, WhaleService whaleService,
WhaleProtocolService whaleProtocolService,
AaveWhaleProtocolHandler aaveWhaleProtocolHandler,
MemberService memberService, TeamService teamService) {
this.aaveService = aaveService;
this.whaleService = whaleService;
this.whaleProtocolService = whaleProtocolService;
this.aaveWhaleProtocolHandler = aaveWhaleProtocolHandler;
this.memberService = memberService;
this.teamService = teamService;
}
Expand Down Expand Up @@ -89,7 +94,7 @@ public BaseResponse init(@RequestParam String address) {

@GetMapping("/init/whale/protocol")
public BaseResponse initWhaleProtocol() {
whaleProtocolService.initWhaleProtocol();
aaveWhaleProtocolHandler.initWhaleProtocol();
return BaseResponse.success();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package com.dl.officialsite.defi.handler;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dl.officialsite.defi.dao.BatchRepository;
import com.dl.officialsite.defi.dao.WhaleProtocolRepository;
import com.dl.officialsite.defi.entity.Whale;
import com.dl.officialsite.defi.entity.WhaleProtocol;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
* @ClassName AaveWhaleProtocolService
* @Author jackchen
* @Date 2024/4/22 22:51
* @Description aave whale protocol service
**/
@Service
@Slf4j
public class AaveWhaleProtocolHandler extends AbstractWhaleProtocolHandler {

private final WhaleProtocolRepository whaleProtocolRepository;

private final BatchRepository batchRepository;

public AaveWhaleProtocolHandler(WhaleProtocolRepository whaleProtocolRepository,
BatchRepository batchRepository) {
this.whaleProtocolRepository = whaleProtocolRepository;
this.batchRepository = batchRepository;
}

private static Integer first = 1000;

private static Integer skip = 0;

@Override
public void initWhaleProtocol() {
while (true) {
List<WhaleProtocol> insertWhaleProtocolList = new ArrayList<>();
String jsonStr = requestProtocolGraph(first, skip, "aave", "https://api.thegraph.com/subgraphs/name/aave/protocol-v2");
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
if (jsonObject.toString().contains("error")) {
log.error("解析aave的graph失败");
log.error(jsonObject.toString());
break;
}
JSONObject data = jsonObject.getJSONObject("data");
JSONArray accounts = data.getJSONArray("accounts");
if (accounts.isEmpty()) {
break;
}
//获取最后的whale数据
WhaleProtocol lastWhaleProtocol = whaleProtocolRepository.findAgoWhaleTxRow();
for (int i = 0; i < accounts.size(); i++) {
WhaleProtocol whaleProtocol = convertToWhaleProtocol((JSONObject) accounts.get(i));
if (ObjectUtils.isEmpty(lastWhaleProtocol)) {
whaleProtocol.setId((long) i + 1);
} else {
whaleProtocol.setId((long) i + lastWhaleProtocol.getId() + 1);
}
insertWhaleProtocolList.add(whaleProtocol);
}
insertWhaleProtocol(insertWhaleProtocolList);
skip += 1000;
}
}

@Override
public void insertWhaleProtocol(List<WhaleProtocol> insertWhaleProtocolList) {
log.info("开始插入数据");
log.info("insertWhaleProtocolList.size() = {}-------------------", insertWhaleProtocolList.size());
if (!insertWhaleProtocolList.isEmpty()) {
if (insertWhaleProtocolList.size() <= 1000) {
batchRepository.batchInsert(insertWhaleProtocolList);
} else {
int batches = insertWhaleProtocolList.size() / 1000;
for (int i = 0; i <= batches; i++) {
int start = i * 1000;
int end = (i + 1) * 1000;
if (end > insertWhaleProtocolList.size()) {
end = insertWhaleProtocolList.size();
}
List<WhaleProtocol> sublist = insertWhaleProtocolList.subList(start, end);
batchRepository.batchInsert(sublist);
}
}
}
log.info("插入数据结束");
}

@Override
public WhaleProtocol convertToWhaleProtocol(JSONObject account) {
WhaleProtocol whaleProtocol = new WhaleProtocol();
//总负债
BigDecimal totalDebt = new BigDecimal(0);
//总质押
BigDecimal totalCollateral = new BigDecimal(0);
//总borrows
BigDecimal totalBorrow = new BigDecimal(0);
//总repays
BigDecimal totalRepay = new BigDecimal(0);
//总deposit
BigDecimal totalDeposit = new BigDecimal(0);
JSONArray positions = account.getJSONArray("positions");
Whale whale = convertToWhale(account);
//处理borrows
for (int i = 0; i < positions.size(); i++) {
//todo 处理positions为空的情况
JSONObject position = (JSONObject) positions.get(i);
JSONArray borrows = position.getJSONArray("borrows");
for (int j = 0; j < borrows.size(); j++) {
JSONObject borrow = (JSONObject) borrows.get(j);
String amountUSD = borrow.getStr("amountUSD");
totalBorrow = totalBorrow.add(new BigDecimal(amountUSD));
}
//处理deposit
Boolean isCollateral = position.getBool("isCollateral", false);
if (isCollateral) {
JSONArray deposits = position.getJSONArray("deposits");
for (int k = 0; k < deposits.size(); k++) {
JSONObject deposit = (JSONObject) deposits.get(k);
String amountUSD = deposit.getStr("amountUSD");
totalDeposit = totalDeposit.add(new BigDecimal(amountUSD));
}
}
//处理repays
JSONArray repays = position.getJSONArray("repays");
for (int l = 0; l < repays.size(); l++) {
JSONObject repay = (JSONObject) repays.get(l);
String amountUSD = repay.getStr("amountUSD");
totalRepay = totalRepay.add(new BigDecimal(amountUSD));
}
}
totalDebt = totalBorrow.subtract(totalRepay);
//处理WhaleProtocol
whaleProtocol.setTotalDebt(totalDebt.toString());
whaleProtocol.setTotalSupply(totalDeposit.toString());
whaleProtocol.setChainId(1);
whaleProtocol.setProtocolName("aave");
whaleProtocol.setWhaleAddress(whale.getAddress());
return whaleProtocol;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.dl.officialsite.defi.handler;

import cn.hutool.json.JSONObject;
import com.dl.officialsite.defi.entity.Whale;
import com.dl.officialsite.defi.entity.WhaleProtocol;
import java.io.IOException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
* @ClassName AbstractWhaleProtocolService
* @Author jackchen
* @Date 2024/4/22 22:34
* @Description query whale protocol service
**/
@Slf4j
public abstract class AbstractWhaleProtocolHandler {

//request protocol graph
public String requestProtocolGraph(Integer first, Integer skip,
String protocolName, String protocolGraphUrl) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
String requestBody =
"{\"query\":\"{\\n accounts(\\n where: {positions_: {side: BORROWER, timestampClosed: null, asset_in: [\\\"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48\\\", \\\"0xdac17f958d2ee523a2206206994597c13d831ec7\\\", \\\"0x6b175474e89094c44da98b954eedeac495271d0f\\\", \\\"0x3Fe6a295459FAe07DF8A0ceCC36F37160FE86AA9\\\"], timestampOpened_gt: 1680942374, timestampOpened_lt: 1712478374}, borrows_: {amountUSD_gt: 20000}}\\n skip: "
+ skip + "\\n first: " + first
+ "\\n ) {\\n id\\n positionCount\\n openPositionCount\\n positions(where: {timestampClosed: null}) {\\n id\\n timestampOpened\\n timestampClosed\\n side\\n isCollateral\\n type\\n principal\\n balance\\n depositCount\\n withdrawCount\\n borrowCount\\n repayCount\\n borrows {\\n hash\\n amount\\n amountUSD\\n timestamp\\n }\\n deposits {\\n hash\\n amount\\n amountUSD\\n timestamp\\n }\\n repays {\\n hash\\n amount\\n amountUSD\\n timestamp\\n }\\n asset {\\n id\\n symbol\\n name\\n decimals\\n }\\n }\\n }\\n}\",\"extensions\":{}}";
RequestBody body = RequestBody.create(mediaType, requestBody);
Request request = new Request.Builder()
.url(protocolGraphUrl)
.post(body)
.addHeader("Content-Type", "application/json")
.build();

Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
log.error("请求{}的graph失败", protocolName);
throw new RuntimeException(e);
}
;
String jsonStr = null;
try {
jsonStr = response.body().string();
} catch (IOException e) {
log.error("解析{}的graph返回失败", protocolName);
throw new RuntimeException(e);
}
return jsonStr;

}

public abstract void initWhaleProtocol();

public abstract void insertWhaleProtocol(List<WhaleProtocol> insertWhaleProtocolList);

public abstract WhaleProtocol convertToWhaleProtocol(JSONObject account);

public Whale convertToWhale(JSONObject account) {
Whale whale = new Whale();
String address = account.getStr("id");
whale.setAddress(address);
return whale;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.dl.officialsite.defi.handler;

import cn.hutool.json.JSONObject;
import com.dl.officialsite.defi.entity.WhaleProtocol;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* @ClassName CompoundWhaleProtocolHandler
* @Author jackchen
* @Date 2024/4/22 22:59
* @Description compound whale protocol handler
**/
@Service
@Slf4j
public class CompoundWhaleProtocolHandler extends AbstractWhaleProtocolHandler{

@Override
public void initWhaleProtocol() {

}

@Override
public void insertWhaleProtocol(List<WhaleProtocol> insertWhaleProtocolList) {

}

@Override
public WhaleProtocol convertToWhaleProtocol(JSONObject account) {
return null;
}
}
Loading

0 comments on commit b51e02e

Please sign in to comment.