區塊鏈交易所基礎開發(8)通過介面查詢區塊鏈各個幣種的提幣情況-neo

尹成發表於2018-05-26

程式碼如下

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
}

// HTTPGet .
func HTTPGet(url string, requestOptions *grequests.RequestOptions) (response []byte, err error) {
	httpResponse, err := grequests.Get(url, requestOptions)
	if err == nil {
		if httpResponse.StatusCode == 200 {
			response = httpResponse.Bytes()
		}
	}
	return
}

// LtcBlocksChainCheck 根據提幣的數量,提幣方地址以及目標方地址來檢查提幣是否已經confirmed.
// 返回值有兩個:提幣狀態以及已收到的提幣數量(扣除手續費)
func LtcBlocksChainCheck(withrawAmount float64, originalAddress string, targetAddress string) (status string, netWithdrawAmount float64, confirmations int64, err error) {
	// originalURL := fmt.Sprintf("", originalAddress)
	// _, err = HTTPGet(originalURL, nil)
	// if err != nil {
	// 	return
	// }
	targetURL := fmt.Sprintf("https://api.blockcypher.com/v1/ltc/main/addrs/%s/full", targetAddress)
	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.")
		return
	}
	// 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)
		// _inputs, _, _, e := jsonparser.Get(value, "inputs")
		// jsonparser.ArrayEach(_inputs, func(ipt []byte, dataType jsonparser.ValueType, offset int, e error) {
		// 	_addr, _, _, e := jsonparser.Get(ipt, "addresses")
		// 	var inputsAddr []string
		// 	if err := json.Unmarshal(_addr, &inputsAddr); err == nil {
		// 		fmt.Println(inputsAddr[0])
		// 	}
		// 	if inputsAddr[0] == originalAddress {
		// 		fmt.Println(inputsAddr[0])
		// 	}
		// })

		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 := LtcBlocksChainCheck(4647000, "LSzJ1bPwmpaw7xsEvW9UEp7WUxZyj2BAYZ", "LYYm41RznLKRFoD4UJDxMMiwQdrSqbNV1G")
	if err != nil {
		fmt.Println("request failed...")
		return
	}
	fmt.Println(fmt.Sprintf("status: %s, net_withdraw_amount: %f, confirmations: %d", status, netReceiveAmount, confirmations))
}

執行效果








網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章