rfqmm

package
v0.0.0-...-6adf9c3 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LiqOpAny = iota
	LiqOpReserve
	LiqOpConfirm
)
View Source
const (
	NativeTokenReference = "ffffffffffffffffffffffffffffffffffffffff"
	TPPolicyAll          = "All"
	TPPolicyPrefixAny2Of = "Any2Of="
	TPPolicyPrefixOneOf  = "OneOf="
)
View Source
const (
	DefaultReportRetryPeriod int64 = 5
	DefaultProcessPeriod     int64 = 5
	DefaultPriceValidPeriod  int64 = 300
	DefaultDstTransferPeriod int64 = 3000
	DefaultGrpcPort          int64 = 5555
	DefaultGrpcGatewayPort   int64 = 6666
)
View Source
const BestPeriodMultiplier = 1.2

Variables

This section is empty.

Functions

func EncodeDataToSign

func EncodeDataToSign(dstChainId uint64, dstAddr eth.Addr, data eth.Hash) []byte

Types

type AmountCalculator

type AmountCalculator interface {
	CalRecvAmt(tokenIn, tokenOut *common.Token, amountIn, baseFee *big.Int, isLightMM bool) (recvAmt, releaseAmt, fee *big.Int, err error)
	CalSendAmt(tokenIn, tokenOut *common.Token, amountOut *big.Int) (sendAmt, releaseAmt, fee *big.Int, err error)
}

type Chain

type Chain struct {
	*ethclient.Client
	ChainId       uint64
	BlockDelay    uint64
	NativeWrap    *common.Token
	RfqContract   *rfq.Rfq
	RfqAddress    eth.Addr
	IWETH         *iweth.Iweth
	MsgFee        *big.Int
	TxOptions     []ethutils.TxOption
	GasPriceCache *dataCache
}

func NewChain

func NewChain(config *RfqMmChainConfig) *Chain

func (Chain) GetChainId

func (c Chain) GetChainId() uint64

func (Chain) GetERC20Balance

func (c Chain) GetERC20Balance(opts *bind.CallOpts, token, account eth.Addr) (*big.Int, error)

func (Chain) GetGasPrice

func (c Chain) GetGasPrice() *big.Int

func (Chain) GetMsgFee

func (c Chain) GetMsgFee(opts *bind.CallOpts, _message []byte) (*big.Int, error)

func (Chain) GetNativeBalance

func (c Chain) GetNativeBalance(account eth.Addr) (*big.Int, error)

func (Chain) GetNativeWrap

func (c Chain) GetNativeWrap() *common.Token

func (Chain) GetQuoteStatus

func (c Chain) GetQuoteStatus(opts *bind.CallOpts, _quoteHash eth.Hash) (uint8, error)

func (Chain) GetRfqFee

func (c Chain) GetRfqFee(opts *bind.CallOpts, _chainId uint64, _amount *big.Int) (*big.Int, error)

func (Chain) VerifyRfqEvent

func (c Chain) VerifyRfqEvent(tx, evID eth.Hash, evName string) (bool, error)

type ChainManager

type ChainManager struct {
	// contains filtered or unexported fields
}

func NewChainManager

func NewChainManager(configs []*RfqMmChainConfig) *ChainManager

func (*ChainManager) GetChain

func (cm *ChainManager) GetChain(chainId uint64) (*Chain, error)

func (*ChainManager) GetERC20Balance

func (cm *ChainManager) GetERC20Balance(chainId uint64, token, account eth.Addr) (*big.Int, error)

func (*ChainManager) GetGasPrice

func (cm *ChainManager) GetGasPrice(chainId uint64) (*big.Int, error)

func (*ChainManager) GetMsgFee

func (cm *ChainManager) GetMsgFee(chainId uint64) (*big.Int, error)

func (*ChainManager) GetNativeBalance

func (cm *ChainManager) GetNativeBalance(chainId uint64, account eth.Addr) (*big.Int, error)

func (*ChainManager) GetNativeWrap

func (cm *ChainManager) GetNativeWrap(chainId uint64) (*common.Token, error)

func (*ChainManager) GetQuoteStatus

