區塊鏈教程基礎開發通過接口查詢幣種的提幣情況-dash
package main
import (
"encoding/json"
"fmt"
"math"
"github.com/buger/jsonparser"
"github.com/levigross/grequests"
)
const min = 0.000000000001
func isEqual(f1, f2 float64) bool {
if f1 < f2 {
return isEqual(f2, f1)
}
return math.Dim(f1, f2) < min
}
func HTTPGet(url string, requestOptions *grequests.RequestOptions) (response []byte, err error) {
if err == nil {
if httpResponse.StatusCode == 200 {
response = httpResponse.Bytes()
}
}
return
}
// DashBlocksChainCheck 根據提幣的數量,提幣方地址以及目標方地址來檢查提幣是否已經confirmed.
// 返回值有兩個:提幣狀態以及已收到的提幣數量(扣除手續費)
func DashBlocksChainCheck(withrawAmount float64, originalAddress string, targetAddress string) (status string, netWithdrawAmount float64, confirmations int64, err error) {
bData, err := HTTPGet(targetURL, nil)
if err != nil {
fmt.Println("error: HTTPGet targetURL failed.")
return
}
transactions, , , err := jsonparser.Get(bData, "txs")
if err != nil {
fmt.Println("error: transaction.")
}
// fmt.Println(string(transactions))
_, err = jsonparser.ArrayEach(transactions, func(value []byte, dataType jsonparser.ValueType, offset int, e error) {
outs, , _, e := jsonparser.Get(value, "outputs")
fees, , _, e := jsonparser.Get(value, "fees")
confirmations, , _, e := jsonparser.Get(value, "confirmations")
fees, e := jsonparser.GetInt(_fees)
confirmations, e = jsonparser.GetInt(_confirmations)
status = "online"
jsonparser.ArrayEach(_outs, func(out []byte, dataType jsonparser.ValueType, offset int, e error) {
_addr, _, _, e := jsonparser.Get(out, "addresses")
_value, _, _, e := jsonparser.Get(out, "value")
var outsAddr []string
e = json.Unmarshal(_addr, &outsAddr)
v, e := jsonparser.GetFloat(_value)
if outsAddr[0] == targetAddress && v == withrawAmount {
fmt.Println("fees: ", fees)
status = "comfirmed"
netWithdrawAmount = v
}
})
})
return
}
func main() {
status, netReceiveAmount, confirmations, err := DashBlocksChainCheck(10000000, "XrBtTsARPgGaqU3RsLtryFxDmaGkNPZdF8", "XnwjLMM2yGeK9tCSAwdkADjiGUaEotXaNV")
if err != nil {
fmt.Println("request failed...")
return
}
fmt.Println(fmt.Sprintf("status: %s, net_withdraw_amount: %f, confirmations: %d", status, netReceiveAmount, confirmations))
}
區塊鏈教程基礎開發通過接口查詢幣種的提幣情況-dash