交易数据详情
This commit is contained in:
parent
bbb4f37b69
commit
962bde48e3
|
|
@ -1,5 +1,7 @@
|
|||
package structs
|
||||
|
||||
import "github.com/ethereum/go-ethereum/common"
|
||||
|
||||
type FiscoBcos struct {
|
||||
RequestType string `json:"request-type" binding:"Required"`
|
||||
|
||||
|
|
@ -72,4 +74,6 @@ type FiscoBcos struct {
|
|||
// page 相关
|
||||
Page int64 `json:"page"` // 分页的第几页
|
||||
Pagenum int64 `json:"page_num"` // 分页每一页的需求数量
|
||||
|
||||
TxHash common.Hash `json:"tx_hash"` //交易hash
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,64 @@
|
|||
package structs
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
)
|
||||
|
||||
type TxResult struct {
|
||||
Hash string `json:"hash"` // 交易哈希
|
||||
BlockHash string `json:"blockHash"` // 区块哈希
|
||||
BlockNumber string `json:"blockNumber"` //区块高度
|
||||
From string `json:"from"` //发送方
|
||||
To string `json:"to"` //接收方
|
||||
Value string `json:"value"` //交易金额
|
||||
Input string `json:"input"` //输入数据
|
||||
}
|
||||
|
||||
type Transaction struct {
|
||||
BlockHash common.Hash `json:"blockHash"`
|
||||
BlockNumber *hexutil.Big `json:"blockNumber"`
|
||||
From common.Address `json:"from"`
|
||||
Gas *hexutil.Big `json:"gas"`
|
||||
GasPrice *hexutil.Big `json:"gasPrice"`
|
||||
Hash common.Hash `json:"hash"`
|
||||
Input hexutil.Bytes `json:"input"`
|
||||
Nonce *hexutil.Big `json:"nonce"`
|
||||
To *common.Address `json:"to"` // 可能是nil(合约创建交易)
|
||||
TransactionIndex *hexutil.Big `json:"transactionIndex"`
|
||||
Value *hexutil.Big `json:"value"`
|
||||
TransactionHash string `json:"transactionHash"`
|
||||
ContractAddress common.Address `json:"contractAddress"`
|
||||
}
|
||||
|
||||
type BlockResult struct {
|
||||
FailedTxSum string `json:"failedTxSum"` // 交易哈希
|
||||
TxSum string `json:"txSum"` // 区块哈希
|
||||
BlockNumber string `json:"blockNumber"` //区块高度
|
||||
}
|
||||
|
||||
type BlockResultInt struct {
|
||||
FailedTxSumInt int64 `json:"failedTxSum"` // 交易哈希
|
||||
TxSumInt int64 `json:"txSum"` // 区块哈希
|
||||
BlockNumberInt int64 `json:"blockNumber"` //区块高度
|
||||
}
|
||||
|
||||
type BlockResultVo struct {
|
||||
BlockResultInt
|
||||
Response
|
||||
}
|
||||
|
||||
type TxResultVo struct {
|
||||
TxResult
|
||||
Response
|
||||
}
|
||||
|
||||
type TransactionVo struct {
|
||||
Transaction
|
||||
Response
|
||||
}
|
||||
|
||||
type ResponseBlockNum struct {
|
||||
Response
|
||||
BlockNum int64 `json:"block_num"`
|
||||
}
|
||||
|
|
@ -168,6 +168,14 @@ func HandleAllRoutes(ctx *macaron.Context, opt api.FiscoBcos, logger *log.Logger
|
|||
user.UnlockUserAmount(ctx, opt.Username, opt.TokenName, opt.Amount, logger)
|
||||
case "transfer amount":
|
||||
user.TransferAmount(ctx, opt.Payer, opt.Payee, opt.TokenName, opt.Amount, logger)
|
||||
case "getBlockNum":
|
||||
user.GetBlockNum(ctx)
|
||||
case "getTotalTransactionCountT":
|
||||
user.GetTotalTransactionCount(ctx)
|
||||
case "getTransactionByHash":
|
||||
user.GetTransactionByHash(ctx, opt.TxHash)
|
||||
case "getTransactionReceipt":
|
||||
user.GetTransactionReceiptT(ctx, opt.TxHash)
|
||||
default:
|
||||
ctx.JSON(http.StatusNotFound, "the request type not found!")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,14 @@
|
|||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/FISCO-BCOS/go-sdk/abi"
|
||||
|
|
@ -538,3 +541,167 @@ func parseOutput(abiStr, name string, receipt *types.Receipt) (*big.Int, error)
|
|||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func GetBlockNum(ctx *macaron.Context) {
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取区块高度
|
||||
blockNumber, err := client.GetBlockNumber(context.Background())
|
||||
if err != nil {
|
||||
log.Fatalf("获取区块高度失败: %v", err)
|
||||
}
|
||||
ctx.JSON(http.StatusOK, &structs.ResponseBlockNum{
|
||||
Response: structs.Response{
|
||||
Status: 0,
|
||||
Message: "success",
|
||||
},
|
||||
BlockNum: blockNumber,
|
||||
})
|
||||
}
|
||||
|
||||
func GetTotalTransactionCount(ctx *macaron.Context) {
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
txCount, err := client.GetTotalTransactionCount(context.Background())
|
||||
if err != nil {
|
||||
log.Fatalf("GetTotalTransactionCount failed: %v", err)
|
||||
}
|
||||
|
||||
var blockresult api.BlockResult
|
||||
json.Unmarshal(txCount, &blockresult)
|
||||
if err != nil {
|
||||
log.Fatalf("JSON反序列化失败: %v", err)
|
||||
}
|
||||
|
||||
var blockresultInt api.BlockResultInt
|
||||
blockresultInt.TxSumInt = hexToInt64(blockresult.TxSum)
|
||||
blockresultInt.FailedTxSumInt = hexToInt64(blockresult.FailedTxSum)
|
||||
blockresultInt.BlockNumberInt = hexToInt64(blockresult.BlockNumber)
|
||||
|
||||
ctx.JSON(http.StatusOK, &structs.BlockResultVo{
|
||||
Response: structs.Response{
|
||||
Status: 0,
|
||||
Message: "query success!",
|
||||
},
|
||||
BlockResultInt: blockresultInt,
|
||||
})
|
||||
}
|
||||
|
||||
func GetTransactionByHash(ctx *macaron.Context, txHash common.Hash) {
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取区块高度
|
||||
tx, err := client.GetTransactionByHash(context.Background(), txHash)
|
||||
if err != nil {
|
||||
log.Printf("获取交易 %s 失败: %v", txHash, err)
|
||||
return
|
||||
}
|
||||
|
||||
var txresult api.TxResult
|
||||
json.Unmarshal(tx, &txresult)
|
||||
if err != nil {
|
||||
log.Fatalf("JSON反序列化失败: %v", err)
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, &structs.TxResultVo{
|
||||
Response: structs.Response{
|
||||
Status: 0,
|
||||
Message: "query success!",
|
||||
},
|
||||
TxResult: txresult,
|
||||
})
|
||||
}
|
||||
|
||||
func GetTransactionReceiptT(ctx *macaron.Context, txHash common.Hash) {
|
||||
configs, err := conf.ParseConfigFile("config.toml")
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
config := &configs[0]
|
||||
|
||||
client, err := client.Dial(config)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, api.UnknownErr(err))
|
||||
return
|
||||
}
|
||||
|
||||
// 获取区块高度
|
||||
tx, err := client.GetTransactionReceipt(context.Background(), txHash)
|
||||
if err != nil {
|
||||
log.Printf("获取交易 %s 失败: %v", txHash, err)
|
||||
return
|
||||
}
|
||||
var transaction api.Transaction
|
||||
data, _ := json.Marshal(tx)
|
||||
json.Unmarshal(data, &transaction)
|
||||
|
||||
ctx.JSON(http.StatusOK, &structs.TransactionVo{
|
||||
Response: structs.Response{
|
||||
Status: 0,
|
||||
Message: "query success!",
|
||||
},
|
||||
Transaction: transaction,
|
||||
})
|
||||
|
||||
fmt.Println("=== 交易回执 ===")
|
||||
fmt.Println(tx.Status)
|
||||
fmt.Println("交易哈希: " + tx.TransactionHash)
|
||||
fmt.Println("区块哈希: " + tx.BlockHash)
|
||||
fmt.Println("区块高度: " + tx.BlockNumber)
|
||||
fmt.Println("发送方: " + tx.From)
|
||||
fmt.Println("接收方: " + tx.To)
|
||||
fmt.Println("合约地址: " + tx.ContractAddress.Hex())
|
||||
fmt.Println("输入数据: " + tx.Input)
|
||||
fmt.Println("输出数据: " + tx.Output)
|
||||
fmt.Println("Gas使用量: " + tx.GasUsed)
|
||||
fmt.Println("日志数量: " + tx.GasUsed)
|
||||
}
|
||||
|
||||
// 十六进制字符串转int64
|
||||
func hexToInt64(hexStr string) int64 {
|
||||
// 去除可能的0x前缀
|
||||
if len(hexStr) >= 2 && hexStr[0:2] == "0x" {
|
||||
hexStr = hexStr[2:]
|
||||
}
|
||||
|
||||
// 转换为十进制
|
||||
n, err := strconv.ParseInt(hexStr, 16, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return n
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue