-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Alethio/lite-explorer-integration
Lite explorer integration
- Loading branch information
Showing
7 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters