skyfffire 3 роки тому
батько
коміт
dda706ef5d

+ 4 - 0
src/main/java/common/config/WebConfig.java

@@ -6,8 +6,10 @@ import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
 import com.jfinal.plugin.druid.DruidPlugin;
 import com.jfinal.server.undertow.UndertowServer;
 import com.jfinal.template.Engine;
+import common.model.EthMevFtm;
 import common.utils.config.MyPropKit;
 import modules.ethmev.EthMevController;
+import modules.ethmevftm.EthMevFtmController;
 import modules.hello.HelloController;
 import modules.swappath.SwapPathController;
 import common.memory.SwapPathMemoryDb;
@@ -41,6 +43,7 @@ public class WebConfig extends JFinalConfig {
         routes.add("/", HelloController.class);
         routes.add("/hello", HelloController.class);
         routes.add("/ethmev", EthMevController.class);
+        routes.add("/ethmevftm", EthMevFtmController.class);
         routes.add("/swappath", SwapPathController.class);
     }
 
@@ -62,6 +65,7 @@ public class WebConfig extends JFinalConfig {
         plugins.add(arp);
 
         arp.addMapping("t_ethereum_mev_v1", "hash", EthMev.class);
+        arp.addMapping("t_ethereum_mev_v1_ftm", "hash", EthMevFtm.class);
         arp.addMapping("t_swap_path", "sum_value_and_level", SwapPath.class);
 
         // 初始化内存数据库线程

+ 7 - 0
src/main/java/common/model/EthMevFtm.java

@@ -0,0 +1,7 @@
+package common.model;
+
+import com.jfinal.plugin.activerecord.Model;
+
+public class EthMevFtm extends Model<EthMevFtm> {
+    public static final EthMevFtm dao = new EthMevFtm().dao();
+}

+ 256 - 0
src/main/java/modules/ethmevftm/EthMevFtmController.java

@@ -0,0 +1,256 @@
+package modules.ethmevftm;
+
+import com.alibaba.fastjson.JSONObject;
+import com.jfinal.aop.Before;
+import com.jfinal.aop.Inject;
+import com.jfinal.core.Controller;
+import com.jfinal.kit.StrKit;
+import common.model.EthMevFtm;
+import common.utils.http.MyRet;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Before(common.interceptor.AuthInterceptor.class)
+public class EthMevFtmController extends Controller {
+    @Inject
+    EthMevFtmService service;
+
+    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");
+
+        EthMevFtm ethMev = new EthMevFtm();
+        ethMev.set("block", block);
+        ethMev.set("hash", hash);
+        ethMev.set("data", data);
+
+        EthMevFtm originData = EthMevFtm.dao.findById(hash);
+        if (originData != null) {
+            if (ethMev.update()) {
+                renderJson(MyRet.create().setOk().setMsg("检测到hash存在,已执行更新逻辑,原始信息已返回").setData(originData));
+            } else {
+                renderJson(MyRet.create().setFail().setMsg("更新失败,请联系开发者,原始信息已返回").setData(originData));
+            }
+        } else if (ethMev.save()) {
+            renderJson(MyRet.create().setOk().setMsg("添加成功。"));
+        } else {
+            renderJson(MyRet.create().setFail().setMsg("添加失败,请联系开发人员。").setData(ethMev));
+        }
+    }
+
+    public void deleteByHash() {
+        if (StrKit.isBlank(getPara("hash"))) {
+            renderJson(MyRet.create().setFail().setMsg("请填写Hash,请检查参数。"));
+            return;
+        }
+
+        String hash = getPara("hash");
+
+        EthMevFtm ethMev = new EthMevFtm();
+        ethMev.set("hash", hash);
+
+        if (EthMevFtm.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));
+        }
+    }
+
+    public void findByHash() {
+        if (StrKit.isBlank(getPara("hash"))) {
+            renderJson(MyRet.create().setFail().setMsg("hash为空。"));
+        }
+
+        String hash = getPara("hash");
+
+        renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
+                EthMevFtm.dao.find("select * from t_ethereum_mev_v1_ftm where hash=?", hash)
+        ));
+    }
+
+    public void findByBlock() {
+        if (StrKit.isBlank(getPara("block"))) {
+            renderJson(MyRet.create().setFail().setMsg("block为空。"));
+        }
+
+        renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
+                EthMevFtm.dao.find("select * from t_ethereum_mev_v1_ftm where block=?", getPara("block"))
+        ));
+    }
+
+    public void findByDataVague() {
+        if (StrKit.isBlank(getPara("dataVague"))) {
+            renderJson(MyRet.create().setFail().setMsg("data模糊值为空。"));
+        }
+
+        String dataVague = "%" + getPara("dataVague") + "%";
+
+        renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
+            EthMevFtm.dao.find("select * from t_ethereum_mev_v1_ftm where data like ?", dataVague)
+        ));
+    }
+
+    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<EthMevFtm> ethMevList = null;
+        if (StrKit.isBlank(getPara("block"))
+                && StrKit.isBlank(getPara("hash"))
+                && StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '^[-0-9]{8,10}' order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("hash"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '^[-0-9]{8,10}' and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, dataVague, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '^[-0-9]{8,10}' and hash=? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, hash, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("hash")) && StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("block"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '^[-0-9]{8,10}' and hash=? and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, hash, dataVague, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("hash"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, dataVague, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? and hash=? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, hash, limit1, limit2);
+        } else {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? and hash=? and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, hash, dataVague, limit1, limit2);
+        }
+
+        service.buildDataObjByEthMevFtmList(ethMevList);
+
+        renderJson(MyRet.create().setOk().setMsg("查询成功").setData(ethMevList));
+    }
+
+    public void findByHashOrBlockOrDataVaguePending() {
+        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<EthMevFtm> ethMevList = null;
+        if (StrKit.isBlank(getPara("block"))
+                && StrKit.isBlank(getPara("hash"))
+                && StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '[-]' order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("hash"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '[-]' and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, dataVague, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '[-]' and hash=? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, hash, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("hash")) && StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("block"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block regexp '[-]' and hash=? and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, hash, dataVague, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("hash"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, dataVague, limit1, limit2);
+        } else if (StrKit.isBlank(getPara("dataVague"))) {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? and hash=? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, hash, limit1, limit2);
+        } else {
+            sql = "select * from t_ethereum_mev_v1_ftm where block=? and hash=? and data like ? order by block desc limit ?,?";
+            ethMevList = EthMevFtm.dao.find(sql, block, hash, dataVague, limit1, limit2);
+        }
+
+        service.buildDataObjByEthMevFtmList(ethMevList);
+
+        renderJson(MyRet.create().setOk().setMsg("查询成功").setData(ethMevList));
+    }
+
+    public void findByJsonCondition() {
+        String jsonConditionStr = getPara("jsonCondition");
+
+        JSONObject jsonCondition = null;
+        try {
+            jsonCondition = JSONObject.parseObject(jsonConditionStr);
+        } catch (Exception e) {
+            renderJson(MyRet.create().setFail().setMsg("查询失败,请提供正确的查询条件"));
+
+            return;
+        }
+
+        // 从数据库读取EthMevFtm
+        List<EthMevFtm> ethMevList = EthMevFtm.dao.findAll();
+        List<EthMevFtm> rstEthMevFtmList = new ArrayList<>();
+
+        // 批量过滤EthMevFtm数据
+        for (EthMevFtm 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) rstEthMevFtmList.add(ethMev);
+        }
+
+        renderJson(MyRet.create().setOk().setMsg("搜索成功").setData(rstEthMevFtmList));
+    }
+}

