2021-05-11 17:21:40 +00:00
|
|
|
package xbalance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-26 15:24:05 +00:00
|
|
|
"encoding/json"
|
2021-05-11 17:21:40 +00:00
|
|
|
"fmt"
|
2021-05-15 16:32:27 +00:00
|
|
|
"sync"
|
2021-05-11 17:21:40 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2021-05-15 16:32:27 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
2021-05-11 17:21:40 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-05-15 16:03:19 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2021-05-11 17:21:40 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-05-15 16:03:19 +00:00
|
|
|
"github.com/slack-go/slack"
|
2021-05-11 17:21:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const ID = "xbalance"
|
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
const stateKey = "state-v1"
|
|
|
|
|
2021-05-11 17:21:40 +00:00
|
|
|
func init() {
|
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type State struct {
|
2021-05-15 16:32:27 +00:00
|
|
|
Asset string `json:"asset"`
|
|
|
|
DailyNumberOfTransfers int `json:"dailyNumberOfTransfers,omitempty"`
|
2021-05-11 17:21:40 +00:00
|
|
|
DailyAmountOfTransfers fixedpoint.Value `json:"dailyAmountOfTransfers,omitempty"`
|
|
|
|
Since int64 `json:"since"`
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
func (s *State) IsOver24Hours() bool {
|
|
|
|
return time.Now().Sub(time.Unix(s.Since, 0)) >= 24*time.Hour
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:59:57 +00:00
|
|
|
func (s *State) PlainText() string {
|
|
|
|
return util.Render(`{{ .Asset }} transfer stats:
|
|
|
|
daily number of transfers: {{ .DailyNumberOfTransfers }}
|
|
|
|
daily amount of transfers {{ .DailyAmountOfTransfers.Float64 }}`, s)
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
func (s *State) SlackAttachment() slack.Attachment {
|
|
|
|
return slack.Attachment{
|
|
|
|
// Pretext: "",
|
|
|
|
// Text: text,
|
2021-05-15 16:51:51 +00:00
|
|
|
Title: s.Asset + " Transfer States",
|
2021-05-15 16:32:27 +00:00
|
|
|
Fields: []slack.AttachmentField{
|
|
|
|
{Title: "Total Number of Transfers", Value: fmt.Sprintf("%d", s.DailyNumberOfTransfers), Short: true},
|
|
|
|
{Title: "Total Amount of Transfers", Value: util.FormatFloat(s.DailyAmountOfTransfers.Float64(), 4), Short: true},
|
|
|
|
},
|
2021-05-15 16:51:12 +00:00
|
|
|
Footer: util.Render("Since {{ . }}", time.Unix(s.Since, 0).Format(time.RFC822)),
|
2021-05-15 16:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) Reset() {
|
2021-06-26 12:09:40 +00:00
|
|
|
var beginningOfTheDay = util.BeginningOfTheDay(time.Now().Local())
|
2021-05-15 16:32:27 +00:00
|
|
|
*s = State{
|
|
|
|
DailyNumberOfTransfers: 0,
|
|
|
|
DailyAmountOfTransfers: 0,
|
|
|
|
Since: beginningOfTheDay.Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:03:19 +00:00
|
|
|
type WithdrawalRequest struct {
|
|
|
|
FromSession string `json:"fromSession"`
|
|
|
|
ToSession string `json:"toSession"`
|
|
|
|
Asset string `json:"asset"`
|
|
|
|
Amount fixedpoint.Value `json:"amount"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WithdrawalRequest) String() string {
|
|
|
|
return fmt.Sprintf("WITHDRAWAL REQUEST: sending %s %s from %s -> %s",
|
|
|
|
util.FormatFloat(r.Amount.Float64(), 4),
|
|
|
|
r.Asset,
|
|
|
|
r.FromSession,
|
|
|
|
r.ToSession,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WithdrawalRequest) PlainText() string {
|
2021-05-15 16:45:08 +00:00
|
|
|
return fmt.Sprintf("Withdrawal request: sending %s %s from %s -> %s",
|
|
|
|
util.FormatFloat(r.Amount.Float64(), 4),
|
|
|
|
r.Asset,
|
|
|
|
r.FromSession,
|
|
|
|
r.ToSession,
|
|
|
|
)
|
2021-05-15 16:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *WithdrawalRequest) SlackAttachment() slack.Attachment {
|
|
|
|
var color = "#DC143C"
|
|
|
|
title := util.Render(`Withdrawal Request {{ .Asset }}`, r)
|
|
|
|
return slack.Attachment{
|
|
|
|
// Pretext: "",
|
|
|
|
// Text: text,
|
|
|
|
Title: title,
|
|
|
|
Color: color,
|
|
|
|
Fields: []slack.AttachmentField{
|
|
|
|
{Title: "Asset", Value: r.Asset, Short: true},
|
|
|
|
{Title: "Amount", Value: util.FormatFloat(r.Amount.Float64(), 4), Short: true},
|
|
|
|
{Title: "From", Value: r.FromSession},
|
|
|
|
{Title: "To", Value: r.ToSession},
|
|
|
|
},
|
2021-05-15 16:51:12 +00:00
|
|
|
Footer: util.Render("Time {{ . }}", time.Now().Format(time.RFC822)),
|
2021-05-15 16:03:19 +00:00
|
|
|
// FooterIcon: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:24:05 +00:00
|
|
|
type Address struct {
|
2021-06-08 17:33:40 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
AddressTag string `json:"addressTag"`
|
|
|
|
Network string `json:"network"`
|
|
|
|
ForeignFee fixedpoint.Value `json:"foreignFee"`
|
2021-05-26 15:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Address) UnmarshalJSON(body []byte) error {
|
|
|
|
var arg interface{}
|
|
|
|
err := json.Unmarshal(body, &arg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch argT := arg.(type) {
|
|
|
|
case string:
|
|
|
|
a.Address = argT
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:37:08 +00:00
|
|
|
type addressTemplate Address
|
|
|
|
return json.Unmarshal(body, (*addressTemplate)(a))
|
2021-05-26 15:24:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 17:21:40 +00:00
|
|
|
type Strategy struct {
|
|
|
|
Notifiability *bbgo.Notifiability
|
2021-05-15 16:32:27 +00:00
|
|
|
*bbgo.Graceful
|
|
|
|
*bbgo.Persistence
|
2021-05-11 17:21:40 +00:00
|
|
|
|
|
|
|
Interval types.Duration `json:"interval"`
|
|
|
|
|
2021-05-26 15:24:05 +00:00
|
|
|
Addresses map[string]Address `json:"addresses"`
|
2021-05-11 17:21:40 +00:00
|
|
|
|
2021-05-15 16:50:15 +00:00
|
|
|
MaxDailyNumberOfTransfer int `json:"maxDailyNumberOfTransfer"`
|
|
|
|
MaxDailyAmountOfTransfer fixedpoint.Value `json:"maxDailyAmountOfTransfer"`
|
2021-05-11 17:21:40 +00:00
|
|
|
|
2021-05-18 00:32:00 +00:00
|
|
|
CheckOnStart bool `json:"checkOnStart"`
|
|
|
|
|
2021-05-11 17:21:40 +00:00
|
|
|
Asset string `json:"asset"`
|
|
|
|
|
|
|
|
// Low is the low balance level for triggering transfer
|
|
|
|
Low fixedpoint.Value `json:"low"`
|
|
|
|
|
|
|
|
// Middle is the middle balance level used for re-fill asset
|
|
|
|
Middle fixedpoint.Value `json:"middle"`
|
|
|
|
|
2021-09-03 06:25:26 +00:00
|
|
|
Verbose bool `json:"verbose"`
|
|
|
|
|
2021-05-11 17:21:40 +00:00
|
|
|
state *State
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:03:19 +00:00
|
|
|
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {}
|
2021-05-11 17:21:40 +00:00
|
|
|
|
|
|
|
func (s *Strategy) checkBalance(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) {
|
2021-09-03 06:25:26 +00:00
|
|
|
if s.Verbose {
|
|
|
|
s.Notifiability.Notify("📝 Checking %s low balance level exchange session...", s.Asset)
|
|
|
|
}
|
|
|
|
|
|
|
|
var total fixedpoint.Value
|
|
|
|
for _, session := range sessions {
|
|
|
|
if b, ok := session.Account.Balance(s.Asset); ok {
|
|
|
|
total += b.Total()
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 17:21:40 +00:00
|
|
|
|
|
|
|
lowLevelSession, lowLevelBalance, err := s.findLowBalanceLevelSession(sessions)
|
|
|
|
if err != nil {
|
2021-09-03 06:25:26 +00:00
|
|
|
s.Notifiability.Notify("Can not find low balance level session: %s", err.Error())
|
|
|
|
log.WithError(err).Errorf("Can not find low balance level session")
|
2021-05-11 17:21:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if lowLevelSession == nil {
|
2021-09-03 06:25:26 +00:00
|
|
|
if s.Verbose {
|
|
|
|
s.Notifiability.Notify("✅ All %s balances are looking good, total value: %f", s.Asset, total.Float64())
|
|
|
|
}
|
2021-05-11 17:21:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:03:28 +00:00
|
|
|
s.Notifiability.Notify("⚠️ Found low level %s balance from session %s: %s", s.Asset, lowLevelSession.Name, lowLevelBalance.String())
|
2021-05-11 17:21:40 +00:00
|
|
|
|
2021-08-16 04:52:12 +00:00
|
|
|
middle := s.Middle
|
|
|
|
if middle == 0 {
|
|
|
|
|
2021-09-03 06:25:26 +00:00
|
|
|
middle = total.DivFloat64(float64(len(sessions))).MulFloat64(0.99)
|
2021-08-16 04:52:12 +00:00
|
|
|
s.Notifiability.Notify("Total value %f %s, setting middle to %f", total.Float64(), s.Asset, middle.Float64())
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredAmount := middle - lowLevelBalance.Available
|
2021-05-11 17:21:40 +00:00
|
|
|
|
2021-08-17 03:04:55 +00:00
|
|
|
s.Notifiability.Notify("Need %f %s to satisfy the middle balance level %f", requiredAmount.Float64(), s.Asset, middle.Float64())
|
2021-05-11 17:21:40 +00:00
|
|
|
|
|
|
|
fromSession, _, err := s.findHighestBalanceLevelSession(sessions, requiredAmount)
|
|
|
|
if err != nil || fromSession == nil {
|
2021-08-19 08:35:16 +00:00
|
|
|
s.Notifiability.Notify("Can not find session with enough balance")
|
2021-05-11 17:21:40 +00:00
|
|
|
log.WithError(err).Errorf("can not find session with enough balance")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
withdrawalService, ok := fromSession.Exchange.(types.ExchangeWithdrawalService)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("exchange %s does not implement withdrawal service, we can not withdrawal", fromSession.ExchangeName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-12 03:16:20 +00:00
|
|
|
if !fromSession.Withdrawal {
|
2021-05-15 16:03:19 +00:00
|
|
|
s.Notifiability.Notify("The withdrawal function exchange session %s is not enabled", fromSession.Name)
|
2021-05-15 02:42:16 +00:00
|
|
|
log.Errorf("The withdrawal function of exchange session %s is not enabled", fromSession.Name)
|
2021-05-12 03:16:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-11 17:21:40 +00:00
|
|
|
toAddress, ok := s.Addresses[lowLevelSession.Name]
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("%s address of session %s not found", s.Asset, lowLevelSession.Name)
|
2021-08-16 04:52:12 +00:00
|
|
|
s.Notifiability.Notify("%s address of session %s not found", s.Asset, lowLevelSession.Name)
|
2021-05-11 17:21:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:33:40 +00:00
|
|
|
if toAddress.ForeignFee > 0 {
|
|
|
|
requiredAmount += toAddress.ForeignFee
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:50:15 +00:00
|
|
|
if s.state != nil {
|
|
|
|
if s.MaxDailyNumberOfTransfer > 0 {
|
|
|
|
if s.state.DailyNumberOfTransfers >= s.MaxDailyNumberOfTransfer {
|
2021-05-15 17:03:28 +00:00
|
|
|
s.Notifiability.Notify("⚠️ Exceeded %s max daily number of transfers %d (current %d), skipping transfer...",
|
2021-05-15 16:50:15 +00:00
|
|
|
s.Asset,
|
|
|
|
s.MaxDailyNumberOfTransfer,
|
|
|
|
s.state.DailyNumberOfTransfers)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.MaxDailyAmountOfTransfer > 0 {
|
|
|
|
if s.state.DailyAmountOfTransfers >= s.MaxDailyAmountOfTransfer {
|
2021-05-15 17:03:28 +00:00
|
|
|
s.Notifiability.Notify("⚠️ Exceeded %s max daily amount of transfers %f (current %f), skipping transfer...",
|
2021-05-15 16:50:15 +00:00
|
|
|
s.Asset,
|
|
|
|
s.MaxDailyAmountOfTransfer.Float64(),
|
|
|
|
s.state.DailyAmountOfTransfers.Float64())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:03:19 +00:00
|
|
|
s.Notifiability.Notify(&WithdrawalRequest{
|
|
|
|
FromSession: fromSession.Name,
|
|
|
|
ToSession: lowLevelSession.Name,
|
|
|
|
Asset: s.Asset,
|
|
|
|
Amount: requiredAmount,
|
|
|
|
})
|
|
|
|
|
2021-05-26 15:24:05 +00:00
|
|
|
if err := withdrawalService.Withdrawal(ctx, s.Asset, requiredAmount, toAddress.Address, &types.WithdrawalOptions{
|
|
|
|
Network: toAddress.Network,
|
|
|
|
AddressTag: toAddress.AddressTag,
|
|
|
|
}); err != nil {
|
2021-05-11 17:21:40 +00:00
|
|
|
log.WithError(err).Errorf("withdrawal failed")
|
2021-05-15 02:42:16 +00:00
|
|
|
s.Notifiability.Notify("withdrawal request failed, error: %v", err)
|
|
|
|
return
|
2021-05-11 17:21:40 +00:00
|
|
|
}
|
2021-05-15 02:42:16 +00:00
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
s.Notifiability.Notify("%s withdrawal request sent", s.Asset)
|
|
|
|
|
|
|
|
if s.state != nil {
|
|
|
|
if s.state.IsOver24Hours() {
|
|
|
|
s.state.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.state.DailyNumberOfTransfers += 1
|
|
|
|
s.state.DailyAmountOfTransfers += requiredAmount
|
|
|
|
s.SaveState()
|
|
|
|
}
|
2021-05-11 17:21:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) findHighestBalanceLevelSession(sessions map[string]*bbgo.ExchangeSession, requiredAmount fixedpoint.Value) (*bbgo.ExchangeSession, types.Balance, error) {
|
|
|
|
var balance types.Balance
|
|
|
|
var maxBalanceLevel fixedpoint.Value = 0
|
|
|
|
var maxBalanceSession *bbgo.ExchangeSession = nil
|
|
|
|
for sessionID := range s.Addresses {
|
|
|
|
session, ok := sessions[sessionID]
|
|
|
|
if !ok {
|
|
|
|
return nil, balance, fmt.Errorf("session %s does not exist", sessionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b, ok := session.Account.Balance(s.Asset); ok {
|
|
|
|
if b.Available-requiredAmount > s.Low && b.Available > maxBalanceLevel {
|
|
|
|
maxBalanceLevel = b.Available
|
|
|
|
maxBalanceSession = session
|
|
|
|
balance = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return maxBalanceSession, balance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) findLowBalanceLevelSession(sessions map[string]*bbgo.ExchangeSession) (*bbgo.ExchangeSession, types.Balance, error) {
|
|
|
|
var balance types.Balance
|
|
|
|
for sessionID := range s.Addresses {
|
|
|
|
session, ok := sessions[sessionID]
|
|
|
|
if !ok {
|
|
|
|
return nil, balance, fmt.Errorf("session %s does not exist", sessionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
balance, ok = session.Account.Balance(s.Asset)
|
|
|
|
if ok {
|
|
|
|
if balance.Available <= s.Low {
|
|
|
|
return session, balance, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, balance, nil
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
func (s *Strategy) SaveState() {
|
|
|
|
if err := s.Persistence.Save(s.state, ID, s.Asset, stateKey); err != nil {
|
|
|
|
log.WithError(err).Errorf("can not save state: %+v", s.state)
|
|
|
|
} else {
|
|
|
|
log.Infof("%s %s state is saved: %+v", ID, s.Asset, s.state)
|
2021-05-15 16:59:57 +00:00
|
|
|
s.Notifiability.Notify("%s %s state is saved", ID, s.Asset, s.state)
|
2021-05-15 16:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) newDefaultState() *State {
|
|
|
|
return &State{
|
|
|
|
Asset: s.Asset,
|
|
|
|
DailyNumberOfTransfers: 0,
|
|
|
|
DailyAmountOfTransfers: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) LoadState() error {
|
|
|
|
var state State
|
|
|
|
if err := s.Persistence.Load(&state, ID, s.Asset, stateKey); err != nil {
|
|
|
|
if err != service.ErrPersistenceNotExists {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.state = s.newDefaultState()
|
|
|
|
s.state.Reset()
|
|
|
|
} else {
|
|
|
|
// we loaded it successfully
|
|
|
|
s.state = &state
|
|
|
|
|
2021-05-15 17:22:55 +00:00
|
|
|
// update Asset name for legacy caches
|
|
|
|
s.state.Asset = s.Asset
|
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
log.Infof("%s %s state is restored: %+v", ID, s.Asset, s.state)
|
2021-05-15 16:52:53 +00:00
|
|
|
s.Notifiability.Notify("%s %s state is restored", ID, s.Asset, s.state)
|
2021-05-15 16:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-11 17:21:40 +00:00
|
|
|
func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error {
|
|
|
|
if s.Interval == 0 {
|
|
|
|
return errors.New("interval can not be zero")
|
|
|
|
}
|
|
|
|
|
2021-05-15 16:32:27 +00:00
|
|
|
if err := s.LoadState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Graceful.OnShutdown(func(ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
s.SaveState()
|
|
|
|
})
|
|
|
|
|
2021-05-18 00:32:00 +00:00
|
|
|
if s.CheckOnStart {
|
|
|
|
s.checkBalance(ctx, sessions)
|
|
|
|
}
|
2021-05-11 17:21:40 +00:00
|
|
|
|
|
|
|
go func() {
|
2021-05-30 10:06:31 +00:00
|
|
|
ticker := time.NewTicker(util.MillisecondsJitter(s.Interval.Duration(), 1000))
|
2021-05-11 17:21:40 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
s.checkBalance(ctx, sessions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|