Skip to content

BaerChain codeExample

woodbaer edited this page Aug 23, 2019 · 3 revisions

Scan block example

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func postMsg(msg string, ip string) []byte {
	cli := &http.Client{
		Timeout: 200 * time.Second,
	}
	body := bytes.NewBuffer([]byte(msg))
	resq, err := http.NewRequest("POST", ip, body)
	if err != nil {
		fmt.Println("resq faild")
	}
	resq.Close = true

	rep, err := cli.Do(resq)
	if err != nil {
		fmt.Println("get response faild")
	}
	defer rep.Body.Close()
	rcv, err := ioutil.ReadAll(rep.Body)
	if err != nil {
		fmt.Println("get body faild")
	}

	return rcv
}

func main() {
	// curl -X POST --data '{"jsonrpc":"2.0","method":"brc_getBlockByNumber","params":["0xc", true],"id":1}' http://127.0.0.1:8171
	var msg string = `{"jsonrpc":"2.0","method":"brc_getBlockByNumber","params":["0x18", true],"id":1}`
	ip := "http://127.0.0.1:8171"
	recv := postMsg(msg, ip)
	fmt.Println(string(recv))

	var jsonMap map[string]interface{}
	err := json.Unmarshal(recv, &jsonMap)
	if err != nil {
		fmt.Println("json unmarshal faild")
		return
	}

	jsonResult := jsonMap["result"].(map[string]interface{})
	jsonTx := jsonResult["transactions"].([]interface{})
	txCount := len(jsonTx)
	for i := 0; i < txCount; i++ {
		tx := jsonTx[i].(map[string]interface{})
        
        // "to" is empty to create a contract
		if tx["to"] == nil {
			fmt.Println("create contract")
		} else if tx["to"] == "0x00000000000000000000000000000000766f7465" { // transaction of data
			// Temporarily call the rpc interface to parse the input content.
			// curl -d '{"jsonrpc":"2.0","method":"brc_getAnalysisData","params":["0xf864b1f00294e79fead329b69540142fabd881099c04424cc49f94042610e447c94ff0824b7aa89fab123930edc8050183010000b1f00294e79fead329b69540142fabd881099c04424cc49f94eee227766ba28fa6abc9ec8fea0c56188bbd54c50183010000"],"id":1}' http://10.0.5.40:8111
			var getAnalysisData string = `{"jsonrpc":"2.0","method":"brc_getAnalysisData","params":["` +
				tx["input"].(string) + `"],"id":1}`
			data := postMsg(getAnalysisData, ip)
			fmt.Println(string(data))

			var dataJsonMap map[string]interface{}
			err = json.Unmarshal(data, &dataJsonMap)
			if err != nil {
				fmt.Println("data json unmarshal is faild")
			}
			dataJsonResult := dataJsonMap["result"].([]interface{})
			dataSize := len(dataJsonResult)
			for i := 0; i < dataSize; i++ {
				ownTx := dataJsonResult[i].(map[string]interface{})
				// type=2,Is a transfer transaction
				if ownTx["type"] == "0x2" {
					fmt.Println("Batch transfer account is:", ownTx["from"].(string))
					fmt.Println("Batch transfer to account is:", ownTx["to"].(string))
					fmt.Println("The amount of the bulk transfer transaction is:", ownTx["transcation_numbers"].(string))
				}
			}
		} else {
            // value=0,it is called contract
    		if tx["value"] == "0x0" {
    			fmt.Println("call contract")
    		} else { // value!=0,it is the original transfer transaction
    			fmt.Println("BRC out address is:", tx["from"].(string))
    			fmt.Println("BRC in address is:", tx["to"].(string))
    			fmt.Println("BRC num is:", tx["value"].(string))
    		}
		}
	}

	//-----------------------------------------------------------------------------------------------------------
	// Pending order to exchange cookies 
    // curl -d '{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["0xf8840605830138809400000000000000000000000000000000766f746580a4e3a2e10394042610e447c94ff0824b7aa89fab123930edc80502018080865af3107a400026a0bbeaef47bc5933d17effc565717afe471b5b75adff9230c74ca160b1877f4a13a01de8ddaaf0fea32a6fecdbc18d9ea86f6fbbe7ee1b70a28b3d811444d379d313"],"id":17}' http://127.0.0.1:8171
	// Create contract 
    // curl -d '{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["0xf9050005058501388000008080b904b0608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506040805190810160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506001908051906020019061009c9291906100a2565b50610147565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e357805160ff1916838001178555610111565b82800160010185558215610111579182015b828111156101105782518255916020019190600101906100f5565b5b50905061011e9190610122565b5090565b61014491905b80821115610140576000816000905550600101610128565b5090565b90565b61035a806101566000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806345653a6d1461005c5780638da5cb5b14610073578063c15bae84146100ca575b600080fd5b34801561006857600080fd5b5061007161015a565b005b34801561007f57600080fd5b5061008861026b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100d657600080fd5b506100df610290565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561011f578082015181840152602081019050610104565b50505050905090810190601f16801561014c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b7f0df29a50b04a74cb4f41000f60d3d0f03a368c07e08d1ed2989948c1fc7ae5086000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff166001604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018060200182810382528381815460018160011615610100020316600290048152602001915080546001816001161561010002031660029004801561025a5780601f1061022f5761010080835404028352916020019161025a565b820191906000526020600020905b81548152906001019060200180831161023d57829003601f168201915b5050935050505060405180910390a1565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103265780601f106102fb57610100808354040283529160200191610326565b820191906000526020600020905b81548152906001019060200180831161030957829003601f168201915b5050505050815600a165627a7a72305820d16c029b297c3c5b55d646346b9631ec5b4d4aa54c3d127b0987060ab1562252002926a05312039e531530d94a31a409b2fc33630528572890f48df97b47cd6771f165b3a07b24c1f91e2cb1d8ba2ab5cc30e11debc9ab39dc52f79a9305e38a4ca6684802"],"id":17}' http://127.0.0.1:8171
	// Call contract 
    // curl -d '{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["0xf866040585013880000094181d14c82a827fc050be55852031d2ff94ab5e91808445653a6d25a07089091496e01e9074faaee4a04c5e62c20c1a9ad9d597cf4d15bf96d3704381a02708c6985340ddab9f781286043e60113dc074c3b427a7f9588db3a2c6dbf247"],"id":17}' http://127.0.0.1:8171
	// Regular transfer
    // curl -d '{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["0xf86580058301388094482e491c48d6f6138b181318faa191f71054d0b585174876e8008026a0d361983b972addec2bf2f48d00699c72a605308ec3516343561b8bc2b805de14a020717a07b4edf76d207f4e618ae4872b10997a5d3a00f0a806bdcb0b04ce286a"],"id":17}' http://127.0.0.1:8171

	// Pending order to exchange cookies 
	//params := "0xf8840605830138809400000000000000000000000000000000766f746580a4e3a2e10394042610e447c94ff0824b7aa89fab123930edc80502018080865af3107a400026a0bbeaef47bc5933d17effc565717afe471b5b75adff9230c74ca160b1877f4a13a01de8ddaaf0fea32a6fecdbc18d9ea86f6fbbe7ee1b70a28b3d811444d379d313"
	// Create contract 
	//params := "0xf9050005058501388000008080b904b0608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506040805190810160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506001908051906020019061009c9291906100a2565b50610147565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e357805160ff1916838001178555610111565b82800160010185558215610111579182015b828111156101105782518255916020019190600101906100f5565b5b50905061011e9190610122565b5090565b61014491905b80821115610140576000816000905550600101610128565b5090565b90565b61035a806101566000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806345653a6d1461005c5780638da5cb5b14610073578063c15bae84146100ca575b600080fd5b34801561006857600080fd5b5061007161015a565b005b34801561007f57600080fd5b5061008861026b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100d657600080fd5b506100df610290565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561011f578082015181840152602081019050610104565b50505050905090810190601f16801561014c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b7f0df29a50b04a74cb4f41000f60d3d0f03a368c07e08d1ed2989948c1fc7ae5086000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff166001604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018060200182810382528381815460018160011615610100020316600290048152602001915080546001816001161561010002031660029004801561025a5780601f1061022f5761010080835404028352916020019161025a565b820191906000526020600020905b81548152906001019060200180831161023d57829003601f168201915b5050935050505060405180910390a1565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103265780601f106102fb57610100808354040283529160200191610326565b820191906000526020600020905b81548152906001019060200180831161030957829003601f168201915b5050505050815600a165627a7a72305820d16c029b297c3c5b55d646346b9631ec5b4d4aa54c3d127b0987060ab1562252002926a05312039e531530d94a31a409b2fc33630528572890f48df97b47cd6771f165b3a07b24c1f91e2cb1d8ba2ab5cc30e11debc9ab39dc52f79a9305e38a4ca6684802"
	// Bulk transfer
	params := "0xf8940705830138809400000000000000000000000000000000766f746580b4f3b2f10294042610e447c94ff0824b7aa89fab123930edc80594482e491c48d6f6138b181318faa191f71054d0b5018405f5e10025a07c2081155e4d0c5032e8600bc3e209636d3bd58d39e597c03d7d25d8e995989aa054e48e79458e045a316e109805068466de4bce0b5ac25ca2899ae5cdff277487"
	// Call contract 
	//params := "0xf866040585013880000094181d14c82a827fc050be55852031d2ff94ab5e91808445653a6d25a07089091496e01e9074faaee4a04c5e62c20c1a9ad9d597cf4d15bf96d3704381a02708c6985340ddab9f781286043e60113dc074c3b427a7f9588db3a2c6dbf247"
	// Regular transfer
	//params := "0xf86580058301388094482e491c48d6f6138b181318faa191f71054d0b585174876e8008026a0d361983b972addec2bf2f48d00699c72a605308ec3516343561b8bc2b805de14a020717a07b4edf76d207f4e618ae4872b10997a5d3a00f0a806bdcb0b04ce286a"
	var inspectMsg string = `{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["` + params + `"],"id":1}`
	recv = postMsg(inspectMsg, ip)
	fmt.Println(string(recv))
	var iptJsonMap map[string]interface{}
	err = json.Unmarshal(recv, &iptJsonMap)
	if err != nil {
		fmt.Println("inspect json unmarshal is faild")
	}
	iptResultMap := iptJsonMap["result"].(map[string]interface{})
    // "to" is empty to create a contract
	if iptResultMap["to"] == nil {
		fmt.Println("create contract")
	} else if iptResultMap["to"] == "0x00000000000000000000000000000000766f7465" {
		// Temporarily call the rpc interface to parse the input content.
		// curl -d '{"jsonrpc":"2.0","method":"brc_getAnalysisData","params":["0xf864b1f00294e79fead329b69540142fabd881099c04424cc49f94042610e447c94ff0824b7aa89fab123930edc8050183010000b1f00294e79fead329b69540142fabd881099c04424cc49f94eee227766ba28fa6abc9ec8fea0c56188bbd54c50183010000"],"id":1}' http://10.0.5.40:8111
		var getAnalysisData string = `{"jsonrpc":"2.0","method":"brc_getAnalysisData","params":["` +
			iptResultMap["data"].(string) + `"],"id":1}`
		data := postMsg(getAnalysisData, ip)
		fmt.Println(string(data))

		var dataJsonMap map[string]interface{}
		err = json.Unmarshal(data, &dataJsonMap)
		if err != nil {
			fmt.Println("data json unmarshal is faild")
		}
		dataJsonResult := dataJsonMap["result"].([]interface{})
		dataSize := len(dataJsonResult)
		for i := 0; i < dataSize; i++ {
			ownTx := dataJsonResult[i].(map[string]interface{})
			// type=2,Is a transfer transaction
			if ownTx["type"] == "0x2" {
				fmt.Println("Batch transfer account is:", ownTx["from"].(string))
					fmt.Println("Batch transfer to account is:", ownTx["to"].(string))
					fmt.Println("The amount of the bulk transfer transaction is:", ownTx["transcation_numbers"].(string))
			}
		}
	} else {
        // value=0,it is called contract
    	if iptResultMap["value"] == "0x0" {
    		fmt.Println("call contract")
    	} else { // value!=0,it is the original transfer transaction
    		fmt.Println("BRC out address is:", iptResultMap["from"].(string))
    		fmt.Println("BRC in address is:", iptResultMap["to"].(string))
    		fmt.Println("BRC num is:", iptResultMap["value"].(string))
    	}
	}
}

Clone this wiki locally