Ver código fonte

history pending增加batchSave

JiahengHe 2 anos atrás
pai
commit
0fbf795560

+ 1 - 1
src/main/java/common/utils/http/MyController.java

@@ -35,7 +35,7 @@ public class MyController extends Controller {
 		if(StringUtils.isEmpty(jsonArrStr)){
 			jsonArrStr = "[]";
 		}
-		List<T> objList = (List<T>)JSONArray.parseArray(jsonArrStr, clazz);
+		List<T> objList = JSONArray.parseArray(jsonArrStr, clazz);
 		return objList;
 	}
 }

+ 24 - 2
src/main/java/modules/tx/history/HistoryController.java

@@ -22,6 +22,20 @@ public class HistoryController extends MyController {
 	@Inject
 	private TxService txService;
 
+	@EmptyInterface(keyArray = {"list"}, listKey = {"chainId", "hash"})
+	public void batchSave() {
+		String requestJson = (String) getRequest().getAttribute("data");
+		JSONObject requestJsonObj = JSONObject.parseObject(requestJson);
+		String transferListStr = requestJsonObj.getString("list");
+		List<String> datas = getList(transferListStr, String.class);
+		List<String> errors = service.batchSave(datas);
+		if (errors.size() < 1) {
+			renderJson(MyRet.ok("Save history successful."));
+		} else {
+			renderJson(MyRet.ok("Save history fail, error list in data.").setData(errors));
+		}
+	}
+
 	@EmptyInterface(keyArray = {"chainId", "hash"})
 	public void appendOrUpdate() {
 		History history = getJsonModel(History.class);
@@ -41,12 +55,20 @@ public class HistoryController extends MyController {
 
 	@EmptyInterface(keyArray = {"chainId", "hash"})
 	public void append(){
-		appendOrUpdate();
+		History history = getJsonModel(History.class);
+		if(service.isHistoryExist(history)){
+
+		} else {
+			appendOrUpdate();
+		}
 	}
 
 	@EmptyInterface(keyArray = {"chainId", "hash"})
 	public void update(){
-		appendOrUpdate();
+		History history = getJsonModel(History.class);
+		if(service.isHistoryExist(history)){
+			appendOrUpdate();
+		}
 	}
 
 	@EmptyInterface(keyArray = {"chainId", "hash"})

+ 20 - 0
src/main/java/modules/tx/history/HistoryService.java

@@ -2,15 +2,19 @@ package modules.tx.history;
 
 import com.alibaba.fastjson.JSONObject;
 import com.jfinal.aop.Inject;
+import com.jfinal.json.FastJson;
 import com.jfinal.plugin.activerecord.Db;
 import com.jfinal.plugin.activerecord.Record;
 import common.model.History;
 import common.model.HistoryTransfer;
+import common.model.Tx;
 import common.utils.http.MyPaginate;
 import modules.tx.TxService;
+import modules.tx.transfer.TransferFactory;
 import modules.tx.transfer.TransferService;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.List;
 
 public class HistoryService {
@@ -22,6 +26,22 @@ public class HistoryService {
 	public boolean isHistoryExist(History history) {
 		return Db.template("hasRecord", "t_history", history.getChainId(), history.getHash()).queryInt() == 1;
 	}
+
+	public List<String> batchSave(List<String> datas){
+		List<String> errorStrs = new ArrayList<>();
+		for(String jsonStr : datas){
+			Tx tx = FastJson.getJson().parse(jsonStr, Tx.class);
+			History history = FastJson.getJson().parse(jsonStr, History.class);
+			List<HistoryTransfer> historyTransferList = TransferFactory.parseHistoryTransferList(
+					history.getChainId(), history.getHash(), jsonStr);
+			try {
+				txService.saveOrUpdate(history, tx, historyTransferList);
+			}catch (Exception ex){
+				errorStrs.add(jsonStr);
+			}
+		}
+		return errorStrs;
+	}
 	
 	public Record findByChainIdAndHash(int chainId, String hash) {
 		return txService.findByChainIdAndHash(chainId, hash, History.class, HistoryTransfer.class);

+ 14 - 0
src/main/java/modules/tx/pending/PendingController.java

@@ -20,6 +20,20 @@ public class PendingController extends MyController {
 	@Inject
 	private TxService txService;
 
+	@EmptyInterface(keyArray = {"list"}, listKey = {"chainId", "hash"})
+	public void batchSave() {
+		String requestJson = (String) getRequest().getAttribute("data");
+		JSONObject requestJsonObj = JSONObject.parseObject(requestJson);
+		String transferListStr = requestJsonObj.getString("list");
+		List<String> datas = getList(transferListStr, String.class);
+		List<String> errors = service.batchSave(datas);
+		if (errors.size() < 1) {
+			renderJson(MyRet.ok("Save pending successful."));
+		} else {
+			renderJson(MyRet.ok("Save pending fail, error list in data.").setData(errors));
+		}
+	}
+
 	@EmptyInterface(keyArray = {"chainId", "hash"})
 	public void appendOrUpdate() {
 		Pending pending = getJsonModel(Pending.class);

+ 19 - 0
src/main/java/modules/tx/pending/PendingService.java

@@ -2,14 +2,17 @@ package modules.tx.pending;
 
 import com.alibaba.fastjson.JSONObject;
 import com.jfinal.aop.Inject;
+import com.jfinal.json.FastJson;
 import com.jfinal.kit.Kv;
 import com.jfinal.plugin.activerecord.Db;
 import com.jfinal.plugin.activerecord.Record;
 import common.model.*;
 import common.utils.http.MyPaginate;
 import modules.tx.TxService;
+import modules.tx.transfer.TransferFactory;
 import modules.tx.transfer.TransferService;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -20,6 +23,22 @@ public class PendingService {
 	@Inject
 	private TxService txService;
 
+	public List<String> batchSave(List<String> datas){
+		List<String> errorStrs = new ArrayList<>();
+		for(String jsonStr : datas){
+			Tx tx = FastJson.getJson().parse(jsonStr, Tx.class);
+			Pending pending = FastJson.getJson().parse(jsonStr, Pending.class);
+			List<PendingTransfer> pendingTransferList = TransferFactory.parsePendingTransferList(
+					pending.getChainId(), pending.getHash(), jsonStr);
+			try {
+				txService.saveOrUpdate(pending, tx, pendingTransferList);
+			}catch (Exception ex){
+				errorStrs.add(jsonStr);
+			}
+		}
+		return errorStrs;
+	}
+
 	public boolean isPendingExist(Pending pending) {
 		return Db.template("hasRecord", "t_pending", pending.getChainId(), pending.getHash()).queryInt() == 1;
 	}

+ 119 - 0
src/test/http/tx/HistoryTest.http

@@ -1,3 +1,122 @@
+###
+POST {{ baseUrl }}/history/batchSave
+Content-Type: application/json
+
+{
+  "list": [
+    {
+      "chainId": 2,
+      "hash": "0x514232df6658072d63d645b24f5e9aa1ed2caede87b6d180b2dd5c5ff42b5f53",
+      "blockNumber": 15987098,
+      "fromAddress": "0x13fbc25180fa1dc051838a2744e41c3f5a651b19",
+      "toAddress": "0x036b758df82270534096ea1d0b321981788779d7",
+      "inputData": "0x760f93ae00000000000000000000000000000000000000000000000a96bb748bbc498000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008a521189bfedf99b1dc84bc21571cf8b799dbd4d0000000000000000000000005186c4fb5752eb4a952c7ef03fae3e543f48383e",
+      "valueStr": 1234,
+      "nonce": 42707,
+      "method": "0x760f93ae",
+      "transactionType": "0x0",
+      "transactionIndex": 5,
+      "gasPriceStr": 1500000008,
+      "gasUsedStr": 173158,
+      "transferList": [
+        {
+          "token": "0x8a521189bfedf99b1dc84bc21571cf8b799dbd4d",
+          "from": "0x036b758df82270534096ea1d0b321981788779d7",
+          "to": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "amount": 195328843806937808896,
+          "amountStr": 195328843806937808896
+        },
+        {
+          "token": "0x5186c4fb5752eb4a952c7ef03fae3e543f48383e",
+          "from": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "to": "0x036b758df82270534096ea1d0b321981788779d7",
+          "amount": 49854422,
+          "amountStr": 49854422
+        },
+        {
+          "token": "0x5186c4fb5752eb4a952c7ef03fae3e543f48383e",
+          "from": "0x036b758df82270534096ea1d0b321981788779d7",
+          "to": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "amount": 49854422,
+          "amountStr": 49854422
+        },
+        {
+          "token": "0x8a521189bfedf99b1dc84bc21571cf8b799dbd4d",
+          "from": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "to": "0x036b758df82270534096ea1d0b321981788779d7",
+          "amount": 194159128885449221189,
+          "amountStr": 194159128885449221189
+        }
+      ],
+      "incomeStr": 0,
+      "profitStr": 0,
+      "costStr": 0,
+      "status": 1,
+      "tradeType": 0,
+      "isMev": 0,
+      "isBot": 0,
+      "timestamp": "1669183616.5028968"
+    },{
+      "chainId": 3,
+      "hash": "0x514232df6658072d63d645b24f5e9aa1ed2caede87b6d180b2dd5c5ff42b5f51",
+      "blockNumber": 15987098,
+      "fromAddress": "0x13fbc25180fa1dc051838a2744e41c3f5a651b19",
+      "toAddress": "0x036b758df82270534096ea1d0b321981788779d7",
+      "inputData": "0x760f93ae00000000000000000000000000000000000000000000000a96bb748bbc498000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000008a521189bfedf99b1dc84bc21571cf8b799dbd4d0000000000000000000000005186c4fb5752eb4a952c7ef03fae3e543f48383e",
+      "valueStr": 12345,
+      "nonce": 42707,
+      "method": "0x760f93ae",
+      "transactionType": "0x0",
+      "transactionIndex": 5,
+      "gasPriceStr": 1500000008,
+      "gasUsedStr": 173158,
+      "transferList": [
+        {
+          "token": "0x8a521189bfedf99b1dc84bc21571cf8b799dbd4d",
+          "from": "0x036b758df82270534096ea1d0b321981788779d7",
+          "to": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "amount": 195328843806937808896,
+          "amountStr": 195328843806937808896
+        },
+        {
+          "token": "0x5186c4fb5752eb4a952c7ef03fae3e543f48383e",
+          "from": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "to": "0x036b758df82270534096ea1d0b321981788779d7",
+          "amount": 49854422,
+          "amountStr": 49854422
+        },
+        {
+          "token": "0x5186c4fb5752eb4a952c7ef03fae3e543f48383e",
+          "from": "0x036b758df82270534096ea1d0b321981788779d7",
+          "to": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "amount": 49854422,
+          "amountStr": 49854422
+        },
+        {
+          "token": "0x8a521189bfedf99b1dc84bc21571cf8b799dbd4d",
+          "from": "0x1ae02bcc3c89dd75c141aa448829d0e3c696d9bb",
+          "to": "0x036b758df82270534096ea1d0b321981788779d7",
+          "amount": 194159128885449221189,
+          "amountStr": 194159128885449221189
+        }
+      ],
+      "incomeStr": 0,
+      "profitStr": 0,
+      "costStr": 0,
+      "status": 1,
+      "tradeType": 0,
+      "isMev": 0,
+      "isBot": 0,
+      "timestamp": "1669183616.5028968"
+    }
+  ],
+  "auth": {
+    "auth": "9d8b7074bf189dcad17189c8f264c0cb",
+    "timestamp": "123123"
+  }
+}
+
+
 ###
 POST {{ baseUrl }}/history/appendOrUpdate
 Content-Type: application/json

+ 100 - 0
src/test/http/tx/PendingTest.http

@@ -1,3 +1,103 @@
+###
+POST {{ baseUrl }}/pending/batchSave
+Content-Type: application/json
+
+{
+  "list": [
+    {
+      "chainId": 4,
+      "hash": "0x25fff6cc8a066a5132a9b2b37e0b1204a7a12b9669572104b4d58c971be07b81",
+      "blockNumber": 123,
+      "fromAddress": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+      "toAddress": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+      "method": "method1",
+      "status": "status",
+      "transactionIndex": 0,
+      "nonce": 0,
+      "gasPriceStr": "133332",
+      "gasUsedStr": "123",
+      "gasLimitStr": "123",
+      "valueStr": "123",
+      "maxPriorityFeePerGasStr": "123",
+      "maxFeePerGasStr": "1",
+      "inputData": "0x",
+      "transactionType": "transactionType",
+      "comment": "comment",
+      "timestamp": 1.2334,
+      "other": "{}",
+      "maybeBot": 1,
+      "ping": 1,
+      "transferList": [
+        {
+          "from": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "to": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "token": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "amountStr": "1000"
+        },
+        {
+          "from": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "to": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "token": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "amountStr": "1000"
+        },
+        {
+          "from": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "to": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "token": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "amountStr": "1000"
+        }
+      ]
+    },{
+      "chainId": 5,
+      "hash": "0x25fff6cc8a066a5132a9b2b37e0b1204a7a12b9669572104b4d58c971be07b86",
+      "blockNumber": 123,
+      "fromAddress": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+      "toAddress": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+      "method": "method1",
+      "status": "status",
+      "transactionIndex": 0,
+      "nonce": 0,
+      "gasPriceStr": "133331",
+      "gasUsedStr": "123",
+      "gasLimitStr": "123",
+      "valueStr": "123",
+      "maxPriorityFeePerGasStr": "123",
+      "maxFeePerGasStr": "1",
+      "inputData": "0x",
+      "transactionType": "transactionType",
+      "comment": "comment",
+      "timestamp": 1.2334,
+      "other": "{}",
+      "maybeBot": 1,
+      "ping": 1,
+      "transferList": [
+        {
+          "from": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "to": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "token": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "amountStr": "1000"
+        },
+        {
+          "from": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "to": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "token": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "amountStr": "1000"
+        },
+        {
+          "from": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "to": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "token": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
+          "amountStr": "1000"
+        }
+      ]
+    }
+  ],
+  "auth": {
+    "auth": "9d8b7074bf189dcad17189c8f264c0cb",
+    "timestamp": "123123"
+  }
+}
+
 ###
 POST {{ baseUrl }}/pending/appendOrUpdate
 Content-Type: application/json