Go 实现 Omni Rpc 接口交互
1. Rpc 接口实现
package net
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"time"
"github.com/astaxie/beego"
)
var userName string
var userPwd string
var url string = "http://172.30.143.249:8336"
func InitRpc() {
//数据库类别
omni := "omni"
userName = beego.AppConfig.String(omni + "::RpcUsername")
userPwd = beego.AppConfig.String(omni + "::RpcPassword")
url = "http://" + beego.AppConfig.String(omni+"::RpcIp") + ":" + beego.AppConfig.String(omni+"::RpcPort")
}
// 发送POST请求
// url:请求地址,data:POST请求提交的数据,contentType:请求体格式,如:application/json
// content:请求放回的内容
func HttpRpcPost(jsonStr string) (content string, err error) {
if userName == "" || userPwd == "" {
return "", errors.New("you mast do InitRpcAuthUser set user and password.")
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(jsonStr)))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(userName, userPwd)
if err != nil {
panic(err)
}
defer req.Body.Close()
client := &http.Client{Timeout: 30 * time.Second}
resp, error := client.Do(req)
if error != nil {
panic(error)
}
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
content = string(result)
return content, nil
}
2. 使用
package models
import (
"XXX/net" // 工具类库导入
"encoding/json"
"strconv"
)
/// 获取块中所有交易 hash
type OmniRpcBlockTransactionsHashResult struct {
Result []string // 响应结果,交易 hash 数组
Error string // 错误日志
}
/// 获取交易详情
type OmniRpcTransactionResult struct {
Result OmniRpcTransactionsInfo // 响应结果,交易详情
Error string // 错误日志
}
type OmniRpcTransactionsInfo struct {
Txid string // 交易 hash
Fee string // 手续费
Sendingaddress string // 发送地址 ":"17rExRiMaJGgHPVuYjHL21mEhfzbRPRkui",
Referenceaddress string // 接收地址":"1BKpa19m5Xy9SvSzC5djPWtCfbuynSDwmb",
Ismine bool //":false,
Version int // ":0,
Type_int int // 币种类型 31 为 USDT
Type string // ":"DEx Accept Offer",
Propertyid int // ":2,
Divisible bool //":true,
Amount string // 交易量 ":"10.00000000",
Valid bool // ":true,
Blockhash string // 块 hash ":"0000000000000000afb6922317c026b31b582c1096074670518bf6dd18df365c",
Blocktime int // 交易成交时间 ":1391336004,
Positioninblock int //":32,
Block int // 块高度 ":283729,
Confirmations int //":135470
}
/// 获取节点详情
type OmniRpcInfoResult struct {
Result OmniRpcInfo // 响应结果,交易详情
Error string // 错误日志
}
// Omni 节点信息
type OmniRpcInfo struct {
Omnicoreversion_int int // ":70000000,
Omnicoreversion string // ":"0.7.0",
Mastercoreversion string // ":"0.7.0",
Bitcoincoreversion string // ":"0.18.1",
Block int // 最高块的块高度
Blocktime int // 最高块的交易时间
Blocktransactions int // 最高块的交易数量
Totaltrades int // ":77,
Totaltransactions int // 当前节点总交易次数
}
// 获取节点信息
func getOmniInfo() (*OmniRpcInfoResult, error) {
jsonStr := "{\"jsonrpc\":\"2.0\", \"method\": \"omni_getinfo\"}"
resultStr, err := net.HttpRpcPost(jsonStr)
if err != nil {
return nil, err
}
var result = OmniRpcInfoResult{}
inputErr := json.Unmarshal([]byte(resultStr), &result)
return &result, inputErr
}
/// 根据块高度获取交易 hash
func getOmniBlockTransactionsHash(blockHeight int) (*OmniRpcBlockTransactionsHashResult, error) {
jsonStr := "{\"jsonrpc\":\"2.0\", \"method\": \"omni_listblocktransactions\", \"params\":[" + strconv.Itoa(blockHeight) + "]}"
resultStr, err := net.HttpRpcPost(jsonStr)
if err != nil {
return nil, err
}
var result = OmniRpcBlockTransactionsHashResult{}
inputErr := json.Unmarshal([]byte(resultStr), &result)
return &result, inputErr
}
/// 根据 txid 获取交易详情
func getOmniTransaction(txid string) (*OmniRpcTransactionResult, error) {
jsonStr := "{\"jsonrpc\":\"2.0\", \"method\": \"omni_gettransaction\", \"params\":[\"" + txid + "\"]}"
resultStr, err := net.HttpRpcPost(jsonStr)
if err != nil {
return nil, err
}
var result = OmniRpcTransactionResult{}
inputErr := json.Unmarshal([]byte(resultStr), &result)
return &result, inputErr
}
}
原文地址:https://www.cnblogs.com/wf-l5201314/p/11969792.html