diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index de61956f15..1c55261cd5 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -14,24 +14,44 @@ import ( // GetTransaction gets txs func GetTransaction(w http.ResponseWriter, r *http.Request) { - // vars := mux.Vars(r) - // address := vars["address"] + connStr := "user=postgres dbname=shyftdb sslmode=disable" + blockExplorerDb, err := sql.Open("postgres", connStr) + if err != nil { + return + } + + getTxResponse := shyftdb.GetTransaction(blockExplorerDb) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } - //tx := shyftdb.GetTransaction() w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - //fmt.Fprintln(w, "Get All Transactions", addresses) + fmt.Fprintln(w, getTxResponse) } // GetAllTransactions gets txs func GetAllTransactions(w http.ResponseWriter, r *http.Request) { - //txs := shyftdb.GetAllTransactions() + connStr := "user=postgres dbname=shyftdb sslmode=disable" + blockExplorerDb, err := sql.Open("postgres", connStr) + if err != nil { + return + } + + txs := shyftdb.GetAllTransactions(blockExplorerDb) + + if err != nil { + http.Error(w, err.Error(), 500) + return + } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - //fmt.Fprintln(w, "Get All Transactions", address) + fmt.Fprintln(w, txs) } // GetBalance gets balance @@ -57,7 +77,6 @@ func GetBalances(w http.ResponseWriter, r *http.Request) { //GetBlock returns block json func GetBlock(w http.ResponseWriter, r *http.Request) { - connStr := "user=postgres dbname=shyftdb sslmode=disable" blockExplorerDb, err := sql.Open("postgres", connStr) if err != nil { diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go index fadd46ea94..dcd5831d8b 100644 --- a/blockExplorerApi/routes.go +++ b/blockExplorerApi/routes.go @@ -48,7 +48,7 @@ var routes = Routes{ Route{ "GetTransaction", "GET", - "/api/get_transaction/{address}", + "/api/get_transaction", GetTransaction, }, Route{ diff --git a/shyftdb/shyft_database_util.go b/shyftdb/shyft_database_util.go index 71c56357a1..88288a8714 100644 --- a/shyftdb/shyft_database_util.go +++ b/shyftdb/shyft_database_util.go @@ -10,7 +10,6 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" "github.com/syndtr/goleveldb/leveldb" - "github.com/syndtr/goleveldb/leveldb/util" "database/sql" @@ -32,6 +31,19 @@ type blockRes struct { Blocks []SBlock } +type txRes struct { + txHash string + to_addr string + from_addr string + blockHash string + amount string + gasPrice string + gas uint64 + nonce uint64 + data []byte + TxEntry []ShyftTxEntryPretty +} + //ShyftTxEntry structure type ShyftTxEntry struct { TxHash common.Hash @@ -50,8 +62,8 @@ type ShyftTxEntryPretty struct { To string From string BlockHash string - Amount *big.Int - GasPrice *big.Int + Amount string + GasPrice string Gas uint64 Nonce uint64 Data []byte @@ -114,10 +126,6 @@ func WriteTransactions(sqldb *sql.DB, tx *types.Transaction, blockHash common.Ha return nil } -// key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) -// if err := db.Put(key, encodedData.Bytes(), nil); err != nil { -// log.Crit("Failed to store TX", "err", err) -// } //WriteAccountBalances(db, tx) // func WriteFromBalance(db *leveldb.DB, tx *types.Transaction) { @@ -302,78 +310,92 @@ func GetBlock(sqldb *sql.DB) []SBlock { return arr.Blocks } -func GetAllTransactions(db *leveldb.DB) []ShyftTxEntryPretty { - var txs []ShyftTxEntryPretty - iter := db.NewIterator(util.BytesPrefix([]byte("tx-")), nil) - for iter.Next() { - var txData ShyftTxEntry - d := gob.NewDecoder(bytes.NewBuffer(iter.Value())) - if err := d.Decode(&txData); err != nil { - log.Crit("Failed to decode tx:", "err", err) - } - prettyFormat := ShyftTxEntryPretty{ - TxHash: txData.TxHash.Hex(), - From: txData.From.Hex(), - To: txData.To.Hex(), - BlockHash: txData.BlockHash, - Amount: txData.Amount, - Gas: txData.Gas, - GasPrice: txData.GasPrice, - Nonce: txData.Nonce, - Data: txData.Data, - } - txs = append(txs, prettyFormat) - // Uncomment to view "pretty" version of data - //fmt.Println("DECODED TX") - //fmt.Println("Tx Hash: ", txData.TxHash.Hex()) - //fmt.Println("From: ", txData.From.Hex()) - //fmt.Println("To: ", txData.To.Hex()) - //fmt.Println("BlockHash: ", txData.BlockHash.Hex()) - //fmt.Println("Amount: ", txData.Amount) - //fmt.Println("Gas: ", txData.Gas) - //fmt.Println("GasPrice: ", txData.GasPrice) - //fmt.Println("Nonce: ", txData.Nonce) - //fmt.Println("Data: ", txData.Data) +//GetAllTransactions getter fn for API +func GetAllTransactions(sqldb *sql.DB) []ShyftTxEntryPretty { + var arr txRes + rows, err := sqldb.Query(` + SELECT + txhash, + to_addr, + from_addr, + blockhash, + amount, + gasprice, + gas, + nonce + FROM txs`) + if err != nil { + fmt.Println("err") } - iter.Release() - return txs + defer rows.Close() + + for rows.Next() { + var txhash string + var to_addr string + var from_addr string + var blockhash string + var amount string + var gasprice string + var gas uint64 + var nonce uint64 + err = rows.Scan( + &txhash, + &to_addr, + &from_addr, + &blockhash, + &amount, + &gasprice, + &gas, + &nonce, + ) + arr.TxEntry = append(arr.TxEntry, ShyftTxEntryPretty{ + TxHash: txhash, + To: to_addr, + From: from_addr, + BlockHash: blockhash, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + Nonce: nonce, + }) + } + return arr.TxEntry } -func GetTransaction(db *leveldb.DB, tx *types.Transaction) ShyftTxEntryPretty { - var prettyFormat ShyftTxEntryPretty - key := append([]byte("tx-")[:], tx.Hash().Bytes()[:]...) - data, err := db.Get(key, nil) - if err != nil { - log.Crit("Could not retrieve TX", "err", err) - } - if len(data) > 0 { - var txData ShyftTxEntry - d := gob.NewDecoder(bytes.NewBuffer(data)) - if err := d.Decode(&txData); err != nil { - log.Crit("Failed to decode tx:", "err", err) - } - prettyFormat = ShyftTxEntryPretty{ - TxHash: txData.TxHash.Hex(), - From: txData.From.Hex(), - To: txData.To.Hex(), - BlockHash: txData.BlockHash, - Amount: txData.Amount, - Gas: txData.Gas, - GasPrice: txData.GasPrice, - Nonce: txData.Nonce, - Data: txData.Data, - } - // Uncomment to view "pretty" version of data - //fmt.Println("DECODED TX") - //fmt.Println("Tx Hash: ", txData.TxHash.Hex()) - //fmt.Println("From: ", txData.From.Hex()) - //fmt.Println("To: ", txData.To.Hex()) - //fmt.Println("BlockHash: ", txData.BlockHash.Hex()) - //fmt.Println("Amount: ", txData.Amount) - //fmt.Println("Gas: ", txData.Gas) - //fmt.Println("GasPrice: ", txData.GasPrice) - //fmt.Println("Nonce: ", txData.Nonce) - //fmt.Println("Data: ", txData.Data) - } - return prettyFormat +//GetTransaction fn returns single tx +func GetTransaction(sqldb *sql.DB) []ShyftTxEntryPretty { + var arr txRes + sqlStatement := `SELECT * FROM txs WHERE nonce=$1;` + row := sqldb.QueryRow(sqlStatement, 7) + var txhash string + var to_addr string + var from_addr string + var blockhash string + var amount string + var gasprice string + var gas uint64 + var nonce uint64 + _ = row.Scan(&txhash, &to_addr, &from_addr, &blockhash, &amount, &gasprice, &gas, &nonce) + + // switch err { + // 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, + To: to_addr, + From: from_addr, + BlockHash: blockhash, + Amount: amount, + GasPrice: gasprice, + Gas: gas, + Nonce: nonce, + }) + + return arr.TxEntry }