2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

指定したアドレスで何BTCを受け取ったのかブロックチェーンから計算する

Last updated at Posted at 2018-06-21

はじめに

ビットコインのアービトラージシステムに触れる機会があり
ブロックチェーンに少し興味を持ちました。

ビットコインも買ってみて
アービトラージで運用して増やしてみたりする中で
自分でもブロックチェーンを使って何かできるかなと思って
やってみることにしました。


ブロックチェーン

正直何にも知りません・・・焦

とりあえずAPIがあったので一通り確認したところ
アドレスを指定してそのアドレスで何ビット受け取っているのかを確認するぐらいであれば
すぐにできそうだったのでやってみました^^

ブロックチェーンAPI


まずはブラウザから試してみる

このURLで表示。
https://blockchain.info/ja/rawaddr/{{.Address}}

おぉ、色んな情報が取れますね。
(値はリファレンスに乗っているものをそのまま転記しています。)

{
    "hash160":"660d4ef3a743e3e696ad990364e555c271ad504b",
    "address":"1AJbsFZ64EpEfS5UAjAfcUG8pH8Jn3rn1F",
    "n_tx":17,
    "n_unredeemed":2,
    "total_received":1031350000,
    "total_sent":931250000,
    "final_balance":100100000,
    "txs":[--Array of Transactions--]
}
{
    "hash":"b6f6991d03df0e2e04dafffcd6bc418aac66049e2cd74b80f14ac86db1e3f0da",
    "ver":1,
    "vin_sz":1,
    "vout_sz":2,
    "lock_time":"Unavailable",
    "size":258,
    "relayed_by":"64.179.201.80",
    "block_height, 12200,
    "tx_index":"12563028",
    "inputs":[


            {
                "prev_out":{
                    "hash":"a3e2bcc9a5f776112497a32b05f4b9e5b2405ed9",
                    "value":"100000000",
                    "tx_index":"12554260",
                    "n":"2"
                },
                "script":"76a914641ad5051edd97029a003fe9efb29359fcee409d88ac"
            }

        ],
    "out":[

                {
                    "value":"98000000",
                    "hash":"29d6a3540acfa0a950bef2bfdc75cd51c24390fd",
                    "script":"76a914641ad5051edd97029a003fe9efb29359fcee409d88ac"
                },

                {
                    "value":"2000000",
                    "hash":"17b5038a413f5c5ee288caa64cfab35a0c01914e",
                    "script":"76a914641ad5051edd97029a003fe9efb29359fcee409d88ac"
                }

        ]
}

APIからやってみる

Goで書いています。
バッチリできました。

func main() {
	addr := "調べたいアドレス"
	body, err := GET(fmt.Sprintf("https://blockchain.info/ja/rawaddr/%s", addr), nil)
	if err != nil {
		fmt.Print(err)
	}

	// リファレンスに合わせた構造体を作っておいてjson.Unmarshal
	res := blockchain.Address{}
	json.Unmarshal(body, &res)

	sum := float64(0)
	for _, txs := range res.Txs {
		// 今回は受け取ったものだけ見ていく
		for _, out := range txs.Out {
			// 対象のアドレスだったら
			if out.Addr == addr {
				// 足す
				sum += float64(out.Value)
			}
		}
	}

	// APIから取れるのはsatoshiなのでBTCに変換
	fmt.Println(sum / 100000000)
}

func GET(endpoint string, values url.Values) ([]byte, error) {
	req, _ := http.NewRequest("GET", endpoint, strings.NewReader(""))
	req.Header.Set("Content-Type", "application/json; charset=UTF-8")
	req.URL.RawQuery = values.Encode()
	return Request(req)
}

func Request(req *http.Request) (body []byte, err error) {
	client := &http.Client{Timeout: time.Duration(30 * time.Second)}
	res, err := client.Do(req)
	if err != nil {
		return
	}
	defer res.Body.Close()

	body, err = ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}

	return
}

最後に

折角なのでAPIの申請をして
ビットコインの決済システムを作ってみようと思います。

ご興味ある方がいたら是非ご連絡ください!^^
今よりも高い単価 or お給料でエンジニアさん大募集中です✨

ブロックチェーンの勉強会+交流会もやっています。
気軽に参加できるものになっていますので
こちらでも是非お待ちしています。
https://beyond-stereotype.connpass.com/

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?