nft质押任务逻辑
This commit is contained in:
@@ -3,65 +3,66 @@ package earn
|
||||
import (
|
||||
"context"
|
||||
ea "github.com/earn-alliance/earnalliance-go"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"nova_task/internal/consts"
|
||||
"nova_task/internal/svc"
|
||||
)
|
||||
|
||||
// Earn 用户数据上报earn平台定时任务
|
||||
type Earn struct {
|
||||
// Cron 用户数据上报earn平台定时任务
|
||||
type Cron struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewEarn(ctx context.Context, svcCtx *svc.ServiceContext) *Earn {
|
||||
e := &Earn{ctx: ctx, svcCtx: svcCtx}
|
||||
func NewCron(ctx context.Context, svcCtx *svc.ServiceContext) cron.Job {
|
||||
e := &Cron{ctx: ctx, svcCtx: svcCtx}
|
||||
if e.svcCtx.Config.EarnCorn.RunOnStart {
|
||||
e.Run()
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Earn) Spec() string {
|
||||
return e.svcCtx.Config.EarnCorn.Spec
|
||||
func (c *Cron) Spec() string {
|
||||
return c.svcCtx.Config.EarnCorn.Spec
|
||||
}
|
||||
|
||||
func (e *Earn) Run() {
|
||||
func (c *Cron) Run() {
|
||||
logx.Debugw("run earn cron task")
|
||||
c, err := e.svcCtx.ConfigModel.FindOneByName(e.ctx, consts.EarnAllianceInviterId)
|
||||
conf, err := c.svcCtx.ConfigModel.FindOneByName(c.ctx, consts.EarnAllianceInviterId)
|
||||
if err != nil {
|
||||
logx.Errorw("find earn alliance inviter id failed", logx.Field("err", err))
|
||||
return
|
||||
}
|
||||
e.pushUserInfo(cast.ToUint(c.Value))
|
||||
e.pushUserBind(cast.ToUint(c.Value))
|
||||
c.pushUserInfo(cast.ToUint(conf.Value))
|
||||
c.pushUserBind(cast.ToUint(conf.Value))
|
||||
}
|
||||
|
||||
func (e *Earn) pushUserInfo(shareId uint) {
|
||||
us, err := e.svcCtx.PromoteBindModel.FindRequirePushUser(e.ctx, shareId)
|
||||
func (c *Cron) pushUserInfo(shareId uint) {
|
||||
us, err := c.svcCtx.PromoteBindModel.FindRequirePushUser(c.ctx, shareId)
|
||||
if err != nil {
|
||||
logx.Errorw("find require push user failed", logx.Field("err", err), logx.Field("share_id", shareId))
|
||||
return
|
||||
}
|
||||
logx.Debugw("find require push user", logx.Field("count", len(us)))
|
||||
for _, u := range us {
|
||||
ui, err := e.svcCtx.UserModel.FindOne(e.ctx, u.InvitedUid)
|
||||
ui, err := c.svcCtx.UserModel.FindOne(c.ctx, u.InvitedUid)
|
||||
if err != nil {
|
||||
logx.Errorw("find user failed", logx.Field("err", err), logx.Field("uid", u.InvitedUid))
|
||||
continue
|
||||
}
|
||||
var twitterId string
|
||||
ut, err := e.svcCtx.TwitterModel.FindOne(e.ctx, u.InvitedUid)
|
||||
ut, err := c.svcCtx.TwitterModel.FindOne(c.ctx, u.InvitedUid)
|
||||
if err == nil {
|
||||
twitterId = ut.TwitterId
|
||||
}
|
||||
|
||||
err = e.svcCtx.PromoteBindModel.UpdatePushUser(e.ctx, u.Id)
|
||||
err = c.svcCtx.PromoteBindModel.UpdatePushUser(c.ctx, u.Id)
|
||||
if err != nil {
|
||||
logx.Errorw("update push user failed", logx.Field("err", err), logx.Field("uid", u.InvitedUid), logx.Field("twitter_id", twitterId), logx.Field("email", ui.Email))
|
||||
} else {
|
||||
e.svcCtx.Earn.SetIdentifiers(cast.ToString(ui.Id), &ea.Identifiers{
|
||||
c.svcCtx.Earn.SetIdentifiers(cast.ToString(ui.Id), &ea.Identifiers{
|
||||
Email: ea.IdentifierFrom(ui.Email),
|
||||
TwitterId: ea.IdentifierFrom(twitterId),
|
||||
})
|
||||
@@ -70,19 +71,19 @@ func (e *Earn) pushUserInfo(shareId uint) {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Earn) pushUserBind(shareId uint) {
|
||||
us, err := e.svcCtx.TouristBindModel.FindRequirePushUser(e.ctx, shareId)
|
||||
func (c *Cron) pushUserBind(shareId uint) {
|
||||
us, err := c.svcCtx.TouristBindModel.FindRequirePushUser(c.ctx, shareId)
|
||||
if err != nil {
|
||||
logx.Errorw("find require push bind role user failed", logx.Field("err", err), logx.Field("share_id", shareId))
|
||||
return
|
||||
}
|
||||
logx.Debugw("find require push bind role user", logx.Field("count", len(us)))
|
||||
for _, u := range us {
|
||||
err = e.svcCtx.PromoteBindModel.UpdatePushRole(e.ctx, u.Id)
|
||||
err = c.svcCtx.PromoteBindModel.UpdatePushRole(c.ctx, u.Id)
|
||||
if err != nil {
|
||||
logx.Errorw("update push user failed", logx.Field("err", err), logx.Field("uid", u.Uid))
|
||||
} else {
|
||||
e.svcCtx.Earn.Track(cast.ToString(u.Uid), "BIND_ROLE", nil, nil)
|
||||
c.svcCtx.Earn.Track(cast.ToString(u.Uid), "BIND_ROLE", nil, nil)
|
||||
logx.Infow("push user info success", logx.Field("uid", u.Uid))
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
package pledge
|
||||
package holder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/spf13/cast"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"nova_task/internal/model"
|
||||
@@ -10,28 +11,28 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Pledge struct {
|
||||
type Cron struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewPledge(ctx context.Context, svcCtx *svc.ServiceContext) *Pledge {
|
||||
pg := &Pledge{
|
||||
func NewCron(ctx context.Context, svcCtx *svc.ServiceContext) cron.Job {
|
||||
pg := &Cron{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
if svcCtx.Config.PledgeCron.RunOnStart {
|
||||
if svcCtx.Config.NftTaskCron.HolderCheckRunOnStart {
|
||||
pg.Run()
|
||||
}
|
||||
return pg
|
||||
}
|
||||
|
||||
func (p *Pledge) Spec() string {
|
||||
return p.svcCtx.Config.PledgeCron.Spec
|
||||
func (c *Cron) Spec() string {
|
||||
return c.svcCtx.Config.NftTaskCron.HolderSpec
|
||||
}
|
||||
|
||||
func (p *Pledge) Run() {
|
||||
logx.Debugw("run pledge cron task")
|
||||
func (c *Cron) Run() {
|
||||
logx.Debugw("run Cron cron task")
|
||||
ols, err := GetOwnerList()
|
||||
if err != nil {
|
||||
logx.Errorw("get owner list error", logx.Field("error", err))
|
||||
@@ -42,7 +43,7 @@ func (p *Pledge) Run() {
|
||||
for _, tk := range o.TokenBalances {
|
||||
balance := cast.ToInt(tk.Balance)
|
||||
logx.Debugw("owner token", logx.Field("address", o.OwnerAddress), logx.Field("token", tk.TokenID), logx.Field("balance", tk.Balance))
|
||||
nft, err := p.svcCtx.NftHolderModel.FindOneByAddressTokenId(p.ctx, o.OwnerAddress, tk.TokenID)
|
||||
nft, err := c.svcCtx.NftHolderModel.FindOneByAddressTokenId(c.ctx, o.OwnerAddress, tk.TokenID)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
nft = &model.NhNftHolder{
|
||||
@@ -60,7 +61,7 @@ func (p *Pledge) Run() {
|
||||
var value int
|
||||
if nft.Id == 0 {
|
||||
// 新增
|
||||
_, err = p.svcCtx.NftHolderModel.Insert(p.ctx, nft)
|
||||
_, err = c.svcCtx.NftHolderModel.Insert(c.ctx, nft)
|
||||
value = balance
|
||||
} else {
|
||||
// 持有数量变化
|
||||
@@ -68,7 +69,7 @@ func (p *Pledge) Run() {
|
||||
if value != 0 {
|
||||
nft.Balance = balance
|
||||
nft.UpdateSeq = updateSeq
|
||||
err = p.svcCtx.NftHolderModel.Update(p.ctx, nft)
|
||||
err = c.svcCtx.NftHolderModel.Update(c.ctx, nft)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +79,7 @@ func (p *Pledge) Run() {
|
||||
|
||||
// 余额有变化,记录变化日志
|
||||
if value != 0 {
|
||||
_, err = p.svcCtx.NftHolderChangeLogModel.Insert(p.ctx, &model.NhNftHolderChangeLog{
|
||||
_, err = c.svcCtx.NftHolderChangeLogModel.Insert(c.ctx, &model.NhNftHolderChangeLog{
|
||||
Address: o.OwnerAddress,
|
||||
TokenId: tk.TokenID,
|
||||
Value: balance,
|
||||
@@ -92,13 +93,13 @@ func (p *Pledge) Run() {
|
||||
}
|
||||
|
||||
// 删除已经不持有的地址,且添加变化日志
|
||||
nfts, err := p.svcCtx.NftHolderModel.FindOtherUpdateSeq(p.ctx, updateSeq)
|
||||
nfts, err := c.svcCtx.NftHolderModel.FindOtherUpdateSeq(c.ctx, updateSeq)
|
||||
if err != nil {
|
||||
logx.Errorw("find other update seq error", logx.Field("error", err))
|
||||
return
|
||||
}
|
||||
for _, nft := range nfts {
|
||||
_, err = p.svcCtx.NftHolderChangeLogModel.Insert(p.ctx, &model.NhNftHolderChangeLog{
|
||||
_, err = c.svcCtx.NftHolderChangeLogModel.Insert(c.ctx, &model.NhNftHolderChangeLog{
|
||||
Address: nft.Address,
|
||||
TokenId: nft.TokenId,
|
||||
Value: -nft.Balance,
|
||||
@@ -108,7 +109,7 @@ func (p *Pledge) Run() {
|
||||
logx.Errorw("delete nft holder error", logx.Field("error", err), logx.Field("address", nft.Address), logx.Field("token", nft.TokenId))
|
||||
}
|
||||
}
|
||||
err = p.svcCtx.NftHolderModel.DeleteOtherUpdateSeq(p.ctx, updateSeq)
|
||||
err = c.svcCtx.NftHolderModel.DeleteOtherUpdateSeq(c.ctx, updateSeq)
|
||||
if err != nil {
|
||||
logx.Errorw("delete other update seq error", logx.Field("error", err), logx.Field("update_seq", updateSeq))
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package pledge
|
||||
package holder
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -6,10 +6,18 @@ import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"nova_task/internal/job/earn"
|
||||
"nova_task/internal/job/pledge"
|
||||
"nova_task/internal/job/holder"
|
||||
"nova_task/internal/job/stake_settle"
|
||||
"nova_task/internal/svc"
|
||||
)
|
||||
|
||||
// cronList 定时器Builder列表
|
||||
var cronList = []func(context.Context, *svc.ServiceContext) cron.Job{
|
||||
earn.NewCron,
|
||||
holder.NewCron,
|
||||
stake_settle.NewCron,
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
@@ -21,15 +29,13 @@ type Spec interface {
|
||||
Spec() string
|
||||
}
|
||||
|
||||
// NewJob 创建定时器服务
|
||||
func NewJob(svcCtx *svc.ServiceContext) *Job {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var cronList = []cron.Job{
|
||||
earn.NewEarn(ctx, svcCtx),
|
||||
pledge.NewPledge(ctx, svcCtx),
|
||||
}
|
||||
var err error
|
||||
c := cron.New()
|
||||
for _, cr := range cronList {
|
||||
c := cron.New(cron.WithSeconds())
|
||||
for _, cf := range cronList {
|
||||
cr := cf(ctx, svcCtx)
|
||||
if cs, ok := cr.(Spec); ok {
|
||||
spec := cs.Spec()
|
||||
if spec == "" {
|
||||
|
||||
41
internal/job/stake_settle/cron.go
Normal file
41
internal/job/stake_settle/cron.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package stake_settle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"nova_task/internal/svc"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cron struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCron(ctx context.Context, svcCtx *svc.ServiceContext) cron.Job {
|
||||
return &Cron{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cron) Spec() string {
|
||||
return c.svcCtx.Config.NftTaskCron.SettleSpec
|
||||
}
|
||||
|
||||
func (c *Cron) Run() {
|
||||
start, end, err := c.svcCtx.ConfigModel.GetNftStakeTaskOpenDate(c.ctx)
|
||||
if err != nil {
|
||||
logx.Errorw("get nft stake task open date failed", logx.Field("err", err))
|
||||
return
|
||||
}
|
||||
end = end.AddDate(0, 0, 1).Add(-time.Second)
|
||||
now := time.Now()
|
||||
if now.Before(start) || now.After(end) {
|
||||
logx.Debugw("now is not in the date range", logx.Field("now", now), logx.Field("start", start), logx.Field("end", end))
|
||||
return
|
||||
}
|
||||
|
||||
logx.Debugw("run settle cron task")
|
||||
}
|
||||
Reference in New Issue
Block a user