Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 2 function to gas #81

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 133 additions & 1 deletion blockchain/service/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (h *HttpHandler) TokenUri(ctx *gin.Context) {
contract := root.Get("contract").String()
tokenId := root.Get("tokenId").String()
eip := root.Get("eip").Int()
if _, ok := h.nftClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.nftClients[blockChainCode].TokenURI(blockChainCode, contract, tokenId, eip)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
Expand All @@ -84,6 +88,10 @@ func (h *HttpHandler) BalanceOf(ctx *gin.Context) {
addr := root.Get("address").String()
tokenId := root.Get("tokenId").String()
eip := root.Get("eip").Int()
if _, ok := h.nftClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.nftClients[blockChainCode].BalanceOf(blockChainCode, contract, addr, tokenId, eip)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
Expand All @@ -104,6 +112,10 @@ func (h *HttpHandler) OwnerOf(ctx *gin.Context) {
contract := root.Get("contract").String()
tokenId := root.Get("tokenId").String()
eip := root.Get("eip").Int()
if _, ok := h.nftClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.nftClients[blockChainCode].OwnerOf(blockChainCode, contract, tokenId, eip)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
Expand All @@ -123,6 +135,10 @@ func (h *HttpHandler) TotalSupply(ctx *gin.Context) {
blockChainCode := root.Get("chain").Int()
contract := root.Get("contract").String()
eip := root.Get("eip").Int()
if _, ok := h.nftClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.nftClients[blockChainCode].TotalSupply(blockChainCode, contract, eip)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
Expand All @@ -139,6 +155,12 @@ func (h *HttpHandler) GetBlockByHash(ctx *gin.Context) {
}
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
hash := gjson.ParseBytes(b).Get("hash").String()

if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}

res, err := h.blockChainClients[blockChainCode].GetBlockByHash(blockChainCode, hash, true)

if err != nil {
Expand All @@ -157,6 +179,10 @@ func (h *HttpHandler) GetBlockByNumber(ctx *gin.Context) {
}
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
number := gjson.ParseBytes(b).Get("number").String()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].GetBlockByNumber(blockChainCode, number, true)

if err != nil {
Expand All @@ -175,6 +201,10 @@ func (h *HttpHandler) GetTxByHash(ctx *gin.Context) {
}
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
hash := gjson.ParseBytes(b).Get("hash").String()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].GetTxByHash(blockChainCode, hash)

if err != nil {
Expand All @@ -193,6 +223,10 @@ func (h *HttpHandler) GetTxReceiptByHash(ctx *gin.Context) {
}
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
hash := gjson.ParseBytes(b).Get("hash").String()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].GetTransactionReceiptByHash(blockChainCode, hash)

if err != nil {
Expand All @@ -212,6 +246,10 @@ func (h *HttpHandler) GetBalance(ctx *gin.Context) {
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
addr := gjson.ParseBytes(b).Get("address").String()
tag := gjson.ParseBytes(b).Get("tag").String()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].Balance(blockChainCode, addr, tag)

if err != nil {
Expand All @@ -235,6 +273,10 @@ func (h *HttpHandler) GetTokenBalance(ctx *gin.Context) {
codeHash := r.Get("contract").String()
abi := r.Get("abi").String()

if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].TokenBalance(blockChainCode, codeHash, addr, abi)
if err != nil {
h.Error(ctx, r.String(), ctx.Request.RequestURI, err.Error())
Expand All @@ -254,6 +296,10 @@ func (h *HttpHandler) GetNonce(ctx *gin.Context) {
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
addr := gjson.ParseBytes(b).Get("address").String()
tag := gjson.ParseBytes(b).Get("tag").String() //pending,latest
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].Nonce(blockChainCode, addr, tag)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
Expand All @@ -270,6 +316,10 @@ func (h *HttpHandler) GetLatestBlock(ctx *gin.Context) {
return
}
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].LatestBlock(blockChainCode)
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
Expand All @@ -279,6 +329,81 @@ func (h *HttpHandler) GetLatestBlock(ctx *gin.Context) {
h.Success(ctx, "", res, ctx.Request.RequestURI)
}

// GasPrice Returns the current price per gas in wei.
func (h *HttpHandler) GasPrice(ctx *gin.Context) {
req := `
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_gasPrice"
}
`

b, err := io.ReadAll(ctx.Request.Body)
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
return
}

blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].SendJsonRpc(blockChainCode, req)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
return
}

h.Success(ctx, string(b), res, ctx.Request.RequestURI)
}

// EstimateGas Generates and returns an estimate of how much gas is necessary to allow the transaction to complete. The transaction will not be added to the blockchain.
func (h *HttpHandler) EstimateGas(ctx *gin.Context) {
req := `
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_estimateGas",
"params": [
{
"from":"%v",
"to": "%v",
"data": "%v"
}
]
}`

b, err := io.ReadAll(ctx.Request.Body)
if err != nil {
h.Error(ctx, "", ctx.Request.RequestURI, err.Error())
return
}
root := gjson.ParseBytes(b)
blockChainCode := root.Get("chain").Int()
from := root.Get("from").String()
to := root.Get("to").String()
data := root.Get("data").String()
if len(data) < 1 {
data = "0x"
}

if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}

req = fmt.Sprintf(req, from, to, data)
res, err := h.blockChainClients[blockChainCode].SendJsonRpc(blockChainCode, req)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
return
}

h.Success(ctx, string(b), res, ctx.Request.RequestURI)
}

