Commit 46184d5c authored by duanjinfei's avatar duanjinfei

add getPoolInfo Api

parent 7318d20a
package cache package cache
import ( import (
"fmt"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
red "github.com/wuban/nft-event/cache/redis" red "github.com/wuban/nft-event/cache/redis"
"time" "time"
) )
...@@ -24,7 +24,7 @@ func init() { ...@@ -24,7 +24,7 @@ func init() {
} }
func NewPool(conn, dbNum, password string) error { func NewPool(conn, dbNum, password string) error {
fmt.Println("redis连接池里的连接为空,重新创建连接池,starting...") log.Info("redis连接池里的连接为空,重新创建连接池,starting...")
pool = &redis.Pool{ pool = &redis.Pool{
MaxIdle: 50, //最大空闲连接数 MaxIdle: 50, //最大空闲连接数
MaxActive: 0, //若为0,则活跃数没有限制 MaxActive: 0, //若为0,则活跃数没有限制
......
package redis package redis
import ( import (
"fmt"
"github.com/garyburd/redigo/redis" "github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
"reflect" "reflect"
"strings" "strings"
"unsafe" "unsafe"
...@@ -18,17 +18,19 @@ func NewStoreRedis(pool *redis.Pool) IRedis { ...@@ -18,17 +18,19 @@ func NewStoreRedis(pool *redis.Pool) IRedis {
return sr return sr
} }
/** /*
*
此方法没有使用 此方法没有使用
*/ */
func (s *StoreRedis) errorLogic(e error) { func (s *StoreRedis) errorLogic(e error) {
i := strings.Index(e.Error(), "connect: connection refused") i := strings.Index(e.Error(), "connect: connection refused")
if i == -1 { if i == -1 {
fmt.Println(i, "不包含") log.Error(i, "不包含")
} }
} }
/** /*
*
设置key,value数据 设置key,value数据
*/ */
func (s *StoreRedis) Set(key, value string) error { func (s *StoreRedis) Set(key, value string) error {
...@@ -40,7 +42,8 @@ func (s *StoreRedis) Set(key, value string) error { ...@@ -40,7 +42,8 @@ func (s *StoreRedis) Set(key, value string) error {
return nil return nil
} }
/** /*
*
设置key,value数据 设置key,value数据
*/ */
func (s *StoreRedis) SetBytes(key string, value []byte) error { func (s *StoreRedis) SetBytes(key string, value []byte) error {
...@@ -52,7 +55,8 @@ func (s *StoreRedis) SetBytes(key string, value []byte) error { ...@@ -52,7 +55,8 @@ func (s *StoreRedis) SetBytes(key string, value []byte) error {
return nil return nil
} }
/** /*
*
设置key的过期时间 设置key的过期时间
*/ */
func (s *StoreRedis) SetKvAndExp(key, value string, expire int) error { func (s *StoreRedis) SetKvAndExp(key, value string, expire int) error {
...@@ -73,27 +77,28 @@ func (s *StoreRedis) SetKvInt(key string, value int) error { ...@@ -73,27 +77,28 @@ func (s *StoreRedis) SetKvInt(key string, value int) error {
return nil return nil
} }
/** /*
*
根据key获取对应数据 根据key获取对应数据
*/ */
func (s *StoreRedis) Get(key string) string { func (s *StoreRedis) Get(key string) string {
value, err := redis.String(s.pool.Get().Do("GET", key)) value, err := redis.String(s.pool.Get().Do("GET", key))
defer s.Close() defer s.Close()
if err != nil { if err != nil {
fmt.Println("redis get failed:", err) log.Error("redis get failed:", err)
} }
return value return value
} }
/** /*
*
根据key获取对应数据 根据key获取对应数据
*/ */
func (s *StoreRedis) GetBytes(key string) []byte { func (s *StoreRedis) GetBytes(key string) []byte {
value, err := s.pool.Get().Do("GET", key) value, err := s.pool.Get().Do("GET", key)
defer s.Close() defer s.Close()
if err != nil { if err != nil {
fmt.Println("redis get failed:", err) log.Error("redis get failed:", err)
} }
if value == nil { if value == nil {
...@@ -103,7 +108,8 @@ func (s *StoreRedis) GetBytes(key string) []byte { ...@@ -103,7 +108,8 @@ func (s *StoreRedis) GetBytes(key string) []byte {
return value.([]byte) return value.([]byte)
} }
/** /*
*
判断key是否存在 判断key是否存在
*/ */
func (s *StoreRedis) IsKeyExists(key string) bool { func (s *StoreRedis) IsKeyExists(key string) bool {
...@@ -112,31 +118,34 @@ func (s *StoreRedis) IsKeyExists(key string) bool { ...@@ -112,31 +118,34 @@ func (s *StoreRedis) IsKeyExists(key string) bool {
return is_key_exit return is_key_exit
} }
/** /*
*
删除key 删除key
*/ */
func (s *StoreRedis) Del(key string) bool { func (s *StoreRedis) Del(key string) bool {
is_key_delete, err := redis.Bool(s.pool.Get().Do("DEL", key)) is_key_delete, err := redis.Bool(s.pool.Get().Do("DEL", key))
defer s.Close() defer s.Close()
if err != nil { if err != nil {
fmt.Println("error:", err) log.Error("error:", err)
} }
return is_key_delete return is_key_delete
} }
/** /*
*
删除key 删除key
*/ */
func (s *StoreRedis) HDel(key string, field string) bool { func (s *StoreRedis) HDel(key string, field string) bool {
is_key_delete, err := redis.Bool(s.pool.Get().Do("HDEL", key, field)) is_key_delete, err := redis.Bool(s.pool.Get().Do("HDEL", key, field))
defer s.Close() defer s.Close()
if err != nil { if err != nil {
fmt.Println("error:", err) log.Error("error:", err)
} }
return is_key_delete return is_key_delete
} }
/** /*
*
对象转换成json后进行存储 对象转换成json后进行存储
*/ */
func (s *StoreRedis) Setnx(key string, value []byte) error { func (s *StoreRedis) Setnx(key string, value []byte) error {
...@@ -205,7 +214,8 @@ func (s *StoreRedis) Close() { ...@@ -205,7 +214,8 @@ func (s *StoreRedis) Close() {
s.pool.Get().Close() s.pool.Get().Close()
} }
/** /*
*
Hincryby方法 Hincryby方法
*/ */
func (s *StoreRedis) HINCRBY(key, field string) { func (s *StoreRedis) HINCRBY(key, field string) {
...@@ -213,7 +223,8 @@ func (s *StoreRedis) HINCRBY(key, field string) { ...@@ -213,7 +223,8 @@ func (s *StoreRedis) HINCRBY(key, field string) {
defer s.Close() defer s.Close()
} }
/** /*
*
HGet方法 HGet方法
*/ */
func (s *StoreRedis) HGet(key, field string) (interface{}, error) { func (s *StoreRedis) HGet(key, field string) (interface{}, error) {
...@@ -222,7 +233,8 @@ func (s *StoreRedis) HGet(key, field string) (interface{}, error) { ...@@ -222,7 +233,8 @@ func (s *StoreRedis) HGet(key, field string) (interface{}, error) {
return value, err return value, err
} }
/** /*
*
HGetAll方法 HGetAll方法
*/ */
func (s *StoreRedis) HGetAll(key string) ([][]byte, error) { func (s *StoreRedis) HGetAll(key string) ([][]byte, error) {
...@@ -231,7 +243,8 @@ func (s *StoreRedis) HGetAll(key string) ([][]byte, error) { ...@@ -231,7 +243,8 @@ func (s *StoreRedis) HGetAll(key string) ([][]byte, error) {
return inter, err return inter, err
} }
/** /*
*
Hset方法 Hset方法
*/ */
func (s *StoreRedis) HSet(key string, field string, value string) (interface{}, error) { func (s *StoreRedis) HSet(key string, field string, value string) (interface{}, error) {
...@@ -240,7 +253,8 @@ func (s *StoreRedis) HSet(key string, field string, value string) (interface{}, ...@@ -240,7 +253,8 @@ func (s *StoreRedis) HSet(key string, field string, value string) (interface{},
return inter, err return inter, err
} }
/** /*
*
Hset方法 Hset方法
*/ */
func (s *StoreRedis) HSetByte(key string, field string, value []byte) (interface{}, error) { func (s *StoreRedis) HSetByte(key string, field string, value []byte) (interface{}, error) {
...@@ -249,7 +263,8 @@ func (s *StoreRedis) HSetByte(key string, field string, value []byte) (interface ...@@ -249,7 +263,8 @@ func (s *StoreRedis) HSetByte(key string, field string, value []byte) (interface
return inter, err return inter, err
} }
/** /*
*
SADD方法 SADD方法
*/ */
func (s *StoreRedis) SAdd(args []interface{}) (interface{}, error) { func (s *StoreRedis) SAdd(args []interface{}) (interface{}, error) {
...@@ -258,7 +273,8 @@ func (s *StoreRedis) SAdd(args []interface{}) (interface{}, error) { ...@@ -258,7 +273,8 @@ func (s *StoreRedis) SAdd(args []interface{}) (interface{}, error) {
return inter, err return inter, err
} }
/** /*
*
Scard方法 Scard方法
*/ */
func (s *StoreRedis) SCard(key string) (interface{}, error) { func (s *StoreRedis) SCard(key string) (interface{}, error) {
...@@ -267,7 +283,8 @@ func (s *StoreRedis) SCard(key string) (interface{}, error) { ...@@ -267,7 +283,8 @@ func (s *StoreRedis) SCard(key string) (interface{}, error) {
return inter, err return inter, err
} }
/** /*
*
Spop方法 Spop方法
*/ */
func (s *StoreRedis) SPop(key string) (interface{}, error) { func (s *StoreRedis) SPop(key string) (interface{}, error) {
...@@ -286,4 +303,3 @@ func (s *StoreRedis) RPopLPushByte(key, key1 string) ([]byte, error) { ...@@ -286,4 +303,3 @@ func (s *StoreRedis) RPopLPushByte(key, key1 string) ([]byte, error) {
defer s.Close() defer s.Close()
return v, nil return v, nil
} }
package controllers package controllers
import ( import (
"bufio"
"context"
"encoding/json" "encoding/json"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/astaxie/beego/orm" "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/ethclient"
log "github.com/sirupsen/logrus"
"github.com/wuban/nft-event/models" "github.com/wuban/nft-event/models"
"github.com/wuban/nft-event/utils" "github.com/wuban/nft-event/utils"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"regexp" "regexp"
) )
...@@ -123,3 +131,49 @@ func (c *LotteryController) ForwardReq() { ...@@ -123,3 +131,49 @@ func (c *LotteryController) ForwardReq() {
json.Unmarshal(body, &res) json.Unmarshal(body, &res)
c.ResponseInfo(200, models.SUCCESS, res) c.ResponseInfo(200, models.SUCCESS, res)
} }
func (c *LotteryController) GetPoolInfo() {
rpc := beego.AppConfig.String("rpcUrl")
client, err := ethclient.Dial(rpc)
if err != nil {
log.Error(models.DIAL_RPC_ERR, err.Error())
c.ResponseInfo(500, models.DIAL_RPC_ERR, err.Error())
}
defer client.Close()
abiBytes, err := os.Open("./sync/Lottery_Func.json")
if err != nil {
log.Error(models.READ_ABI_ERR, err)
c.ResponseInfo(500, models.READ_ABI_ERR, err.Error())
}
// 解析ABI文件
contractAbi, err := abi.JSON(bufio.NewReader(abiBytes))
if err != nil {
log.Error(models.PARSE_ABI_ERR, err)
c.ResponseInfo(500, models.PARSE_ABI_ERR, err.Error())
}
data, err := contractAbi.Pack("getPoolInfo")
if err != nil {
log.Error(models.PACK_ABI_ERR, err)
c.ResponseInfo(500, models.PACK_ABI_ERR, err.Error())
}
contractStr := beego.AppConfig.String("lotteryContract")
contractAddr := common.HexToAddress(contractStr)
// 执行合约调用
log.Info("data:", data)
result, err := client.CallContract(context.TODO(), ethereum.CallMsg{
To: &contractAddr,
Data: data,
}, nil)
if err != nil {
log.Error(models.CALL_CONTRACT_ERR, err)
c.ResponseInfo(500, models.CALL_CONTRACT_ERR, err.Error())
}
res := &models.PoolInfoRes{}
// 解析返回值
err = contractAbi.UnpackIntoInterface(res, "getPoolInfo", result)
if err != nil {
log.Error(models.UNPACK_ABI_RETURN_PARM_ERR, err)
c.ResponseInfo(500, models.UNPACK_ABI_RETURN_PARM_ERR, err.Error())
}
c.ResponseInfo(200, models.SUCCESS, res)
}
package models package models
const ( const (
SUCCESS = "success" SUCCESS = "success"
FAILED = "failed" FAILED = "failed"
PARAM_ERR = "Input param error" PARAM_ERR = "Input param error"
DATA_NIL = "Get data is nil" DATA_NIL = "Get data is nil"
PERIOD_ERR = "Get period list err" PERIOD_ERR = "Get period list err"
USER_ERR = "Get user list err" USER_ERR = "Get user list err"
SORTBY_ERR = "Get sort condition err" SORTBY_ERR = "Get sort condition err"
DIAL_RPC_ERR = "Dial rpc error"
READ_ABI_ERR = "Failed to read ABI file"
PARSE_ABI_ERR = "Failed to parse ABI"
CALL_CONTRACT_ERR = "Failed to pack contract call data"
PACK_ABI_ERR = "ABI pack func name err"
UNPACK_ABI_RETURN_PARM_ERR = "Failed to unpack contract output"
) )
package models package models
import (
"github.com/ethereum/go-ethereum/common"
"math/big"
)
type LotteryPeriodListRes struct { type LotteryPeriodListRes struct {
PoolAddr string `json:"poolAddr"` PoolAddr string `json:"poolAddr"`
TokenIdList []string `json:"tokenIdList"` TokenIdList []string `json:"tokenIdList"`
...@@ -21,3 +26,11 @@ type FilterRes struct { ...@@ -21,3 +26,11 @@ type FilterRes struct {
Msg interface{} `json:"msg"` Msg interface{} `json:"msg"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type PoolInfoRes struct {
Started_ []bool `abi:"started_" json:"started_"`
NftAddrs_ []common.Address `abi:"nftAddrs_" json:"nftAddrs_"`
NftInfo_ [][]string `abi:"nftInfo_" json:"nftInfo_"`
PoolInfo_ [][]*big.Int `abi:"poolInfo_" json:"poolInfo_"`
PoolHistory_ [][]*big.Int `abi:"poolHistory_" json:"poolHistory_"`
}
...@@ -10,6 +10,7 @@ func init() { ...@@ -10,6 +10,7 @@ func init() {
beego.Router("/api/v1/event/get/joinUserList", &controllers.LotteryController{}, "get:GetUserList") beego.Router("/api/v1/event/get/joinUserList", &controllers.LotteryController{}, "get:GetUserList")
beego.Router("/api/v1/event/get/sortCondition", &controllers.LotteryController{}, "get:GetHashSortByCondition") 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/param", &controllers.LotteryController{}, "get:ForwardReq")
beego.Router("/api/v1/event/get/poolInfo", &controllers.LotteryController{}, "get:GetPoolInfo")
beego.Router("/getToken", &controllers.AuthController{}, "get:GetToken") beego.Router("/getToken", &controllers.AuthController{}, "get:GetToken")
// 添加鉴权过滤器 // 添加鉴权过滤器
//beego.InsertFilter("/*", beego.BeforeRouter, utils.AuthFilter) //beego.InsertFilter("/*", beego.BeforeRouter, utils.AuthFilter)
......
[
{
"inputs": [
{
"internalType": "address",
"name": "pool",
"type": "address"
},
{
"internalType": "uint256",
"name": "t",
"type": "uint256"
}
],
"name": "drawLottery",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getPoolInfo",
"outputs": [
{
"internalType": "bool[]",
"name": "started_",
"type": "bool[]"
},
{
"internalType": "address[]",
"name": "nftAddrs_",
"type": "address[]"
},
{
"internalType": "string[][]",
"name": "nftInfo_",
"type": "string[][]"
},
{
"internalType": "uint256[][]",
"name": "poolInfo_",
"type": "uint256[][]"
},
{
"internalType": "uint256[][]",
"name": "poolHistory_",
"type": "uint256[][]"
}
],
"stateMutability": "view",
"type": "function"
}
]
\ No newline at end of file
...@@ -41,12 +41,11 @@ func SyncLogs() { ...@@ -41,12 +41,11 @@ func SyncLogs() {
} }
func init() { func init() {
startBlock := beego.AppConfig.String("deployedBlock") deployBlock := beego.AppConfig.String("deployedBlock")
cache.Redis.Set(LastSyncBlockKey, startBlock) cache.Redis.Set(LastSyncBlockKey, deployBlock)
var err error var err error
rpc := beego.AppConfig.String("rpcUrl") rpc := beego.AppConfig.String("rpcUrl")
deployBlock := beego.AppConfig.String("deployedBlock") log.Info("get rpc ", rpc)
fmt.Println("get rpc ", rpc)
pullTask = &PullEvent{contractHandler: make(map[common.Address]logHandler)} pullTask = &PullEvent{contractHandler: make(map[common.Address]logHandler)}
client, err := ethclient.Dial(rpc) client, err := ethclient.Dial(rpc)
if err != nil { if err != nil {
......
...@@ -3,10 +3,9 @@ package test ...@@ -3,10 +3,9 @@ package test
import ( import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing"
"runtime"
"path/filepath" "path/filepath"
_ "nft-event/routers" "runtime"
"testing"
"github.com/astaxie/beego" "github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
...@@ -14,11 +13,10 @@ import ( ...@@ -14,11 +13,10 @@ import (
func init() { func init() {
_, file, _, _ := runtime.Caller(0) _, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator)))) apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
beego.TestBeegoInit(apppath) beego.TestBeegoInit(apppath)
} }
// TestBeego is a sample to run an endpoint test // TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) { func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil) r, _ := http.NewRequest("GET", "/", nil)
...@@ -28,12 +26,11 @@ func TestBeego(t *testing.T) { ...@@ -28,12 +26,11 @@ func TestBeego(t *testing.T) {
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String()) beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() { Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() { Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200) So(w.Code, ShouldEqual, 200)
}) })
Convey("The Result Should Not Be Empty", func() { Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0) So(w.Body.Len(), ShouldBeGreaterThan, 0)
}) })
}) })
} }
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