2023-03-25 17:32:47 +00:00
|
|
|
package xfunding
|
2023-03-25 17:54:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-26 06:42:13 +00:00
|
|
|
"time"
|
2023-03-25 17:54:39 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FundingFee struct {
|
|
|
|
Asset string `json:"asset"`
|
|
|
|
Amount fixedpoint.Value `json:"amount"`
|
2023-03-25 18:12:00 +00:00
|
|
|
Txn int64 `json:"txn"`
|
2023-03-26 06:42:13 +00:00
|
|
|
Time time.Time `json:"time"`
|
2023-03-25 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProfitStats struct {
|
|
|
|
*types.ProfitStats
|
|
|
|
|
|
|
|
FundingFeeCurrency string `json:"fundingFeeCurrency"`
|
|
|
|
TotalFundingFee fixedpoint.Value `json:"totalFundingFee"`
|
|
|
|
FundingFeeRecords []FundingFee `json:"fundingFeeRecords"`
|
2023-03-25 18:12:00 +00:00
|
|
|
LastFundingFeeTxn int64 `json:"lastFundingFeeTxn"`
|
2023-03-26 06:42:13 +00:00
|
|
|
LastFundingFeeTime time.Time `json:"lastFundingFeeTime"`
|
2023-03-25 17:54:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProfitStats) AddFundingFee(fee FundingFee) error {
|
|
|
|
if s.FundingFeeCurrency == "" {
|
|
|
|
s.FundingFeeCurrency = fee.Asset
|
|
|
|
} else if s.FundingFeeCurrency != fee.Asset {
|
|
|
|
return fmt.Errorf("unexpected error, funding fee currency is not matched, given: %s, wanted: %s", fee.Asset, s.FundingFeeCurrency)
|
|
|
|
}
|
2023-03-25 18:12:00 +00:00
|
|
|
|
2023-03-25 18:13:22 +00:00
|
|
|
if s.LastFundingFeeTxn == fee.Txn {
|
|
|
|
return errDuplicatedFundingFeeTxnId
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:12:00 +00:00
|
|
|
s.FundingFeeRecords = append(s.FundingFeeRecords, fee)
|
|
|
|
s.TotalFundingFee = s.TotalFundingFee.Add(fee.Amount)
|
|
|
|
s.LastFundingFeeTxn = fee.Txn
|
2023-03-26 06:42:13 +00:00
|
|
|
s.LastFundingFeeTime = fee.Time
|
2023-03-25 17:54:39 +00:00
|
|
|
return nil
|
|
|
|
}
|