1. 程式人生 > >區塊鏈教程交易所基礎開發通過接口查詢各個幣種的提幣情況-btc

區塊鏈教程交易所基礎開發通過接口查詢各個幣種的提幣情況-btc

term stop images 學院 color ddr image status response

兄弟連區塊鏈教程交易所基礎開發通過接口查詢各個幣種的提幣情況-btc“區塊鏈+時代無疑會是下一個風口,然而現在的區塊鏈行業專業型人才正在遭遇瓶頸”兄弟連教育區塊鏈培訓學院院長尹成表示,“希望能通過兄弟連教育區塊鏈學院為社會為企業培養並輸送更多優質的區塊鏈高精尖型技術。
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))
}
}

效果如下:
技術分享圖片

區塊鏈教程交易所基礎開發通過接口查詢各個幣種的提幣情況-btc