Skip to content

Commit

Permalink
完善部分交易函数, 删除废弃的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
chai2010 committed Dec 3, 2019
1 parent ff2f9cc commit 7dbcf01
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 145 deletions.
73 changes: 8 additions & 65 deletions api_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,18 @@

package ethrpc

import (
"fmt"
"regexp"
)

// 数据库接口
type DbAPI interface {
PutString(dbname, key, value string) (bool, error)
GetString(dbname, key string) (string, error)
PutHex(dbname, key, value string) (bool, error)
GetHex(dbname, key string) (string, error)
}

// 构造数据库接口
func NewDbAPI(host string) DbAPI {
return &dbAPI{host}
}

// 保存字符串到本地数据库
func DbPutString(host string, dbname, key, value string) (bool, error) {
var ok bool
var err = ethrpcCall(host, "db_putString", &ok, dbname, key, value)
return ok, err
}

// 从本地数据库读取字符串数据
func DbGetString(host string, dbname, key string) (string, error) {
var s string
var err = ethrpcCall(host, "db_getString", &s, dbname, key)
return s, err
func db_putString(host string, dbname, key, value string) (bool, error) {
panic("DEPRECATED")
}

// 保存十六进制字符串到本地数据库
func DbPutHex(host string, dbname, key, value string) (bool, error) {
if mached, _ := regexp.MatchString(`^0[xX][0-9a-fA-F]+$`, value); !mached {
return false, fmt.Errorf("ethrpc.DbPutHex: invalid hex: %q", value)
}

var ok bool
var err = ethrpcCall(host, "db_putHex", &ok, dbname, key, value)
return ok, err
}

// 从本地数据库读取十六进制字符串数据
func DbGetHex(host string, dbname, key string) (string, error) {
var s string
var err = ethrpcCall(host, "db_getHex", &s, dbname, key)
if err != nil {
return "", err
}
if mached, _ := regexp.MatchString(`^0[xX][0-9a-fA-F]+$`, s); !mached {
return s, fmt.Errorf("ethrpc.DbPutHex: invalid hex: %q", s)
}
return s, nil
func db_getString(host string, dbname, key string) (string, error) {
panic("DEPRECATED")
}

type dbAPI struct{ host string }

func (p *dbAPI) PutString(dbname, key, value string) (bool, error) {
return DbPutString(p.host, dbname, key, value)
func db_putHex(host string, dbname, key, value string) (bool, error) {
panic("DEPRECATED")
}
func (p *dbAPI) GetString(dbname, key string) (string, error) {
return DbGetString(p.host, dbname, key)

}
func (p *dbAPI) PutHex(dbname, key, value string) (bool, error) {
return DbPutHex(p.host, dbname, key, value)

}
func (p *dbAPI) GetHex(dbname, key string) (string, error) {
return DbGetHex(p.host, dbname, key)
func db_getHex(host string, dbname, key string) (string, error) {
panic("DEPRECATED")
}
60 changes: 26 additions & 34 deletions api_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,47 +202,47 @@ func EthEstimateGas(host string, tx *TxIn) (result int64, err error) {
return parseInt64(s)
}

// todo list

func eth_getBlockByHash(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getBlockByHash", &todo)
// 根据hash查询交易信息
func EthGetTransactionByHash(host, hash string) (tx TxResult, err error) {
err = ethrpcCall(host, "eth_getTransactionByHash", &tx, hash)
return
}

func eth_getBlockByNumber(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getBlockByNumber", &todo)
// 根据区块hash和偏移量查询交易信息
func EthGetTransactionByBlockHashAndIndex(host, blockHash string, index int) (tx TxResult, err error) {
err = ethrpcCall(host, "eth_getTransactionByBlockHashAndIndex", &tx, blockHash, fmt.Sprintf("0x%x", index))
return
}

func eth_getTransactionByHash(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getTransactionByHash", &todo)
// 查询交易回执
func EthGetTransactionReceipt(host, hash string) (txReceipt TxReceipt, err error) {
err = ethrpcCall(host, "eth_getTransactionReceipt", &txReceipt, hash)
return
}

func eth_getTransactionByBlockHashAndIndex(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getTransactionByBlockHashAndIndex", &todo)
// 查询等待处理的交易列表
func EthPendingTransactions(host string) (txList []TxResult, err error) {
err = ethrpcCall(host, "eth_pendingTransactions", &txList)
return
}

func eth_getTransactionByBlockNumberAndIndex(host string) (err error) {
// todo list

func eth_getBlockByHash(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getTransactionByBlockNumberAndIndex", &todo)
err = ethrpcCall(host, "eth_getBlockByHash", &todo)
return
}

func eth_getTransactionReceipt(host string) (err error) {
func eth_getBlockByNumber(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getTransactionReceipt", &todo)
err = ethrpcCall(host, "eth_getBlockByNumber", &todo)
return
}

func eth_pendingTransactions(host string) (err error) {
func eth_getTransactionByBlockNumberAndIndex(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_pendingTransactions", &todo)
err = ethrpcCall(host, "eth_getTransactionByBlockNumberAndIndex", &todo)
return
}

Expand All @@ -259,27 +259,19 @@ func eth_getUncleByBlockNumberAndIndex(host string) (err error) {
}

func eth_getCompilers(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_getCompilers", &todo)
return
panic("DEPRECATED")
}

func eth_compileLLL(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_compileLLL", &todo)
return
func eth_compileSolidity(host string) (err error) {
panic("DEPRECATED")
}

func eth_compileSolidity(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_compileSolidity", &todo)
return
func eth_compileLLL(host string) (err error) {
panic("DEPRECATED")
}

func eth_compileSerpent(host string) (err error) {
var todo string
err = ethrpcCall(host, "eth_compileSerpent", &todo)
return
panic("DEPRECATED")
}

func eth_newFilter(host string) (err error) {
Expand Down
63 changes: 37 additions & 26 deletions api_eth_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package ethrpc

import (
"encoding/json"
"fmt"
"math/big"
)

Expand All @@ -18,36 +17,48 @@ type SyncingStatus struct {

// 输入的交易数据
type TxIn struct {
From string
From string // 不参与签名
To string
Gas int64
GasPrice *big.Int
Value *big.Int
Gas string
GasPrice string
Value string
Data string
Nonce int64
Nonce string
}

// 编码为JSON
func (p *TxIn) MarshalJSON() ([]byte, error) {
var x = struct {
From string `json:"from"`
To string
Gas string
GasPrice string
Value string
Data string
Nonce string
}{
From: p.From,
To: p.To,
Gas: fmt.Sprintf("0x%x", p.Gas),
GasPrice: fmt.Sprintf("0x%x", p.GasPrice),
Value: fmt.Sprintf("0x%x", p.Value),
Data: p.Data,
Nonce: fmt.Sprintf("0x%x", p.Nonce),
}
// 查询返回的交易数据
type TxResult 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"`
V string `json:"v"`
R string `json:"r"`
S string `json:"s"`
}

return json.Marshal(x)
// 交易回执
type TxReceipt struct {
TransactionHash string `json:"transactionHash"`
TransactionIndex string `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
BlockNumber string `json:"blockNumber"`
From string `json:"from"`
To string `json:"to"`
CumulativeGasUsed string `json:"cumulativeGasUsed"`
GasUsed string `json:"gasUsed"`
ContractAddress string `json:"contractAddress"`
Logs json.RawMessage `json:"logs"`
LogsBloom string `json:"logsBloom"`
Root string `json:"root"`
Status string `json:"status"`
}

type Block struct {
Expand Down
34 changes: 14 additions & 20 deletions api_shh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,38 @@

package ethrpc

// todo ist

func shh_post(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_post", &todo)
func shh_version(host string) (version string, err error) {
err = ethrpcCall(host, "shh_version", &version)
return
}

func shh_version(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_version", &todo)
func shh_post(host string, msg *ShhPostObject) (ok bool, err error) {
err = ethrpcCall(host, "shh_post", &ok, msg)
return
}

func shh_newIdentity(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_newIdentity", &todo)
func shh_newIdentity(host string) (id string, err error) {
err = ethrpcCall(host, "shh_newIdentity", &id)
return
}

func shh_hasIdentity(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_hasIdentity", &todo)
func shh_hasIdentity(host, id string) (exists bool, err error) {
err = ethrpcCall(host, "shh_hasIdentity", &exists, id)
return
}

func shh_newGroup(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_newGroup", &todo)
func shh_newGroup(host string) (id string, err error) {
err = ethrpcCall(host, "shh_newGroup", &id)
return
}

func shh_addToGroup(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_addToGroup", &todo)
func shh_addToGroup(host, id string) (ok bool, err error) {
err = ethrpcCall(host, "shh_addToGroup", &ok, id)
return
}

// todo ist

func shh_newFilter(host string) (err error) {
var todo string
err = ethrpcCall(host, "shh_newFilter", &todo)
Expand Down

0 comments on commit 7dbcf01

Please sign in to comment.