Skip to content

Commit

Permalink
Merge pull request #5 from Alethio/lite-explorer-integration
Browse files Browse the repository at this point in the history
Lite explorer integration
  • Loading branch information
kwix authored Nov 8, 2019
2 parents 38a0ff8 + 0d5742b commit 281147c
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 2 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
Port string
DevCorsEnabled bool
DevCorsHost string
EthClientURL string
}

type API struct {
Expand Down
58 changes: 58 additions & 0 deletions api/handlers_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package api

import (
"encoding/hex"
"fmt"

"github.com/Alethio/memento/utils"
"github.com/alethio/web3-go/ethrpc"
"github.com/gin-gonic/gin"
)

func (a *API) AccountCodeHandler(c *gin.Context) {
address := utils.CleanUpHex(c.Param("address"))
if len(address) != 40 {
BadRequest(c, fmt.Errorf("bad request: address is malformed"))
return
}

eth, err := ethrpc.NewWithDefaults(a.config.EthClientURL)
if err != nil {
Error(c, err)
return
}

code, err := eth.GetCode(fmt.Sprintf("0x%s", address))
if err != nil {
Error(c, err)
return
}

OK(c, map[string]interface{}{
"code": hex.EncodeToString(code),
})
}

func (a *API) AccountBalanceHandler(c *gin.Context) {
address := utils.CleanUpHex(c.Param("address"))
if len(address) != 40 {
BadRequest(c, fmt.Errorf("bad request: address is malformed"))
return
}

eth, err := ethrpc.NewWithDefaults(a.config.EthClientURL)
if err != nil {
Error(c, err)
return
}

balance, err := eth.GetBalanceAtBlock(fmt.Sprintf("0x%s", address), fmt.Sprintf("0x%x", a.core.Metrics().GetLatestBLock()))
if err != nil {
Error(c, err)
return
}

OK(c, map[string]interface{}{
"balance": balance.String(),
})
}
63 changes: 63 additions & 0 deletions api/handlers_search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package api

import (
"database/sql"
"fmt"

"github.com/Alethio/memento/utils"
"github.com/gin-gonic/gin"
)

func (a *API) SearchHandler(c *gin.Context) {
query := utils.CleanUpHex(c.Param("query"))

if len(query) != 64 {
BadRequest(c, fmt.Errorf("invalid request: invalid query string"))
return
}

var count int
err := a.core.DB().QueryRow(`select count(*) from txs where tx_hash = $1`, query).Scan(&count)
if err != nil {
Error(c, err)
return
}
if count > 0 {
OK(c, map[string]interface{}{
"entity": "tx",
"data": nil,
})
return
}

var number int64
err = a.core.DB().QueryRow(`select number from blocks where block_hash = $1`, query).Scan(&number)
if err != nil && err != sql.ErrNoRows {
Error(c, err)
return
}
if err != sql.ErrNoRows {
OK(c, map[string]interface{}{
"entity": "block",
"data": map[string]interface{}{
"number": number,
},
})
return
}

err = a.core.DB().QueryRow(`select count(*) from uncles where block_hash = $1`, query).Scan(&count)
if err != nil {
Error(c, err)
return
}
if count > 0 {
OK(c, map[string]interface{}{
"entity": "uncle",
"data": nil,
})
return
}

OK(c, map[string]interface{}{})
}
16 changes: 14 additions & 2 deletions api/handlers_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (a *API) TxDetailsHandler(c *gin.Context) {
return
}

OK(c, types.Tx{
tx := types.Tx{
TxHash: &txHash,
IncludedInBlock: &includedInBlock,
TxIndex: &txIndex,
Expand All @@ -62,7 +62,19 @@ func (a *API) TxDetailsHandler(c *gin.Context) {
TxLogsBloom: &txLogsBloom,
BlockCreationTime: &blockCreationTime,
LogEntriesTriggered: &logEntriesTriggered,
})
}

var msgError bool
var msgErrorString string
if tx.MsgStatus != nil && *tx.MsgStatus == "0x0" {
msgError = true
msgErrorString = "Error"
}

tx.MsgError = &msgError
tx.MsgErrorString = &msgErrorString

OK(c, tx)
}

func (a *API) TxLogEntriesHandler(c *gin.Context) {
Expand Down
4 changes: 4 additions & 0 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ func (a *API) setRoutes() {
explorer.GET("/uncle/:hash", a.UncleDetailsHandler)
explorer.GET("/tx/:txHash", a.TxDetailsHandler)
explorer.GET("/tx/:txHash/log-entries", a.TxLogEntriesHandler)
explorer.GET("/search/:query", a.SearchHandler)

explorer.GET("/account/:address/txs", a.AccountTxsHandler)
explorer.GET("/account/:address/code", a.AccountCodeHandler)
explorer.GET("/account/:address/balance", a.AccountBalanceHandler)
}
2 changes: 2 additions & 0 deletions api/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Tx struct {
CumulativeGasUsed *string `json:"cumulativeGasUsed,omitempty"`
MsgPayload *storable.ByteArray `json:"msgPayload,omitempty"`
MsgStatus *string `json:"msgStatus,omitempty"`
MsgError *bool `json:"msgError,omitempty"`
MsgErrorString *string `json:"msgErrorString,omitempty"`
Creates *storable.ByteArray `json:"creates,omitempty"`
TxLogsBloom *storable.ByteArray `json:"txLogsBloom,omitempty"`
BlockCreationTime *storable.DatetimeToJSONUnix `json:"blockCreationTime,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var runCmd = &cobra.Command{
Port: viper.GetString("api.port"),
DevCorsEnabled: viper.GetBool("api.dev-cors"),
DevCorsHost: viper.GetString("api.dev-cors-host"),
EthClientURL: viper.GetString("eth.client.http"),
})
go a.Run()

Expand Down

0 comments on commit 281147c

Please sign in to comment.