浏览代码

history模块ok

龚成明 3 年之前
父节点
当前提交
1b656f5a9f

+ 44 - 29
.idea/httpRequests/http-requests-log.http

@@ -1,3 +1,47 @@
+POST http://localhost:8888/history/findByChainIdAndHash
+Content-Type: application/json
+
+{
+  "chainId": 1,
+  "hash": "0x25fff6cc8a066a5132a9b2b37e0b1204a7a12b9669572104b4d58c971be07b8d"
+}
+
+<> 2022-11-20T204747.200.json
+
+###
+
+POST http://localhost:8888/history/appendOrUpdate
+Content-Type: application/json
+
+{
+  "chainId": 1,
+  "hash": "0x25fff6cc8a066a5132a9b2b37e0b1204a7a12b9669572104b4d58c971be07b8d",
+  "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"
+    }
+  ]
+}
+
+<> 2022-11-20T204643.200.json
+
+###
+
 POST http://localhost:8888/pending/findByChainIdAndHash
 Content-Type: application/json
 
@@ -800,32 +844,3 @@ Content-Type: application/json
 
 ###
 
-POST http://localhost:8888/token/appendOrUpdate
-Content-Type: application/json
-
-{
-  "chainId": 1,
-  "hash": "0x8A9009847570FdbCc676c7fD547aB26A358a5005",
-  "decimals": 18,
-  "name": "测试",
-  "symbol": "TestToken",
-  "totalAmountStr": "100000",
-  "totalValueStr": "30000",
-  "priceStr": "0.0001"
-}
-
-<> 2022-11-20T115449-1.200.json
-
-###
-
-POST http://localhost:8888/factory/findByChainId
-Content-Type: application/json
-
-{
-  "chainId": 1
-}
-
-<> 2022-11-20T115449.200.json
-
-###
-

+ 8 - 1
src/main/java/common/all.sqlt

