From 68f829f12a9c5c97e07191f4defaaa1246b0168e Mon Sep 17 00:00:00 2001 From: Dustin Brickwood Date: Mon, 13 Aug 2018 12:05:31 -0400 Subject: [PATCH] internal Tx endpoint successfully fetching internal tx data --- core/shyft_database_util.go | 2 +- core/shyft_get_utils.go | 60 ++++++++++++++--------- eth/shyft_tracer.go | 1 - shyft-cli/postgres_setup/drop_tables.psql | 3 +- shyftBlockExplorerApi/handler.go | 37 +++++++------- shyftBlockExplorerApi/routes.go | 26 +++++----- 6 files changed, 70 insertions(+), 59 deletions(-) diff --git a/core/shyft_database_util.go b/core/shyft_database_util.go index 41879fd4b2..26c2892d7b 100644 --- a/core/shyft_database_util.go +++ b/core/shyft_database_util.go @@ -372,7 +372,7 @@ func InsertTx(sqldb *sql.DB, txData stypes.ShyftTxEntryPretty) { //InsertInternalTx writes internal tx to Postgres Db func InsertInternalTx(sqldb *sql.DB, i stypes.InteralWrite) { var returnValue string - sqlStatement := `INSERT INTO internaltxs(type, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` + sqlStatement := `INSERT INTO internaltxs(action, txhash, from_addr, to_addr, amount, gas, gasUsed, time, input, output) VALUES(($1), ($2), ($3), ($4), ($5), ($6), ($7), ($8), ($9), ($10)) RETURNING txHash` qerr := sqldb.QueryRow(sqlStatement, i.Action, strings.ToLower(i.Hash), strings.ToLower(i.From), strings.ToLower(i.To), i.Value, i.Gas, i.GasUsed, i.Time, i.Input, i.Output).Scan(&returnValue) if qerr != nil { fmt.Println(qerr) diff --git a/core/shyft_get_utils.go b/core/shyft_get_utils.go index a698392c52..7304e45e65 100644 --- a/core/shyft_get_utils.go +++ b/core/shyft_get_utils.go @@ -429,31 +429,43 @@ func SGetAllInternalTransactions(sqldb *sql.DB) string { //GetInternalTransaction fn returns single tx func SGetInternalTransaction(sqldb *sql.DB, txHash string) string { + var arr stypes.InternalArray + var internaltx string + sqlStatement := `SELECT * FROM internaltxs WHERE txhash=$1;` - row := sqldb.QueryRow(sqlStatement, txHash) - var txhash, action, to_addr, from_addr, amount, input, output string - var id int - var gas, gasUsed uint64 - var age string - - row.Scan( - &id, &txhash, &action, &to_addr, &from_addr, &amount, &gas, &gasUsed, &age, &input, &output, - ) - - internaltx := stypes.InteralWrite{ - ID: id, - Hash: txhash, - Action: action, - To: to_addr, - From: from_addr, - Value: amount, - Gas: gas, - GasUsed: gasUsed, - Time: age, - Input: input, - Output: output, + rows, err := sqldb.Query(sqlStatement, txHash) + if err != nil { + fmt.Println("err") } - json, _ := json.Marshal(internaltx) + defer rows.Close() - return string(json) + for rows.Next() { + var txhash, action, to_addr, from_addr, amount, input, output string + var id int + var gas, gasUsed uint64 + var age string + + err = rows.Scan( + &id, &txhash, &action, &to_addr, &from_addr, &amount, &gas, &gasUsed, &age, &input, &output, + ) + + arr.InternalEntry = append(arr.InternalEntry, stypes.InteralWrite{ + ID: id, + Hash: txhash, + Action: action, + To: to_addr, + From: from_addr, + Value: amount, + Gas: gas, + GasUsed: gasUsed, + Time: age, + Input: input, + Output: output, + }) + + tx, _ := json.Marshal(arr.InternalEntry) + newtx := string(tx) + internaltx = newtx + } + return internaltx } diff --git a/eth/shyft_tracer.go b/eth/shyft_tracer.go index 083b21a06b..ba740d0028 100644 --- a/eth/shyft_tracer.go +++ b/eth/shyft_tracer.go @@ -2,7 +2,6 @@ package eth import ( "context" - "fmt" "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/params" diff --git a/shyft-cli/postgres_setup/drop_tables.psql b/shyft-cli/postgres_setup/drop_tables.psql index 368c809a42..85b27ccaf3 100644 --- a/shyft-cli/postgres_setup/drop_tables.psql +++ b/shyft-cli/postgres_setup/drop_tables.psql @@ -1,5 +1,4 @@ -\connect -shyftdb; +\connect shyftdb; DROP TABLE internalTxs; DROP TABLE txs; DROP TABLE blocks; diff --git a/shyftBlockExplorerApi/handler.go b/shyftBlockExplorerApi/handler.go index 055c0152ab..2a396d55c2 100644 --- a/shyftBlockExplorerApi/handler.go +++ b/shyftBlockExplorerApi/handler.go @@ -195,36 +195,37 @@ func GetRecentBlock(w http.ResponseWriter, r *http.Request) { } //GetInternalTransactions gets internal txs -func GetInternalTransactions(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - address := vars["address"] +func GetInternalTransactionsByHash(w http.ResponseWriter, r *http.Request) { + sqldb, err := core.DBConnection() - // mostRecentBlock := core.SGetRecentBlock(sqldb) - // if err != nil { - // http.Error(w, err.Error(), 500) - // return - // } + vars := mux.Vars(r) + txHash := vars["txHash"] + + internalTxs := core.SGetInternalTransaction(sqldb, txHash) + 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 InternalTransactions", address) + fmt.Fprintln(w, internalTxs) } //GetInternalTransactionsHash gets internal txs hash -func GetInternalTransactionsHash(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - transactionHash := vars["transaction_hash"] +func GetInternalTransactions(w http.ResponseWriter, r *http.Request) { + sqldb, err := core.DBConnection() - //mostRecentBlock := core.SGetRecentBlock(sqldb) - // if err != nil { - // http.Error(w, err.Error(), 500) - // return - // } + internalTxs := core.SGetAllInternalTransactions(sqldb) + 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 Internal Transaction Hash", transactionHash) + fmt.Fprintln(w, internalTxs) } func BroadcastTx(w http.ResponseWriter, r *http.Request) { diff --git a/shyftBlockExplorerApi/routes.go b/shyftBlockExplorerApi/routes.go index fd6517eb5e..4c23fabe65 100644 --- a/shyftBlockExplorerApi/routes.go +++ b/shyftBlockExplorerApi/routes.go @@ -58,34 +58,34 @@ var routes = Routes{ GetTransaction, }, Route{ - Name: "GetRecentBlock", - Method: "GET", - Pattern: "/api/get_recent_block", + Name: "GetRecentBlock", + Method: "GET", + Pattern: "/api/get_recent_block", HandlerFunc: GetRecentBlock, }, Route{ - Name: "GetAllTransactionsFromBlock", - Method: "GET", - Pattern: "/api/get_all_transactions_from_block/{blockNumber}", + Name: "GetAllTransactionsFromBlock", + Method: "GET", + Pattern: "/api/get_all_transactions_from_block/{blockNumber}", HandlerFunc: GetAllTransactionsFromBlock, }, Route{ - Name: "GetAllBlocksMinedByAddress", - Method: "GET", - Pattern: "/api/get_blocks_mined/{coinbase}", + Name: "GetAllBlocksMinedByAddress", + Method: "GET", + Pattern: "/api/get_blocks_mined/{coinbase}", HandlerFunc: GetAllBlocksMinedByAddress, }, Route{ "GetInternalTransactions", "GET", - "/api/get_internal_transactions/{address}", + "/api/get_internal_transactions/", GetInternalTransactions, }, Route{ - "GetInternalTransactionsHash", + "GetInternalTransactionsByHash", "GET", - "/api/get_internal_transactions_hash/{transactions_hash}", - GetInternalTransactionsHash, + "/api/get_internal_transactions/{txHash}", + GetInternalTransactionsByHash, }, Route{ "BroadcastTx",