func (cm *ChainManager) GetQuoteStatus(chainId uint64, quoteHash eth.Hash) (uint8, error)

func (*ChainManager) GetRfqContract

func (cm *ChainManager) GetRfqContract(chainId uint64) (eth.Addr, error)

func (*ChainManager) GetRfqFee

func (cm *ChainManager) GetRfqFee(srcChainId, dstChainId uint64, amount *big.Int) (*big.Int, error)

func (*ChainManager) VerifyRfqEvent

func (cm *ChainManager) VerifyRfqEvent(chainId uint64, tx eth.Hash, evName string) (bool, error)

type ChainOverride

type ChainOverride struct {
	SrcChainId, DstChainId uint64
	Perc                   uint32
}

type ChainQuerier

type ChainQuerier interface {
	GetRfqContract(chainId uint64) (eth.Addr, error)
	GetRfqFee(srcChainId, dstChainId uint64, amount *big.Int) (*big.Int, error)
	GetMsgFee(chainId uint64) (*big.Int, error)
	GetGasPrice(chainId uint64) (*big.Int, error)
	GetNativeWrap(chainId uint64) (*common.Token, error)
	GetERC20Balance(chainId uint64, token, account eth.Addr) (*big.Int, error)
	GetNativeBalance(chainId uint64, accoun eth.Addr) (*big.Int, error)
	GetQuoteStatus(chainId uint64, quoteHash eth.Hash) (uint8, error)
	VerifyRfqEvent(chainId uint64, tx eth.Hash, evName string) (bool, error)
}

type Client

type Client struct {
	proto.ApiClient
	// contains filtered or unexported fields
}

func NewClient

func NewClient(server string, ops ...grpc.DialOption) *Client

func (*Client) Close

func (c *Client) Close()

func (*Client) Price

func (c *Client) Price(ctx context.Context, in *proto.PriceRequest, opts ...grpc.CallOption) (*proto.PriceResponse, error)

func (*Client) Quote

func (c *Client) Quote(ctx context.Context, in *proto.QuoteRequest, opts ...grpc.CallOption) (*proto.QuoteResponse, error)

type DefaultAmtCalculator

type DefaultAmtCalculator struct {
	// fixed cost related fields
	DstGasCost uint64
	SrcGasCost uint64
	GasPrice   map[uint64]uint64

	// personalized fee related fieds
	// 100% = 1000000
	FeePercGlobal        uint32
	PerChainPairOverride map[uint64]map[uint64]uint32
	PerTokenPairOverride map[string]map[string]uint32

	// helper
	Querier       ChainQuerier
	PriceProvider PriceProvider
}

func NewDefaultAmtCalculator

func NewDefaultAmtCalculator(feeConfig *FeeConfig, querier ChainQuerier, priceProvider PriceProvider) *DefaultAmtCalculator

func (*DefaultAmtCalculator) CalRecvAmt

func (ac *DefaultAmtCalculator) CalRecvAmt(tokenIn, tokenOut *common.Token, amountIn, baseFeeForLMM *big.Int, isLightMM bool) (amountOut, releaseAmt, fee *big.Int, err error)

func (*DefaultAmtCalculator) CalSendAmt

func (ac *DefaultAmtCalculator) CalSendAmt(tokenIn, tokenOut *common.Token, amountOut *big.Int) (*big.Int, *big.Int, *big.Int, error)

func (*DefaultAmtCalculator) SetDstGasCost

func (ac *DefaultAmtCalculator) SetDstGasCost(gasCost uint64)

func (*DefaultAmtCalculator) SetGasPrice

func (ac *DefaultAmtCalculator) SetGasPrice(prices []*GasPrice)

func (*DefaultAmtCalculator) SetGlobalFeePerc

func (ac *DefaultAmtCalculator) SetGlobalFeePerc(feePerc uint32) error

func (*DefaultAmtCalculator) SetMaxFeeUsdValue

func (ac *DefaultAmtCalculator) SetMaxFeeUsdValue(maxFeeUsdValue uint64)

func (*DefaultAmtCalculator) SetMinFeeUsdValue

func (ac *DefaultAmtCalculator) SetMinFeeUsdValue(minFeeUsdValue uint64)

func (*DefaultAmtCalculator) SetPerChainPairFeePercOverride

func (ac *DefaultAmtCalculator) SetPerChainPairFeePercOverride(overrides []*ChainOverride) error

func (*DefaultAmtCalculator) SetPerTokenPairFeePercOverride

func (ac *DefaultAmtCalculator) SetPerTokenPairFeePercOverride(overrides []*TokenOverride) error

func (*DefaultAmtCalculator) SetSrcGasCost

func (ac *DefaultAmtCalculator) SetSrcGasCost(gasCost uint64)

type DefaultLiquidityProvider

type DefaultLiquidityProvider struct {
	// contains filtered or unexported fields
}

func NewDefaultLiquidityProvider

func NewDefaultLiquidityProvider(cm *ChainManager, lm *LiqManager) *DefaultLiquidityProvider

func (DefaultLiquidityProvider) AskForFreezing

func (d DefaultLiquidityProvider) AskForFreezing(chainId uint64, token eth.Addr, amount *big.Int, isNative bool) (int64, error)

func (*DefaultLiquidityProvider) DstTransfer

func (d *DefaultLiquidityProvider) DstTransfer(transferNative bool, _quote rfq.RFQQuote, opts ...ethutils.TxOption) (eth.Hash, error)

func (DefaultLiquidityProvider) FreezeLiquidity

func (d DefaultLiquidityProvider) FreezeLiquidity(chainId uint64, token eth.Addr, amount *big.Int, until int64, hash eth.Hash, isNative bool) error

func (DefaultLiquidityProvider) GetLiquidityProviderAddr

func (d DefaultLiquidityProvider) GetLiquidityProviderAddr(chainId uint64) (eth.Addr, error)

func (DefaultLiquidityProvider) GetTokens

func (d DefaultLiquidityProvider) GetTokens() []*common.Token

func (DefaultLiquidityProvider) HasTokenPair

func (d DefaultLiquidityProvider) HasTokenPair(srcToken, dstToken *common.Token) bool

func (DefaultLiquidityProvider) IsPaused

func (d DefaultLiquidityProvider) IsPaused() bool

func (*DefaultLiquidityProvider) SetupTokenPairs

func (d *DefaultLiquidityProvider) SetupTokenPairs(policies []string)

policy str is one of {"All", "Any2Of=<chainId>-<symbol>,<chainId>-<symbol>...", "OneOf=<chainId>-<symbol>,<chainId>-<symbol>"}

func (*DefaultLiquidityProvider) SrcRelease

func (d *DefaultLiquidityProvider) SrcRelease(_quote rfq.RFQQuote, _execMsgCallData []byte, opts ...ethutils.TxOption) (eth.Hash, error)

func (DefaultLiquidityProvider) UnfreezeLiquidity

func (d DefaultLiquidityProvider) UnfreezeLiquidity(chainId uint64, hash eth.Hash) error

type DefaultRequestSigner

type DefaultRequestSigner struct {
	Signer  ethutils.Signer
	Address eth.Addr
}

func NewRequestSigner

func NewRequestSigner(config *RequestSignerConfig) *DefaultRequestSigner

func (*DefaultRequestSigner) Sign

func (rs *DefaultRequestSigner) Sign(data []byte) ([]byte, error)

func (*DefaultRequestSigner) Verify

func (rs *DefaultRequestSigner) Verify(data, sig []byte) bool

type FeeConfig

type FeeConfig struct {
	DstGasCost     uint64
	SrcGasCost     uint64
	PercGlobal     uint32
	ChainOverrides []*ChainOverride
	TokenOverrides []*TokenOverride
	GasPrices      []*GasPrice
}

type GasPrice

type GasPrice struct {
	ChainId uint64
	Price   uint64
}

type LPConfig

type LPConfig struct {
	ChainId       uint64
	Address       string
	Keystore      string
	Passphrase    string
	Liqs          []*LiquidityConfig
	ReleaseNative bool
}

type LiqManager

type LiqManager struct {
	LPs map[uint64]*LiqProvider
}

func NewLiqManager

func NewLiqManager(configs []*LPConfig) *LiqManager

func (*LiqManager) AskForFreezing