+ 144 - 0
src/main/java/modules/ethmevftm/EthMevFtmService.java

@@ -0,0 +1,144 @@
+package modules.ethmevftm;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import common.model.EthMevFtm;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+public class EthMevFtmService {
+    public void buildDataObjByEthMevFtmList(List<EthMevFtm> ethMevList) {
+        /*
+         * 为什么要在服务器进行数据处理:
+         * 服务器内存大,CPU好,而且网站客户少,当然要充分压榨服务器性能了
+         */
+        String findHashSql = "select * from t_ethereum_mev_v1_ftm where hash=?";
+
+        // 处理对象中的各类地址
+        for (EthMevFtm 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 {
+                EthMevFtm toObj = EthMevFtm.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_ftm where hash=?";
+            JSONArray tradeInfoList = dataObj.getJSONArray("tradeInfo");
+            if (tradeInfoList == null) {
+                tradeInfoList = JSONArray.parseArray("[]");
+            }
+            for (JSONObject tradeInfo : tradeInfoList.toJavaList(JSONObject.class)) {
+                // tokenObj信息处理
+                EthMevFtm tokenObj = null;
+                try {
+                    tokenObj = EthMevFtm.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 {
+                    EthMevFtm fromLpObj = EthMevFtm.dao.findFirst(fromToSql, tradeInfo.getString("from"));
+                    EthMevFtm toLpObj = EthMevFtm.dao.findFirst(fromToSql, tradeInfo.getString("to"));
+
+                    EthMevFtm[] fromToArray = new EthMevFtm[]{fromLpObj, toLpObj};
+                    for (int index = 0; index < 2; index++) {
+                        EthMevFtm 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);
+        }
+    }
+}

+ 12 - 0
src/test/http/EthMev.http

@@ -7,3 +7,15 @@ timestamp=12345&auth=338209cfc53e9db6d344073335e47cf0
 
 
 ###
+POST {{ baseUrl }}/ethmevftm/findByHashOrBlockOrDataVague
+Content-Type: application/x-www-form-urlencoded
+
+# dataVague=0x397FF1542f962076d0BFE58eA045FfA2d347ACa0&timestamp=12345&auth=338209cfc53e9db6d344073335e47cf0
+block=1&timestamp=12345&auth=338209cfc53e9db6d344073335e47cf0
+
+###
+POST {{ baseUrl }}/ethmevftm/appendOrUpdate
+Content-Type: application/x-www-form-urlencoded
+
+# dataVague=0x397FF1542f962076d0BFE58eA045FfA2d347ACa0&timestamp=12345&auth=338209cfc53e9db6d344073335e47cf0
+block=1&hash=1&data={}&timestamp=12345&auth=338209cfc53e9db6d344073335e47cf0