database

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const MaxSlippage = 10000

Variables

View Source
var (
	ConfigDBFile string
	ConfigDir    string
)
View Source
var (
	ErrNoNetworksFound  = errors.New("no networks found")
	ErrNetworkNotFound  = errors.New("network not found")
	ErrNoCustomEndpoint = errors.New("no custom endpoints found")
)
View Source
var (
	DefaultSlippage = float64(100) // 1% (support up to 2 decimals)

)
View Source
var ErrNoAmountModeFound = errors.New("no amount mode found")
View Source
var ErrNoEndpointsFound = errors.New("no endpoints found")
View Source
var ErrNoMiscFound = errors.New("no misc found")
View Source
var ErrNoTargetTypesFound = errors.New("no target types found")
View Source
var ErrNoTradeTypesFound = errors.New("no trade types found")
View Source
var ErrWalletNotFound = errors.New("wallet not found")

Functions

func Close

func Close() error

Close closes the database connection. Use only when shutting down the program.

func ExactBuyTrigger

func ExactBuyTrigger(price, target *big.Int, stopLoss bool) bool

ExactBuyTrigger is the function used to trigger a buy.

func ExactSellTrigger

func ExactSellTrigger(price, target *big.Int, stopLoss bool) bool

ExactSellTrigger is the function used to trigger a sell.

func FetchBalanceByContractAndNetworkID

func FetchBalanceByContractAndNetworkID(contract string, networkID uint) (*big.Int, error)

FetchBalance returns the balance of the token by the contract address and network id. The network id is the internal database id of the network. It's not related to the chain id.

func InitDB

func InitDB() error

func LoadAllAmountModes

func LoadAllAmountModes() error

LoadAllAmountModes fetches all amount modes from the database and sets them as a global variable..

func LoadAllTargetTypes

func LoadAllTargetTypes() error

LoadAllTargetTypes fetches all trade types from the database and sets them as a global variable.

func LoadAllTradeTypes

func LoadAllTradeTypes() error

LoadAllTradeTypes fetches all trade types from the database and sets them as a global variable..

func SaveTokenUniqueByContractAndNetworkID

func SaveTokenUniqueByContractAndNetworkID(token *Token) error

SaveTokenUniqueByContractAndNetworkID saves the token in the database. The contract and network id are used to identify the token. If a token with the same contract and network id exists, nothing happens.

func SaveTrade

func SaveTrade(t *Trade) error

SaveTrade saves the trade to the database.

func UpdateBalanceByContractAndNetworkID

func UpdateBalanceByContractAndNetworkID(contract string, networkID uint, balance *big.Int) error

UpdateBalanceByContractAndNetworkID updates the balance of the token by the contract address and network id. The network id is the internal database id of the network. It's not related to the chain id.

Types

type AmountMode

type AmountMode struct {
	gorm.Model `yaml:"-"`
	Type       string `yaml:"type"`
	// contains filtered or unexported fields
}

func (*AmountMode) Get

func (a *AmountMode) Get() *AmountMode

Get retruns the amount mode.

func (*AmountMode) GetName

func (a *AmountMode) GetName() string

GetName returns the name of the AmountMode.

type AmountModes

type AmountModes []*AmountMode
var DefaultAmountModes AmountModes

func (AmountModes) GetAmountIn

func (a AmountModes) GetAmountIn() *AmountMode

GetAmountIn returns the amountIn database object.

func (AmountModes) GetAmountOut

func (a AmountModes) GetAmountOut() *AmountMode

GetAmountOut returns the amountOut database object.

type Dex

type Dex struct {
	gorm.Model `yaml:"-"`
	Name       string `yaml:"name"`
	Router     string `yaml:"router"`
	Factory    string `yaml:"factory"`
	Fee        int64  `yaml:"fee"`
	Predefined bool   `yaml:"-"`
	// Trades     []*Trade `yaml:"-"`
	NetworkID uint `yaml:"-"`
	// contains filtered or unexported fields
}

Dex is the database model for a decentralized exchange.

func NewDex

func NewDex(name, router, factory string, fee int64, predefined bool) *Dex

func (*Dex) Get

func (d *Dex) Get() *Dex

Get returns the dex.

func (*Dex) GetFactory

func (d *Dex) GetFactory() string

GetFactory returns the factory contract of the dex.

func (*Dex) GetFee

func (d *Dex) GetFee() int64

GetFee returns the fee of the dex.

func (*Dex) GetFeeBigInt

func (d *Dex) GetFeeBigInt() *big.Int

GetFeeBigInt returns the fee of the dex as a *big.Int.

func (*Dex) GetName

func (d *Dex) GetName() string

GetName returns the name of the dex.

func (*Dex) GetPredefined

func (d *Dex) GetPredefined() bool

GetPredefined returns whether the dex is predefined.

func (*Dex) GetRouter

func (d *Dex) GetRouter() string

GetRouter returns the router contract of the dex.

func (*Dex) SetPredefined

func (d *Dex) SetPredefined(predefined bool)

SetPredefined sets whether the dex is predefined.

type Endpoint

type Endpoint struct {
	gorm.Model `yaml:"-"`
	URL        string `yaml:"url"`
	Custom     bool   `yaml:"-"`
	NetworkID  uint   `yaml:"-"`
	// contains filtered or unexported fields
}

Endpoint represents a node endpoint, either http or ws.

func NewEndpoint

func NewEndpoint(url string, custom bool) *Endpoint

NewEndpoint creates a new endpoint.

func (*Endpoint) Get

func (e *Endpoint) Get() *Endpoint

Get returns the endpoint.

func (*Endpoint) GetCustom

func (e *Endpoint) GetCustom() bool

func (*Endpoint) GetURL

func (e *Endpoint) GetURL() string

GetURL returns the url of the endpoint.

func (*Endpoint) SetURL

func (e *Endpoint) SetURL(url string)

SetUrl sets the url of the endpoint.

type Endpoints

type Endpoints []*Endpoint

func (Endpoints) GetEndpointByURL

func (e Endpoints) GetEndpointByURL(url string) *Endpoint

GetEndpointByURL returns the endpoint with the given url.

func (Endpoints) GetUrls

func (e Endpoints) GetUrls() []string

GetUrls returns the urls of the endpoints.

type Misc

type Misc struct {
	gorm.Model
	TermsAndConditions bool
	// contains filtered or unexported fields
}

func FetchMisc

func FetchMisc() (*Misc, error)

func (*Misc) CreateTermsAndConditions

func (m *Misc) CreateTermsAndConditions(accepted bool) error

CreateTermsAndConditions sets the terms and conditions in the database.

func (*Misc) GetTermsAndConditions

func (m *Misc) GetTermsAndConditions() bool

GetTermsAndConditions returns where the terms and conditions are accepted or not.

type Network

