将上链GET改成POST

This commit is contained in:
闵宪瑞 2025-05-21 15:00:23 +08:00
parent af460222ce
commit 5f3d740c7f

16
main.go
View File

@ -180,8 +180,10 @@ func getHandler(storage *SimpleStorage) http.HandlerFunc {
// getByTxHashHandler 通过交易哈希查询存储在智能合约中的值
func getByTxHashHandler(client *ethclient.Client, storage *SimpleStorage) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 记录请求开始
log.Printf("收到请求: %s %s", r.Method, r.URL.String())
txHashStr := r.URL.Query().Get("txHash")
log.Printf("请求参数 - txHash: %s", txHashStr)
if txHashStr == "" {
writeResponse(w, &Response{Msg: "error", Data: "缺少 txHash 参数"})
return
@ -189,8 +191,10 @@ func getByTxHashHandler(client *ethclient.Client, storage *SimpleStorage) http.H
// 通过交易哈希获取键值
txHash := common.HexToHash(txHashStr)
log.Printf("转换后的交易哈希: %s", txHash.Hex())
// 获取交易详情
// 获取交易详情
log.Println("正在获取交易详情...")
tx, _, err := getTransactionDetails(client, txHash)
if err != nil {
writeResponse(w, &Response{Msg: "error", Data: fmt.Sprintf("无法获取交易详情: %v", err)})
@ -198,20 +202,20 @@ func getByTxHashHandler(client *ethclient.Client, storage *SimpleStorage) http.H
}
// 解析交易输入
log.Println("正在解析交易输入...")
methodName, args, err := parseTransactionInput(string(abiJSON), tx.Data())
if err != nil {
writeResponse(w, &Response{Msg: "error", Data: fmt.Sprintf("无法解析交易输入: %v", err)})
return
}
// 假设方法名为 "set",并且第一个参数是键
log.Printf("解析结果 - 方法名: %s, 参数数量: %d", methodName, len(args))
// 方法名为 "set",并且第一个参数是键
if methodName == "set" && len(args) > 0 {
key, ok := args[0].(*big.Int)
if !ok {
writeResponse(w, &Response{Msg: "error", Data: "键值类型不匹配"})
return
}
// 通过键值调用智能合约的 get 方法
callOpts := &bind.CallOpts{
Pending: false,
@ -222,7 +226,7 @@ func getByTxHashHandler(client *ethclient.Client, storage *SimpleStorage) http.H
return
}
// 假设结果只包含一个字符串值
// 结果只包含一个字符串值
if len(result) > 0 {
writeResponse(w, &Response{Msg: "ok", Data: result[0]})
} else {