很多了解比原链的都知道,比原链是专注信息和数字资产在链上交互和流转的公链项目。所以我们通过调一遍开发文档的来实现信息上链。 首先用postman进行请求演示,然后我们用golang写个demo, 在我们用golang代码实现之前,我们先要做一些准备工作。首先确保自己在本地已经搭建好了比原的节点,如果你还没有搭建好节点,请参考开发文档:https://docs.bytom.io/mydoc_build_environment.cn.html,然后接着往下操作
step1:确保自己账户是有足够BTM的,如果没有可以去比原链水龙头领取BTM,领取地址:http://test.blockmeta.com/faucet.php
step2:发行自己的资产,参考:http://8btc.com/forum.php?mod=viewthread&tid=242940&extra=
step3: 信息上链的本质就是其实就是创建并发送一笔交易,我们都知道通过api发起交易主要有三个步骤,先 build → sign → submit,分别对应的api是 build-transaction、sign-transaction、submit-transaction。然后就直接用自己选择的开发语言去实现数据上链。
step4:
参考代码:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
//build-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type BytomAccount struct {
AccountId string `json:"account_id"`
Amount int `json:"amount"`
AssetId string `json:"asset_id"`
Type string `json:"type"`
}
type BaseTransaction struct{}
type TransactionParams struct {
BaseTransaction *BaseTransaction `json:"base_transaction"`
Actions []BytomAccount `json:"actions"`
Ttl int `json:"ttl"`
TimeRange int `json:"time_range"`
}
//sign-transaction params
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type Transaction struct {
}
type SignParams struct {
Password string `json:"password"`
Transaction Transaction `json:"transaction"`
}
//submit-transaction
//https://bytom.github.io/mydoc_RPC_call.cn.html#build-transaction
type SubmitParams struct {
RawTransaction string `json:"raw_transaction"`
}
type SubmitResponse struct {
TxId string `json:"tx_id"`
}
func main() {
account1, account2, account3 := BytomAccount{}, BytomAccount{}, BytomAccount{}
account1.AccountId = "0KTCS3R5G0A02"
account1.Amount = 10000000
account1.AssetId = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
account1.Type = "spend_account"
account2.AccountId = "0KTCS3R5G0A02"
account2.Amount = 100
account2.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account2.Type = "spend_account"
account3.AccountId = "0KTCS3R5G0A02"
account3.Amount = 100
account3.AssetId = "608037f96e8d1613d900c67a0730cc90e2a03311fb7d091588f7eb551a6103cd"
account3.Type = "retire"
//var array
var actions []BytomAccount
//append three params
array_actions := append(actions, account1, account2, account3)
transaction_params := &TransactionParams{}
transaction_params.Actions = array_actions
transaction_params.Ttl = 0
transaction_params.TimeRange = 1521625823
//本地测试网节点
//build-transaction
port := "http://127.0.0.1:9888/build-transaction"
value, err := SendTransactionRetire(transaction_params, port)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println("销毁资产接口返回的参数:", value)
//sign-transaction
//
//submit-transaction
//
}
//send post request
func SendTransactionRetire(params *TransactionParams, port string) (v interface{}, err error) {
//以本地测试网节点连接
ParamsStr, err := json.Marshal(params)
if err != nil {
return nil, err
}
jsonStr := bytes.NewBuffer(ParamsStr)
fmt.Println(jsonStr)
req, err := http.NewRequest("POST", port, jsonStr)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var bodyBytes []byte
if resp.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
}
return string(bodyBytes), nil
}
step5:上面的代码只是build-transaction一个步骤,另外sign-transaction和submit-transaction请求需要自己去组织参数进行请求。请求完submit-transaction 获得返回的交易hash,去区块链浏览器上查看自己的上链信息。区块链浏览器地址:http://52.82.46.157:8082/
网友评论