func (d *LiqManager) AskForFreezing(chainId uint64, token eth.Addr, amount *big.Int) (int64, error)

func (*LiqManager) ConfirmLiquidity

func (d *LiqManager) ConfirmLiquidity(chainId uint64, token eth.Addr, amount *big.Int, until int64, hash eth.Hash) error

func (*LiqManager) GetChains

func (d *LiqManager) GetChains() []uint64

func (*LiqManager) GetLP

func (d *LiqManager) GetLP(chainId uint64) (*LiqProvider, error)

func (*LiqManager) GetLiqNeedApprove

func (d *LiqManager) GetLiqNeedApprove(chainId uint64) ([]*common.Token, []*big.Int, error)

func (*LiqManager) GetLiquidityProvider

func (d *LiqManager) GetLiquidityProvider(chainId uint64) (eth.Addr, error)

func (*LiqManager) GetSigner

func (d *LiqManager) GetSigner(chainId uint64) (eth.Addr, ethutils.Signer, error)

func (*LiqManager) GetTokens

func (d *LiqManager) GetTokens() map[uint64][]*common.Token

func (*LiqManager) ReleaseInLiquidity

func (d *LiqManager) ReleaseInLiquidity(chainId uint64, token eth.Addr, amount *big.Int) error

func (*LiqManager) ReleaseNative

func (d *LiqManager) ReleaseNative(chainId uint64) (bool, error)

func (*LiqManager) ReserveLiquidity

func (d *LiqManager) ReserveLiquidity(chainId uint64, token eth.Addr, amount *big.Int, until int64, hash eth.Hash) error

func (*LiqManager) TransferOutLiquidity

func (d *LiqManager) TransferOutLiquidity(chainId uint64, token eth.Addr, amount *big.Int, hash eth.Hash) error

func (*LiqManager) UnfreezeLiquidity

func (d *LiqManager) UnfreezeLiquidity(chainId uint64, hash eth.Hash) error

func (*LiqManager) UpdateLiqAmt

func (d *LiqManager) UpdateLiqAmt(querier ChainQuerier)

type LiqOpDetail

type LiqOpDetail struct {
	Type   int
	Until  int64
	Token  string
	Amount *big.Int
	Hash   eth.Hash
}

type LiqProvider

type LiqProvider struct {
	// contains filtered or unexported fields
}

func NewLiqProvider

func NewLiqProvider(config *LPConfig) *LiqProvider

type Liquidity

type Liquidity struct {
	// contains filtered or unexported fields
}

func (*Liquidity) Status

func (liq *Liquidity) Status() string

type LiquidityConfig

type LiquidityConfig struct {
	Address    string
	Symbol     string
	Amount     string
	Approve    string
	Decimals   int32
	FreezeTime int64
}

type LiquidityProvider

type LiquidityProvider interface {
	// IsPaused returns whether the LiquidityProvider is paused or not
	IsPaused() bool
	// GetTokens returns a list of all supported tokens
	GetTokens() []*common.Token
	// SetupTokenPairs sets up supported token pairs based on a given policy list.
	SetupTokenPairs(policies []string)
	// HasTokenPair check if a given token pair is supported
	HasTokenPair(srcToken, dstToken *common.Token) bool
	// GetLiquidityProviderAddr returns the address of liquidity provider on specified chain
	GetLiquidityProviderAddr(chainId uint64) (eth.Addr, error)
	// AskForFreezing checks if there is sufficient liquidity for specified token on specified chain and returns freeze time
	AskForFreezing(chainId uint64, token eth.Addr, amount *big.Int, isNative bool) (int64, error)
	// FreezeLiquidity will freeze a certain liquidity for specified amount until specified timestamp with an index of hash.
	FreezeLiquidity(chainId uint64, token eth.Addr, amount *big.Int, until int64, hash eth.Hash, isNative bool) error
	// UnfreezeLiquidity will try to unfreeze a certain liquidity with specified hash.
	UnfreezeLiquidity(chainId uint64, hash eth.Hash) error
	// DstTransfer should send tx on dstChain to transfer dstToken to user
	DstTransfer(transferNative bool, _quote rfq.RFQQuote, opts ...ethutils.TxOption) (eth.Hash, error)
	// SrcRelease should send tx on srcChain to release srcToken to mm
	SrcRelease(_quote rfq.RFQQuote, _execMsgCallData []byte, opts ...ethutils.TxOption) (eth.Hash, error)
}

type PriceProvider

type PriceProvider interface {
	GetPrice(token *common.Token) (float64, error)
}

type RequestSigner

type RequestSigner interface {
	Sign(data []byte) ([]byte, error)
	Verify(data, sig []byte) bool
}

type RequestSignerConfig

type RequestSignerConfig struct {
	ChainId    uint64
	Keystore   string
	Passphrase string
}

type RfqMmChainConfig

type RfqMmChainConfig struct {
	ChainId                                             uint64
	Name, Gateway                                       string
	BlkInterval, BlkDelay, MaxBlkDelta, ForwardBlkDelay uint64
	GasLimit                                            uint64
	AddGasEstimateRatio                                 float64
	// Legacy gas price flags
	AddGasGwei   uint64
	MinGasGwei   uint64
	MaxGasGwei   uint64
	ForceGasGwei string
	// EIP-1559 gas price flags
	MaxFeePerGasGwei         uint64
	MaxPriorityFeePerGasGwei uint64
	// if ProxyPort > 0, a proxy with this port will be created to support some special chain such as harmony, celo.
	// chainID will be used to determined which type proxy to create, so make sure the chainID is supported in the "endpoint-proxy"
	// create a proxy to the Gateway, and eth-client will be created to "127.0.0.1:ProxyPort"
	// more detail, https://github.com/celer-network/endpoint-proxy
	ProxyPort int

	Rfq    string
	Native *common.Token
}

type Server

type Server struct {
	Ctl               chan bool
	RfqClient         *rfqserver.Client
	Config            *ServerConfig
	ChainCaller       ChainQuerier
	LiquidityProvider LiquidityProvider
	AmountCalculator  AmountCalculator
	RequestSigner     RequestSigner
}

func (*Server) DefaultProcessOrder

func (s *Server) DefaultProcessOrder()

func (*Server) Price

func (s *Server) Price(ctx context.Context, request *proto.PriceRequest) (response *proto.PriceResponse, err error)

func (*Server) Quote

func (s *Server) Quote(ctx context.Context, request *proto.QuoteRequest) (response *proto.QuoteResponse, err error)

func (*Server) ReportConfigs

func (s *Server) ReportConfigs()

func (*Server) Serve

func (s *Server) Serve(ops ...grpc.ServerOption)

func (*Server) SignQuoteHash

func (s *Server) SignQuoteHash(ctx context.Context, request *proto.SignQuoteHashRequest) (*proto.SignQuoteHashResponse, error)

func (*Server) StopProcessing

func (s *Server) StopProcessing(reason string)

func (*Server) Tokens

func (s *Server) Tokens(ctx context.Context, request *proto.TokensRequest) (*proto.TokensResponse, error)

func (*Server) ValidateQuote

func (s *Server) ValidateQuote(quote *proto.Quote, sig []byte) bool

type ServerConfig

type ServerConfig struct {
	// the period for retrying report supported tokens to rfq server
	ReportRetryPeriod int64
	// the period for processing pending orders
	ProcessPeriod int64
	// indicates the period for a price to be valid
	PriceValidPeriod int64
	// minimum dst transfer period, in order to give mm enough time for dst transfer
	DstTransferPeriod int64
	// token pair policy list
	TPPolicyList []string
	// port num that mm grpc service would listen on
	GrpcPort int64
	// port num that mm restful api would listen on
	GrpcGatewayPort int64
	// light mm, which needs a relayer to interact with rfq server
	LightMM bool
	// if not set, will use localhost
	Host string
}

type TokenOverride

type TokenOverride struct {
	SrcChainId, DstChainId uint64
	SrcToken, DstToken     string
	Perc                   uint32
}

type Update

type Update struct {
	Hash   eth.Hash
	Status rfqproto.OrderStatus
}

Directories

Path Synopsis
Package proto is a reverse proxy.
Package proto is a reverse proxy.

Jump to

Keyboard shortcuts

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