Commit d2847d49 authored by duanjinfei's avatar duanjinfei

get sortBy API add param & add getWinnerSign API

parent 46184d5c
......@@ -11,6 +11,7 @@ goBackBlockNum = 5
lotteryContract = 0x847bF863DB75e25853DE42516bb4E0eFA715d9de
secret = 0x2Fc9Ba75f1c78ECc77D1a8fD3BDd6bA83eA68b7e
signAccPrv = 84ffa1fd6216691a968ad3ce78ca66a5d3d2c56772fcd78b6b8e2a7976487e0f
# redis config
[cache]
collectionName = redis
......
......@@ -3,12 +3,14 @@ package controllers
import (
"bufio"
"context"
"encoding/binary"
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
log "github.com/sirupsen/logrus"
"github.com/wuban/nft-event/models"
......@@ -77,19 +79,21 @@ func (c *LotteryController) GetUserList() {
}
func (c *LotteryController) GetHashSortByCondition() {
poolAddr := c.GetString("poolAddr")
period := c.GetString("period")
if period == "" {
if period == "" || poolAddr == "" || !utils.IsAddress(poolAddr) {
c.ResponseInfo(500, models.PARAM_ERR, nil)
}
contractAddr := beego.AppConfig.String("lotteryContract")
o := orm.NewOrm()
lotteries := make([]models.Lottery, 0)
_, err := o.QueryTable("lottery").Filter("contract_addr", contractAddr).Filter("period", period).OrderBy("-sort_by").All(&lotteries)
_, err := o.QueryTable("lottery").Filter("contract_addr", contractAddr).Filter("poolAddr", poolAddr).Filter("period", period).OrderBy("-sort_by").All(&lotteries)
if err != nil {
c.ResponseInfo(500, models.SORTBY_ERR, err.Error())
}
if len(lotteries) == 0 {
c.ResponseInfo(200, models.DATA_NIL, nil)
return
}
lotteryHashLastSix := &models.LotteryHashLastSix{
Hash: lotteries[0].Hash,
......@@ -177,3 +181,49 @@ func (c *LotteryController) GetPoolInfo() {
}
c.ResponseInfo(200, models.SUCCESS, res)
}
func (c *LotteryController) GetBeforePeriodWinnerAndSign() {
poolAddr := c.GetString("poolAddr")
period, err := c.GetInt64("period")
beforeNum, err := c.GetInt64("beforeNum")
if err != nil || poolAddr == "" {
c.ResponseInfo(500, models.PARAM_ERR, nil)
}
if beforeNum >= period {
c.ResponseInfo(500, models.PARAM_ERR, nil)
}
contractAddr := beego.AppConfig.String("lotteryContract")
signPrv, err := crypto.HexToECDSA(beego.AppConfig.String("signAccPrv"))
if err != nil {
c.ResponseInfo(200, models.FAILED, err.Error())
}
o := orm.NewOrm()
winnerListRes := make([]*models.WinnerListRes, 0)
for i := period - beforeNum; i < period; i++ {
lottery := &models.Lottery{}
o.QueryTable("lottery").Filter("contract_addr", contractAddr).Filter("period", i).OrderBy("-sort_by").One(lottery)
periodBytes := make([]byte, 8)
binary.BigEndian.PutUint64(periodBytes, uint64(lottery.Period))
winnerAddr := utils.HexToAddr(lottery.User)
hashData := []byte(lottery.Hash)
hash := crypto.Keccak256Hash(common.LeftPadBytes(periodBytes, 32), winnerAddr.Bytes(), hashData)
signature, err := crypto.Sign(hash.Bytes(), signPrv)
if err != nil {
c.ResponseInfo(500, models.FAILED, nil)
}
signature[64] += 27
winnerRes := &models.WinnerListRes{
PoolAddr: lottery.PoolAddr,
Period: lottery.Period,
Winner: lottery.User,
TxHash: lottery.Hash,
Sign: "0x" + common.Bytes2Hex(signature),
}
winnerListRes = append(winnerListRes, winnerRes)
}
if len(winnerListRes) > 0 {
c.ResponseInfo(200, models.SUCCESS, winnerListRes)
} else {
c.ResponseInfo(200, models.DATA_NIL, nil)
}
}
......@@ -34,3 +34,11 @@ type PoolInfoRes struct {
PoolInfo_ [][]*big.Int `abi:"poolInfo_" json:"poolInfo_"`
PoolHistory_ [][]*big.Int `abi:"poolHistory_" json:"poolHistory_"`
}
type WinnerListRes struct {
PoolAddr string `json:"poolAddr"`
Period int64 `json:"period"`
Winner string `json:"winner"`
TxHash string `json:"txHash"`
Sign string `json:"sign"`
}
......@@ -11,6 +11,7 @@ func init() {
beego.Router("/api/v1/event/get/sortCondition", &controllers.LotteryController{}, "get:GetHashSortByCondition")
beego.Router("/api/v1/event/get/param", &controllers.LotteryController{}, "get:ForwardReq")
beego.Router("/api/v1/event/get/poolInfo", &controllers.LotteryController{}, "get:GetPoolInfo")
beego.Router("/api/v1/event/get/winnerSign", &controllers.LotteryController{}, "get:GetBeforePeriodWinnerAndSign")
beego.Router("/getToken", &controllers.AuthController{}, "get:GetToken")
// 添加鉴权过滤器
//beego.InsertFilter("/*", beego.BeforeRouter, utils.AuthFilter)
......
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