EthMevController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.jfinal.aop.Before;
  4. import com.jfinal.core.Controller;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.jfinal.kit.StrKit;
  7. import interceptor.AuthInterceptor;
  8. import model.EthMev;
  9. import util.MyRet;
  10. import java.math.BigDecimal;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. public class EthMevController extends Controller {
  14. @Before(AuthInterceptor.class)
  15. public void appendOrUpdate() {
  16. if (StrKit.isBlank(getPara("block"))
  17. || StrKit.isBlank(getPara("hash"))
  18. || StrKit.isBlank(getPara("data"))) {
  19. renderJson(MyRet.create().setFail().setMsg("缺少参数,请检查参数。"));
  20. return;
  21. }
  22. String block = getPara("block");
  23. String hash = getPara("hash");
  24. String data = getPara("data");
  25. EthMev ethMev = new EthMev();
  26. ethMev.set("block", block);
  27. ethMev.set("hash", hash);
  28. ethMev.set("data", data);
  29. if (EthMev.dao.findById(hash) != null) {
  30. if (ethMev.update()) {
  31. renderJson(MyRet.create().setOk().setMsg("检测到hash存在,已执行更新逻辑").setData(ethMev));
  32. } else {
  33. renderJson(MyRet.create().setFail().setMsg("更新失败,请联系开发者").setData(ethMev));
  34. }
  35. } else if (ethMev.save()) {
  36. renderJson(MyRet.create().setOk().setMsg("添加成功。"));
  37. } else {
  38. renderJson(MyRet.create().setFail().setMsg("添加失败,请联系开发人员。").setData(ethMev));
  39. }
  40. }
  41. @Before(AuthInterceptor.class)
  42. public void deleteByHash() {
  43. if (StrKit.isBlank(getPara("hash"))) {
  44. renderJson(MyRet.create().setFail().setMsg("请填写Hash,请检查参数。"));
  45. return;
  46. }
  47. String hash = getPara("hash");
  48. EthMev ethMev = new EthMev();
  49. ethMev.set("hash", hash);
  50. if (EthMev.dao.findById(hash) == null) {
  51. renderJson(MyRet.create().setOk().setMsg("查不到数据,无法执行删除操作。").setData(ethMev));
  52. return;
  53. }
  54. if (ethMev.delete()) {
  55. renderJson(MyRet.create().setOk().setMsg("删除成功").setData(ethMev));
  56. } else {
  57. renderJson(MyRet.create().setFail().setMsg("删除失败,请联系开发人员。").setData(ethMev));
  58. }
  59. }
  60. @Before(AuthInterceptor.class)
  61. public void findByHash() {
  62. if (StrKit.isBlank(getPara("hash"))) {
  63. renderJson(MyRet.create().setFail().setMsg("hash为空。"));
  64. }
  65. String hash = getPara("hash");
  66. renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
  67. EthMev.dao.find("select * from t_ethereum_mev_v1 where hash=?", hash)
  68. ));
  69. }
  70. @Before(AuthInterceptor.class)
  71. public void findByBlock() {
  72. if (StrKit.isBlank(getPara("block"))) {
  73. renderJson(MyRet.create().setFail().setMsg("block为空。"));
  74. }
  75. int block = getParaToInt("block");
  76. renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
  77. EthMev.dao.find("select * from t_ethereum_mev_v1 where block=?", block)
  78. ));
  79. }
  80. @Before(AuthInterceptor.class)
  81. public void findByDataVague() {
  82. if (StrKit.isBlank(getPara("dataVague"))) {
  83. renderJson(MyRet.create().setFail().setMsg("data模糊值为空。"));
  84. }
  85. String dataVague = "%" + getPara("dataVague") + "%";
  86. renderJson(MyRet.create().setOk().setMsg("查询成功").setData(
  87. EthMev.dao.find("select * from t_ethereum_mev_v1 where data like ?", dataVague)
  88. ));
  89. }
  90. @Before(AuthInterceptor.class)
  91. public void findByHashOrBlockOrDataVague() {
  92. String block = getPara("block");
  93. String hash = getPara("hash");
  94. String dataVague = getPara("dataVague");
  95. String limit1Str = getPara("limit1");
  96. String limit2Str = getPara("limit2");
  97. int limit1 = 0;
  98. int limit2 = 200;
  99. if (!StrKit.isBlank(dataVague)) {
  100. dataVague = "%" + dataVague + "%";
  101. }
  102. if (!StrKit.isBlank(limit1Str) && !StrKit.isBlank(limit2Str)) {
  103. limit1 = getParaToInt("limit1");
  104. limit2 = getParaToInt("limit2");
  105. }
  106. String sql = "";
  107. List<EthMev> ethMevList = null;
  108. if (StrKit.isBlank(getPara("block"))
  109. && StrKit.isBlank(getPara("hash"))
  110. && StrKit.isBlank(getPara("dataVague"))) {
  111. sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' order by block desc limit ?,?";
  112. ethMevList = EthMev.dao.find(sql, limit1, limit2);
  113. } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("hash"))) {
  114. sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' and data like ? order by block desc limit ?,?";
  115. ethMevList = EthMev.dao.find(sql, dataVague, limit1, limit2);
  116. } else if (StrKit.isBlank(getPara("block")) && StrKit.isBlank(getPara("dataVague"))) {
  117. sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' and hash=? order by block desc limit ?,?";
  118. ethMevList = EthMev.dao.find(sql, hash, limit1, limit2);
  119. } else if (StrKit.isBlank(getPara("hash")) && StrKit.isBlank(getPara("dataVague"))) {
  120. sql = "select * from t_ethereum_mev_v1 where block=? order by block desc limit ?,?";
  121. ethMevList = EthMev.dao.find(sql, block, limit1, limit2);
  122. } else if (StrKit.isBlank(getPara("block"))) {
  123. sql = "select * from t_ethereum_mev_v1 where block regexp '^[-0-9]+$' and hash=? and data like ? order by block desc limit ?,?";
  124. ethMevList = EthMev.dao.find(sql, hash, dataVague, limit1, limit2);
  125. } else if (StrKit.isBlank(getPara("hash"))) {
  126. sql = "select * from t_ethereum_mev_v1 where block=? and data like ? order by block desc limit ?,?";
  127. ethMevList = EthMev.dao.find(sql, block, dataVague, limit1, limit2);
  128. } else if (StrKit.isBlank(getPara("dataVague"))) {
  129. sql = "select * from t_ethereum_mev_v1 where block=? and hash=? order by block desc limit ?,?";
  130. ethMevList = EthMev.dao.find(sql, block, hash, limit1, limit2);
  131. } else {
  132. sql = "select * from t_ethereum_mev_v1 where block=? and hash=? and data like ? order by block desc limit ?,?";
  133. ethMevList = EthMev.dao.find(sql, block, hash, dataVague, limit1, limit2);
  134. }
  135. // dexArray/toArray = (EthMevModel.find_by_block(2))
  136. // tokenArray = (EthMevModel.find_by_block(1))
  137. // lpArray = (EthMevModel.find_by_block(0))
  138. /*
  139. * 为什么要在服务器进行数据处理:
  140. * 服务器内存大,CPU好,而且网站客户少,当然要充分压榨服务器性能了
  141. */
  142. String findHashSql = "select * from t_ethereum_mev_v1 where hash=?";
  143. // 处理对象中的各类地址
  144. for (EthMev ethMev : ethMevList) {
  145. // dataObj容错
  146. JSONObject dataObj = null;
  147. try {
  148. dataObj = JSONObject.parseObject(ethMev.getStr("data"));
  149. // 处理dataObj
  150. ethMev.put("dataObj", dataObj);
  151. ethMev.remove("data");
  152. } catch (Exception e) {
  153. e.printStackTrace();
  154. dataObj = JSONObject.parseObject("{}");
  155. // 处理dataObj
  156. ethMev.put("dataObj", dataObj);
  157. ethMev.remove("data");
  158. }
  159. // 查询lpHash
  160. // lpObj容错
  161. try {
  162. EthMev toObj = EthMev.dao.findFirst(findHashSql, dataObj.getString("toAdd"));
  163. JSONObject toObjDataObj = JSONObject.parseObject(toObj.getStr("data"));
  164. ethMev.put("toName", toObjDataObj.getString("name"));
  165. } catch (Exception ignored) {}
  166. // 处理tokenHash和lpHash
  167. String fromToSql = "select * from t_ethereum_mev_v1 where hash=?";
  168. JSONArray tradeInfoList = dataObj.getJSONArray("tradeInfo");
  169. if (tradeInfoList == null) {
  170. tradeInfoList = JSONArray.parseArray("[]");
  171. }
  172. for (JSONObject tradeInfo : tradeInfoList.toJavaList(JSONObject.class)) {
  173. // tokenObj信息处理
  174. EthMev tokenObj = null;
  175. try {
  176. tokenObj = EthMev.dao.findFirst(findHashSql, tradeInfo.getString("token"));
  177. if (tokenObj != null) {
  178. JSONObject tokenObjDataObj = JSONObject.parseObject(tokenObj.getStr("data"));
  179. if (tokenObjDataObj.getString("LP") == null) {
  180. tradeInfo.put("tokenSymbol", tokenObjDataObj.getString("symbol"));
  181. BigDecimal amount = tradeInfo.getBigDecimal("amount");
  182. BigDecimal ten = new BigDecimal("10");
  183. int decimals = tokenObjDataObj.getInteger("decimals");
  184. BigDecimal realAmount = amount.divide(ten.pow(decimals));
  185. tradeInfo.put("amount", realAmount.doubleValue());
  186. } else {
  187. tradeInfo.put("tokenSymbol", tokenObjDataObj.getString("name"));
  188. tradeInfo.put("amount", 1);
  189. }
  190. } else if (tradeInfo.getString("token").equals("0xeth")) {
  191. tradeInfo.put("tokenSymbol", "Ethereum");
  192. BigDecimal amount = tradeInfo.getBigDecimal("amount");
  193. BigDecimal ten = new BigDecimal("10");
  194. int decimals = 18;
  195. BigDecimal realAmount = amount.divide(ten.pow(decimals));
  196. tradeInfo.put("amount", realAmount.doubleValue());
  197. }
  198. } catch (Exception e) {
  199. System.err.println(tokenObj);
  200. // e.printStackTrace();
  201. }
  202. // fromOrToObj信息处理
  203. try {
  204. EthMev fromLpObj = EthMev.dao.findFirst(fromToSql, tradeInfo.getString("from"));
  205. EthMev toLpObj = EthMev.dao.findFirst(fromToSql, tradeInfo.getString("to"));
  206. EthMev[] fromToArray = new EthMev[]{fromLpObj, toLpObj};
  207. for (int index = 0; index < 2; index++) {
  208. EthMev fromOrToObj = fromToArray[index];
  209. JSONObject dataObjOfFromOrToObj = null;
  210. // 第一种情况,屁都不是的情况
  211. if (fromOrToObj == null) {
  212. continue;
  213. }
  214. try {
  215. dataObjOfFromOrToObj = JSONObject.parseObject(fromOrToObj.getStr("data"));
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. }
  219. // 第二种情况,有symbol的情况
  220. if (dataObjOfFromOrToObj.getString("symbol") != null) {
  221. if (index == 0) {
  222. tradeInfo.put("fromName", dataObjOfFromOrToObj.getString("symbol"));
  223. } else {
  224. tradeInfo.put("toName", dataObjOfFromOrToObj.getString("symbol"));
  225. }
  226. }
  227. // 第三种情况,只有name的情况
  228. else if (dataObjOfFromOrToObj.getString("name") != null) {
  229. if (index == 0) {
  230. tradeInfo.put("fromName", dataObjOfFromOrToObj.getString("name"));
  231. } else {
  232. tradeInfo.put("toName", dataObjOfFromOrToObj.getString("name"));
  233. }
  234. }
  235. // 第四种情况,有Symbol0和Symbol1的情况,也就是LP
  236. else if (dataObjOfFromOrToObj.getString("symbol0") != null
  237. && dataObjOfFromOrToObj.getString("symbol1") != null) {
  238. String symbol0 = dataObjOfFromOrToObj.getString("symbol0");
  239. String symbol1 = dataObjOfFromOrToObj.getString("symbol1");
  240. String router = dataObjOfFromOrToObj.getString("router");
  241. String viewName = String.format("%s-%s-%s-LP", router.substring(0, 4), symbol0, symbol1);
  242. if (index == 0) {
  243. tradeInfo.put("fromName", viewName);
  244. } else {
  245. tradeInfo.put("toName", viewName);
  246. }
  247. }
  248. }
  249. } catch (Exception e) {
  250. e.printStackTrace();
  251. }
  252. }
  253. dataObj.put("tradeInfo", tradeInfoList);
  254. }
  255. renderJson(MyRet.create().setOk().setMsg("查询成功").setData(ethMevList));
  256. }
  257. @Before(AuthInterceptor.class)
  258. public void findByJsonCondition() {
  259. String jsonConditionStr = getPara("jsonCondition");
  260. JSONObject jsonCondition = null;
  261. try {
  262. jsonCondition = JSONObject.parseObject(jsonConditionStr);
  263. } catch (Exception e) {
  264. renderJson(MyRet.create().setFail().setMsg("查询失败,请提供正确的查询条件"));
  265. return;
  266. }
  267. // 从数据库读取EthMev
  268. List<EthMev> ethMevList = EthMev.dao.findAll();
  269. List<EthMev> rstEthMevList = new ArrayList<>();
  270. // 批量过滤EthMev数据
  271. for (EthMev ethMev : ethMevList) {
  272. // 获取data的json
  273. JSONObject dataJson = null;
  274. try {
  275. dataJson = JSONObject.parseObject(ethMev.getStr("data"));
  276. } catch (Exception e) {
  277. continue;
  278. }
  279. // 按条件过滤该ethMev
  280. boolean isOk = true;
  281. for (String key : jsonCondition.keySet()) {
  282. String valueVague = jsonCondition.getString(key);
  283. if (StrKit.isBlank(dataJson.getString(key))
  284. || !dataJson.getString(key).contains(valueVague)) {
  285. isOk = false;
  286. break;
  287. }
  288. }
  289. // 成功就添加了呀
  290. if (isOk) rstEthMevList.add(ethMev);
  291. }
  292. renderJson(MyRet.create().setOk().setMsg("搜索成功").setData(rstEthMevList));
  293. }
  294. }