-
Notifications
You must be signed in to change notification settings - Fork 13
BaerChain Block Scan Code Example
weidong edited this page Apr 16, 2020
·
1 revision
1. Scan block code example Back Dir
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:8111
var msg string = `{"jsonrpc":"2.0","method":"brc_getBlockByNumber","params":["0x51", true],"id":1}`
// ip is the address of the rpc interface
ip := "http://127.0.0.1:8111"
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 in data
fmt.Println("data transaction")
// Currently calling the rpc interface to parse the input content
// curl -d '{"jsonrpc":"2.0","method":"brc_getAnalysisData","params":["0xf864b1f00294e79fead329b69540142fabd881099c04424cc49f94042610e447c94ff0824b7aa89fab123930edc8050183010000b1f00294e79fead329b69540142fabd881099c04424cc49f94eee227766ba28fa6abc9ec8fea0c56188bbd54c50183010000"],"id":1}' http://127.0.0.1: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{})
// When type is 2, it means transfer transaction
if ownTx["type"] == "0x2" {
fmt.Println("No.", i, "batch transfer out account is:", ownTx["from"].(string))
fmt.Println("No.", i, "batch transfer to the account is:", ownTx["to"].(string))
fmt.Println("No", i, " batch transfer transaction amount is:", ownTx["transcation_numbers"].(string))
} else if ownTx["type"] == "0x1" {
fmt.Println("vote")
} else if ownTx["type"] == "0x3" {
fmt.Println("pendingorder")
if ownTx["pendingorder_type"] == "0x1" {
fmt.Println("sell")
} else if ownTx["pendingorder_type"] == "0x2" {
fmt.Println("buy")
}
if ownTx["token_type"] == "0x1" {
fmt.Println("cookie")
}
if ownTx["buy_type"] == "0x0" {
fmt.Println("market price")
} else if ownTx["buy_type"] == "0x1" {
fmt.Println("limit price")
}
} else if ownTx["type"] == "0x4" {
fmt.Println("cancel pendingorder")
} else if ownTx["type"] == "0x8" {
fmt.Println("Receive income")
if ownTx["m_receivingType"] == 1 {
fmt.Println("Receiving the block income")
} else if ownTx["m_receivingType"] == 2 {
fmt.Println("Represents the receipt of matching system revenue")
}
} else if ownTx["type"] == "0x9" {
fmt.Println("Automated cookie transfer")
if ownTx["m_autoExType"] == 1 {
fmt.Println("Indicates that the transaction initiating address specifies that you use BRC to exchange cookies, and the transaction fee is paid by yourself")
} else if ownTx["m_autoExType"] == 2 {
fmt.Println("Indicates that the transaction originating address specifies the transfer address to use BRC to exchange cookies, and the transaction fee is paid by the transfer address")
}
fmt.Println("The number of BRC used to redeem cookies is:", ownTx["autoExNum"].(string))
fmt.Println("The amount of BRC transferred is:", ownTx["transferNum"].(string))
fmt.Println("The address to initiate the transfer transaction is:", ownTx["from"].(string))
fmt.Println("The address to accept the transfer is:", ownTx["to"].(string))
}
}
} else {
// When the input is not "", it is the calling contract
if tx["input"] != "" {
fmt.Println("Call contract")
} else if tx["input"] == "" { // When input is "", it is a transfer transaction
fmt.Println("The transfer-out account is:", tx["from"].(string))
fmt.Println("Transfer to account:", tx["to"].(string))
fmt.Println("The transaction amount is:", tx["value"].(string))
}
}
fmt.Println()
}
}2. Deserialization instruction description Back Dir
- Request
{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["Here is the transaction serialization data"],"id":17}
-
params parameter description:
- If it is a pending order redemption cookie
params := "0xf8840605830138809400000000000000000000000000000000766f746580a4e3a2e10394042610e447c94ff0824b7aa89fab123930edc80502018080865af3107a400026a0bbeaef47bc5933d17effc565717afe471b5b75adff9230c74ca160b1877f4a13a01de8ddaaf0fea32a6fecdbc18d9ea86f6fbbe7ee1b70a28b3d811444d379d313"- If it is to create a contract
params := "0xf9050005058501388000008080b904b0608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506040805190810160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506001908051906020019061009c9291906100a2565b50610147565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100e357805160ff1916838001178555610111565b82800160010185558215610111579182015b828111156101105782518255916020019190600101906100f5565b5b50905061011e9190610122565b5090565b61014491905b80821115610140576000816000905550600101610128565b5090565b90565b61035a806101566000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806345653a6d1461005c5780638da5cb5b14610073578063c15bae84146100ca575b600080fd5b34801561006857600080fd5b5061007161015a565b005b34801561007f57600080fd5b5061008861026b565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100d657600080fd5b506100df610290565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561011f578082015181840152602081019050610104565b50505050905090810190601f16801561014c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b7f0df29a50b04a74cb4f41000f60d3d0f03a368c07e08d1ed2989948c1fc7ae5086000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff166001604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018060200182810382528381815460018160011615610100020316600290048152602001915080546001816001161561010002031660029004801561025a5780601f1061022f5761010080835404028352916020019161025a565b820191906000526020600020905b81548152906001019060200180831161023d57829003601f168201915b5050935050505060405180910390a1565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103265780601f106102fb57610100808354040283529160200191610326565b820191906000526020600020905b81548152906001019060200180831161030957829003601f168201915b5050505050815600a165627a7a72305820d16c029b297c3c5b55d646346b9631ec5b4d4aa54c3d127b0987060ab1562252002926a05312039e531530d94a31a409b2fc33630528572890f48df97b47cd6771f165b3a07b24c1f91e2cb1d8ba2ab5cc30e11debc9ab39dc52f79a9305e38a4ca6684802"- If it is a calling contract
params := "0xf866040585013880000094181d14c82a827fc050be55852031d2ff94ab5e91808445653a6d25a07089091496e01e9074faaee4a04c5e62c20c1a9ad9d597cf4d15bf96d3704381a02708c6985340ddab9f781286043e60113dc074c3b427a7f9588db3a2c6dbf247"- If it is a regular transfer
params := "0xf86580058301388094482e491c48d6f6138b181318faa191f71054d0b585174876e8008026a0d361983b972addec2bf2f48d00699c72a605308ec3516343561b8bc2b805de14a020717a07b4edf76d207f4e618ae4872b10997a5d3a00f0a806bdcb0b04ce286a"- If it is a batch transfer
params := "0xf90100800584013880009400000000000000000000000000000000766f746580b89ef89cb3f202944d51ba213bad0f7ed97d336b2c00dc76576aac98940a96d44fc3b13c1157fe8648bd5ecca375d20af80185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac9894695193772208fb21b8cae0096ddea871d74e82cb0185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac989474ad8b5c9ef47a8cc01cb1cf7da7510b54e8318f0185e8d4a5100026a059b3df5536033ca7d4e68563b5248121533aee2e840a0859a782196233b01ee2a0522b0db1dac45f4e0219583b867585419983e99746be4a4afecab55ee7187c1e" -
Instruction example
- This time use batch transfer as a template
curl -d '{"jsonrpc":"2.0","method":"brc_inspectTransaction","params":["0xf90100800584013880009400000000000000000000000000000000766f746580b89ef89cb3f202944d51ba213bad0f7ed97d336b2c00dc76576aac98940a96d44fc3b13c1157fe8648bd5ecca375d20af80185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac9894695193772208fb21b8cae0096ddea871d74e82cb0185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac989474ad8b5c9ef47a8cc01cb1cf7da7510b54e8318f0185e8d4a5100026a059b3df5536033ca7d4e68563b5248121533aee2e840a0859a782196233b01ee2a0522b0db1dac45f4e0219583b867585419983e99746be4a4afecab55ee7187c1e"],"id":17}' http://127.0.0.1:8111 -
Return result
{
"id": 17,
"jsonrpc": "2.0",
"result": {
"data": "0xf89cb3f202944d51ba213bad0f7ed97d336b2c00dc76576aac98940a96d44fc3b13c1157fe8648bd5ecca375d20af80185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac9894695193772208fb21b8cae0096ddea871d74e82cb0185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac989474ad8b5c9ef47a8cc01cb1cf7da7510b54e8318f0185e8d4a51000",
"from": "0x4d51ba213bad0f7ed97d336b2c00dc76576aac98",
"gas": "0x1388000",
"gasPrice": "0x5",
"hash": "0x874b5ad48085b3585e740f40c389a82e497a5d44ccd316de33c06aec4a9c7192",
"nonce": "0x0",
"r": "0x59b3df5536033ca7d4e68563b5248121533aee2e840a0859a782196233b01ee2",
"s": "0x522b0db1dac45f4e0219583b867585419983e99746be4a4afecab55ee7187c1e",
"sighash": "0x3f01c09b9142117dad06657f6a7357016184ee840c0c97f0389c11225ad0d350",
"to": "0x00000000000000000000000000000000766f7465",
"v": "0x1",
"value": "0x0"
}
}3. Deserialization code example Back Dir
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() {
ip := "http://127.0.0.1:8111"
params := "0xf90100800584013880009400000000000000000000000000000000766f746580b89ef89cb3f202944d51ba213bad0f7ed97d336b2c00dc76576aac98940a96d44fc3b13c1157fe8648bd5ecca375d20af80185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac9894695193772208fb21b8cae0096ddea871d74e82cb0185e8d4a51000b3f202944d51ba213bad0f7ed97d336b2c00dc76576aac989474ad8b5c9ef47a8cc01cb1cf7da7510b54e8318f0185e8d4a5100026a059b3df5536033ca7d4e68563b5248121533aee2e840a0859a782196233b01ee2a0522b0db1dac45f4e0219583b867585419983e99746be4a4afecab55ee7187c1e"
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{})
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://127.0.0.1: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{})
// When type is 2, it means transfer transaction
if ownTx["type"] == "0x2" {
fmt.Println("No.", i, "batch transfer out account is:", ownTx["from"].(string))
fmt.Println("No.", i, "batch transfer to the account is:", ownTx["to"].(string))
fmt.Println("No", i, " batch transfer transaction amount is:", ownTx["transcation_numbers"].(string))
}
}
} else {
// When the input is not "", it is the calling contract
if iptResultMap["input"] != "" {
fmt.Println("Call contract")
} else { // When input is "", it is the original transfer transaction
fmt.Println("The transfer-out account is:", iptResultMap["from"].(string))
fmt.Println("Accounts that accept transfers is", iptResultMap["to"].(string))
fmt.Println("transfer amount is:", iptResultMap["value"].(string))
}
}
}