Commit f3508f06 authored by 贾浩@五瓣科技's avatar 贾浩@五瓣科技

update api

parent d7218749
...@@ -111,13 +111,19 @@ func getRecord(c *gin.Context) { ...@@ -111,13 +111,19 @@ func getRecord(c *gin.Context) {
} }
func getWithdrawProof(c *gin.Context) { func getWithdrawProof(c *gin.Context) {
address := c.Query("address") tmp := struct {
if !common.IsHexAddress(address) || len(address) != 42 { Address string `json:"address"`
}{}
if err := c.ShouldBindJSON(&tmp); err != nil {
c.JSON(200, withError(InvalidParams))
return
}
if !common.IsHexAddress(tmp.Address) || len(tmp.Address) != 42 {
c.JSON(200, withError(InvalidParams)) c.JSON(200, withError(InvalidParams))
return return
} }
amount, proofs := validator.GetMerkleProof(common.HexToAddress(address), "") amount, proofs := validator.GetMerkleProof(common.HexToAddress(tmp.Address), "")
c.JSON(200, withSuccess(gin.H{ c.JSON(200, withSuccess(gin.H{
"amount": amount, "amount": amount,
...@@ -125,6 +131,59 @@ func getWithdrawProof(c *gin.Context) { ...@@ -125,6 +131,59 @@ func getWithdrawProof(c *gin.Context) {
})) }))
} }
func getMerkleNodes(c *gin.Context) {
tmp := struct {
Date string `json:"date"`
Depth uint `json:"depth"`
RootHash string `json:"rootHash"`
}{}
if err := c.ShouldBindJSON(&tmp); err != nil {
c.JSON(200, withError(InvalidParams))
return
}
_, err := time.Parse(time.DateOnly, tmp.Date)
if err != nil {
c.JSON(200, withError(InvalidParams))
return
}
if tmp.Depth == 0 {
tmp.Depth = 3
}
nodes := validator.GetDailyMerkleNodes(tmp.Date, int(tmp.Depth), common.HexToHash(tmp.RootHash))
c.JSON(200, withSuccess(nodes))
}
func getMerkleSumNodes(c *gin.Context) {
tmp := struct {
Date string `json:"date"`
Depth uint `json:"depth"`
RootHash string `json:"rootHash"`
}{}
if err := c.ShouldBindJSON(&tmp); err != nil {
c.JSON(200, withError(InvalidParams))
return
}
_, err := time.Parse(time.DateOnly, tmp.Date)
if err != nil {
c.JSON(200, withError(InvalidParams))
return
}
if tmp.Depth == 0 {
tmp.Depth = 3
}
nodes, vals := validator.GetDailyMerkleSumNodes(tmp.Date, int(tmp.Depth), common.HexToHash(tmp.RootHash))
c.JSON(200, withSuccess(gin.H{
"nodes": nodes,
"vals": vals,
}))
}
func withSuccess(data interface{}) interface{} { func withSuccess(data interface{}) interface{} {
return map[string]interface{}{ return map[string]interface{}{
"code": 0, "code": 0,
......
package api package api
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func initRouter(engine *gin.Engine) { func initRouter(engine *gin.Engine) {
g := engine.Group("/api/v1") g := engine.Group("/api/v1")
g.POST("/workload", getWorkload) g.POST("/workload", getWorkload)
g.POST("/reward", getReward) g.POST("/reward", getReward)
g.POST("/record", getRecord) g.POST("/record", getRecord)
proof := g.Group("/proof") proof := g.Group("/proof")
{ {
proof.GET("/withdraw", getWithdrawProof) proof.POST("/withdraw", getWithdrawProof)
}
tree := g.Group("/tree")
{
tree.POST("/merkleNodes", getMerkleNodes)
tree.POST("/merkleSumNodes", getMerkleSumNodes)
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment