Skip to content

Commit

Permalink
Merge pull request #2 from decert-me/feature/auto-update
Browse files Browse the repository at this point in the history
Feature 合约跟踪功能
  • Loading branch information
0xdwong authored Jun 28, 2023
2 parents 6b23784 + abdaf68 commit 2658c2b
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 205 deletions.
7 changes: 5 additions & 2 deletions internal/app/api/v1/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ func AddCollection(c *gin.Context) {
var req request.AddCollectionReq
_ = c.ShouldBindJSON(&req)
address := c.GetString("address")
if len(req.IDs) == 0 || address == "" {
req.Chain = global.ChainName[req.ChainID]
if address == "" || req.Chain == "" {
response.FailWithMessage("Error", c)
return
}
if err := service.AddCollection(req.IDs, address, req.Flag); err != nil {

if err := service.AddCollection(address, req); err != nil {
global.LOG.Error("Error!", zap.Error(err))
response.FailWithMessage("Error", c)
} else {
Expand Down Expand Up @@ -104,6 +106,7 @@ func UpdatedCollection(c *gin.Context) {
func RefreshUserData(c *gin.Context) {
var req request.RefreshUserDataReq
_ = c.ShouldBindJSON(&req)
req.Address = strings.ToLower(req.Address)
if err := service.RefreshUserData(req.Address); err != nil {
global.LOG.Error("Error!", zap.Error(err))
response.FailWithMessage("Error", c)
Expand Down
9 changes: 4 additions & 5 deletions internal/app/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"go.uber.org/zap"
"gorm.io/gorm"
"nft-collect/internal/app/config"
"nft-collect/pkg/cache"
)

var (
DB *gorm.DB // 数据库链接
LOG *zap.Logger // 日志框架
CONFIG config.Server // 配置信息
Cache *cache.BigCacheStore // 缓存
DB *gorm.DB // 数据库链接
LOG *zap.Logger // 日志框架
CONFIG config.Server // 配置信息
ChainName map[uint]string // 链名称
)
14 changes: 0 additions & 14 deletions internal/app/initialize/cache.go

This file was deleted.

9 changes: 8 additions & 1 deletion internal/app/initialize/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

// 获取默认NFT合约信息
// InitNFTContract 获取默认NFT合约信息
func InitNFTContract() {
for _, api := range global.CONFIG.NFT.APIConfig {
for _, v := range global.CONFIG.NFT.DefContract {
Expand All @@ -19,3 +19,10 @@ func InitNFTContract() {
}
}
}

func InitChainName() {
global.ChainName = make(map[uint]string)
for _, api := range global.CONFIG.NFT.APIConfig {
global.ChainName[api.ChainID] = api.Chain
}
}
11 changes: 0 additions & 11 deletions internal/app/middleware/cache.go

This file was deleted.

4 changes: 1 addition & 3 deletions internal/app/model/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ type Collection struct {
global.MODEL
Chain string `gorm:"column:chain;index:chain_address_contract_token,unique" json:"chain" form:"chain"` // 区块链的简称(eth, bnb, polygon, moonbeam, arbitrum, optimism, platon, avalanche, cronos)
AccountAddress string `gorm:"column:account_address;type:char(42);index:chain_address_contract_token,unique" json:"account_address" form:"account_address"` // 资产持有者的地址
Status uint8 `gorm:"default:1;" json:"status" form:"status"` // 显示状态(1:隐藏 2:显示)
Flag uint8 `gorm:"default:1;" json:"flag" form:"flag"` // 添加状态(1:未添加 2:已添加)
Status uint8 `gorm:"default:0;" json:"status" form:"status"` // 显示状态(0:初始状态 1:隐藏 2:显示)
NFTScanOwn
}

Expand All @@ -18,7 +17,6 @@ type CollectionUpdate struct {
Chain string `gorm:"column:chain;index:chain_address_contract_token,unique" json:"chain" form:"chain"` // 区块链的简称(eth, bnb, polygon, moonbeam, arbitrum, optimism, platon, avalanche, cronos)
AccountAddress string `gorm:"column:account_address;type:char(42);index:chain_address_contract_token,unique" json:"account_address" form:"account_address"` // 资产持有者的地址
Status uint8 `gorm:"-" json:"status" form:"status"` // 显示状态(1:隐藏 2:显示)
Flag uint8 `gorm:"-" json:"flag" form:"flag"` // 添加状态(1:未添加 2:已添加)
NFTScanOwn
}

Expand Down
7 changes: 5 additions & 2 deletions internal/app/model/request/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ type AddContractReq struct {
}

type AddCollectionReq struct {
Flag int64 `json:"flag"`
IDs []string `json:"ids" form:"ids"`
ChainID uint `json:"chain_id" form:"chain_id"`
Chain string
ContractAddress string `json:"contract_address" form:"contract_address"` // 合约地址
HideIDs []string `json:"hide_ids" form:"hide_ids"`
ShowIDs []string `json:"show_ids" form:"show_ids"`
}

type UpdatedCollectionReq struct {
Expand Down
9 changes: 4 additions & 5 deletions internal/app/router/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ import (
)

func InitAccountRouter(Router *gin.RouterGroup) {
accountRouterWithCache := Router.Group("account").Use(middleware.Addr())
accountRouterWithAddr := Router.Group("account").Use(middleware.Addr())
accountRouterWithAuth := Router.Group("account").Use(middleware.Auth()) // auth
{
accountRouterWithCache.GET("/own/:address/contract", v1.GetContract) // Get the list of user NFT contracts
accountRouterWithCache.GET("/own/:address", v1.GetCollection) // Get the NFT data by the user
accountRouterWithAddr.GET("/own/:address/contract", v1.GetContract) // Get the list of user NFT contracts
accountRouterWithAddr.GET("/own/:address", v1.GetCollection) // Get the NFT data by the user
accountRouterWithAddr.POST("/own/refreshUserData", v1.RefreshUserData) // refresh user data
}
{
accountRouterWithAuth.GET("/contract/:address", v1.GetCollectionByContract) // Get the NFT data by the Contract
}
{
accountRouterWithAuth.POST("/own/collection", v1.AddCollection) // add collection
accountRouterWithAuth.PUT("/own/collection/:id", v1.UpdatedCollection) // update collection status
accountRouterWithAuth.POST("/own/refreshUserData", v1.RefreshUserData) // add collection

}
}
11 changes: 6 additions & 5 deletions internal/app/service/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func GetContract(address, account string) (res []response.GetContractRes, err er
if errFirst != nil && errFirst != gorm.ErrRecordNotFound {
return res, errFirst
}
// 获取默认合约
var contractDefault []string
err = db.Model(&model.ContractDefault{}).Raw("SELECT contract_id FROM contract_default").Scan(&contractDefault).Error
if errFirst == gorm.ErrRecordNotFound {
Expand All @@ -33,7 +34,7 @@ func GetContract(address, account string) (res []response.GetContractRes, err er
dealList = contractDefault
} else if address != common.HexToAddress("0").String() {
dealList = slice.DiffSlice[string](contractDefault, user.ContractIDs)
go updateAllCollection(address, dealList, false) // update all collection
go updateAllCollection(address, dealList, false, false) // update all collection
}

if len(user.ContractIDs) != len(user.Counts) {
Expand All @@ -46,7 +47,7 @@ func GetContract(address, account string) (res []response.GetContractRes, err er
// TODO 优化
var count int64
err = db.Model(&model.Collection{}).
Raw("SElECT COUNT(1) FROM collection a JOIN contract b ON a.contract_address=b.contract_address AND a.chain=b.chain WHERE b.id = ? AND account_address= ? AND a.flag=2", id, address).
Raw("SElECT COUNT(1) FROM collection a JOIN contract b ON a.contract_address=b.contract_address AND a.chain=b.chain WHERE b.id = ? AND account_address= ? AND a.status=2", id, address).
Scan(&count).Error
contractMap[id] = count
}
Expand Down Expand Up @@ -121,7 +122,7 @@ func initAccount(address string) (err error) {
if err != nil {
return err
}
updateAllCollection(address, uuidList, true)
updateAllCollection(address, uuidList, true, false)
return err
}

Expand All @@ -148,13 +149,13 @@ func updateContractCount(address string) (err error) {

// TODO: 添加状态 需要确定是否过滤
err = db.Model(&model.Collection{}).Where("chain", nftContract.Chain).
Where("contract_address", nftContract.ContractAddress).Where("account_address", address).Where("flag", 2).
Where("contract_address", nftContract.ContractAddress).Where("account_address", address).
Count(&count).Error
if err != nil {
return err
}
err = db.Model(&model.Collection{}).Where("chain", nftContract.Chain).
Where("contract_address", nftContract.ContractAddress).Where("account_address", address).Where("flag", 2).Where("status", 2).
Where("contract_address", nftContract.ContractAddress).Where("account_address", address).Where("status", 2).
Count(&countShow).Error
if err != nil {
return err
Expand Down
22 changes: 0 additions & 22 deletions internal/app/service/cahce.go

This file was deleted.

Loading

0 comments on commit 2658c2b

Please sign in to comment.