github.com/shyftnetwork/go-empyrean@v1.8.3-0.20191127201940-fbfca9338f04/shyftBlockExplorerApi/handler.go (about) 1 package main 2 3 ///@NOTE Shyft handler functions when endpoints are hit 4 import ( 5 "fmt" 6 "net/http" 7 8 _ "github.com/lib/pq" 9 10 "bytes" 11 "encoding/json" 12 "io/ioutil" 13 14 "github.com/ShyftNetwork/go-empyrean/ethdb" 15 "github.com/gorilla/mux" 16 ) 17 18 // GetTransaction gets txs 19 func GetTransaction(w http.ResponseWriter, r *http.Request) { 20 vars := mux.Vars(r) 21 txHash := vars["txHash"] 22 getTxResponse, err := ethdb.SGetTransaction(txHash) 23 if err != nil { 24 http.Error(w, err.Error(), 500) 25 return 26 } 27 28 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 29 w.WriteHeader(http.StatusOK) 30 31 fmt.Fprintln(w, getTxResponse) 32 } 33 34 // GetAllTransactions gets txs 35 func GetAllTransactions(w http.ResponseWriter, r *http.Request) { 36 txs, err := ethdb.SGetAllTransactions() 37 38 if err != nil { 39 http.Error(w, err.Error(), 500) 40 return 41 } 42 43 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 44 w.WriteHeader(http.StatusOK) 45 46 fmt.Fprintln(w, txs) 47 } 48 49 // GetAllTransactions gets txs 50 func GetAllTransactionsFromBlock(w http.ResponseWriter, r *http.Request) { 51 vars := mux.Vars(r) 52 blockNumber := vars["blockNumber"] 53 txsFromBlock, err := ethdb.SGetAllTransactionsFromBlock(blockNumber) 54 55 if err != nil { 56 http.Error(w, err.Error(), 500) 57 return 58 } 59 60 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 61 w.WriteHeader(http.StatusOK) 62 63 fmt.Fprintln(w, txsFromBlock) 64 } 65 66 func GetAllBlocksMinedByAddress(w http.ResponseWriter, r *http.Request) { 67 vars := mux.Vars(r) 68 coinbase := vars["coinbase"] 69 70 blocksMined, err := ethdb.SGetAllBlocksMinedByAddress(coinbase) 71 72 if err != nil { 73 http.Error(w, err.Error(), 500) 74 return 75 } 76 77 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 78 w.WriteHeader(http.StatusOK) 79 80 fmt.Fprintln(w, blocksMined) 81 } 82 83 // GetAccount gets balance 84 func GetAccount(w http.ResponseWriter, r *http.Request) { 85 vars := mux.Vars(r) 86 address := vars["address"] 87 88 getAccountBalance, err := ethdb.SGetAccount(address) 89 90 if err != nil { 91 http.Error(w, err.Error(), 500) 92 return 93 } 94 95 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 96 w.WriteHeader(http.StatusOK) 97 98 fmt.Fprintln(w, getAccountBalance) 99 } 100 101 // GetAccount gets balance 102 func GetAccountTxs(w http.ResponseWriter, r *http.Request) { 103 vars := mux.Vars(r) 104 address := vars["address"] 105 106 getAccountTxs, err := ethdb.SGetAccountTxs(address) 107 108 if err != nil { 109 http.Error(w, err.Error(), 500) 110 return 111 } 112 113 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 114 w.WriteHeader(http.StatusOK) 115 116 fmt.Fprintln(w, getAccountTxs) 117 } 118 119 // GetAllAccounts gets balances 120 func GetAllAccounts(w http.ResponseWriter, r *http.Request) { 121 allAccounts, err := ethdb.SGetAllAccounts() 122 if err != nil { 123 http.Error(w, err.Error(), 500) 124 return 125 } 126 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 127 w.WriteHeader(http.StatusOK) 128 129 fmt.Fprintln(w, allAccounts) 130 } 131 132 //GetBlock returns block json 133 func GetBlock(w http.ResponseWriter, r *http.Request) { 134 vars := mux.Vars(r) 135 blockNumber := vars["blockNumber"] 136 137 getBlockResponse, err := ethdb.SGetBlock(blockNumber) 138 139 if err != nil { 140 http.Error(w, err.Error(), 500) 141 return 142 } 143 144 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 145 w.WriteHeader(http.StatusOK) 146 147 fmt.Fprintln(w, getBlockResponse) 148 } 149 150 // GetAllBlocks response 151 func GetAllBlocks(w http.ResponseWriter, r *http.Request) { 152 blocks, err := ethdb.SGetAllBlocks() 153 if err != nil { 154 http.Error(w, err.Error(), 500) 155 return 156 } 157 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 158 w.WriteHeader(http.StatusOK) 159 fmt.Fprintln(w, blocks) 160 } 161 162 func GetRecentBlock(w http.ResponseWriter, r *http.Request) { 163 mostRecentBlock, err := ethdb.SGetRecentBlock() 164 if err != nil { 165 http.Error(w, err.Error(), 500) 166 return 167 } 168 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 169 w.WriteHeader(http.StatusOK) 170 fmt.Fprintln(w, mostRecentBlock) 171 172 } 173 174 //GetInternalTransactions gets internal txs 175 func GetInternalTransactionsByHash(w http.ResponseWriter, r *http.Request) { 176 vars := mux.Vars(r) 177 txHash := vars["txHash"] 178 179 internalTxs, err := ethdb.SGetInternalTransaction(txHash) 180 if err != nil { 181 http.Error(w, err.Error(), 500) 182 return 183 } 184 185 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 186 w.WriteHeader(http.StatusOK) 187 188 fmt.Fprintln(w, internalTxs) 189 } 190 191 //GetInternalTransactionsHash gets internal txs hash 192 func GetInternalTransactions(w http.ResponseWriter, r *http.Request) { 193 internalTxs, err := ethdb.SGetAllInternalTransactions() 194 if err != nil { 195 http.Error(w, err.Error(), 500) 196 return 197 } 198 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 199 w.WriteHeader(http.StatusOK) 200 201 fmt.Fprintln(w, internalTxs) 202 } 203 204 func BroadcastTx(w http.ResponseWriter, r *http.Request) { 205 // Example return result (returns tx hash): 206 // {"jsonrpc":"2.0","id":1,"result":"0xafa4c62f29dbf16bbfac4eea7cbd001a9aa95c59974043a17f863172f8208029"} 207 208 // http params 209 vars := mux.Vars(r) 210 transactionHash := vars["transaction_hash"] 211 212 // format the transactionHash into a proper sendRawTransaction jsonrpc request 213 formatted_json := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["%s"],"id":0}`, transactionHash)) 214 215 // send json rpc request 216 resp, _ := http.Post("http://localhost:8545", "application/json", bytes.NewBuffer(formatted_json)) 217 body, _ := ioutil.ReadAll(resp.Body) 218 byt := []byte(string(body)) 219 220 // read json and return result as http response, be it an error or tx hash 221 var dat map[string]interface{} 222 if err := json.Unmarshal(byt, &dat); err != nil { 223 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 224 w.WriteHeader(http.StatusOK) 225 fmt.Fprintln(w, "ERROR parsing json") 226 } 227 tx_hash := dat["result"] 228 if tx_hash == nil { 229 errMap := dat["error"].(map[string]interface{}) 230 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 231 w.WriteHeader(http.StatusOK) 232 fmt.Fprintln(w, "ERROR:", errMap["message"]) 233 } else { 234 w.Header().Set("Content-Type", "application/json; charset=UTF-8") 235 w.WriteHeader(http.StatusOK) 236 fmt.Fprintln(w, "Transaction Hash:", tx_hash) 237 } 238 }