mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
reformated response for all getter fns
This commit is contained in:
parent
84892a4a12
commit
7e9aa2c8bd
1 changed files with 44 additions and 57 deletions
|
|
@ -3,6 +3,7 @@ package shyftdb
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
|
@ -18,9 +19,9 @@ import (
|
||||||
|
|
||||||
//SBlock type
|
//SBlock type
|
||||||
type SBlock struct {
|
type SBlock struct {
|
||||||
hash string
|
Hash string
|
||||||
coinbase string
|
Coinbase string
|
||||||
number string
|
Number string
|
||||||
}
|
}
|
||||||
|
|
||||||
//blockRes struct
|
//blockRes struct
|
||||||
|
|
@ -32,16 +33,7 @@ type blockRes struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type txRes struct {
|
type txRes struct {
|
||||||
txHash string
|
TxEntry []ShyftTxEntryPretty
|
||||||
to_addr string
|
|
||||||
from_addr string
|
|
||||||
blockHash string
|
|
||||||
amount string
|
|
||||||
gasPrice string
|
|
||||||
gas uint64
|
|
||||||
nonce uint64
|
|
||||||
data []byte
|
|
||||||
TxEntry []ShyftTxEntryPretty
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//ShyftTxEntry structure
|
//ShyftTxEntry structure
|
||||||
|
|
@ -62,8 +54,8 @@ type ShyftTxEntryPretty struct {
|
||||||
To string
|
To string
|
||||||
From string
|
From string
|
||||||
BlockHash string
|
BlockHash string
|
||||||
Amount string
|
Amount uint64
|
||||||
GasPrice string
|
GasPrice uint64
|
||||||
Gas uint64
|
Gas uint64
|
||||||
Nonce uint64
|
Nonce uint64
|
||||||
Data []byte
|
Data []byte
|
||||||
|
|
@ -252,8 +244,9 @@ func WriteMinerReward(db *leveldb.DB, block *types.Block) {
|
||||||
// Getters
|
// Getters
|
||||||
//////////
|
//////////
|
||||||
//GetAllBlocks returns []SBlock blocks for API
|
//GetAllBlocks returns []SBlock blocks for API
|
||||||
func GetAllBlocks(sqldb *sql.DB) []SBlock {
|
func GetAllBlocks(sqldb *sql.DB) string {
|
||||||
var arr blockRes
|
var arr blockRes
|
||||||
|
var blockArr string
|
||||||
rows, err := sqldb.Query(`
|
rows, err := sqldb.Query(`
|
||||||
SELECT
|
SELECT
|
||||||
number,
|
number,
|
||||||
|
|
@ -274,45 +267,43 @@ func GetAllBlocks(sqldb *sql.DB) []SBlock {
|
||||||
&hash,
|
&hash,
|
||||||
&coinbase,
|
&coinbase,
|
||||||
)
|
)
|
||||||
|
|
||||||
arr.Blocks = append(arr.Blocks, SBlock{
|
arr.Blocks = append(arr.Blocks, SBlock{
|
||||||
hash: hash,
|
Hash: hash,
|
||||||
number: num,
|
Number: num,
|
||||||
coinbase: coinbase,
|
Coinbase: coinbase,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
blocks, _ := json.Marshal(arr.Blocks)
|
||||||
|
blocksFmt := string(blocks)
|
||||||
|
blockArr = blocksFmt
|
||||||
}
|
}
|
||||||
return arr.Blocks
|
return blockArr
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetBlock queries to send single block info
|
//GetBlock queries to send single block info
|
||||||
//TODO provide blockHash arg passed from handler.go
|
//TODO provide blockHash arg passed from handler.go
|
||||||
func GetBlock(sqldb *sql.DB) []SBlock {
|
func GetBlock(sqldb *sql.DB) string {
|
||||||
var arr blockRes
|
|
||||||
sqlStatement := `SELECT * FROM blocks WHERE number=$1;`
|
sqlStatement := `SELECT * FROM blocks WHERE number=$1;`
|
||||||
row := sqldb.QueryRow(sqlStatement, 3)
|
row := sqldb.QueryRow(sqlStatement, 3)
|
||||||
var num string
|
var num string
|
||||||
var hash string
|
var hash string
|
||||||
var coinbase string
|
var coinbase string
|
||||||
err := row.Scan(&num, &hash, &coinbase)
|
row.Scan(&num, &hash, &coinbase)
|
||||||
|
|
||||||
switch err {
|
block := SBlock{
|
||||||
case sql.ErrNoRows:
|
Hash: hash,
|
||||||
fmt.Println("No rows were returned!")
|
Number: num,
|
||||||
case nil:
|
Coinbase: coinbase,
|
||||||
fmt.Println(num, hash, coinbase)
|
|
||||||
default:
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
arr.Blocks = append(arr.Blocks, SBlock{
|
json, _ := json.Marshal(block)
|
||||||
hash: hash,
|
return string(json)
|
||||||
number: num,
|
|
||||||
coinbase: coinbase,
|
|
||||||
})
|
|
||||||
return arr.Blocks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetAllTransactions getter fn for API
|
//GetAllTransactions getter fn for API
|
||||||
func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty {
|
func GetAllTransactions(sqldb *sql.DB) string {
|
||||||
var arr txRes
|
var arr txRes
|
||||||
|
var txx string
|
||||||
rows, err := sqldb.Query(`
|
rows, err := sqldb.Query(`
|
||||||
SELECT
|
SELECT
|
||||||
txhash,
|
txhash,
|
||||||
|
|
@ -328,14 +319,13 @@ func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty {
|
||||||
fmt.Println("err")
|
fmt.Println("err")
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var txhash string
|
var txhash string
|
||||||
var to_addr string
|
var to_addr string
|
||||||
var from_addr string
|
var from_addr string
|
||||||
var blockhash string
|
var blockhash string
|
||||||
var amount string
|
var amount uint64
|
||||||
var gasprice string
|
var gasprice uint64
|
||||||
var gas uint64
|
var gas uint64
|
||||||
var nonce uint64
|
var nonce uint64
|
||||||
err = rows.Scan(
|
err = rows.Scan(
|
||||||
|
|
@ -348,6 +338,7 @@ func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty {
|
||||||
&gas,
|
&gas,
|
||||||
&nonce,
|
&nonce,
|
||||||
)
|
)
|
||||||
|
|
||||||
arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{
|
arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{
|
||||||
TxHash: txhash,
|
TxHash: txhash,
|
||||||
To: to_addr,
|
To: to_addr,
|
||||||
|
|
@ -358,13 +349,16 @@ func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty {
|
||||||
Gas: gas,
|
Gas: gas,
|
||||||
Nonce: nonce,
|
Nonce: nonce,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tx, _ := json.Marshal(arr.TxEntry)
|
||||||
|
newtx := string(tx)
|
||||||
|
txx = newtx
|
||||||
}
|
}
|
||||||
return arr.TxEntry
|
return txx
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetTransaction fn returns single tx
|
//GetTransaction fn returns single tx
|
||||||
func GetTransaction(sqldb *sql.DB) []ShyftTxEntryPretty {
|
func GetTransaction(sqldb *sql.DB) string {
|
||||||
var arr txRes
|
|
||||||
sqlStatement := `SELECT
|
sqlStatement := `SELECT
|
||||||
txhash,
|
txhash,
|
||||||
to_addr,
|
to_addr,
|
||||||
|
|
@ -380,22 +374,13 @@ func GetTransaction(sqldb *sql.DB) []ShyftTxEntryPretty {
|
||||||
var to_addr string
|
var to_addr string
|
||||||
var from_addr string
|
var from_addr string
|
||||||
var blockhash string
|
var blockhash string
|
||||||
var amount string
|
var amount uint64
|
||||||
var gasprice string
|
var gasprice uint64
|
||||||
var gas uint64
|
var gas uint64
|
||||||
var nonce uint64
|
var nonce uint64
|
||||||
row.Scan(&txhash, &to_addr, &from_addr, &blockhash, &amount, &gasprice, &gas, &nonce)
|
row.Scan(&txhash, &to_addr, &from_addr, &blockhash, &amount, &gasprice, &gas, &nonce)
|
||||||
|
|
||||||
// switch err {
|
tx := ShyftTxEntryPretty{
|
||||||
// case sql.ErrNoRows:
|
|
||||||
// fmt.Println("No rows were returned!")
|
|
||||||
// case nil:
|
|
||||||
// fmt.Println(txhash, to_addr, from_addr, blockhash, amount, gasprice, gas, nonce)
|
|
||||||
// default:
|
|
||||||
// panic(err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{
|
|
||||||
TxHash: txhash,
|
TxHash: txhash,
|
||||||
To: to_addr,
|
To: to_addr,
|
||||||
From: from_addr,
|
From: from_addr,
|
||||||
|
|
@ -404,6 +389,8 @@ func GetTransaction(sqldb *sql.DB) []ShyftTxEntryPretty {
|
||||||
GasPrice: gasprice,
|
GasPrice: gasprice,
|
||||||
Gas: gas,
|
Gas: gas,
|
||||||
Nonce: nonce,
|
Nonce: nonce,
|
||||||
})
|
}
|
||||||
return arr.TxEntry
|
json, _ := json.Marshal(tx)
|
||||||
|
|
||||||
|
return string(json)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue