pancakeswapnft/userservice.go

101 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-11-25 07:04:16 +00:00
package pancakeswapnft
import (
"context"
"github.com/machinebox/graphql"
"github.com/pkg/errors"
"strings"
)
const (
userQuery = `
query ($where: User_filter) {
users(where: $where) {
id
numberTokensListed
numberTokensPurchased
numberTokensSold
totalVolumeInBNBTokensPurchased
totalVolumeInBNBTokensSold
totalFeesCollectedInBNB
buyTradeHistory {
id
block
timestamp
askPrice
netPrice
buyer {
id
}
seller {
id
}
nft {
tokenId
}
withBNB
}
sellTradeHistory {
id
block
timestamp
askPrice
netPrice
buyer {
id
}
seller {
id
}
nft {
tokenId
}
withBNB
}
askOrderHistory {
id
block
askPrice
timestamp
seller {
id
}
orderType
}
averageTokenPriceInBNBPurchased
averageTokenPriceInBNBSold
}
}`
)
type UserService struct {
graphClient *graphql.Client
}
func NewUserService(graphclient *graphql.Client) *UserService {
return &UserService{
graphClient: graphclient,
}
}
func (s *UserService) GetUser(ctx context.Context, address string) (User, error) {
req := graphql.NewRequest(userQuery)
req.Var("where", struct {
Id string `json:"id"`
}{
Id: strings.ToLower(address),
})
var respData struct {
Users []User `json:"users"`
}
if err := s.graphClient.Run(ctx, req, &respData); err != nil {
return User{}, errors.Wrap(err, "requesting graphql")
}
if len(respData.Users) == 0 {
return User{}, errors.Errorf("no user found with id %s", address)
}
return respData.Users[0], nil
}