func (h *HttpHandler) SendRawTx(ctx *gin.Context) {
b, err := io.ReadAll(ctx.Request.Body)
if err != nil {
Expand All @@ -301,7 +426,7 @@ func (h *HttpHandler) SendRawTx(ctx *gin.Context) {
backup["extra"] = extra

if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}

Expand Down Expand Up @@ -331,6 +456,10 @@ func (h *HttpHandler) HandlerReq(ctx *gin.Context) {
}
blockChainCode := gjson.ParseBytes(b).Get("chain").Int()
data := gjson.ParseBytes(b).Get("data").String()
if _, ok := h.blockChainClients[blockChainCode]; !ok {
h.Error(ctx, string(b), ctx.Request.RequestURI, fmt.Sprintf("blockchain:%v is not supported", blockChainCode))
return
}
res, err := h.blockChainClients[blockChainCode].SendJsonRpc(blockChainCode, data)
if err != nil {
h.Error(ctx, string(b), ctx.Request.RequestURI, err.Error())
Expand All @@ -348,6 +477,9 @@ const (
func (h *HttpHandler) Success(c *gin.Context, req string, resp interface{}, path string) {
req = strings.Replace(req, "\t", "", -1)
req = strings.Replace(req, "\n", "", -1)
if v, ok := resp.(string); ok {
resp = strings.Replace(v, "\n", "", -1)
}
h.log.Printf("path=%v,req=%v,resp=%v\n", path, req, resp)
mp := make(map[string]interface{})
mp["code"] = SUCCESS
Expand Down
24 changes: 24 additions & 0 deletions cmd/blockchain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,28 @@ curl -X POST \
"eip":721
}'

// query GasPrice
curl -X POST \
http://127.0.0.1:9002/api/chain/gas/price \
-H 'Content-Type: application/json' \
-H 'Postman-Token: e65c8170-65f9-482f-92d6-49ed01194444' \
-H 'cache-control: no-cache' \
-d '{
"chain": 200
}'

//query EstimateGas
curl -X POST \
http://127.0.0.1:9002/api/chain/gas/estimateGas \
-H 'Content-Type: application/json' \
-H 'Postman-Token: 9a8fb36b-0ed3-47c5-b82f-eecd8631c2be' \
-H 'cache-control: no-cache' \
-d '{
"chain": 202,
"from":"0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567",
"data":"0x"
}'


``````
2 changes: 2 additions & 0 deletions cmd/blockchain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func main() {
root.POST("/account/nonce", srv.GetNonce)
root.POST("/block/latest", srv.GetLatestBlock)
root.POST("/tx/sendRawTransaction", srv.SendRawTx)
root.POST("/gas/price", srv.GasPrice)
root.POST("/gas/estimateGas", srv.EstimateGas)
root.POST("/nft/tokenUri", srv.TokenUri)
root.POST("/nft/balanceOf", srv.BalanceOf)
root.POST("/nft/owner", srv.OwnerOf)
Expand Down
2 changes: 2 additions & 0 deletions cmd/easynode/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func startBlockchain(configPath string, ctx context.Context) {
root.POST("/account/nonce", srv.GetNonce)
root.POST("/block/latest", srv.GetLatestBlock)
root.POST("/tx/sendRawTransaction", srv.SendRawTx)
root.POST("/gas/price", srv.GasPrice)
root.POST("/gas/estimateGas", srv.EstimateGas)
root.POST("/nft/tokenUri", srv.TokenUri)
root.POST("/nft/balanceOf", srv.BalanceOf)
root.POST("/nft/owner", srv.OwnerOf)
Expand Down