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

尹成發表於2018-05-26

程式碼如下


package main

import (
	"fmt"
	"strconv"

	"github.com/buger/jsonparser"
	"github.com/levigross/grequests"
)

// 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
}

// RemoveTailZeroCharacter .
func RemoveTailZeroCharacter(s string) string {
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] != '0' {
			return s[:i+1]
		}
	}
	return "0"
}

// BtcBlocksChainCheck 根據提幣的數量,提幣方地址以及目標方地址來檢查提幣是否已經confirmed.
// 返回值有兩個:提幣狀態以及已收到的提幣數量(扣除手續費)
func BtcBlocksChainCheck(withdrawAmount float64, originalAddress string, targetAddress string) (status string, netWithdrawAmount float64, err error) {
	url := fmt.Sprintf("https://blockchain.info/rawaddr/%s", targetAddress)
	bData, err := HTTPGet(url, nil)
	if err != nil {
		return
	}
	transactions, _, _, err := jsonparser.Get(bData, "txs")
	jsonparser.ArrayEach(transactions, func(value []byte, dataType jsonparser.ValueType, offset int, e error) {
		inputs, _, _, e := jsonparser.Get(value, "inputs")
		outs, _, _, e := jsonparser.Get(value, "out")
		var totalIn, totalOut, missedTotalIn float64
		jsonparser.ArrayEach(inputs, func(ipt []byte, dataType jsonparser.ValueType, offset int, e error) {
			prevOut, _, _, e := jsonparser.Get(ipt, "prev_out")
			_addr, _, _, e := jsonparser.Get(prevOut, "addr")
			_spent, _, _, e := jsonparser.Get(prevOut, "spent")
			_value, _, _, e := jsonparser.Get(prevOut, "value")

			a, e := jsonparser.GetBoolean(_spent)
			b := string(_addr)
			c, e := jsonparser.GetFloat(_value)
			if a && b == originalAddress {
				totalIn += c
			}

		})

		jsonparser.ArrayEach(outs, func(out []byte, dataType jsonparser.ValueType, offset int, e error) {
			_addr, _, _, e := jsonparser.Get(out, "addr")
			_spent, _, _, e := jsonparser.Get(out, "spent")
			_value, _, _, e := jsonparser.Get(out, "value")

			a, e := jsonparser.GetBoolean(_spent)
			b := string(_addr)
			c, e := jsonparser.GetFloat(_value)
			if a && b == targetAddress {
				totalOut += c
			}
			if !a && b == originalAddress {
				missedTotalIn += c
			}
		})

		if totalIn > 0 && totalOut > 0 {
			netTotalIn := totalIn - missedTotalIn

			strWithdrawAmount := strconv.FormatFloat(withdrawAmount*1e15, 'f', 0, 64)
			strNetTotalIn := strconv.FormatFloat(netTotalIn*1e5, 'f', 0, 64)
			strTotalOut := strconv.FormatFloat(totalOut*1e5, 'f', 0, 64)

			strWithdrawAmount = RemoveTailZeroCharacter(strWithdrawAmount) // 去除字串尾部的0字元
			strNetTotalIn = RemoveTailZeroCharacter(strNetTotalIn)
			strTotalOut = RemoveTailZeroCharacter(strTotalOut)

			floatWithdrawAmount, _ := strconv.ParseFloat(strWithdrawAmount, 64)
			floatNetTotalIn, _ := strconv.ParseFloat(strNetTotalIn, 64)
			floatTotalOut, _ := strconv.ParseFloat(strTotalOut, 64)
			scale := floatWithdrawAmount / withdrawAmount
			finishedWithdrawAmount := floatNetTotalIn / scale
			netReceiveAmount := floatTotalOut / scale

			// 已完成的提幣數量,未扣除提幣的手續費
			fmt.Println("finished_withdraw_amount:", finishedWithdrawAmount)
			// 已收到幣的實際數量,扣除了提幣的手續費
			fmt.Println("net_receive_amount:", netReceiveAmount)
			if withdrawAmount == finishedWithdrawAmount {
				status = "confirmed"
			} else {
				status = "online"
			}
			netWithdrawAmount = netReceiveAmount
			return
		}
	})
	return
}

func main() {
	status, netReceiveAmount, err := BtcBlocksChainCheck(0.04907017, "3BMEXebzKg6WbeUiGmZ8n8rzhu4Axutaf2", "35TWgWwVeU7mu6JZvGzRgwx1W6dw4F6xXx")
	if err != nil {
		fmt.Println("request failed...")
	} else {
		fmt.Println(fmt.Sprintf("status: %s, net_withdraw_amount: %f", status, netReceiveAmount))
	}
}
效果如下








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



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

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

尹成學院微信:備註:CSDN





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



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

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

尹成學院微信:備註:CSDN

相關文章