@@ -39,11 +39,18 @@ select exists(
 
 
 
-#tx信息处理
 #namespace("tx")
 #include("/modules/tx/tx.sqlt")
 #end
 
+#namespace("history")
+#include("/modules/tx/history/history.sqlt")
+#end
+
+#namespace("pending")
+#include("/modules/tx/pending/pending.sqlt")
+#end
+
     
 
 

+ 3 - 1
src/main/java/common/jfinal/AppConfig.java

@@ -19,6 +19,7 @@ import modules.address.token.TokenController;
 import modules.chain.ChainController;
 import modules.hello.HelloController;
 import modules.tx.TxController;
+import modules.tx.history.HistoryController;
 import modules.tx.pending.PendingController;
 
 public class AppConfig extends JFinalConfig {
@@ -30,7 +31,7 @@ public class AppConfig extends JFinalConfig {
 
 	static void loadConfig() {
 		if (p == null) {
-			p = MyPropKit.useFirstFound("/webapp/backend/evi.properties", "./evi-dev.properties");
+			p = MyPropKit.useFirstFound("/webapp/backend/evi-blockchain.properties", "./evi-dev.properties");
 		}
 	}
 
@@ -62,6 +63,7 @@ public class AppConfig extends JFinalConfig {
 		
 		routes.add("/tx", TxController.class);
 		routes.add("/pending", PendingController.class);
+		routes.add("/history", HistoryController.class);
 	}
 
 	@Override

+ 47 - 0
src/main/java/modules/tx/history/HistoryController.java

@@ -0,0 +1,47 @@
+package modules.tx.history;
+
+import com.jfinal.aop.Inject;
+import common.interceptor.empty.EmptyInterface;
+import common.model.History;
+import common.model.HistoryTransfer;
+import common.model.Tx;
+import common.utils.http.MyController;
+import common.utils.http.MyRet;
+import modules.tx.TxFactory;
+import modules.tx.transfer.TransferFactory;
+
+import java.util.List;
+
+public class HistoryController extends MyController {
+	@Inject
+	private HistoryService service;
+
+	@EmptyInterface(keyArray = {"chainId", "hash"})
+	public void appendOrUpdate() {
+		History history = getJsonModel(History.class);
+
+		if (service.isHistoryExist(history)) {
+			service.update(history);
+			
+			renderJson(MyRet.ok("Update history ok.").setData(history));
+		} else {
+			Tx tx = TxFactory.parseTx(history);
+			
+			String requestJson = (String) getRequest().getAttribute("data");
+			List<HistoryTransfer> historyTransferList = TransferFactory.parseHistoryTransferList(
+					history.getChainId(), history.getHash(), requestJson);
+			
+			service.append(history, tx, historyTransferList);
+
+			renderJson(MyRet.ok("Append history ok.").setData(history));
+		}
+	}
+
+	@EmptyInterface(keyArray = {"chainId", "hash"})
+	public void findByChainIdAndHash() {
+		History history = getJsonModel(History.class);
+
+		history = History.dao.findByIds(history.getHash(), history.getChainId());
+		renderJson(MyRet.ok("History is found.").setData(history));
+	}
+}

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

@@ -0,0 +1,40 @@
+package modules.tx.history;
+
+import com.jfinal.aop.Inject;
+import com.jfinal.plugin.activerecord.Db;
+import common.model.History;
+import common.model.HistoryTransfer;
+import common.model.Tx;
+import modules.tx.TxService;
+import modules.tx.transfer.TransferService;
+
+import java.util.List;
+
+public class HistoryService {
+	@Inject
+	private TransferService transferService;
+	@Inject
+	private TxService txService;
+
+	public boolean append(History history, Tx tx, List<HistoryTransfer> historyTransferList) {
+		boolean isTxExist = txService.isTxExist(tx);
+		return Db.tx(() -> {
+			// 1. 保存tx
+			if (!isTxExist) txService.append(tx);
+			// 2.保存history实体
+			history.save();
+			// 3.保存Transfer列表
+			transferService.saveTransferList(historyTransferList);
+
+			return true;
+		});
+	}
+
+	public boolean update(History history) {
+		return history.update();
+	}
+
+	public boolean isHistoryExist(History history) {
+		return Db.template("hasRecord", "t_history", history.getChainId(), history.getHash()).queryInt() == 1;
+	}
+}

+ 0 - 0
src/main/java/modules/tx/history/history.sqlt


+ 20 - 0
src/main/java/modules/tx/transfer/TransferFactory.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.jfinal.json.FastJson;
 import com.jfinal.kit.StrKit;
 import com.jfinal.plugin.activerecord.Model;
+import common.model.HistoryTransfer;
 import common.model.PendingTransfer;
 import common.model.Tx;
 
@@ -30,4 +31,23 @@ public class TransferFactory {
 		
 		return pendingTransferList;
 	}
+
+	public static List<HistoryTransfer> parseHistoryTransferList(int chainId, String hash, String requestJson) {
+		JSONObject requestJsonObj = JSONObject.parseObject(requestJson);
+		String transferListStr = requestJsonObj.getString("transferList");
+		if (StrKit.isBlank(transferListStr)) transferListStr = "[]";
+		JSONArray transferList = JSONArray.parseArray(transferListStr);
+		List<HistoryTransfer> historyTransferList = new ArrayList<>();
+
+		for (JSONObject transfer : transferList.toJavaList(JSONObject.class)) {
+			HistoryTransfer historyTransfer = FastJson.getJson().parse(transfer.toJSONString(), HistoryTransfer.class);
+
+			historyTransfer.setChainId(chainId);
+			historyTransfer.setHash(hash);
+
+			historyTransferList.add(historyTransfer);
+		}
+
+		return historyTransferList;
+	}
 }

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

@@ -0,0 +1,37 @@
+###
+POST {{ baseUrl }}/history/appendOrUpdate
+Content-Type: application/json
+
+{
+  "chainId": 1,
+  "hash": "0x25fff6cc8a066a5132a9b2b37e0b1204a7a12b9669572104b4d58c971be07b8d",
+  "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"
+    }
+  ]
+}
+
+###
+POST {{ baseUrl }}/history/findByChainIdAndHash
+Content-Type: application/json
+
+{
+  "chainId": 1,
+  "hash": "0x25fff6cc8a066a5132a9b2b37e0b1204a7a12b9669572104b4d58c971be07b8d"
+}