1. 程式人生 > >兄弟連區塊鏈入門到精通視頻教程通過接口查詢幣種的提幣情況-tether

兄弟連區塊鏈入門到精通視頻教程通過接口查詢幣種的提幣情況-tether

手續費 draw imp ddr address fun amp status 查詢

package main

import (
"errors"
"fmt"
"math"
"strconv"
"strings"

"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) {

httpResponse, err := grequests.Get(url, requestOptions)
if err == nil {
if httpResponse.StatusCode == 200 {
response = httpResponse.Bytes()
}
}
return
}

// TetherBlocksChainCheck 根據提幣的數量,提幣方地址以及目標方地址來檢查提幣是否已經confirmed.
// 返回值有兩個:提幣狀態以及已收到的提幣數量(扣除手續費)
func TetherBlocksChainCheck(withdrawAmount float64, originalAddress string, targetAddress string) (status string, netWithdrawAmount float64, confirmations int64, err error) {

url := fmt.Sprintf("https://api.etherscan.io/api?module=account&action=txlist&address=%s&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=YourApiKeyToken", targetAddress)
bData, err := HTTPGet(url, nil)
if err != nil {
return
}
transactions, , , err := jsonparser.Get(bData, "result")
_, err = jsonparser.ArrayEach(transactions, func(value []byte, dataType jsonparser.ValueType, offset int, e error) {
from, , _, e := jsonparser.Get(value, "from")
to, , _, e := jsonparser.Get(value, "to")
value, , _, e := jsonparser.Get(value, "value")
txreceiptStatus, , _, e := jsonparser.Get(value, "txreceipt_status")
gasPrice, , _, e := jsonparser.Get(value, "gasPrice")
gasUsed, , _, e := jsonparser.Get(value, "gasUsed")
confirmations, , _, e := jsonparser.Get(value, "confirmations")

    sf := strings.ToLower(string(_from))
    st := strings.ToLower(string(_to))
    sv := strings.ToLower(string(_value))
    iv, _ := strconv.ParseFloat(sv, 64)
    sts := string(_txreceiptStatus)
    sgp := string(_gasPrice)
    igp, _ := strconv.ParseFloat(sgp, 64)
    sgu := string(_gasUsed)
    igu, _ := strconv.ParseFloat(sgu, 64)
    sc := string(_confirmations)

    if sf == strings.ToLower(originalAddress) && st == strings.ToLower(targetAddress) && isEqual(iv, withdrawAmount) {
        // fmt.Println(sf, st, sv, iv, sgp, igp, sgu, igu, sc)

        // 已完成的提幣數量,未扣除提幣的手續費
        fmt.Println("gas:", igp*igu)
        // 已收到幣的實際數量,扣除了提幣的手續費
        fmt.Println("net_receive_amount:", iv)
        if sts == "" || sts == "1" {
            status = "confirmed"
        } else {
            status = "online"
        }
        netWithdrawAmount = iv
        confirmations, _ = strconv.ParseInt(sc, 10, 64)
    } else {
        e = errors.New("this is a new error")
    }
})
return

}

func main() {
status, netReceiveAmount, confirmations, err := TetherBlocksChainCheck(1000000000000000, "0x92fcfb7ef38d344f04fc62e66a060b76035bf02f", "0x4b3db569fa1e7247b5870c4d29955504fed1db7d")
if err != nil {
fmt.Println("request failed...")
return
}
fmt.Println(fmt.Sprintf("status: %s, net_withdraw_amount: %f, confirmations: %d", status, netReceiveAmount, confirmations))
}
效果如下:
技術分享圖片

兄弟連區塊鏈入門到精通視頻教程通過接口查詢幣種的提幣情況-tether