type Network struct {
	gorm.Model     `yaml:"-"`
	Endpoints      Endpoints `yaml:"endpoints" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Tokens         []*Token  `yaml:"tokens" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Dexes          []*Dex    `yaml:"dexes" gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	Name           string    `yaml:"name" gorm:"unique"`
	FullName       string    `yaml:"fullName"`
	Multicall      string    `yaml:"multicall"`
	NativeCurrency string    `yaml:"nativeCurrency"`
	WETH           string    `yaml:"weth"` // represents the native currency as token
	GasLimit       uint64    `yaml:"gasLimit"`

	ChainID        uint32 `yaml:"chainId"`
	IsTestnet      bool   `yaml:"isTestnet"`
	EIP1559Enabled bool   `yaml:"eip1559Enabled"`
	Predefined     bool   `yaml:"-"`
	// contains filtered or unexported fields
}

func FetchAllNetworks

func FetchAllNetworks(inclTestnets bool) ([]*Network, error)

FetchAllNetworks fetches all networks from the database.

func (*Network) Connectors

func (n *Network) Connectors() []*Token

func (*Network) CreateCustomEndpoint

func (n *Network) CreateCustomEndpoint(endpointURL string) error

func (*Network) Get

func (n *Network) Get() *Network

Get returns the network.

func (*Network) GetChainID

func (n *Network) GetChainID() uint32

func (*Network) GetCustomEndpoint

func (n *Network) GetCustomEndpoint() (*Endpoint, bool)

GetCustomEndpoint returns the custom endpoint for the given network.

func (*Network) GetDexes

func (n *Network) GetDexes() []*Dex

func (*Network) GetEndpoints

func (n *Network) GetEndpoints() Endpoints

func (*Network) GetFullName

func (n *Network) GetFullName() string

func (*Network) GetGasLimit

func (n *Network) GetGasLimit() uint64

func (*Network) GetID

func (n *Network) GetID() uint

func (*Network) GetMulticall

func (n *Network) GetMulticall() string

func (*Network) GetName

func (n *Network) GetName() string

func (*Network) GetNativeCurrency

func (n *Network) GetNativeCurrency() string

func (*Network) GetTokens

func (n *Network) GetTokens() []*Token

func (*Network) GetWETH

func (n *Network) GetWETH() string

func (*Network) IsNativeCurrency

func (n *Network) IsNativeCurrency(currency string) bool

IsNativeCurrency returns true if the given currency is native to the chain.

func (*Network) NewNativeCurrencyToken

func (n *Network) NewNativeCurrencyToken() *Token

func (*Network) RemoveCustomEndpoint

func (n *Network) RemoveCustomEndpoint() error

func (*Network) Testnet

func (n *Network) Testnet() bool

type Networks

type Networks []*Network

func (Networks) GetNetworkByName

func (networks Networks) GetNetworkByName(name string) *Network

type RawTarget

type RawTarget struct {
	gorm.Model
	Price       string
	Amount      string
	Slippage    float64
	GasPrice    *big.Int `gorm:"-"`
	ExactPrice  bool
	ExactAmount bool
	Stoploss    bool
	Skip        bool // Skip is true, if the raw target has been converted to a database.Target already by calling Target()
	TradeID     uint
	// contains filtered or unexported fields
}

RawTarget is a struct that contains the raw values of the price and amount of a target Currently the price and amount bust be a decimal string.

func NewRawTarget

func NewRawTarget(price, amount string, exactPrice, exactAmount bool, slippage float64, gasPrice *big.Int, stoploss bool) *RawTarget

NewRawTarget creates a new raw target.

func (*RawTarget) Target

func (t *RawTarget) Target(baseToken *Token, actualToken *Token, amountMode *AmountMode, targetType *TargetType, tradeID uint) *Target

Target turns a raw target into a target.

type Target

type Target struct {
	gorm.Model
	// The trading path of the target.
	Path []common.Address `gorm:"-"`
	// The price of the target. Convert to *big.Int, normalized with decimals.
	Price string
	// The amount of the target. Convert to *big.Int, normalized with decimals.
	// The amount is always in the base currency of the trade.
	Amount string
	// Either the minAmountOut of maxAmountIn which is required in order to complete the trade.
	// This value is only known when the trigger price has been hit and must be set then.
	AmountMinMax string

	TxHash string
	// The actual amount of the target.
	// This value is used for the swap.
	// For a sell target, this amount is only know when the trigger price has been hit.
	ActualAmount *big.Int `gorm:"-"`

	PercentageAmount float64 `gorm:"-"`
	PercentagePrice  float64 `gorm:"-"`
	TargetTypeID     uint
	AmountModeID     uint
	AmountMode       *AmountMode `gorm:"foreignkey:AmountModeID"`
	// Slippage in percent muliplied by 100 to support up to 2 decimals.
	Slippage   *float64
	TargetType *TargetType `gorm:"foreignkey:TargetTypeID"`
	TradeID    uint
	Deadline   *time.Duration `gorm:"-"` // TODO: replace with unix timestamp and store in database
	GasLimit   *uint64
	// In WEI
	GasPrice *big.Int `gorm:"-"`

	Hit                  bool
	Confirmed            bool
	Failed               bool
	AmountDecimals       uint8
	ActualAmountDecimals uint8
	PriceDecimals        uint8
	IsStopLoss           bool
	TriggerFunc          func(*big.Int, *big.Int, bool) bool `gorm:"-"`
	ExecutionPrice       decimal.Decimal                     `gorm:"-"`
	// contains filtered or unexported fields
}

Target is the database model for a target.

func NewExactTarget

func NewExactTarget(
	price string, priceDecimals uint8,
	amount string, amountDecimals uint8,
	actualAmount *big.Int, actualAmountDecimals uint8,
	amountMode *AmountMode, targetType *TargetType,
	slippage float64, gasPrice *big.Int, stoploss bool,
	tradeID uint,
	triggerFunc func(*big.Int, *big.Int, bool) bool,
) *Target

NewExactTarget creates a new target. The price and the amount must be an exact value and covertable to a *big.Int. The amount is always in the base currency of the trade. It is possible to set the actual amount to nil, as it will be calculated when the trigger price is hit.

func NewPercentageAmountTarget

func NewPercentageAmountTarget(
	price string, priceDecimals uint8,
	percentageAmount float64, amountDecimals uint8,
	actualAmountDecimals uint8,
	amountMode *AmountMode, targetType *TargetType,
	slippage float64, gasPrice *big.Int, stoploss bool,
	tradeID uint,
	triggerFunc func(*big.Int, *big.Int, bool) bool,
) *Target

NewPercentageAmountTarget create a new target where the amount is a percentage of the total amount of the trade.

func NewPercentagePriceAndAmountTarget

func NewPercentagePriceAndAmountTarget(
	percentagePrice float64, priceDecimals uint8,
	percentageAmount float64, amountDecimals uint8,
	actualAmountDecimals uint8,
	amountMode *AmountMode, targetType *TargetType,
	slippage float64, gasPrice *big.Int, stoploss bool,
	tradeID uint,
	triggerFunc func(*big.Int, *big.Int, bool) bool,
) *Target

NewPercentagePriceAndAmountTarget creates a new target where the trade is triggered as soon as the price changes by a certain percentage and the amount is a percentage of the total amount of the trade.

func NewPercentagePriceTarget

func NewPercentagePriceTarget(
	percentage float64, priceDecimals uint8,
	amount string, amountDecimals uint8,
	actualAmount *big.Int, actualAmountDecimals uint8,
	amountMode *AmountMode, targetType *TargetType,
	slippage float64, gasPrice *big.Int, stoploss bool,
	tradeID uint,
	triggerFunc func(*big.Int, *big.Int, bool) bool,
) *Target

NewPercentagePriceTarget create a new target where the trade is triggered as soon as the price changes by a certain percentage.

func NewTargetWithDefaults

func NewTargetWithDefaults() *Target

NewTargetWithDefaults creates a new target with default values.

func (*Target) GetActualAmount

func (t *Target) GetActualAmount() *big.Int

GetActualAmount returns the actual amount.

func (*Target) GetActualAmountDecimals

func (t *Target) GetActualAmountDecimals() uint8

GetActualAmountDecimals returns the actual amount decimals.

func (*Target) GetAmount

func (t *Target) GetAmount() *big.Int

GetAmount returns the amount as a *big.Int.

func (*Target) GetAmountDecimals

func (t *Target) GetAmountDecimals() uint8

GetAmountDecimals returns the amount decimals.

func (*Target) GetAmountMinMax

func (t *Target) GetAmountMinMax() *big.Int

GetAmountMinMax returns the amount min/max.

func (*Target) GetAmountMode

func (t *Target) GetAmountMode() *AmountMode

GetAmountMode returns the amount mode.

func (*Target) GetConfirmed

func (t *Target) GetConfirmed() bool

GetConfirmed returns whether the target transaction has been confirmed.

func (*Target) GetDeadline

func (t *Target) GetDeadline() *time.Duration

GetDeadline returns the deadline.

func (*Target) GetExecutionPrice

func (t *Target) GetExecutionPrice() decimal.Decimal

GetExecutionPrice returns the execution price.

func (*Target) GetFailed

func (t *Target) GetFailed() bool

GetFailed returns the failed status.

func (*Target) GetGasLimit

func (t *Target) GetGasLimit() *uint64

GetGasLimit returns the gas limit.

func (*Target) GetGasPrice

func (t *Target) GetGasPrice() *big.Int

GetGasPrice returns the gas price.

func (*Target) GetHit

func (t *Target) GetHit() bool

GetHit returns whether the target has been hit.

func (*Target) GetPath

func (t *Target) GetPath() []common.Address

GetPath returns the path.

func (*Target) GetPercentageAmount

func (t *Target) GetPercentageAmount() float64

GetPercentageAmount returns the percentage amount.

func (*Target) GetPercentagePrice

func (t *Target) GetPercentagePrice() float64

GetPercentagePrice returns the percentage price.

func (*Target) GetPrice

func (t *Target) GetPrice() *big.Int

GetPrice returns the price as a *big.Int.

func (*Target) GetPriceDecimals

func (t *Target) GetPriceDecimals() uint8

GetPriceDecimals returns the price decimals.

func (*Target) GetSlippage

func (t *Target) GetSlippage() float64

GetSlippage returns the slippage.

func (*Target) GetStopLoss

func (t *Target) GetStopLoss() bool

GetStopLoss returns whether the target is a stop loss.

func (*Target) GetTargetType

func (t *Target) GetTargetType() *TargetType

GetTargetType returns the target type.

func (*Target) GetTxHash

func (t *Target) GetTxHash() string

GetTxHash returns the transaction hash.

func (*Target) InvertAmountMode

func (t *Target) InvertAmountMode()

func (*Target) MarkStopLoss

func (t *Target) MarkStopLoss()

MarkStopLoss marks the target as a stop loss.

func (*Target) MarketSwapPossible

func (t *Target) MarketSwapPossible(tradeInfo *uniswap.Trade, token *Token) bool

SwapPossible returns whether a swap is possible.

func (*Target) SetActualAmount

func (t *Target) SetActualAmount(actualAmount *big.Int)

SetActualAmount sets the actual amount.

func (*Target) SetActualAmountDecimals

func (t *Target) SetActualAmountDecimals(actualAmountDecimals uint8)

SetActualAmountDecimals sets the actual amount decimals.

func (*Target) SetAmountMinMax

func (t *Target) SetAmountMinMax(amountMinMax string)

SetAmountMinMaxx sets the amount min/max.

func (*Target) SetAmountMode

func (t *Target) SetAmountMode(amountMode *AmountMode)

SetAmountMode sets the amount mode.

func (*Target) SetConfirmed

func (t *Target) SetConfirmed(confirmed bool)

SetConfirmed sets whether the target transaction has been confirmed.

func (*Target) SetDeadline

func (t *Target) SetDeadline(deadline *time.Duration)

SetDeadline sets the deadline.

func (*Target) SetDefaults

func (t *Target) SetDefaults()

func (*Target) SetExecutionPrice

func (t *Target) SetExecutionPrice(executionPrice decimal.Decimal)

SetExecutionPrice sets the execution price.

func (*Target) SetFailed

func (t *Target) SetFailed()

SetFailed marks the target as failed.

func (*Target) SetGasLimit

func (t *Target) SetGasLimit(gasLimit *uint64)

SetGasLimit sets the gas limit.

func (*Target) SetGasPrice

func (t *Target) SetGasPrice(gasPrice *big.Int)

SetGasPrice sets the gas price.

func (*Target) SetHit

func (t *Target) SetHit(hit bool)

SetHit sets the hit.

func (*Target) SetPath

func (t *Target) SetPath(path []common.Address)

SetPath sets the path.

func (*Target) SetPercentageAmount

func (t *Target) SetPercentageAmount(percentageAmount float64)

SetPercentageAmount sets the percentage amount.

func (*Target) SetPrice

func (t *Target) SetPrice(target string)

SetTarget sets the price.

func (*Target) SetPriceDecimals

func (t *Target) SetPriceDecimals(decimals uint8)

SetPriceDecimals sets the target decimals.

func (*Target) SetSlippage

func (t *Target) SetSlippage(slippage *float64)

SetSlippage sets the slippage.

func (*Target) SetTargetType

func (t *Target) SetTargetType(targetType *TargetType)

SetTargetType sets the target type.

func (*Target) SetTxHash

func (t *Target) SetTxHash(txHash string)

SetTxHash sets the transaction hash.

func (*Target) ViewAmount

func (t *Target) ViewAmount() string

ViewAmount returns the amount as a string showing significant figures or a percentage value.

func (*Target) ViewPrice

func (t *Target) ViewPrice() string

ViewPrice returns the price as a string showing significant figures or a percentage value.

type TargetType

type TargetType struct {
	gorm.Model `yaml:"-"`
	Type       string `yaml:"type"`
	// contains filtered or unexported fields
}

func (*TargetType) Get

func (t *TargetType) Get() *TargetType

Get returns the target type.

func (*TargetType) GetType

func (t *TargetType) GetType() string

GetType returns the type of the TargetType.

func (*TargetType) Is

func (t *TargetType) Is(other *TargetType) bool

Is helps to compare two target types. // TODO: implement that method for trade types and amount modes too.

type TargetTypes

type TargetTypes []*TargetType
var DefaultTargetTypes TargetTypes

func (TargetTypes) GetBuy

func (t TargetTypes) GetBuy() *TargetType

GetBuy returns the buyTarget database object.

func (TargetTypes) GetSell

func (t TargetTypes) GetSell() *TargetType

GetSell returns the sellTarget database object.

type Targets

type Targets []*Target

type Token

type Token struct {
	gorm.Model `yaml:"-"`
	Contract   string `yaml:"contract"`
	Symbol     string `yaml:"symbol"`
	Balance    string `yaml:"-"` // converted to big.Int
	NetworkID  uint   `yaml:"-"`

	Decimals   uint8 `yaml:"decimals"`
	Connector  bool  `yaml:"connector"`
	Predefined bool  `yaml:"-"`

	Native bool `yaml:"native"`
	// contains filtered or unexported fields
}

Token represents a token with some basic informations must be identical to the struct internal/blockchain/multicall/token.

func NewToken

func NewToken(address string, symbol string, decimals uint8, native bool, balance *big.Int) *Token

NewToken creates a new token. TODO: add field "connector".

func (*Token) Get

func (t *Token) Get() *Token

Get returns the token.

func (*Token) GetBalance

func (t *Token) GetBalance() *big.Int

GetBalance returns the balance of the token.

func (*Token) GetBalanceDecimal

func (t *Token) GetBalanceDecimal(decimals uint8) decimal.Decimal

GetBalanceDecimal returns the balance of the token as a decimal.

func (*Token) GetContract

func (t *Token) GetContract() string

GetContract returns the contract address.

func (*Token) GetDecimals

func (t *Token) GetDecimals() uint8

GetDecimals returns the decimals of the token.

func (*Token) GetNative

func (t *Token) GetNative() bool

GetNative returns whether the token is native or not.

func (*Token) GetPredefined

func (t *Token) GetPredefined() bool

GetPredefined returns whether the token is predefined or not.

func (*Token) GetSymbol

func (t *Token) GetSymbol() string

GetSymbol returns the symbol of the token.

func (*Token) IsConnector

func (t *Token) IsConnector() bool

Connector returns whether the token is a connector token or not. Connector tokens are used in the path finding.

func (*Token) SetBalance

func (t *Token) SetBalance(value *big.Int)

SetBalance sets the balance of the tokn.

func (*Token) SetDecimals

func (t *Token) SetDecimals(decimals uint8)

SetDecimals sets the decimals of the token.

func (*Token) SetNative

func (t *Token) SetNative(native bool)

SetNative sets the native flag of the token.

func (*Token) SetNetworkID

func (t *Token) SetNetworkID(networkID uint)

SetNetworkID sets the network id of the token.

func (*Token) SetPredefined

func (t *Token) SetPredefined(predefined bool)

SetPredefined sets the predefined flag of the token.

func (*Token) SetSymbol

func (t *Token) SetSymbol(symbol string)

SetSymbol sets the symbol of the token.

func (*Token) ToUniswap

func (t *Token) ToUniswap(weth string) (*uniswap.Token, error)

ToUniswap converts a token to a uniswap.Token.

type Trade

type Trade struct {
	gorm.Model
	RawBuyTargets  []*RawTarget `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	RawSellTargets []*RawTarget `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	BuyTargets     Targets      `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	SellTargets    Targets      `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
	InitPrice      string       // convert to *big.Int, normalized with decimals
	Token0         *Token       `gorm:"foreignkey:Token0ID"`
	Token0ID       uint
	Token1         *Token `gorm:"foreignkey:Token1ID"`
	Token1ID       uint

	TradeType   *TradeType `gorm:"foreignkey:TradeTypeID"`
	TradeTypeID uint
	Endpoint    *Endpoint `gorm:"foreignkey:EndpointID"`
	EndpointID  uint
	Network     *Network `gorm:"foreignkey:NetworkID"`
	NetworkID   uint
	Dex         *Dex `gorm:"foreignkey:DexID"`
	DexID       uint

	Failed bool
	// contains filtered or unexported fields
}

Trade is the database model for a trade.

func NewTrade

func NewTrade(token0, token1 *Token, buyTargets, sellTargets Targets, tradeType *TradeType, endpoint *Endpoint, network *Network, dex *Dex) *Trade

NewTrade creates a new trade.

func (*Trade) AmountInTrade

func (t *Trade) AmountInTrade() *big.Int

AmountInTrade returns the amount in trade for the trade.

func (*Trade) AverageBuyPrice

func (t *Trade) AverageBuyPrice() decimal.Decimal

AverageBuyPrice returns the average buy price for the trade.

func (*Trade) GetBuyTargetHit

func (t *Trade) GetBuyTargetHit() int

GetBuyTargetHit returns the buy target hit counter.

func (*Trade) GetBuyTargets

func (t *Trade) GetBuyTargets() []*Target

GetBuyTargets returns the buy targets for the trade.

func (*Trade) GetDex

func (t *Trade) GetDex() *Dex

GetDex returns the dex for the trade.

func (*Trade) GetEndpoint

func (t *Trade) GetEndpoint() *Endpoint

GetEndpoint returns the endpoint for the trade.

func (*Trade) GetInitPrice

func (t *Trade) GetInitPrice() *big.Int

GetInitPrice returns the init price for the trade.

func (*Trade) GetNetwork

func (t *Trade) GetNetwork() *Network

GetNetwork returns the network for the trade.

func (*Trade) GetNextBuyTarget

func (t *Trade) GetNextBuyTarget() *Target

GetNextBuyTarget returns the first buy target that has not been hit. If all buy targets have been hit, it returns nil.

func (*Trade) GetNextSellTarget

func (t *Trade) GetNextSellTarget() *Target

GetNextSellTarget returns the first sell target that has not been hit. If all sell targets have been hit, it returns it returns nil.

func (*Trade) GetSellTargetHit

func (t *Trade) GetSellTargetHit() int

GetSellTargetHit returns the sell target hit counter.

func (*Trade) GetSellTargets

func (t *Trade) GetSellTargets() []*Target

GetSellTargets returns the sell targets for the trade.

func (*Trade) GetToken0

func (t *Trade) GetToken0() *Token

GetToken0 returns the token0 for the trade.

func (*Trade) GetToken1

func (t *Trade) GetToken1() *Token

GetToken1 returns the token1 for the trade.

func (*Trade) HasStoploss

func (t *Trade) HasStoploss() bool

HasStoploss returns whether the trade has a stoploss target.

func (*Trade) IncrBuyTargetHit

func (t *Trade) IncrBuyTargetHit()

IncrBuyTargetHit increments the buy target hit counter.

func (*Trade) IncrSellTargetHit

func (t *Trade) IncrSellTargetHit()

IncrSellTargetHit increments the sell target hit counter.

func (*Trade) InvertTokens

func (t *Trade) InvertTokens()

InvertTokens inverts the tokens for the trade.

func (*Trade) SetBuyTargets

func (t *Trade) SetBuyTargets(targets []*Target)

SetTargets sets the targets for the trade. Required for the market trades, otherwise not.

func (*Trade) SetInitPrice

func (t *Trade) SetInitPrice(price string)

SetInitPrice sets the init price for the trade.

func (*Trade) SetToken0

func (t *Trade) SetToken0(token *Token)

SetToken0 sets the token0 for the trade.

func (*Trade) SetToken1

func (t *Trade) SetToken1(token *Token)

SetToken1 sets the token1 for the trade.

func (*Trade) TotalBought

func (t *Trade) TotalBought() *big.Int

TotalBought returns the total amount that has been bought.

type TradeType

type TradeType struct {
	gorm.Model `yaml:"-"`
	Type       string `yaml:"type"`
	// contains filtered or unexported fields
}

func (*TradeType) Get

func (t *TradeType) Get() *TradeType

Get returns the trade type.

func (*TradeType) GetType

func (t *TradeType) GetType() string

GetName returns the type name of the TradeType.

func (*TradeType) Is

func (t *TradeType) Is(other *TradeType) bool

type TradeTypes

type TradeTypes []*TradeType
var DefaultTradeTypes TradeTypes

func (TradeTypes) GetMarket

func (t TradeTypes) GetMarket() *TradeType

GetMarketTrade returns the marketTrade database object.

func (TradeTypes) GetOrder

func (t TradeTypes) GetOrder() *TradeType

GetOrderTrade returns the orderTrade database object.

func (TradeTypes) GetSnipe

func (t TradeTypes) GetSnipe() *TradeType

GetSnipeTrade returns the snipeTrade database object.

type Wallet

type Wallet struct {
	gorm.Model

	FirstUse    bool `gorm:"-"`
	Wallet      string
	WalletIndex uint
	Label       string // label is either "trade" for wallets used for trading or "unlock" for wallets used to unlock the bot
	// contains filtered or unexported fields
}

func FetchWallet

func FetchWallet() (*Wallet, error)

func (*Wallet) GetNonce

func (w *Wallet) GetNonce() int64

GetNonce returns the nonce for the wallet.

func (*Wallet) GetPrivateKey

func (w *Wallet) GetPrivateKey() *ecdsa.PrivateKey

GetPrivateKey returns the private key for the wallet, make sure to load it first with wallet.Load().

func (*Wallet) GetWallet

func (w *Wallet) GetWallet() string

func (*Wallet) GetWalletIndex

func (w *Wallet) GetWalletIndex() uint

func (*Wallet) IncrementNonce

func (w *Wallet) IncrementNonce()

IncrementNonce increments the nonce.

func (*Wallet) LastNonceUpdate

func (w *Wallet) LastNonceUpdate() time.Time

LastNonceUpdate returns the time of the last nonce update.

func (*Wallet) NewWallet

func (w *Wallet) NewWallet(walletAddr string, walletIndex uint, label string) error

func (*Wallet) SetNonce

func (w *Wallet) SetNonce(nonce uint64)

SetNonce sets the nonce for the wallet.

func (*Wallet) SetPrivateKey

func (w *Wallet) SetPrivateKey(key *ecdsa.PrivateKey)

Jump to

Keyboard shortcuts

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