code.vegaprotocol.io/vega@v0.79.0/core/types/transfer.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package types
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	"code.vegaprotocol.io/vega/libs/stringer"
    24  	proto "code.vegaprotocol.io/vega/protos/vega"
    25  )
    26  
    27  type FinancialAmount struct {
    28  	Asset  string
    29  	Amount *num.Uint
    30  }
    31  
    32  func (f *FinancialAmount) Clone() *FinancialAmount {
    33  	cpy := *f
    34  	cpy.Amount = f.Amount.Clone()
    35  	return &cpy
    36  }
    37  
    38  type Transfer struct {
    39  	Owner      string
    40  	Amount     *FinancialAmount
    41  	Type       TransferType
    42  	MinAmount  *num.Uint
    43  	Market     string
    44  	TransferID *string
    45  }
    46  
    47  func (t *Transfer) Clone() *Transfer {
    48  	cpy := *t
    49  	cpy.Amount = t.Amount.Clone()
    50  	cpy.MinAmount = t.MinAmount.Clone()
    51  	return &cpy
    52  }
    53  
    54  // Merge creates a new Transfer.
    55  func (t *Transfer) Merge(oth *Transfer) *Transfer {
    56  	if t.Owner != oth.Owner {
    57  		panic(fmt.Sprintf("invalid transfer merge, different owner specified, this should never happen: %v, %v", t.String(), oth.String()))
    58  	}
    59  
    60  	if t.Amount.Asset != oth.Amount.Asset {
    61  		panic(fmt.Sprintf("invalid transfer merge, different assets specified, this should never happen: %v, %v", t.String(), oth.String()))
    62  	}
    63  
    64  	if t.Type != oth.Type {
    65  		panic(fmt.Sprintf("invalid transfer merge, different types specified, this should never happen: %v, %v", t.String(), oth.String()))
    66  	}
    67  
    68  	if t.Market != oth.Market {
    69  		panic(fmt.Sprintf("invalid transfer merge, different markets specified, this should never happen: %v, %v", t.String(), oth.String()))
    70  	}
    71  
    72  	return &Transfer{
    73  		Owner: t.Owner,
    74  		Amount: &FinancialAmount{
    75  			Asset:  t.Amount.Asset,
    76  			Amount: num.Sum(t.Amount.Amount, oth.Amount.Amount),
    77  		},
    78  		Type:       t.Type,
    79  		MinAmount:  num.Sum(t.MinAmount, t.MinAmount),
    80  		Market:     t.Market,
    81  		TransferID: t.TransferID,
    82  	}
    83  }
    84  
    85  func (f FinancialAmount) String() string {
    86  	return fmt.Sprintf(
    87  		"asset(%s) amount(%s)",
    88  		f.Asset,
    89  		stringer.PtrToString(f.Amount),
    90  	)
    91  }
    92  
    93  func (f *FinancialAmount) IntoProto() *proto.FinancialAmount {
    94  	return &proto.FinancialAmount{
    95  		Asset:  f.Asset,
    96  		Amount: num.UintToString(f.Amount),
    97  	}
    98  }
    99  
   100  func FinancialAmountFromProto(p *proto.FinancialAmount) (*FinancialAmount, error) {
   101  	amount, overflow := num.UintFromString(p.Amount, 10)
   102  	if overflow {
   103  		return nil, errors.New("invalid amount")
   104  	}
   105  
   106  	return &FinancialAmount{
   107  		Asset:  p.Asset,
   108  		Amount: amount,
   109  	}, nil
   110  }
   111  
   112  func (t *Transfer) IntoProto() *proto.Transfer {
   113  	return &proto.Transfer{
   114  		Owner:     t.Owner,
   115  		Amount:    t.Amount.IntoProto(),
   116  		Type:      t.Type,
   117  		MinAmount: num.UintToString(t.MinAmount),
   118  		MarketId:  t.Market,
   119  	}
   120  }
   121  
   122  func TransferFromProto(p *proto.Transfer) (*Transfer, error) {
   123  	amount, err := FinancialAmountFromProto(p.Amount)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	minAmount, overflow := num.UintFromString(p.MinAmount, 10)
   129  	if overflow {
   130  		return nil, errors.New("invalid min amount")
   131  	}
   132  
   133  	return &Transfer{
   134  		Owner:     p.Owner,
   135  		Amount:    amount,
   136  		Type:      p.Type,
   137  		MinAmount: minAmount,
   138  		Market:    p.MarketId,
   139  	}, nil
   140  }
   141  
   142  func (t *Transfer) String() string {
   143  	return fmt.Sprintf(
   144  		"owner(%s) amount(%s) type(%s) minAmount(%s)",
   145  		t.Owner,
   146  		stringer.PtrToString(t.Amount),
   147  		t.Type.String(),
   148  		stringer.PtrToString(t.MinAmount),
   149  	)
   150  }
   151  
   152  type TransferType = proto.TransferType
   153  
   154  const (
   155  	// Default value, always invalid.
   156  	TransferTypeUnspecified TransferType = proto.TransferType_TRANSFER_TYPE_UNSPECIFIED
   157  	// Loss.
   158  	TransferTypeLoss TransferType = proto.TransferType_TRANSFER_TYPE_LOSS
   159  	// Win.
   160  	TransferTypeWin TransferType = proto.TransferType_TRANSFER_TYPE_WIN
   161  	// Mark to market loss.
   162  	TransferTypeMTMLoss TransferType = proto.TransferType_TRANSFER_TYPE_MTM_LOSS
   163  	// Mark to market win.
   164  	TransferTypeMTMWin TransferType = proto.TransferType_TRANSFER_TYPE_MTM_WIN
   165  	// Margin too low.
   166  	TransferTypeMarginLow TransferType = proto.TransferType_TRANSFER_TYPE_MARGIN_LOW
   167  	// Margin too high.
   168  	TransferTypeMarginHigh TransferType = proto.TransferType_TRANSFER_TYPE_MARGIN_HIGH
   169  	// Margin was confiscated.
   170  	TransferTypeMarginConfiscated TransferType = proto.TransferType_TRANSFER_TYPE_MARGIN_CONFISCATED
   171  	// Pay maker fee.
   172  	TransferTypeMakerFeePay TransferType = proto.TransferType_TRANSFER_TYPE_MAKER_FEE_PAY
   173  	// Receive maker fee.
   174  	TransferTypeMakerFeeReceive TransferType = proto.TransferType_TRANSFER_TYPE_MAKER_FEE_RECEIVE
   175  	// Pay infrastructure fee.
   176  	TransferTypeInfrastructureFeePay TransferType = proto.TransferType_TRANSFER_TYPE_INFRASTRUCTURE_FEE_PAY
   177  	// Receive infrastructure fee.
   178  	TransferTypeInfrastructureFeeDistribute TransferType = proto.TransferType_TRANSFER_TYPE_INFRASTRUCTURE_FEE_DISTRIBUTE
   179  	// Pay liquidity fee.
   180  	TransferTypeLiquidityFeePay TransferType = proto.TransferType_TRANSFER_TYPE_LIQUIDITY_FEE_PAY
   181  	// Receive liquidity fee.
   182  	TransferTypeLiquidityFeeDistribute TransferType = proto.TransferType_TRANSFER_TYPE_LIQUIDITY_FEE_DISTRIBUTE
   183  	// Bond too low.
   184  	TransferTypeBondLow TransferType = proto.TransferType_TRANSFER_TYPE_BOND_LOW
   185  	// Bond too high.
   186  	TransferTypeBondHigh TransferType = proto.TransferType_TRANSFER_TYPE_BOND_HIGH
   187  	// Actual withdraw from system.
   188  	TransferTypeWithdraw TransferType = proto.TransferType_TRANSFER_TYPE_WITHDRAW
   189  	// Deposit funds.
   190  	TransferTypeDeposit TransferType = proto.TransferType_TRANSFER_TYPE_DEPOSIT
   191  	// Bond slashing.
   192  	TransferTypeBondSlashing TransferType = proto.TransferType_TRANSFER_TYPE_BOND_SLASHING
   193  	// Reward payout.
   194  	TransferTypeRewardPayout               TransferType = proto.TransferType_TRANSFER_TYPE_REWARD_PAYOUT
   195  	TransferTypeTransferFundsSend          TransferType = proto.TransferType_TRANSFER_TYPE_TRANSFER_FUNDS_SEND
   196  	TransferTypeTransferFundsDistribute    TransferType = proto.TransferType_TRANSFER_TYPE_TRANSFER_FUNDS_DISTRIBUTE
   197  	TransferTypeClearAccount               TransferType = proto.TransferType_TRANSFER_TYPE_CLEAR_ACCOUNT
   198  	TransferTypeCheckpointBalanceRestore   TransferType = proto.TransferType_TRANSFER_TYPE_CHECKPOINT_BALANCE_RESTORE
   199  	TransferTypeSuccessorInsuranceFraction TransferType = proto.TransferType_TRANSFER_TYPE_SUCCESSOR_INSURANCE_FRACTION
   200  	TransferTypeSpot                       TransferType = proto.TransferType_TRANSFER_TYPE_SPOT
   201  	TransferTypeHoldingAccount             TransferType = proto.TransferType_TRANSFER_TYPE_HOLDING_LOCK
   202  	TransferTypeReleaseHoldingAccount      TransferType = proto.TransferType_TRANSFER_TYPE_HOLDING_RELEASE
   203  	// Liquidity fees.
   204  	TransferTypeLiquidityFeeAllocate          TransferType = proto.TransferType_TRANSFER_TYPE_LIQUIDITY_FEE_ALLOCATE
   205  	TransferTypeLiquidityFeeNetDistribute     TransferType = proto.TransferType_TRANSFER_TYPE_LIQUIDITY_FEE_NET_DISTRIBUTE
   206  	TransferTypeSLAPenaltyBondApply           TransferType = proto.TransferType_TRANSFER_TYPE_SLA_PENALTY_BOND_APPLY
   207  	TransferTypeSLAPenaltyLpFeeApply          TransferType = proto.TransferType_TRANSFER_TYPE_SLA_PENALTY_LP_FEE_APPLY
   208  	TransferTypeLiquidityFeeUnpaidCollect     TransferType = proto.TransferType_TRANSFER_TYPE_LIQUIDITY_FEE_UNPAID_COLLECT
   209  	TransferTypeSlaPerformanceBonusDistribute TransferType = proto.TransferType_TRANSFER_TYPE_SLA_PERFORMANCE_BONUS_DISTRIBUTE
   210  	// perps funding.
   211  	TransferTypePerpFundingLoss TransferType = proto.TransferType_TRANSFER_TYPE_PERPETUALS_FUNDING_LOSS
   212  	TransferTypePerpFundingWin  TransferType = proto.TransferType_TRANSFER_TYPE_PERPETUALS_FUNDING_WIN
   213  	TransferTypeRewardsVested   TransferType = proto.TransferType_TRANSFER_TYPE_REWARDS_VESTED
   214  
   215  	TransferTypeFeeReferrerRewardPay        TransferType = proto.TransferType_TRANSFER_TYPE_FEE_REFERRER_REWARD_PAY
   216  	TransferTypeFeeReferrerRewardDistribute TransferType = proto.TransferType_TRANSFER_TYPE_FEE_REFERRER_REWARD_DISTRIBUTE
   217  
   218  	TransferTypeOrderMarginLow    TransferType = proto.TransferType_TRANSFER_TYPE_ORDER_MARGIN_LOW
   219  	TransferTypeOrderMarginHigh   TransferType = proto.TransferType_TRANSFER_TYPE_ORDER_MARGIN_HIGH
   220  	TransferTypeIsolatedMarginLow TransferType = proto.TransferType_TRANSFER_TYPE_ISOLATED_MARGIN_LOW
   221  	// AMM account juggling.
   222  	TransferTypeAMMLow     TransferType = proto.TransferType_TRANSFER_TYPE_AMM_LOW
   223  	TransferTypeAMMHigh    TransferType = proto.TransferType_TRANSFER_TYPE_AMM_HIGH
   224  	TransferTypeAMMRelease TransferType = proto.TransferType_TRANSFER_TYPE_AMM_RELEASE
   225  	// additional fees transfers.
   226  	TransferTypeBuyBackFeePay TransferType = proto.TransferType_TRANSFER_TYPE_BUY_BACK_FEE_PAY
   227  	TransferTypeTreasuryPay   TransferType = proto.TransferType_TRANSFER_TYPE_TREASURY_FEE_PAY
   228  	// Pay high maker fee.
   229  	TransferTypeHighMakerRebatePay TransferType = proto.TransferType_TRANSFER_TYPE_HIGH_MAKER_FEE_REBATE_PAY
   230  	// Receive high maker rebate.
   231  	TransferTypeHighMakerRebateReceive TransferType = proto.TransferType_TRANSFER_TYPE_HIGH_MAKER_FEE_REBATE_RECEIVE
   232  )