v2utils

package
v0.0.0-...-7ece11e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2023 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	TradeDirectionSell0 = iota
	TradeDirectionSell1
)

Variables

This section is empty.

Functions

func BuildAcceptUnstakingInstructions

func BuildAcceptUnstakingInstructions(
	stakingPoolID, nftID common.Hash,
	unstakingAmount uint64,
	otaReceiverNft, otaReceiverUnstakingToken string,
	txReqID common.Hash, shardID byte,
) ([][]string, error)

func BuildAcceptWithdrawLiquidityInstructions

func BuildAcceptWithdrawLiquidityInstructions(
	metaData metadataPdexv3.WithdrawLiquidityRequest,
	token0ID, token1ID common.Hash,
	token0Amount, token1Amount, shareAmount uint64,
	txReqID common.Hash, shardID byte,
) ([][]string, error)

func BuildDistributeMiningOrderRewardInsts

func BuildDistributeMiningOrderRewardInsts(
	pairID string,
	makingTokenID common.Hash,
	mintingAmount uint64,
	mintingTokenID common.Hash,
) [][]string

func BuildDistributeStakingRewardInst

func BuildDistributeStakingRewardInst(
	stakingToken string,
	rewards map[common.Hash]uint64,
) [][]string

func BuildMatchAddLiquidityInstructions

func BuildMatchAddLiquidityInstructions(
	incomingContributionState statedb.Pdexv3ContributionState,
	poolPairID string, nftID common.Hash,
) ([][]string, error)

func BuildMatchAndReturnAddLiquidityInstructions

func BuildMatchAndReturnAddLiquidityInstructions(
	token0ContributionState, token1ContributionState statedb.Pdexv3ContributionState,
	shareAmount, returnedToken0ContributionAmount, actualToken0ContributionAmount,
	returnedToken1ContributionAmount, actualToken1ContributionAmount uint64,
	nftID common.Hash,
) ([][]string, error)

func BuildMintBlockRewardInst

func BuildMintBlockRewardInst(
	pairID string,
	mintingAmount uint64,
	mintingTokenID common.Hash,
) [][]string

func BuildModifyParamsInst

func BuildModifyParamsInst(
	params metadataPdexv3.Pdexv3Params,
	errorMsg string,
	shardID byte,
	reqTxID common.Hash,
	status string,
) []string

func BuildRefundAddLiquidityInstructions

func BuildRefundAddLiquidityInstructions(
	waitingContributionState, incomingContributionState statedb.Pdexv3ContributionState,
) ([][]string, error)

func BuildRejectUnstakingInstructions

func BuildRejectUnstakingInstructions(
	metaData metadataPdexv3.UnstakingRequest,
	txReqID common.Hash, shardID byte,
) ([][]string, error)

func BuildRejectWithdrawLiquidityInstructions

func BuildRejectWithdrawLiquidityInstructions(
	metaData metadataPdexv3.WithdrawLiquidityRequest,
	txReqID common.Hash, shardID byte,
) ([][]string, error)

func BuildWithdrawLPFeeInsts

func BuildWithdrawLPFeeInsts(
	pairID string,
	nftID common.Hash,
	receivers map[common.Hash]metadataPdexv3.ReceiverInfo,
	shardID byte,
	reqTxID common.Hash,
	status string,
) [][]string

func BuildWithdrawProtocolFeeInsts

func BuildWithdrawProtocolFeeInsts(
	pairID string,
	address string,
	amounts map[common.Hash]uint64,
	shardID byte,
	reqTxID common.Hash,
	status string,
) [][]string

func BuildWithdrawStakingRewardInsts

func BuildWithdrawStakingRewardInsts(
	stakingPoolID string,
	nftID common.Hash,
	receivers map[common.Hash]metadataPdexv3.ReceiverInfo,
	shardID byte,
	reqTxID common.Hash,
	status string,
) [][]string

func GetMakingVolumes

func GetMakingVolumes(
	pairChange [2]*big.Int, orderChange map[string][2]*big.Int,
	nftIDs map[string]string,
) (*big.Int, map[string]*big.Int, byte)

func GetPDEXRewardsForBlock

