Commit 46184d5c authored by duanjinfei's avatar duanjinfei

add getPoolInfo Api

parent 7318d20a
package cache
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
red "github.com/wuban/nft-event/cache/redis"
"time"
)
......@@ -24,7 +24,7 @@ func init() {
}
func NewPool(conn, dbNum, password string) error {
fmt.Println("redis连接池里的连接为空,重新创建连接池,starting...")
log.Info("redis连接池里的连接为空,重新创建连接池,starting...")
pool = &redis.Pool{
MaxIdle: 50, //最大空闲连接数
MaxActive: 0, //若为0,则活跃数没有限制
......
package redis
import (
"fmt"
"github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
"reflect"
"strings"
"unsafe"
......@@ -18,17 +18,19 @@ func NewStoreRedis(pool *redis.Pool) IRedis {
return sr
}
/**
/*
*
此方法没有使用
*/
func (s *StoreRedis) errorLogic(e error) {
i := strings.Index(e.Error(), "connect: connection refused")
if i == -1 {
fmt.Println(i, "不包含")
log.Error(i, "不包含")
}
}
/**
/*
*
设置key,value数据
*/
func (s *StoreRedis) Set(key, value string) error {
......@@ -40,7 +42,8 @@ func (s *StoreRedis) Set(key, value string) error {
return nil
}
/**
/*
*
设置key,value数据
*/
func (s *StoreRedis) SetBytes(key string, value []byte) error {
......@@ -52,7 +55,8 @@ func (s *StoreRedis) SetBytes(key string, value []byte) error {
return nil
}
/**
/*
*
设置key的过期时间
*/
func (s *StoreRedis) SetKvAndExp(key, value string, expire int) error {
......@@ -73,27 +77,28 @@ func (s *StoreRedis) SetKvInt(key string, value int) error {
return nil
}
/**
/*
*
根据key获取对应数据
*/
func (s *StoreRedis) Get(key string) string {
value, err := redis.String(s.pool.Get().Do("GET", key))
defer s.Close()
if err != nil {
fmt.Println("redis get failed:", err)
log.Error("redis get failed:", err)
}
return value
}
/**
/*
*
根据key获取对应数据
*/
func (s *StoreRedis) GetBytes(key string) []byte {
value, err := s.pool.Get().Do("GET", key)
defer s.Close()
if err != nil {
fmt.Println("redis get failed:", err)
log.Error("redis get failed:", err)
}
if value == nil {
......@@ -103,7 +108,8 @@ func (s *StoreRedis) GetBytes(key string) []byte {
return value.([]byte)
}
/**
/*
*
判断key是否存在
*/
func (s *StoreRedis) IsKeyExists(key string) bool {
......@@ -112,31 +118,34 @@ func (s *StoreRedis) IsKeyExists(key string) bool {
return is_key_exit
}
/**
/*
*
删除key
*/
func (s *StoreRedis) Del(key string) bool {
is_key_delete, err := redis.Bool(s.pool.Get().Do("DEL", key))
defer s.Close()
if err != nil {
fmt.Println("error:", err)
log.Error("error:", err)
}
return is_key_delete
}
/**
/*
*
删除key
*/
func (s *StoreRedis) HDel(key string, field string) bool {
is_key_delete, err := redis.Bool(s.pool.Get().Do("HDEL", key, field))
defer s.Close()
if err != nil {
fmt.Println("error:", err)
log.Error("error:", err)
}
return is_key_delete
}
/**
/*
*
对象转换成json后进行存储
*/
func (s *StoreRedis) Setnx(key string, value []byte) error {
......@@ -205,7 +214,8 @@ func (s *StoreRedis) Close() {
s.pool.Get().Close()
}
/**
/*
*
Hincryby方法
*/
func (s *StoreRedis) HINCRBY(key, field string) {
......@@ -213,7 +223,8 @@ func (s *StoreRedis) HINCRBY(key, field string) {
defer s.Close()
}
/**
/*
*
HGet方法
*/
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
}
/**
/*
*
HGetAll方法
*/
func (s *StoreRedis) HGetAll(key string) ([][]byte, error) {
......@@ -231,7 +243,8 @@ func (s *StoreRedis) HGetAll(key string) ([][]byte, error) {
return inter, err
}
/**
/*
*
Hset方法
*/
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{},
return inter, err
}
/**
/*
*
Hset方法
*/
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
return inter, err
}
/**
/*
*
SADD方法
*/
func (s *StoreRedis) SAdd(args []interface{}) (interface{}, error) {
......@@ -258,7 +273,8 @@ func (s *StoreRedis) SAdd(args []interface{}) (interface{}, error) {
return inter, err
}
/**
/*
*
Scard方法
*/
func (s *StoreRedis) SCard(key string) (interface{}, error) {
......@@ -267,7 +283,8 @@ func (s *StoreRedis) SCard(key string) (interface{}, error) {
return inter, err
}
/**
/*
*
Spop方法
*/
func (s *StoreRedis) SPop(key string) (interface{}, error) {
......@@ -286,4 +303,3 @@ func (s *StoreRedis) RPopLPushByte(key, key1 string) ([]byte, error) {
defer s.Close()
return v, nil
}
package controllers
import (
"bufio"
"context"
"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/ethclient"
log "github.com/sirupsen/logrus"
"github.com/wuban/nft-event/models"
"github.com/wuban/nft-event/utils"
"io/ioutil"
"net/http"
"os"
"regexp"
)
......@@ -123,3 +131,49 @@ func (c *LotteryController) ForwardReq() {
json.Unmarshal(body, &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)
}
......@@ -8,4 +8,10 @@ const (
PERIOD_ERR = "Get period list err"
USER_ERR = "Get user list 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
import (
"github.com/ethereum/go-ethereum/common"
"math/big"
)
type LotteryPeriodListRes struct {
PoolAddr string `json:"poolAddr"`
TokenIdList []string `json:"tokenIdList"`
......@@ -21,3 +26,11 @@ type FilterRes struct {
Msg interface{} `json:"msg"`
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() {
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/param", &controllers.LotteryController{}, "get:ForwardReq")
beego.Router("/api/v1/event/get/poolInfo", &controllers.LotteryController{}, "get:GetPoolInfo")
beego.Router("/getToken", &controllers.AuthController{}, "get:GetToken")
// 添加鉴权过滤器
//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() {
}
func init() {
startBlock := beego.AppConfig.String("deployedBlock")
cache.Redis.Set(LastSyncBlockKey, startBlock)
deployBlock := beego.AppConfig.String("deployedBlock")
cache.Redis.Set(LastSyncBlockKey, deployBlock)
var err error
rpc := beego.AppConfig.String("rpcUrl")
deployBlock := beego.AppConfig.String("deployedBlock")
fmt.Println("get rpc ", rpc)
log.Info("get rpc ", rpc)
pullTask = &PullEvent{contractHandler: make(map[common.Address]logHandler)}
client, err := ethclient.Dial(rpc)
if err != nil {
......
......@@ -3,10 +3,9 @@ package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "nft-event/routers"
"runtime"
"testing"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
......@@ -14,11 +13,10 @@ import (
func init() {
_, 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)
}
// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
......@@ -36,4 +34,3 @@ func TestBeego(t *testing.T) {
})
})
}
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