区块链结构体创建
这个在线工具使用起来非常简单,只需要将JSON数据粘贴在左边,就会在右边自动成生成对应的结构体定义:
注:以下为以太坊交易信息
{"jsonrpc":"2.0","id":1,"result":{"blockHash":"0x153407ebeb73141a816492eba75d39a6d65c558e33b104d26693cb1eadc131c0","blockNumber":"0x3dd1ec","from":"0x6e60f5243e1a3f0be3f407b5afe9e5395ee82aa2","gas":"0x5208","gasPrice":"0x1264c45600","hash":"0x9994c8fe157d6547a3b484b1995bf3429253a1e3998bf092df270807377ed134","input":"0x","nonce":"0x648","to":"0xac965f9832efd7684bbbd7ceed5891a337bca302","transactionIndex":"0x0","value":"0x1bc16d674ec80000","type":"0x0","v":"0x539bb","r":"0x7afca8f7bc610abe5a4e2326a9df04f5da234c9fcca63e836c450ac442411f5c","s":"0x3139d40ca81fba2dc0ea5acb5d3d0d3040f21a0898c4ae39ff0343242a044a53"}}
type AutoGenerated struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id"`
Result struct {
BlockHash string `json:"blockHash"`
BlockNumber string `json:"blockNumber"`
From string `json:"from"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
Hash string `json:"hash"`
Input string `json:"input"`
Nonce string `json:"nonce"`
To string `json:"to"`
TransactionIndex string `json:"transactionIndex"`
Value string `json:"value"`
Type string `json:"type"`
V string `json:"v"`
R string `json:"r"`
S string `json:"s"`
} `json:"result"`
}
使用type from json 方法,将会弹出提示输入内容的弹框,将根据粘贴的json内容生成结构体
注:以下为以太坊区块数据
{"jsonrpc":"2.0","id":1,"result":{"difficulty":"0xa319c","extraData":"0xd883010a05846765746888676f312e31362e36856c696e7578","gasLimit":"0x7a1200","gasUsed":"0x0","hash":"0x199ea7129792539f5ab3f90e6b68fe745d677af1862bcf37986d683748205100","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x6e60f5243e1a3f0be3f407b5afe9e5395ee82aa2","mixHash":"0xa15f85597acdfb5bd6d63c0394365c41c647e3cad02b543f81677630949721e9","nonce":"0x146d903669ebc81d","number":"0x3dd21b","parentHash":"0x0bc06a66b3adf832876d1c9eb409ad449d3c71c25685214388370a5d05209a56","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x21b","stateRoot":"0x4798d9bae39233ead7b303d8ca8fefea7c125f275dde919c5b460c510bb6c4bb","timestamp":"0x60f790ec","totalDifficulty":"0x32706575c9c","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]}}
type T struct {
Jsonrpc string `json:"jsonrpc"`
Id int `json:"id"`
Result struct {
Difficulty string `json:"difficulty"`
ExtraData string `json:"extraData"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
Hash string `json:"hash"`
LogsBloom string `json:"logsBloom"`
Miner string `json:"miner"`
MixHash string `json:"mixHash"`
Nonce string `json:"nonce"`
Number string `json:"number"`
ParentHash string `json:"parentHash"`
ReceiptsRoot string `json:"receiptsRoot"`
Sha3Uncles string `json:"sha3Uncles"`
Size string `json:"size"`
StateRoot string `json:"stateRoot"`
Timestamp string `json:"timestamp"`
TotalDifficulty string `json:"totalDifficulty"`
Transactions []interface{} `json:"transactions"`
TransactionsRoot string `json:"transactionsRoot"`
Uncles []interface{} `json:"uncles"`
} `json:"result"`
}
有时候还需要将数据表转为结构体,可利用建表sql语句转为结构体
CREATE TABLE `member` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'id',
`phone` char(11) NOT NULL COMMENT '手机号',
`pay_password` varchar(50) NOT NULL DEFAULT '' COMMENT '支付密码',
`address` char(42) NOT NULL DEFAULT '' COMMENT '区块链地址',
`total_amount` int(10) NOT NULL DEFAULT '0' COMMENT '总积分',
`available_amount` int(10) NOT NULL DEFAULT '0' COMMENT '可用积分',
`freeze_amount` int(10) NOT NULL DEFAULT '0' COMMENT '冻结积分',
`total_recharge` int(10) NOT NULL DEFAULT '0' COMMENT '总充值金额',
`salt` varchar(20) NOT NULL DEFAULT '' COMMENT '盐',
`token` varchar(20) NOT NULL DEFAULT '' COMMENT '登录token',
`status` int(10) NOT NULL DEFAULT '0' COMMENT '用户状态:正常(0);禁用(1)',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4
type Member struct {
Id int64 `json:"id"` // id
Phone string `json:"phone"` // 手机号
PayPassword string `json:"pay_password"` // 支付密码
Address string `json:"address"` // 区块链地址
TotalAmount int64 `json:"total_amount"` // 总积分
AvailableAmount int64 `json:"available_amount"` // 可用积分
FreezeAmount int64 `json:"freeze_amount"` // 冻结积分
TotalRecharge int64 `json:"total_recharge"` // 总充值金额
Salt string `json:"salt"` // 盐
Token string `json:"token"` // 登录token
Status int64 `json:"status"` // 用户状态:正常(0);禁用(1)
CreateTime time.Time `json:"create_time"` // 创建时间
UpdateTime time.Time `json:"update_time"` // 更新时间
}
注:以下结构体测试均为以太坊系节点
type Block struct {
Number string `json:"number"`
Size string `json:"size"`
Timestamp string `json:"timestamp"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Coinbase string `json:"miner"`
TxDatas []*Transaction `json:"transactions"`
type BlockTxHash struct {
Number string `json:"number"`
Size string `json:"size"`
Timestamp string `json:"timestamp"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Coinbase string `json:"miner"`
TxHexs []string `json:"transactions"`
}
type Transaction struct {
BlockHash string `json:"blockHash"`
BlockNumber string `json:"blockNumber"`
From string `json:"from"`
Recipient string `json:"to"`
GasLimit string `json:"gas"`
GasPrice string `json:"gasPrice"`
Hash string `json:"hash"`
Payload string `json:"input"`
Nonce string `json:"nonce"`
R string `json:"r"`
S string `json:"s"`
V string `json:"v"`
Index string `json:"transactionIndex"`
Value string `json:"value"`
}
type TxReceipt struct {
Hash string `json:"transactionHash"`
Index string `json:"transactionIndex"`
BlockNumber string `json:"blockNumber"`
BlockHash string `json:"blockHash"`
GasUsedTotal string `json:"cumulativeGasUsed"`
GasUsed string `json:"gasUsed"`
Contract string `json:"contractAddress"`
LogsBloom string `json:"logsBloom"`
Status string `json:"status"`
From string `json:"from"`
To string `json:"to"`
Logs []*ReceiptLogs `json:"logs"`
}
type ReceiptLogs struct {
Address string `json:"address"`
BlockNumber string `json:"blockNumber"`
BlockHash string `json:"blockHash"`
Index string `json:"transactionIndex"`
Hash string `json:"transactionHash"`
Data string `json:"data"`
LogIndex string `json:"logIndex"`
Removed bool `json:"removed"`
Topics []string `json:"topics"`
}
type Request struct {
ID string `json:"id"`
Mthd string `json:"method"`
Args []interface{} `json:"params"`
Version string `json:"jsonrpc"`
}
本系列文章: 从零开发区块链应用(一)--golang配置文件管理工具viper 从零开发区块链应用(二)--mysql安装及数据库表的安装创建 从零开发区块链应用(三)--mysql初始化及gorm框架使用 从零开发区块链应用(四)--自定义业务错误信息 从零开发区块链应用(五)--golang网络请求 从零开发区块链应用(六)--gin框架使用 从零开发区块链应用(七)--gin框架参数获取 从零开发区块链应用(八)--结构体初识 从零开发区块链应用(九)--区块链结构体创建 从零开发区块链应用(十)--golang协程使用 从零开发区块链应用(十一)--以太坊地址生成 从零开发区块链应用(十二)--以太坊余额查询 从零开发区块链应用(十三)--以太坊区块查询 从零开发区块链应用(十四)--以太坊交易哈希查询 从零开发区块链应用(十五)--以太坊交易匹配查询 从零开发区块链应用(十六)--ETH转账处理
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!