func GetPDEXRewardsForBlock(
	beaconHeight uint64,
	mintingBlocks int, decayIntervals int, pdexRewardFirstInterval uint64,
	decayRateBPS int, bps int,
) uint64

func HasInsufficientLiquidity

func HasInsufficientLiquidity(poolPair rawdbv2.Pdexv3PoolPair) bool

func MaybeAcceptTrade

func MaybeAcceptTrade(amountIn, fee uint64, tradePath []string, receiver privacy.OTAReceiver,
	reserves []*rawdbv2.Pdexv3PoolPair, lpFeesPerShares []map[common.Hash]*big.Int,
	protocolFees, stakingPoolFees []map[common.Hash]uint64,
	tradeDirections []byte,
	tokenToBuy common.Hash, minAmount uint64, orderbooks []OrderBookIterator,
) (*metadataPdexv3.AcceptedTrade, []*rawdbv2.Pdexv3PoolPair, error)

MaybeAcceptTrade() performs a trade determined by input amount, path, directions & order book state. Upon success, state changes are applied in memory & collected in an instruction. A returned error means the trade is refunded

func MintPDEXGenesis

func MintPDEXGenesis(
	instStatus string,
	contentStr string,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func OrderRefundTx

func OrderRefundTx(
	acn instructionPdexv3.Action,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func SplitOrderRewardLiquidityMining

func SplitOrderRewardLiquidityMining(
	volume map[string]*big.Int, amount *big.Int, tokenID common.Hash,
) map[string]uint64

func SplitTradingReward

func SplitTradingReward(
	reward *big.Int, ratio uint, bps uint,
	ammMakingAmt *big.Int, orderMakingAmts map[string]*big.Int,
) (uint64, map[string]uint64)

func TrackFee

func TrackFee(
	fee uint64, feeInPRV bool, sellingTokenID common.Hash, baseLPPerShare *big.Int, bps uint,
	tradePath []string, reserves []*rawdbv2.Pdexv3PoolPair,
	lpFeesPerShares []map[common.Hash]*big.Int, protocolFees, stakingPoolFees []map[common.Hash]uint64,
	tradeDirections []byte, orderbooks []OrderBookIterator,
	poolFees []uint, feeRateBPS uint,
	acceptedMeta *metadataPdexv3.AcceptedTrade,
	protocolFeePercent, stakingPoolRewardPercent uint, stakingRewardTokens []common.Hash,
	defaultOrderTradingRewardRatioBPS uint, orderTradingRewardRatioBPS map[string]uint,
) (*metadataPdexv3.AcceptedTrade, []map[string]map[common.Hash]uint64, []map[common.Hash]map[string]*big.Int, error)

func TradeAcceptTx

func TradeAcceptTx(
	acn instructionPdexv3.Action,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func TradeRefundTx

func TradeRefundTx(
	acn instructionPdexv3.Action,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func WithdrawLPFee

func WithdrawLPFee(
	instStatus string,
	contentStr string,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func WithdrawOrderAcceptTx

func WithdrawOrderAcceptTx(
	acn instructionPdexv3.Action,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func WithdrawProtocolFee

func WithdrawProtocolFee(
	instStatus string,
	contentStr string,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

func WithdrawStakingReward

func WithdrawStakingReward(
	instStatus string,
	contentStr string,
	producerPrivateKey *privacy.PrivateKey,
	shardID byte,
	transactionStateDB *statedb.StateDB,
) (metadataCommon.Transaction, error)

Types

type ContributionStatus

type ContributionStatus struct {
	Status                  byte   `json:"Status"`
	Token0ID                string `json:"Token0ID"`
	Token0ContributedAmount uint64 `json:"Token0ContributedAmount"`
	Token0ReturnedAmount    uint64 `json:"Token0ReturnedAmount"`
	Token1ID                string `json:"Token1ID"`
	Token1ContributedAmount uint64 `json:"Token1ContributedAmount"`
	Token1ReturnedAmount    uint64 `json:"Token1ReturnedAmount"`
	PoolPairID              string `json:"PoolPairID"`
}

type DifMapHashBigInt

type DifMapHashBigInt map[common.Hash]*big.Int

func (DifMapHashBigInt) GetDiff

func (dm DifMapHashBigInt) GetDiff(compareWith DifMapHashBigInt) map[string]bool

type DifMapHashUint64

type DifMapHashUint64 map[common.Hash]uint64

func (DifMapHashUint64) GetDiff

func (dm DifMapHashUint64) GetDiff(compareWith DifMapHashUint64) map[string]bool

type DifMapStringBigInt

type DifMapStringBigInt map[string]*big.Int

func (DifMapStringBigInt) GetDiff

func (dm DifMapStringBigInt) GetDiff(compareWith DifMapStringBigInt) map[string]bool

type DifMapStringMapUint64Uint64

type DifMapStringMapUint64Uint64 map[string]map[uint64]uint64

func (DifMapStringMapUint64Uint64) GetDiff

type MakingVolumeChange

type MakingVolumeChange struct {
	Volume map[string]bool
}

func NewMakingVolumeChange

func NewMakingVolumeChange() *MakingVolumeChange

type MatchingOrder

type MatchingOrder struct {
	*rawdbv2.Pdexv3Order
}

func (*MatchingOrder) ApplyBalanceChanges

func (order *MatchingOrder) ApplyBalanceChanges(change0, change1 *big.Int) error

func (MatchingOrder) BuyAmountFromOrder

func (order MatchingOrder) BuyAmountFromOrder(incomingTradeSellAmount uint64, incomingTradeDirection byte) (uint64, error)

BuyAmountFromOrder() computes the theoretical (by rate) output amount given incoming trade's sell amount. Order balance only needs to be non-zero since it can be partially matched

func (*MatchingOrder) CanMatch

func (order *MatchingOrder) CanMatch(incomingTradeDirection byte) (bool, error)

CanMatch() returns true if - incoming trade is of opposite direction (sell0 vs sell1) - outstanding order balance is not too small (would exchange for at least 1 unit of TokenBuy using this order's own rate)

func (*MatchingOrder) Match

func (order *MatchingOrder) Match(maxSellAmountAfterFee uint64, tradeDirection byte) (uint64, uint64, *big.Int, *big.Int, error)

Match() swaps this order's selling balance with token sold in a trade, following this order's rate

func (*MatchingOrder) MatchPoolAmount

func (order *MatchingOrder) MatchPoolAmount(maxSellAmountAfterFee uint64, tradeDirection byte, pr TradingPair) (uint64, uint64, *big.Int, *big.Int, error)

MatchPoolAmount() uses pool values instead of this order's rate

func (MatchingOrder) SellAmountToOrder

func (order MatchingOrder) SellAmountToOrder(incomingTradeBuyAmount uint64, incomingTradeDirection byte) (uint64, error)

func (*MatchingOrder) UnmarshalJSON

func (order *MatchingOrder) UnmarshalJSON(data []byte) error

type MintNftStatus

type MintNftStatus struct {
	NftID       string `json:"NftID"`
	Status      byte   `json:"Status"`
	BurntAmount uint64 `json:"BurntAmount"`
}

type OrderBookIterator

type OrderBookIterator interface {
	NextOrder(tradeDirection byte) (*MatchingOrder, string, error)
	NftIDs() map[string]string
}

type OrderRewardChange

type OrderRewardChange struct {
	UncollectedReward map[string]bool
}

func NewOrderRewardChange

func NewOrderRewardChange() *OrderRewardChange

type PoolPairChange

type PoolPairChange struct {
	IsChanged         bool
	Shares            map[string]*ShareChange
	OrderIDs          map[string]bool
	LpFeesPerShare    map[string]bool
	ProtocolFees      map[string]bool
	StakingPoolFees   map[string]bool
	MakingVolume      map[string]*MakingVolumeChange
	OrderRewards      map[string]*OrderRewardChange
	LmRewardsPerShare map[string]bool
	LmLockedShare     map[string]map[uint64]bool
}

func NewPoolPairChange

func NewPoolPairChange() *PoolPairChange

type ShareChange

type ShareChange struct {
	IsChanged             bool
	TradingFees           map[string]bool
	LastLPFeesPerShare    map[string]bool
	LastLmRewardsPerShare map[string]bool
}

func NewShareChange

func NewShareChange() *ShareChange

type StakerChange

type StakerChange struct {
	IsChanged           bool
	Rewards             map[string]bool
	LastRewardsPerShare map[string]bool
}

func NewStakerChange

func NewStakerChange() *StakerChange

type StakingPoolChange

type StakingPoolChange struct {
	Stakers         map[string]*StakerChange
	RewardsPerShare map[string]bool
}

func NewStakingChange

func NewStakingChange() *StakingPoolChange

type StakingStatus

type StakingStatus struct {
	Status        byte   `json:"Status"`
	NftID         string `json:"NftID"`
	StakingPoolID string `json:"StakingPoolID"`
	Liquidity     uint64 `json:"Liquidity"`
}

type StateChange

type StateChange struct {
	PoolPairs    map[string]*PoolPairChange
	StakingPools map[string]*StakingPoolChange
}

func NewStateChange

func NewStateChange() *StateChange

type TradingPair

type TradingPair struct {
	*rawdbv2.Pdexv3PoolPair
}

func NewTradingPair

func NewTradingPair() *TradingPair

func NewTradingPairWithValue

func NewTradingPairWithValue(
	reserve *rawdbv2.Pdexv3PoolPair,
) *TradingPair

func (*TradingPair) AddLMRewards

func (tp *TradingPair) AddLMRewards(
	tokenID common.Hash, amount *big.Int, baseLPPerShare *big.Int,
	rootLmRewardsPerShare map[common.Hash]*big.Int,
) map[common.Hash]*big.Int

func (*TradingPair) AddLPFee

func (tp *TradingPair) AddLPFee(
	tokenID common.Hash, amount *big.Int, baseLPPerShare *big.Int,
	rootLpFeesPerShare map[common.Hash]*big.Int,
) map[common.Hash]*big.Int

func (*TradingPair) AddStakingAndProtocolFee

func (tp *TradingPair) AddStakingAndProtocolFee(
	tokenID common.Hash, amount *big.Int,
	rootProtocolFees, rootStakingPoolFees map[common.Hash]uint64,
	protocolFeePercent, stakingPoolRewardPercent uint, stakingRewardTokens []common.Hash,
) (map[common.Hash]uint64, map[common.Hash]uint64, *big.Int)

func (TradingPair) AmountToSell

func (tp TradingPair) AmountToSell(buyAmount uint64, tradeDirection byte) (uint64, error)

BuyAmount() computes the input amount given output, based on reserve amounts

func (*TradingPair) ApplyReserveChanges

func (tp *TradingPair) ApplyReserveChanges(change0, change1 *big.Int) error

func (TradingPair) BuyAmount

func (tp TradingPair) BuyAmount(sellAmount uint64, tradeDirection byte) (uint64, error)

BuyAmount() computes the output amount given input, based on reserve amounts. Deduct fees before calling this

func (*TradingPair) SwapToReachOrderRate

func (tp *TradingPair) SwapToReachOrderRate(maxSellAmountAfterFee uint64, tradeDirection byte, ord *MatchingOrder) (uint64, uint64, *big.Int, *big.Int, error)

SwapToReachOrderRate() does a *partial* swap using liquidity in the pool, such that the price afterwards does not exceed an order's rate It returns an error when the pool runs out of liquidity Upon success, it updates the reserve values and returns (buyAmount, sellAmountRemain, token0Change, token1Change)

func (*TradingPair) UnmarshalJSON

func (tp *TradingPair) UnmarshalJSON(data []byte) error

type UnstakingStatus

type UnstakingStatus struct {
	Status        byte   `json:"Status"`
	NftID         string `json:"NftID"`
	StakingPoolID string `json:"StakingPoolID"`
	Liquidity     uint64 `json:"Liquidity"`
}

type WithdrawStatus

type WithdrawStatus struct {
	Status       byte   `json:"Status"`
	Token0ID     string `json:"Token0ID"`
	Token0Amount uint64 `json:"Token0Amount"`
	Token1ID     string `json:"Token1ID"`
	Token1Amount uint64 `json:"Token1Amount"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL