| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- package controller;
- import com.alibaba.fastjson.JSONArray;
- import com.jfinal.aop.Before;
- import com.jfinal.core.Controller;
- import com.alibaba.fastjson.JSONObject;
- import com.jfinal.kit.StrKit;
- import interceptor.AuthInterceptor;
- import model.EthMev;
- import util.MyRet;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- import java.util.List;
- public class EthMevController extends Controller {
- @Before(AuthInterceptor.class)
- public void appendOrUpdate() {
- if (StrKit.isBlank(getPara("block"))
- || StrKit.isBlank(getPara("hash"))
- || StrKit.isBlank(getPara("data"))) {
- renderJson(MyRet.create().setFail().setMsg("缺少参数,请检查参数。"));
- return;
- }
- String block = getPara("block");
- String hash = getPara("hash");
- String data = getPara("data");
- EthMev ethMev = new EthMev();
- ethMev.set("block", block);
- ethMev.set("hash", hash);
- ethMev.set("data", data);
- if (EthMev.dao.findById(hash) != null) {
- if (ethMev.update()) {
- renderJson(MyRet.create().setOk().setMsg("检测到hash存在,已执行更新逻辑").setData(ethMev));
- } else {
- renderJson(MyRet.create().setFail().setMsg("更新失败,请联系开发者").setData(ethMev));
- }
- } else if (ethMev.save()) {
- renderJson(MyRet.create().setOk().setMsg("添加成功。"));
- } else {
- renderJson(MyRet.create().setFail().setMsg("添加失败,请联系开发人员。").setData(ethMev));
- }
- }
- @Before(AuthInterceptor.class)
- public void deleteByHash() {
- if (StrKit.isBlank(getPara("hash"))) {
- renderJson(MyRet.create().setFail().setMsg("请填写Hash,请检查参数。"));
- return;
- }
- String hash = getPara("hash");
- EthMev ethMev = new EthMev();
- ethMev.set("hash", hash);
- if (EthMev.dao.findById(hash) == null) {
- renderJson(MyRet.create().setOk().setMsg("查不到数据,无法执行删除操作。").setData(ethMev));
- return;
- }
- if (ethMev.delete()) {
- renderJson(MyRet.create().setOk().setMsg("删除成功").setData(ethMev));
- } else {
- renderJson(MyRet.create().setFail().setMsg("删除失败,请联系开发人员。").setData(ethMev));
- }
- }
- @Before(AuthInterceptor.class)
- public void findByHash() {
- if (StrKit.isBlank(getPara("hash"))) {
- renderJson(MyRet.create().setFail().setMsg("hash为空。"));
- }
- String hash = getPara("hash");
- renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
- EthMev.dao.find("select * from t_ethereum_mev_v1 where hash=?", hash)
- ));
- }
- @Before(AuthInterceptor.class)
- public void findByBlock() {
- if (StrKit.isBlank(getPara("block"))) {
- renderJson(MyRet.create().setFail().setMsg("block为空。"));
- }
- int block = getParaToInt("block");
- renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
- EthMev.dao.find("select * from t_ethereum_mev_v1 where block=?", block)
- ));
- }
- @Before(AuthInterceptor.class)
- public void findByDataVague() {
- if (StrKit.isBlank(getPara("dataVague"))) {
- renderJson(MyRet.create().setFail().setMsg("data模糊值为空。"));
- }
- String dataVague = "%" + getPara("dataVague") + "%";
- renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
- EthMev.dao.find("select * from t_ethereum_mev_v1 where data like ?", dataVague)
- ));
- }
- @Before(AuthInterceptor.class)
- public void findByHashOrBlockOrDataVague() {
- String block = getPara("block");
- String hash = getPara("hash");
- String dataVague = getPara("dataVague");
- String limit1Str = getPara("limit1");
- String limit2Str = getPara("limit2");
- int limit1 = 0;
- int limit2 = 200;
- if (!StrKit.isBlank(dataVague)) {
- dataVague = "%" + dataVague + "%";
- }
-
- if (!StrKit.isBlank(limit1Str) && !StrKit.isBlank(limit2Str)) {
- limit1 = getParaToInt("limit1");
- limit2 = getParaToInt("limit2");
- }
- String sql = "";
- List<EthMev> ethMevList = null;
- if (StrKit.isBlank(getPara("block"))
- && StrKit.isBlank(getPara("hash"))
- && StrKit.isBlank(getPara("dataVague"))) {
- sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, limit1, limit2);
- } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("hash"))) {
- sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' and data like ? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, dataVague, limit1, limit2);
- } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("dataVague"))) {
- sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' and hash=? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, hash, limit1, limit2);
- } else if (StrKit.isBlank(getPara("hash")) && StrKit.isBlank(getPara("dataVague"))) {
- sql = "select * from t_ethereum_mev_v1 where block=? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, block, limit1, limit2);
- } else if (StrKit.isBlank(getPara("block"))) {
- sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' and hash=? and data like ? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, hash, dataVague, limit1, limit2);
- } else if (StrKit.isBlank(getPara("hash"))) {
- sql = "select * from t_ethereum_mev_v1 where block=? and data like ? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, block, dataVague, limit1, limit2);
- } else if (StrKit.isBlank(getPara("dataVague"))) {
- sql = "select * from t_ethereum_mev_v1 where block=? and hash=? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, block, hash, limit1, limit2);
- } else {
- sql = "select * from t_ethereum_mev_v1 where block=? and hash=? and data like ? order by block desc limit ?,?";
- ethMevList = EthMev.dao.find(sql, block, hash, dataVague, limit1, limit2);
- }
- // dexArray/toArray = (EthMevModel.find_by_block(2))
- // tokenArray = (EthMevModel.find_by_block(1))
- // lpArray = (EthMevModel.find_by_block(0))
- /*
- * 为什么要在服务器进行数据处理:
- * 服务器内存大,CPU好,而且网站客户少,当然要充分压榨服务器性能了
- */
- String findHashSql = "select * from t_ethereum_mev_v1 where hash=?";
- // 处理对象中的各类地址
- for (EthMev ethMev : ethMevList) {
- // dataObj容错
- JSONObject dataObj = null;
- try {
- dataObj = JSONObject.parseObject(ethMev.getStr("data"));
- // 处理dataObj
- ethMev.put("dataObj", dataObj);
- ethMev.remove("data");
- } catch (Exception e) {
- e.printStackTrace();
- dataObj = JSONObject.parseObject("{}");
- // 处理dataObj
- ethMev.put("dataObj", dataObj);
- ethMev.remove("data");
- }
- // 查询lpHash
- // lpObj容错
- try {
- EthMev toObj = EthMev.dao.findFirst(findHashSql, dataObj.getString("toAdd"));
- JSONObject toObjDataObj = JSONObject.parseObject(toObj.getStr("data"));
- ethMev.put("toName", toObjDataObj.getString("name"));
- } catch (Exception ignored) {}
- // 处理tokenHash和lpHash
- String fromToSql = "select * from t_ethereum_mev_v1 where hash=?";
- JSONArray tradeInfoList = dataObj.getJSONArray("tradeInfo");
- if (tradeInfoList == null) {
- tradeInfoList = JSONArray.parseArray("[]");
- }
- for (JSONObject tradeInfo : tradeInfoList.toJavaList(JSONObject.class)) {
- // tokenObj信息处理
- EthMev tokenObj = null;
- try {
- tokenObj = EthMev.dao.findFirst(findHashSql, tradeInfo.getString("token"));
- if (tokenObj != null) {
- JSONObject tokenObjDataObj = JSONObject.parseObject(tokenObj.getStr("data"));
- if (tokenObjDataObj.getString("LP") == null) {
- tradeInfo.put("tokenSymbol", tokenObjDataObj.getString("symbol"));
- BigDecimal amount = tradeInfo.getBigDecimal("amount");
- BigDecimal ten = new BigDecimal("10");
- int decimals = tokenObjDataObj.getInteger("decimals");
- BigDecimal realAmount = amount.divide(ten.pow(decimals));
- tradeInfo.put("amount", realAmount.doubleValue());
- } else {
- tradeInfo.put("tokenSymbol", tokenObjDataObj.getString("name"));
- tradeInfo.put("amount", 1);
- }
- } else if (tradeInfo.getString("token").equals("0xeth")) {
- tradeInfo.put("tokenSymbol", "Ethereum");
- BigDecimal amount = tradeInfo.getBigDecimal("amount");
- BigDecimal ten = new BigDecimal("10");
- int decimals = 18;
- BigDecimal realAmount = amount.divide(ten.pow(decimals));
- tradeInfo.put("amount", realAmount.doubleValue());
- }
- } catch (Exception e) {
- System.err.println(tokenObj);
- // e.printStackTrace();
- }
- // fromOrToObj信息处理
- try {
- EthMev fromLpObj = EthMev.dao.findFirst(fromToSql, tradeInfo.getString("from"));
- EthMev toLpObj = EthMev.dao.findFirst(fromToSql, tradeInfo.getString("to"));
- EthMev[] fromToArray = new EthMev[]{fromLpObj, toLpObj};
- for (int index = 0; index < 2; index++) {
- EthMev fromOrToObj = fromToArray[index];
- JSONObject dataObjOfFromOrToObj = null;
- // 第一种情况,屁都不是的情况
- if (fromOrToObj == null) {
- continue;
- }
- try {
- dataObjOfFromOrToObj = JSONObject.parseObject(fromOrToObj.getStr("data"));
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 第二种情况,有symbol的情况
- if (dataObjOfFromOrToObj.getString("symbol") != null) {
- if (index == 0) {
- tradeInfo.put("fromName", dataObjOfFromOrToObj.getString("symbol"));
- } else {
- tradeInfo.put("toName", dataObjOfFromOrToObj.getString("symbol"));
- }
- }
- // 第三种情况,只有name的情况
- else if (dataObjOfFromOrToObj.getString("name") != null) {
- if (index == 0) {
- tradeInfo.put("fromName", dataObjOfFromOrToObj.getString("name"));
- } else {
- tradeInfo.put("toName", dataObjOfFromOrToObj.getString("name"));
- }
- }
- // 第四种情况,有Symbol0和Symbol1的情况,也就是LP
- else if (dataObjOfFromOrToObj.getString("symbol0") != null
- && dataObjOfFromOrToObj.getString("symbol1") != null) {
- String symbol0 = dataObjOfFromOrToObj.getString("symbol0");
- String symbol1 = dataObjOfFromOrToObj.getString("symbol1");
- String router = dataObjOfFromOrToObj.getString("router");
- String viewName = String.format("%s-%s-%s-LP", router.substring(0, 4), symbol0, symbol1);
- if (index == 0) {
- tradeInfo.put("fromName", viewName);
- } else {
- tradeInfo.put("toName", viewName);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- dataObj.put("tradeInfo", tradeInfoList);
- }
- renderJson(MyRet.create().setOk().setMsg("查询成功").setData(ethMevList));
- }
- @Before(AuthInterceptor.class)
- public void findByJsonCondition() {
- String jsonConditionStr = getPara("jsonCondition");
- JSONObject jsonCondition = null;
- try {
- jsonCondition = JSONObject.parseObject(jsonConditionStr);
- } catch (Exception e) {
- renderJson(MyRet.create().setFail().setMsg("查询失败,请提供正确的查询条件"));
- return;
- }
- // 从数据库读取EthMev
- List<EthMev> ethMevList = EthMev.dao.findAll();
- List<EthMev> rstEthMevList = new ArrayList<>();
- // 批量过滤EthMev数据
- for (EthMev ethMev : ethMevList) {
- // 获取data的json
- JSONObject dataJson = null;
- try {
- dataJson = JSONObject.parseObject(ethMev.getStr("data"));
- } catch (Exception e) {
- continue;
- }
- // 按条件过滤该ethMev
- boolean isOk = true;
- for (String key : jsonCondition.keySet()) {
- String valueVague = jsonCondition.getString(key);
- if (StrKit.isBlank(dataJson.getString(key))
- || !dataJson.getString(key).contains(valueVague)) {
- isOk = false;
- break;
- }
- }
- // 成功就添加了呀
- if (isOk) rstEthMevList.add(ethMev);
- }
- renderJson(MyRet.create().setOk().setMsg("搜索成功").setData(rstEthMevList));
- }
- }
|