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

update api

parent d7218749
......@@ -111,13 +111,19 @@ func getRecord(c *gin.Context) {
}
func getWithdrawProof(c *gin.Context) {
address := c.Query("address")
if !common.IsHexAddress(address) || len(address) != 42 {
tmp := struct {
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))
return
}
amount, proofs := validator.GetMerkleProof(common.HexToAddress(address), "")
amount, proofs := validator.GetMerkleProof(common.HexToAddress(tmp.Address), "")
c.JSON(200, withSuccess(gin.H{
"amount": amount,
......@@ -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{} {
return map[string]interface{}{
"code": 0,
......
package api
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
func initRouter(engine *gin.Engine) {
g := engine.Group("/api/v1")
g.POST("/workload", getWorkload)
......@@ -12,6 +12,12 @@ func initRouter(engine *gin.Engine) {
proof := g.Group("/proof")
{
proof.GET("/withdraw", getWithdrawProof)
proof.POST("/withdraw", getWithdrawProof)
}
tree := g.Group("/tree")
{
tree.POST("/merkleNodes", getMerkleNodes)
tree.POST("/merkleSumNodes", getMerkleSumNodes)
}
}
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