當前位置:
首頁 > 知識 > 區塊鏈教程之基礎開發通過介面查詢幣種提幣情況bch

區塊鏈教程之基礎開發通過介面查詢幣種提幣情況bch

區塊鏈教程之基礎開發通過介面查詢幣種提幣情況bch

代碼如下

package main

import (

"encoding/json"

"fmt"

"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

}

// BchBlocksChainCheck 根據提幣的數量,提幣方地址以及目標方地址來檢查提幣是否已經confirmed.

// 返回值有兩個:提幣狀態以及已收到的提幣數量(扣除手續費)

func BchBlocksChainCheck(withdrawAmount float64, originalAddress string, targetAddress string) (status string, netWithdrawAmount float64, confirmations int64, err error) {

targetURL := fmt.Sprintf("https://bch-chain.api.btc.com/v3/address/%s/tx", targetAddress)

bData, err := HTTPGet(targetURL, nil)

if err != nil {

fmt.Println("error: HTTPGet targetURL failed.")

return

}

//fmt.Println(string(bData))

_, err = jsonparser.ArrayEach(bData, func(value []byte, dataType jsonparser.ValueType, offset int, e error) {

_outs, _, _, e := jsonparser.Get(value, "outputs")

_confirmations, _, _, e := jsonparser.Get(value, "confirmations")

_fees, _, _, e := jsonparser.Get(value, "fee")

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 outputs []string

e = json.Unmarshal(_addr, &outputs)

v, e := jsonparser.GetFloat(_value)

if outputs[0] == targetAddress && v == withdrawAmount {

fmt.Println("fees: ", fees)

status = "confirmed"

netWithdrawAmount = v

}

})

})

return

}

func main() {

status, netReceiveAmount, confirmations, err := BchBlocksChainCheck(1567646685, "1JnCVng5JaVSmPxtm3wjkUXVDv2HXZBJJw", "15um5NFHM39xPdnnxwbEY4LeSMC9CE9cky")

if err != nil {

fmt.Println("request failed...")

return

}

fmt.Println(fmt.Sprintf("status: %s, net_withdraw_amount: %f, confirmations: %d", status, netReceiveAmount, confirmations))

}

區塊鏈教程之基礎開發通過介面查詢幣種提幣情況bch

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

在docker for win中使用portainer管理容器
typescript入門,可以一起探討提點意見互相學習

TAG:程序員小新人學習 |