Skip to content

Commit

Permalink
Merge pull request #36 from dylenfu/master
Browse files Browse the repository at this point in the history
implement nft explorer api
  • Loading branch information
dylenfu authored Apr 15, 2021
2 parents cd56c96 + 07ad6c2 commit 615a5de
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 92 deletions.
158 changes: 158 additions & 0 deletions nft_http/controllers/explorer_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package controllers

import (
"fmt"
"github.com/astaxie/beego/logs"
"github.com/ethereum/go-ethereum/common"
"math/big"
"poly-bridge/models"

"github.com/astaxie/beego"
)

type ExplorerController struct {
beego.Controller
}

func (c *ExplorerController) Transactions() {
var req TransactionBriefsReq
if !input(&c.Controller, &req) {
return
}

relations := make([]*TransactionBriefRelation, 0)
limit := req.PageSize
offset := req.PageSize * req.PageNo
db.Raw("select wp.*, tr.amount as token_id, tr.asset as src_asset "+
"from wrapper_transactions wp "+
"left join src_transfers as tr on wp.hash=tr.tx_hash "+
"where wp.standard=? "+
"order by wp.time desc "+
"limit ? offset ?", models.TokenTypeErc721, limit, offset).
Find(&relations)

transactionNum := txCounter.Number()
totalPage := (int(transactionNum) + req.PageSize - 1) / req.PageSize
totalCnt := int(transactionNum)

list := make([]*TransactionBriefRsp, 0)
for _, v := range relations {
tk := selectNFTAsset(v.SrcAsset)
data := new(TransactionBriefRsp).instance(tk.TokenBasicName, v)
list = append(list, data)
}

resp := new(TransactionBriefsRsp).instance(req.PageSize, req.PageNo, totalPage, totalCnt, list)
output(&c.Controller, resp)
}

func (c *ExplorerController) TransactionsOfAddress() {
var req TransactionBriefsOfAddressReq
if !input(&c.Controller, &req) {
return
}

relations := make([]*TransactionBriefRelation, 0)
limit := req.PageSize
offset := req.PageSize * req.PageNo
db.Raw("select wp.*, tr.amount as token_id, tr.asset as src_asset "+
"from wrapper_transactions wp "+
"left join src_transfers as tr on wp.hash=tr.tx_hash "+
"where wp.standard=? and (wp.user in ? or wp.dst_user in ?) "+
"order by wp.time desc "+
"limit ? offset ?",
models.TokenTypeErc721, req.Addresses, req.Addresses, limit, offset).
Find(&relations)

var transactionNum int64
db.Model(&models.SrcTransfer{}).
Joins("inner join wrapper_transactions on src_transfers.tx_hash = wrapper_transactions.hash").
Where("src_transfers.standard = ? and (`from` in ? or src_transfers.dst_user in ?)", models.TokenTypeErc721, req.Addresses, req.Addresses).
Count(&transactionNum)
totalPage := (int(transactionNum) + req.PageSize - 1) / req.PageSize
totalCnt := int(transactionNum)

list := make([]*TransactionBriefRsp, 0)
for _, v := range relations {
tk := selectNFTAsset(v.SrcAsset)
data := new(TransactionBriefRsp).instance(tk.TokenBasicName, v)
list = append(list, data)
}

resp := new(TransactionBriefsRsp).instance(req.PageSize, req.PageNo, totalPage, totalCnt, list)
output(&c.Controller, resp)
}

func (c *ExplorerController) TransactionDetail() {
var req TransactionDetailReq
if !input(&c.Controller, &req) {
return
}

relation := new(TransactionDetailRelation)
res := db.Table("src_transactions").
Select("src_transactions.hash as src_hash, poly_transactions.hash as poly_hash, dst_transactions.hash as dst_hash, src_transactions.chain_id as chain_id, src_transfers.asset as token_hash").
Where("src_transactions.hash = ?", req.Hash).
Joins("left join src_transfers on src_transactions.hash = src_transfers.tx_hash").
Joins("left join poly_transactions on src_transactions.hash = poly_transactions.src_hash").
Joins("left join dst_transactions on poly_transactions.hash = dst_transactions.poly_hash").
Preload("WrapperTransaction").
Preload("SrcTransaction").
Preload("SrcTransaction.SrcTransfer").
Preload("PolyTransaction").
Preload("DstTransaction").
Preload("DstTransaction.DstTransfer").
Order("src_transactions.time desc").
Find(relation)

if res.RowsAffected == 0 {
output(&c.Controller, nil)
return
}

data := new(TransactionDetailRsp).instance(relation)
fillMetaInfo(data)

output(&c.Controller, data)
}

func fillMetaInfo(data *TransactionDetailRsp) {
if data.Transaction == nil {
fmt.Println("----------1")
return
}

chainId := data.Transaction.SrcChainId
sdk := selectNode(chainId)
wrapper := selectWrapper(chainId)
if wrapper == emptyAddr {
return
}
if data.SrcTransaction == nil {
return
}

nftAsset := selectNFTAsset(data.SrcTransaction.AssetHash)
tokenId, ok := string2Big(data.Transaction.TokenId)
if !ok {
return
}
nftAddr := common.HexToAddress(nftAsset.Hash)
urlList, err := sdk.GetTokensById(wrapper, nftAddr, []*big.Int{tokenId})
if err != nil {
logs.Error("fillMetaInfo err: %v", err)
return
}
if len(urlList) == 0 {
return
}
url := urlList[tokenId]

item, err := getProfileItemWithTokenId(nftAsset.TokenBasicName, tokenId, url)
if err != nil {
logs.Error("fillMetaInfo err: %v", err)
return
}

data.Meta = item
}
25 changes: 19 additions & 6 deletions nft_http/controllers/item_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ func (c *ItemController) Items() {
empty()
return
}
profile, err := fetcher.Fetch(nftAsset.TokenBasicName, &mcm.FetchRequestParams{
TokenId: models.NewBigInt(tokenId),
Url: url,
})
item := new(Item).instance(nftAsset.TokenBasicName, tokenId, profile)
item, err := getProfileItemWithTokenId(nftAsset.TokenBasicName, tokenId, url)
if err != nil {
logs.Error("getProfileWithTokenId err: %v", err)
empty()
return
}
data := new(ItemsOfAddressRsp).instance(req.PageSize, req.PageNo, totalPage, totalCnt, []*Item{item})
output(&c.Controller, data)
return
Expand All @@ -120,6 +121,18 @@ func (c *ItemController) Items() {
output(&c.Controller, data)
}

func getProfileItemWithTokenId(assetName string, tokenId *big.Int, url string) (*Item, error) {
profile, err := fetcher.Fetch(assetName, &mcm.FetchRequestParams{
TokenId: models.NewBigInt(tokenId),
Url: url,
})
if err != nil {
return nil, err
}
item := new(Item).instance(assetName, tokenId, profile)
return item, nil
}

func getProfileItemsWithChainData(data map[*big.Int]string, nftAsset *models.Token) []*Item {
assetName := nftAsset.TokenBasicName
profileReqs := make([]*mcm.FetchRequestParams, 0)
Expand Down Expand Up @@ -168,4 +181,4 @@ func getProfileItemsWithChainData(data map[*big.Int]string, nftAsset *models.Tok

func string2Big(str string) (*big.Int, bool) {
return new(big.Int).SetString(str, 10)
}
}
Loading

0 comments on commit 615a5de

Please sign in to comment.