pancakeswapnft/user.go

39 lines
1.4 KiB
Go

package pancakeswapnft
import (
"time"
)
type User struct {
Address string `json:"id"`
NumberTokensListed int64 `json:"numberTokensListed,string"`
NumberTokensPurchased int64 `json:"numberTokensPurchased,string"`
NumberTokensSold int64 `json:"numberTokensSold,string"`
TotalVolumeInBNBTokensPurchased float64 `json:"totalVolumeInBNBTokensPurchased,string"`
TotalVolumeInBNBTokensSold float64 `json:"totalVolumeInBNBTokensSold,string"`
TotalFeesCollectedInBNB float64 `json:"totalFeesCollectedInBNB,string"`
BuyTradeHistory []Transaction `json:"buyTradeHistory"`
SellTradeHistory []Transaction `json:"sellTradeHistory"`
AskOrderHistory []AskOrder `json:"askOrderHistory"`
AverageTokenPriceInBNBPurchased float64 `json:"averageTokenPriceInBNBPurchased,string"`
AverageTokenPriceInBNBSold float64 `json:"averageTokenPriceInBNBSold,string"`
}
func (u *User) GetPnl() float64 {
var res float64
for _, transaction := range u.SellTradeHistory {
res += transaction.NetPrice
}
for _, buy := range u.BuyTradeHistory {
buyTime := time.Unix(buy.Timestamp, 0)
for _, sell := range u.SellTradeHistory {
sellTime := time.Unix(sell.Timestamp, 0)
if sell.Nft.TokenID == buy.Nft.TokenID && sellTime.After(buyTime) {
res -= buy.AskPrice
}
}
}
return res
}